Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | b05405a | 2012-01-23 15:35:52 +0100 | [diff] [blame] | 26 | #include <netinet/tcp.h> |
| 27 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 29 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/compat.h> |
| 31 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 32 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 33 | #include <common/memory.h> |
| 34 | #include <common/mini-clist.h> |
| 35 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 36 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 37 | #include <common/time.h> |
| 38 | #include <common/uri_auth.h> |
| 39 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 44 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 45 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | #include <proto/backend.h> |
| 47 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 48 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 49 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 51 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 53 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 55 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 59 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 62 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | #include <proto/task.h> |
| 64 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 65 | const char HTTP_100[] = |
| 66 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 67 | |
| 68 | const struct chunk http_100_chunk = { |
| 69 | .str = (char *)&HTTP_100, |
| 70 | .len = sizeof(HTTP_100)-1 |
| 71 | }; |
| 72 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 73 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 74 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 75 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 76 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 77 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 78 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 81 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 83 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 85 | |
| 86 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 87 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 94 | const char *HTTP_401_fmt = |
| 95 | "HTTP/1.0 401 Unauthorized\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 98 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 100 | "\r\n" |
| 101 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 102 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 103 | const char *HTTP_407_fmt = |
| 104 | "HTTP/1.0 407 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
| 107 | "Content-Type: text/html\r\n" |
| 108 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 114 | [HTTP_ERR_200] = 200, /* used by "monitor-uri" */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 115 | [HTTP_ERR_400] = 400, |
| 116 | [HTTP_ERR_403] = 403, |
| 117 | [HTTP_ERR_408] = 408, |
| 118 | [HTTP_ERR_500] = 500, |
| 119 | [HTTP_ERR_502] = 502, |
| 120 | [HTTP_ERR_503] = 503, |
| 121 | [HTTP_ERR_504] = 504, |
| 122 | }; |
| 123 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 124 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 125 | [HTTP_ERR_200] = |
| 126 | "HTTP/1.0 200 OK\r\n" |
| 127 | "Cache-Control: no-cache\r\n" |
| 128 | "Connection: close\r\n" |
| 129 | "Content-Type: text/html\r\n" |
| 130 | "\r\n" |
| 131 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n", |
| 132 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 133 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | "Cache-Control: no-cache\r\n" |
| 136 | "Connection: close\r\n" |
| 137 | "Content-Type: text/html\r\n" |
| 138 | "\r\n" |
| 139 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 140 | |
| 141 | [HTTP_ERR_403] = |
| 142 | "HTTP/1.0 403 Forbidden\r\n" |
| 143 | "Cache-Control: no-cache\r\n" |
| 144 | "Connection: close\r\n" |
| 145 | "Content-Type: text/html\r\n" |
| 146 | "\r\n" |
| 147 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 148 | |
| 149 | [HTTP_ERR_408] = |
| 150 | "HTTP/1.0 408 Request Time-out\r\n" |
| 151 | "Cache-Control: no-cache\r\n" |
| 152 | "Connection: close\r\n" |
| 153 | "Content-Type: text/html\r\n" |
| 154 | "\r\n" |
| 155 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 156 | |
| 157 | [HTTP_ERR_500] = |
| 158 | "HTTP/1.0 500 Server Error\r\n" |
| 159 | "Cache-Control: no-cache\r\n" |
| 160 | "Connection: close\r\n" |
| 161 | "Content-Type: text/html\r\n" |
| 162 | "\r\n" |
| 163 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 164 | |
| 165 | [HTTP_ERR_502] = |
| 166 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 167 | "Cache-Control: no-cache\r\n" |
| 168 | "Connection: close\r\n" |
| 169 | "Content-Type: text/html\r\n" |
| 170 | "\r\n" |
| 171 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 172 | |
| 173 | [HTTP_ERR_503] = |
| 174 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 175 | "Cache-Control: no-cache\r\n" |
| 176 | "Connection: close\r\n" |
| 177 | "Content-Type: text/html\r\n" |
| 178 | "\r\n" |
| 179 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 180 | |
| 181 | [HTTP_ERR_504] = |
| 182 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 183 | "Cache-Control: no-cache\r\n" |
| 184 | "Connection: close\r\n" |
| 185 | "Content-Type: text/html\r\n" |
| 186 | "\r\n" |
| 187 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 188 | |
| 189 | }; |
| 190 | |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 191 | /* status codes available for the stats admin page (strictly 4 chars length) */ |
| 192 | const char *stat_status_codes[STAT_STATUS_SIZE] = { |
| 193 | [STAT_STATUS_DENY] = "DENY", |
| 194 | [STAT_STATUS_DONE] = "DONE", |
| 195 | [STAT_STATUS_ERRP] = "ERRP", |
| 196 | [STAT_STATUS_EXCD] = "EXCD", |
| 197 | [STAT_STATUS_NONE] = "NONE", |
| 198 | [STAT_STATUS_PART] = "PART", |
| 199 | [STAT_STATUS_UNKN] = "UNKN", |
| 200 | }; |
| 201 | |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | /* We must put the messages here since GCC cannot initialize consts depending |
| 204 | * on strlen(). |
| 205 | */ |
| 206 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 207 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | #define FD_SETS_ARE_BITFIELDS |
| 209 | #ifdef FD_SETS_ARE_BITFIELDS |
| 210 | /* |
| 211 | * This map is used with all the FD_* macros to check whether a particular bit |
| 212 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 213 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 214 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 215 | * exclusively to the macros. |
| 216 | */ |
| 217 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 218 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 219 | |
| 220 | #else |
| 221 | #error "Check if your OS uses bitfields for fd_sets" |
| 222 | #endif |
| 223 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 224 | void init_proto_http() |
| 225 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 226 | int i; |
| 227 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 228 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 229 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 230 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 231 | if (!http_err_msgs[msg]) { |
| 232 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 233 | abort(); |
| 234 | } |
| 235 | |
| 236 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 237 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 238 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 239 | |
| 240 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 241 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 242 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 243 | * printable characters above. |
| 244 | */ |
| 245 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 246 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 247 | for (i = 0; i < 32; i++) { |
| 248 | FD_SET(i, hdr_encode_map); |
| 249 | FD_SET(i, url_encode_map); |
| 250 | } |
| 251 | for (i = 127; i < 256; i++) { |
| 252 | FD_SET(i, hdr_encode_map); |
| 253 | FD_SET(i, url_encode_map); |
| 254 | } |
| 255 | |
| 256 | tmp = "\"#{|}"; |
| 257 | while (*tmp) { |
| 258 | FD_SET(*tmp, hdr_encode_map); |
| 259 | tmp++; |
| 260 | } |
| 261 | |
| 262 | tmp = "\"#"; |
| 263 | while (*tmp) { |
| 264 | FD_SET(*tmp, url_encode_map); |
| 265 | tmp++; |
| 266 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 267 | |
| 268 | /* memory allocations */ |
| 269 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 270 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 271 | pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 272 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 274 | /* |
| 275 | * We have 26 list of methods (1 per first letter), each of which can have |
| 276 | * up to 3 entries (2 valid, 1 null). |
| 277 | */ |
| 278 | struct http_method_desc { |
| 279 | http_meth_t meth; |
| 280 | int len; |
| 281 | const char text[8]; |
| 282 | }; |
| 283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 284 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 285 | ['C' - 'A'] = { |
| 286 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 287 | }, |
| 288 | ['D' - 'A'] = { |
| 289 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 290 | }, |
| 291 | ['G' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 293 | }, |
| 294 | ['H' - 'A'] = { |
| 295 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 296 | }, |
| 297 | ['P' - 'A'] = { |
| 298 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 299 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 300 | }, |
| 301 | ['T' - 'A'] = { |
| 302 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 303 | }, |
| 304 | /* rest is empty like this : |
| 305 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 306 | */ |
| 307 | }; |
| 308 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 309 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 310 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 311 | * RFC2616 for those chars. |
| 312 | */ |
| 313 | |
| 314 | const char http_is_spht[256] = { |
| 315 | [' '] = 1, ['\t'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_crlf[256] = { |
| 319 | ['\r'] = 1, ['\n'] = 1, |
| 320 | }; |
| 321 | |
| 322 | const char http_is_lws[256] = { |
| 323 | [' '] = 1, ['\t'] = 1, |
| 324 | ['\r'] = 1, ['\n'] = 1, |
| 325 | }; |
| 326 | |
| 327 | const char http_is_sep[256] = { |
| 328 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 329 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 330 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 331 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 332 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 333 | }; |
| 334 | |
| 335 | const char http_is_ctl[256] = { |
| 336 | [0 ... 31] = 1, |
| 337 | [127] = 1, |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 342 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 343 | * them correctly. Instead, define every non-CTL char's status. |
| 344 | */ |
| 345 | const char http_is_token[256] = { |
| 346 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 347 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 348 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 349 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 350 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 351 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 352 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 353 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 354 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 355 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 356 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 357 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 358 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 359 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 360 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 361 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 362 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 363 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 364 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 365 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 366 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 367 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 368 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 369 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 370 | }; |
| 371 | |
| 372 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 373 | /* |
| 374 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 375 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 376 | */ |
| 377 | const char http_is_ver_token[256] = { |
| 378 | ['.'] = 1, ['/'] = 1, |
| 379 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 380 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 381 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 382 | }; |
| 383 | |
| 384 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 385 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 386 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 387 | */ |
| 388 | #if defined(DEBUG_FSM) |
| 389 | static void http_silent_debug(int line, struct session *s) |
| 390 | { |
| 391 | int size = 0; |
| 392 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 393 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 394 | line, |
| 395 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 396 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 397 | write(-1, trash, size); |
| 398 | size = 0; |
| 399 | size += snprintf(trash + size, sizeof(trash) - size, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 400 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n", |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 401 | line, |
| 402 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 403 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward); |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 404 | |
| 405 | write(-1, trash, size); |
| 406 | } |
| 407 | #else |
| 408 | #define http_silent_debug(l,s) do { } while (0) |
| 409 | #endif |
| 410 | |
| 411 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 412 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 413 | * the last CRLF. Text length is measured first, so it cannot be NULL. |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 414 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 415 | * of headers is automatically adjusted. The number of bytes added is returned |
| 416 | * on success, otherwise <0 is returned indicating an error. |
| 417 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 418 | int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 419 | { |
| 420 | int bytes, len; |
| 421 | |
| 422 | len = strlen(text); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 423 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 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 | /* |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 431 | * Adds a header and its CRLF at the tail of the message's buffer, just before |
| 432 | * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 438 | int http_header_add_tail2(struct http_msg *msg, |
| 439 | struct hdr_idx *hdr_idx, const char *text, int len) |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 440 | { |
| 441 | int bytes; |
| 442 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 443 | bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 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. |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 594 | * The ctx is always updated accordingly, as well as the buffer and HTTP |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 599 | int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx) |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 600 | { |
| 601 | int cur_idx = ctx->idx; |
| 602 | char *sol = ctx->line; |
| 603 | struct hdr_idx_elem *hdr; |
| 604 | int delta, skip_comma; |
| 605 | |
| 606 | if (!cur_idx) |
| 607 | return 0; |
| 608 | |
| 609 | hdr = &idx->v[cur_idx]; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 610 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 611 | /* This was the only value of the header, we must now remove it entirely. */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 612 | delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 613 | http_msg_move_end(msg, delta); |
| 614 | idx->used--; |
| 615 | hdr->len = 0; /* unused entry */ |
| 616 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 617 | if (idx->tail == ctx->idx) |
| 618 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 619 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 620 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 621 | 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] | 622 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 623 | return ctx->idx; |
| 624 | } |
| 625 | |
| 626 | /* 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] | 627 | * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the |
| 628 | * last entry of the list, we remove the last separator. |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 629 | */ |
| 630 | |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 631 | skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 632 | delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma, |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 633 | sol + ctx->val + ctx->vlen + ctx->tws + skip_comma, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 634 | NULL, 0); |
| 635 | hdr->len += delta; |
| 636 | http_msg_move_end(msg, delta); |
| 637 | ctx->val = ctx->del; |
Willy Tarreau | 588bd4f | 2011-09-01 22:22:28 +0200 | [diff] [blame] | 638 | ctx->tws = ctx->vlen = 0; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 639 | return ctx->idx; |
| 640 | } |
| 641 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 642 | /* This function handles a server error at the stream interface level. The |
| 643 | * stream interface is assumed to be already in a closed state. An optional |
| 644 | * message is copied into the input buffer, and an HTTP status code stored. |
| 645 | * 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] | 646 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 648 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 649 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 650 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 651 | buffer_auto_read(si->ob); |
| 652 | buffer_abort(si->ob); |
| 653 | buffer_auto_close(si->ob); |
| 654 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 655 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 656 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 657 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 658 | t->txn.status = status; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 659 | bo_inject(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 660 | } |
| 661 | if (!(t->flags & SN_ERR_MASK)) |
| 662 | t->flags |= err; |
| 663 | if (!(t->flags & SN_FINST_MASK)) |
| 664 | t->flags |= finst; |
| 665 | } |
| 666 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 667 | /* This function returns the appropriate error location for the given session |
| 668 | * and message. |
| 669 | */ |
| 670 | |
| 671 | struct chunk *error_message(struct session *s, int msgnum) |
| 672 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 673 | if (s->be->errmsg[msgnum].str) |
| 674 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 675 | else if (s->fe->errmsg[msgnum].str) |
| 676 | return &s->fe->errmsg[msgnum]; |
| 677 | else |
| 678 | return &http_err_chunks[msgnum]; |
| 679 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 680 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 681 | /* |
| 682 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 683 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 684 | */ |
| 685 | static http_meth_t find_http_meth(const char *str, const int len) |
| 686 | { |
| 687 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 688 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 689 | |
| 690 | m = ((unsigned)*str - 'A'); |
| 691 | |
| 692 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 693 | for (h = http_methods[m]; h->len > 0; h++) { |
| 694 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 695 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 696 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 697 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 698 | }; |
| 699 | return HTTP_METH_OTHER; |
| 700 | } |
| 701 | return HTTP_METH_NONE; |
| 702 | |
| 703 | } |
| 704 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 705 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 706 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 707 | * It is returned otherwise. |
| 708 | */ |
| 709 | static char * |
| 710 | http_get_path(struct http_txn *txn) |
| 711 | { |
| 712 | char *ptr, *end; |
| 713 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 714 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 715 | end = ptr + txn->req.sl.rq.u_l; |
| 716 | |
| 717 | if (ptr >= end) |
| 718 | return NULL; |
| 719 | |
| 720 | /* RFC2616, par. 5.1.2 : |
| 721 | * Request-URI = "*" | absuri | abspath | authority |
| 722 | */ |
| 723 | |
| 724 | if (*ptr == '*') |
| 725 | return NULL; |
| 726 | |
| 727 | if (isalpha((unsigned char)*ptr)) { |
| 728 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 729 | ptr++; |
| 730 | while (ptr < end && |
| 731 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 732 | ptr++; |
| 733 | /* skip '://' */ |
| 734 | if (ptr == end || *ptr++ != ':') |
| 735 | return NULL; |
| 736 | if (ptr == end || *ptr++ != '/') |
| 737 | return NULL; |
| 738 | if (ptr == end || *ptr++ != '/') |
| 739 | return NULL; |
| 740 | } |
| 741 | /* skip [user[:passwd]@]host[:[port]] */ |
| 742 | |
| 743 | while (ptr < end && *ptr != '/') |
| 744 | ptr++; |
| 745 | |
| 746 | if (ptr == end) |
| 747 | return NULL; |
| 748 | |
| 749 | /* OK, we got the '/' ! */ |
| 750 | return ptr; |
| 751 | } |
| 752 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 753 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 754 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 755 | * left unchanged and will follow normal proxy processing. |
| 756 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 757 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 758 | { |
| 759 | struct http_txn *txn; |
| 760 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 761 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | char *path; |
| 763 | int len; |
| 764 | |
| 765 | /* 1: create the response header */ |
| 766 | rdr.len = strlen(HTTP_302); |
| 767 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 768 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 769 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 770 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 771 | srv = target_srv(&s->target); |
| 772 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 773 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 774 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 775 | return; |
| 776 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 777 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 778 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 779 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 780 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 781 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 782 | |
| 783 | /* 3: add the request URI */ |
| 784 | txn = &s->txn; |
| 785 | path = http_get_path(txn); |
| 786 | if (!path) |
| 787 | return; |
| 788 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 789 | len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 790 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 791 | return; |
| 792 | |
| 793 | memcpy(rdr.str + rdr.len, path, len); |
| 794 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 795 | |
| 796 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 797 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 798 | rdr.len += 29; |
| 799 | } else { |
| 800 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 801 | rdr.len += 23; |
| 802 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 803 | |
| 804 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 805 | si->shutr(si); |
| 806 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 807 | si->err_type = SI_ET_NONE; |
| 808 | si->err_loc = NULL; |
| 809 | si->state = SI_ST_CLO; |
| 810 | |
| 811 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 812 | 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] | 813 | |
| 814 | /* 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] | 815 | if (srv) |
| 816 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 819 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | * that the server side is closed. Note that err_type is actually a |
| 821 | * bitmask, where almost only aborts may be cumulated with other |
| 822 | * values. We consider that aborted operations are more important |
| 823 | * than timeouts or errors due to the fact that nobody else in the |
| 824 | * logs might explain incomplete retries. All others should avoid |
| 825 | * being cumulated. It should normally not be possible to have multiple |
| 826 | * aborts at once, but just in case, the first one in sequence is reported. |
| 827 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 828 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 829 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 830 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 831 | |
| 832 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 833 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 834 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 835 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 836 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 837 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 838 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 839 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 840 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 841 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 842 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 843 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 844 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 845 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 846 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 847 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 848 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 849 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 850 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 851 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 852 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 853 | } |
| 854 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 855 | extern const char sess_term_cond[8]; |
| 856 | extern const char sess_fin_state[8]; |
| 857 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 858 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 859 | struct pool_head *pool2_capture; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 860 | struct pool_head *pool2_uniqueid; |
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, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 953 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 954 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 955 | const char *msg_start = msg->buf->p; |
| 956 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 957 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 959 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 960 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 961 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 962 | |
| 963 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 964 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 965 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 966 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 967 | state = HTTP_MSG_ERROR; |
| 968 | break; |
| 969 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 970 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 971 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 972 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 973 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 974 | goto http_msg_rpcode; |
| 975 | } |
| 976 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 977 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 978 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 979 | state = HTTP_MSG_ERROR; |
| 980 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 981 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 982 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 983 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 984 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 985 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 986 | |
| 987 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 988 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 989 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 990 | } |
| 991 | |
| 992 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 993 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 994 | http_msg_rsp_reason: |
| 995 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 996 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 997 | msg->sl.st.r_l = 0; |
| 998 | goto http_msg_rpline_eol; |
| 999 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1000 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1001 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1002 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1003 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1004 | goto http_msg_rpreason; |
| 1005 | } |
| 1006 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1007 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1008 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1009 | goto http_msg_rsp_reason; |
| 1010 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1011 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1012 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1013 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1014 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1015 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1016 | http_msg_rpline_eol: |
| 1017 | /* We have seen the end of line. Note that we do not |
| 1018 | * necessarily have the \n yet, but at least we know that we |
| 1019 | * have EITHER \r OR \n, otherwise the response would not be |
| 1020 | * complete. We can then record the response length and return |
| 1021 | * to the caller which will be able to register it. |
| 1022 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1023 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1024 | return ptr; |
| 1025 | |
| 1026 | #ifdef DEBUG_FULL |
| 1027 | default: |
| 1028 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1029 | exit(1); |
| 1030 | #endif |
| 1031 | } |
| 1032 | |
| 1033 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1034 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1035 | if (ret_state) |
| 1036 | *ret_state = state; |
| 1037 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1038 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1039 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | } |
| 1041 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1042 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1043 | * This function parses a request line between <ptr> and <end>, starting with |
| 1044 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1045 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1046 | * will give undefined results. |
| 1047 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1048 | * and that msg->sol points to the beginning of the request. |
| 1049 | * If a complete line is found (which implies that at least one CR or LF is |
| 1050 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1051 | * returned indicating an incomplete line (which does not mean that parts have |
| 1052 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1053 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1054 | * upon next call. |
| 1055 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1056 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1057 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1058 | * 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] | 1059 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1060 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1061 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1062 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1063 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1064 | { |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1065 | const char *msg_start = msg->buf->p; |
| 1066 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1068 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1069 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1070 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1071 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1074 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1076 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1079 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1080 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1082 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1084 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1086 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | msg->sl.rq.v_l = 0; |
| 1088 | goto http_msg_rqline_eol; |
| 1089 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1090 | state = HTTP_MSG_ERROR; |
| 1091 | break; |
| 1092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1094 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1096 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | goto http_msg_rquri; |
| 1098 | } |
| 1099 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1100 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1101 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1102 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1103 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1104 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1105 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1106 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1107 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1110 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1111 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1112 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1114 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1115 | /* non-ASCII chars are forbidden unless option |
| 1116 | * accept-invalid-http-request is enabled in the frontend. |
| 1117 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1118 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1119 | if (msg->err_pos < -1) |
| 1120 | goto invalid_char; |
| 1121 | if (msg->err_pos == -1) |
| 1122 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1123 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1124 | } |
| 1125 | |
| 1126 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1127 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1128 | goto http_msg_req09_uri_e; |
| 1129 | } |
| 1130 | |
| 1131 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1132 | invalid_char: |
| 1133 | msg->err_pos = ptr - msg_buf; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1134 | state = HTTP_MSG_ERROR; |
| 1135 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1136 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1137 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1138 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1139 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1140 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1141 | goto http_msg_rqver; |
| 1142 | } |
| 1143 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1144 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1145 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1146 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1147 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1148 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1149 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1150 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1151 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1152 | |
| 1153 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1154 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1155 | http_msg_rqline_eol: |
| 1156 | /* We have seen the end of line. Note that we do not |
| 1157 | * necessarily have the \n yet, but at least we know that we |
| 1158 | * have EITHER \r OR \n, otherwise the request would not be |
| 1159 | * complete. We can then record the request length and return |
| 1160 | * to the caller which will be able to register it. |
| 1161 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1162 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1163 | return ptr; |
| 1164 | } |
| 1165 | |
| 1166 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1167 | state = HTTP_MSG_ERROR; |
| 1168 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1169 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1170 | #ifdef DEBUG_FULL |
| 1171 | default: |
| 1172 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1173 | exit(1); |
| 1174 | #endif |
| 1175 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1176 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1177 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1178 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1179 | if (ret_state) |
| 1180 | *ret_state = state; |
| 1181 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1182 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1183 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1185 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1186 | /* |
| 1187 | * Returns the data from Authorization header. Function may be called more |
| 1188 | * than once so data is stored in txn->auth_data. When no header is found |
| 1189 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1190 | * searching again for something we are unable to find anyway. |
| 1191 | */ |
| 1192 | |
| 1193 | char get_http_auth_buff[BUFSIZE]; |
| 1194 | |
| 1195 | int |
| 1196 | get_http_auth(struct session *s) |
| 1197 | { |
| 1198 | |
| 1199 | struct http_txn *txn = &s->txn; |
| 1200 | struct chunk auth_method; |
| 1201 | struct hdr_ctx ctx; |
| 1202 | char *h, *p; |
| 1203 | int len; |
| 1204 | |
| 1205 | #ifdef DEBUG_AUTH |
| 1206 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1207 | #endif |
| 1208 | |
| 1209 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1210 | return 0; |
| 1211 | |
| 1212 | if (txn->auth.method) |
| 1213 | return 1; |
| 1214 | |
| 1215 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1216 | |
| 1217 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (txn->flags & TX_USE_PX_CONN) { |
| 1220 | h = "Proxy-Authorization"; |
| 1221 | len = strlen(h); |
| 1222 | } else { |
| 1223 | h = "Authorization"; |
| 1224 | len = strlen(h); |
| 1225 | } |
| 1226 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1227 | if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1228 | return 0; |
| 1229 | |
| 1230 | h = ctx.line + ctx.val; |
| 1231 | |
| 1232 | p = memchr(h, ' ', ctx.vlen); |
| 1233 | if (!p || p == h) |
| 1234 | return 0; |
| 1235 | |
| 1236 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1237 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1238 | |
| 1239 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1240 | |
| 1241 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1242 | get_http_auth_buff, BUFSIZE - 1); |
| 1243 | |
| 1244 | if (len < 0) |
| 1245 | return 0; |
| 1246 | |
| 1247 | |
| 1248 | get_http_auth_buff[len] = '\0'; |
| 1249 | |
| 1250 | p = strchr(get_http_auth_buff, ':'); |
| 1251 | |
| 1252 | if (!p) |
| 1253 | return 0; |
| 1254 | |
| 1255 | txn->auth.user = get_http_auth_buff; |
| 1256 | *p = '\0'; |
| 1257 | txn->auth.pass = p+1; |
| 1258 | |
| 1259 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1260 | return 1; |
| 1261 | } |
| 1262 | |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1266 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1267 | /* |
| 1268 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1269 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1270 | * ensuring that the message does not wrap. The function can be preempted |
| 1271 | * everywhere when data are missing and recalled at the exact same location |
| 1272 | * with no information loss. The message may even be realigned between two |
| 1273 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1274 | * 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] | 1275 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1276 | * the first state, so that none of the msg pointers has to be initialized |
| 1277 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1278 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1279 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1280 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1281 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1282 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1283 | struct buffer *buf = msg->buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1284 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1285 | state = msg->msg_state; |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1286 | ptr = buf->p + msg->next; |
| 1287 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1289 | if (unlikely(ptr >= end)) |
| 1290 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1292 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | /* |
| 1294 | * First, states that are specific to the response only. |
| 1295 | * We check them first so that request and headers are |
| 1296 | * closer to each other (accessed more often). |
| 1297 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1299 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1301 | /* we have a start of message, but we have to check |
| 1302 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1303 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1304 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1305 | char *beg = buf->p; |
| 1306 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1307 | if (unlikely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1308 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1309 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1310 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1311 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1312 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1313 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1314 | hdr_idx_init(idx); |
| 1315 | state = HTTP_MSG_RPVER; |
| 1316 | goto http_msg_rpver; |
| 1317 | } |
| 1318 | |
| 1319 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1320 | goto http_msg_invalid; |
| 1321 | |
| 1322 | if (unlikely(*ptr == '\n')) |
| 1323 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1324 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1325 | /* stop here */ |
| 1326 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1328 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1330 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1331 | /* stop here */ |
| 1332 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1334 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1335 | case HTTP_MSG_RPVER_SP: |
| 1336 | case HTTP_MSG_RPCODE: |
| 1337 | case HTTP_MSG_RPCODE_SP: |
| 1338 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1339 | ptr = (char *)http_parse_stsline(msg, buf->data, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1340 | state, ptr, end, |
| 1341 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1342 | if (unlikely(!ptr)) |
| 1343 | return; |
| 1344 | |
| 1345 | /* we have a full response and we know that we have either a CR |
| 1346 | * or an LF at <ptr>. |
| 1347 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1349 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1350 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1351 | if (likely(*ptr == '\r')) |
| 1352 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1353 | goto http_msg_rpline_end; |
| 1354 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1356 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1357 | /* msg->sol must point to the first of CR or LF. */ |
| 1358 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1359 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1360 | /* stop here */ |
| 1361 | |
| 1362 | /* |
| 1363 | * Second, states that are specific to the request only |
| 1364 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1366 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1367 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1368 | /* we have a start of message, but we have to check |
| 1369 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1370 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1371 | */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1372 | char *beg = buf->p; |
| 1373 | |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1374 | if (likely(ptr != beg)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1375 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1376 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1377 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 1378 | bi_fast_delete(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1379 | } |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1380 | msg->sol = msg->som = ptr - buf->p; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1381 | /* we will need this when keep-alive will be supported |
| 1382 | hdr_idx_init(idx); |
| 1383 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1384 | state = HTTP_MSG_RQMETH; |
| 1385 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | } |
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(!HTTP_IS_CRLF(*ptr))) |
| 1389 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | if (unlikely(*ptr == '\n')) |
| 1392 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1393 | 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] | 1394 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1395 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1396 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1397 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1399 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1400 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1403 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | case HTTP_MSG_RQMETH_SP: |
| 1405 | case HTTP_MSG_RQURI: |
| 1406 | case HTTP_MSG_RQURI_SP: |
| 1407 | case HTTP_MSG_RQVER: |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1408 | ptr = (char *)http_parse_reqline(msg, buf->data, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1409 | state, ptr, end, |
| 1410 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (unlikely(!ptr)) |
| 1412 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | /* we have a full request and we know that we have either a CR |
| 1415 | * or an LF at <ptr>. |
| 1416 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1419 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | if (likely(*ptr == '\r')) |
| 1421 | 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] | 1422 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1425 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | /* check for HTTP/0.9 request : no version information available. |
| 1427 | * msg->sol must point to the first of CR or LF. |
| 1428 | */ |
| 1429 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1430 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1433 | 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] | 1434 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1435 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1436 | /* |
| 1437 | * Common states below |
| 1438 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1440 | http_msg_hdr_first: |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1441 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1443 | goto http_msg_hdr_name; |
| 1444 | } |
| 1445 | |
| 1446 | if (likely(*ptr == '\r')) |
| 1447 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1448 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1449 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1451 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | /* assumes msg->sol points to the first char */ |
| 1453 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1454 | 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] | 1455 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1456 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1458 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1459 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1460 | goto http_msg_invalid; |
| 1461 | |
| 1462 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1463 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1464 | |
| 1465 | /* and we still accept this non-token character */ |
| 1466 | 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] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1469 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1470 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1472 | 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] | 1473 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1475 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1478 | goto http_msg_hdr_val; |
| 1479 | } |
| 1480 | |
| 1481 | if (likely(*ptr == '\r')) |
| 1482 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1483 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1484 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1486 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1488 | 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] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1491 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1492 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1493 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1494 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1495 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | goto http_msg_hdr_l1_sp; |
| 1497 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1498 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1499 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | goto http_msg_complete_header; |
| 1501 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1503 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1504 | /* assumes msg->sol points to the first char, and msg->sov |
| 1505 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 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 | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1510 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 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 */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1528 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1529 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 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. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1535 | * Assumes msg->sol points to the first char, msg->sov points |
| 1536 | * to the first character of the value and msg->eol to the |
| 1537 | * first CR or LF so we know how the line ends. We insert last |
| 1538 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1540 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r', |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | idx, idx->tail) < 0)) |
| 1542 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1544 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1545 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1546 | goto http_msg_hdr_name; |
| 1547 | } |
| 1548 | |
| 1549 | if (likely(*ptr == '\r')) |
| 1550 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1551 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1552 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1554 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1556 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1557 | ptr++; |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1558 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1559 | msg->eoh = msg->sol; |
| 1560 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1561 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1563 | |
| 1564 | case HTTP_MSG_ERROR: |
| 1565 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1566 | break; |
| 1567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | #ifdef DEBUG_FULL |
| 1569 | default: |
| 1570 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1571 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1572 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1573 | } |
| 1574 | http_msg_ood: |
| 1575 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1576 | msg->msg_state = state; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1577 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1578 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | http_msg_invalid: |
| 1581 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1583 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1584 | return; |
| 1585 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1587 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1588 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1589 | * nothing is done and 1 is returned. |
| 1590 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1591 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1592 | { |
| 1593 | int delta; |
| 1594 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1595 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1596 | |
| 1597 | if (msg->sl.rq.v_l != 0) |
| 1598 | return 1; |
| 1599 | |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1600 | cur_end = msg->buf->p + msg->sol + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1601 | delta = 0; |
| 1602 | |
| 1603 | if (msg->sl.rq.u_l == 0) { |
| 1604 | /* if no URI was set, add "/" */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1605 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1606 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1607 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1608 | } |
| 1609 | /* add HTTP version */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1610 | delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1611 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1612 | cur_end += delta; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1613 | cur_end = (char *)http_parse_reqline(msg, msg->buf->data, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1614 | HTTP_MSG_RQMETH, |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1615 | msg->buf->p + msg->sol, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1616 | NULL, NULL); |
| 1617 | if (unlikely(!cur_end)) |
| 1618 | return 0; |
| 1619 | |
| 1620 | /* we have a full HTTP/1.0 request now and we know that |
| 1621 | * we have either a CR or an LF at <ptr>. |
| 1622 | */ |
| 1623 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1624 | return 1; |
| 1625 | } |
| 1626 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1627 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1628 | * and "keep-alive" values. If we already know that some headers may safely |
| 1629 | * be removed, we remove them now. The <to_del> flags are used for that : |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1630 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1631 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1632 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1633 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1634 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1635 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1636 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1637 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1638 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1639 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1640 | const char *hdr_val = "Connection"; |
| 1641 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1643 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1644 | return; |
| 1645 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1646 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1647 | hdr_val = "Proxy-Connection"; |
| 1648 | hdr_len = 16; |
| 1649 | } |
| 1650 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1651 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1652 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1653 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1654 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1655 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1656 | if (to_del & 2) |
| 1657 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1658 | else |
| 1659 | txn->flags |= TX_CON_KAL_SET; |
| 1660 | } |
| 1661 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1662 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1663 | if (to_del & 1) |
| 1664 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1665 | else |
| 1666 | txn->flags |= TX_CON_CLO_SET; |
| 1667 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1670 | txn->flags |= TX_HDR_CONN_PRS; |
| 1671 | return; |
| 1672 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1673 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1674 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1675 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1676 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1677 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1678 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1679 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1680 | { |
| 1681 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1682 | const char *hdr_val = "Connection"; |
| 1683 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1684 | |
| 1685 | ctx.idx = 0; |
| 1686 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1687 | |
| 1688 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1689 | hdr_val = "Proxy-Connection"; |
| 1690 | hdr_len = 16; |
| 1691 | } |
| 1692 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1693 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1694 | while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1695 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1696 | if (wanted & TX_CON_KAL_SET) |
| 1697 | txn->flags |= TX_CON_KAL_SET; |
| 1698 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1699 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1700 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1701 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1702 | if (wanted & TX_CON_CLO_SET) |
| 1703 | txn->flags |= TX_CON_CLO_SET; |
| 1704 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1705 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1706 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1707 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1708 | |
| 1709 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1710 | return; |
| 1711 | |
| 1712 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1713 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1714 | hdr_val = "Connection: close"; |
| 1715 | hdr_len = 17; |
| 1716 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1717 | hdr_val = "Proxy-Connection: close"; |
| 1718 | hdr_len = 23; |
| 1719 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1720 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1724 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1725 | hdr_val = "Connection: keep-alive"; |
| 1726 | hdr_len = 22; |
| 1727 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1728 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1729 | hdr_len = 28; |
| 1730 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1731 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1732 | } |
| 1733 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1734 | } |
| 1735 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1736 | /* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1737 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1738 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1739 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1740 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1741 | * 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] | 1742 | * 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] | 1743 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1744 | int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1745 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1746 | const struct buffer *buf = msg->buf; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1747 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1748 | const char *ptr_old = ptr; |
| 1749 | const char *end = buf->data + buf->size; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1750 | const char *stop = bi_end(buf); |
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 | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1759 | if (ptr == stop) |
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 */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1772 | if (ptr == ptr_old) |
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; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1778 | if (ptr == stop) |
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; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1791 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 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; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1806 | if (ptr == stop) |
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; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1812 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 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, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1823 | * which may or may not be present. We save that into ->next and |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1824 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1825 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1826 | msg->sov += ptr - ptr_old; |
| 1827 | msg->next = buffer_count(buf, buf->p, 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 | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1837 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1838 | * message <msg>. The first visited position is msg->next. If the end of |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 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 | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1842 | * except maybe msg->next and msg->sov if it could parse some lines, and returns |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1843 | * zero. If a parse error is encountered, the function returns < 0 and does not |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1844 | * change anything except maybe msg->next and msg->sov. Note that the message |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1852 | int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1853 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1854 | const struct buffer *buf = msg->buf; |
| 1855 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1856 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1857 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1858 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1859 | const char *ptr = b_ptr(buf, msg->next); |
| 1860 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1861 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* scan current line and stop at LF or CRLF */ |
| 1864 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1865 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1866 | return 0; |
| 1867 | |
| 1868 | if (*ptr == '\n') { |
| 1869 | if (!p1) |
| 1870 | p1 = ptr; |
| 1871 | p2 = ptr; |
| 1872 | break; |
| 1873 | } |
| 1874 | |
| 1875 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1876 | if (p1) { |
| 1877 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1878 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1879 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1880 | p1 = ptr; |
| 1881 | } |
| 1882 | |
| 1883 | ptr++; |
| 1884 | if (ptr >= buf->data + buf->size) |
| 1885 | ptr = buf->data; |
| 1886 | } |
| 1887 | |
| 1888 | /* after LF; point to beginning of next line */ |
| 1889 | p2++; |
| 1890 | if (p2 >= buf->data + buf->size) |
| 1891 | p2 = buf->data; |
| 1892 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1893 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1894 | if (bytes < 0) |
| 1895 | bytes += buf->size; |
| 1896 | |
| 1897 | /* schedule this line for forwarding */ |
| 1898 | msg->sov += bytes; |
| 1899 | if (msg->sov >= buf->size) |
| 1900 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1901 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 1902 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1903 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1904 | * Everything was scheduled for forwarding, there's nothing |
| 1905 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1906 | */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1907 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1908 | msg->msg_state = HTTP_MSG_DONE; |
| 1909 | return 1; |
| 1910 | } |
| 1911 | /* OK, next line then */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1912 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 1917 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1918 | * ->som, ->next in order to include this part into the next forwarding phase. |
| 1919 | * Note that the caller must ensure that ->p points to the first byte to parse. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1920 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1921 | * not enough data are available, the function does not change anything and |
| 1922 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1923 | * does not change anything. Note: this function is designed to parse wrapped |
| 1924 | * CRLF at the end of the buffer. |
| 1925 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1926 | int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1927 | { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1928 | const struct buffer *buf = msg->buf; |
| 1929 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1930 | int bytes; |
| 1931 | |
| 1932 | /* NB: we'll check data availabilty at the end. It's not a |
| 1933 | * problem because whatever we match first will be checked |
| 1934 | * against the correct length. |
| 1935 | */ |
| 1936 | bytes = 1; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1937 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1938 | if (*ptr == '\r') { |
| 1939 | bytes++; |
| 1940 | ptr++; |
| 1941 | if (ptr >= buf->data + buf->size) |
| 1942 | ptr = buf->data; |
| 1943 | } |
| 1944 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1945 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1946 | return 0; |
| 1947 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1948 | if (*ptr != '\n') { |
| 1949 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1950 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1951 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1952 | |
| 1953 | ptr++; |
| 1954 | if (ptr >= buf->data + buf->size) |
| 1955 | ptr = buf->data; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1956 | /* prepare the CRLF to be forwarded (between ->som and ->sov) */ |
| 1957 | msg->som = 0; |
| 1958 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1959 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1960 | return 1; |
| 1961 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1962 | |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 1963 | /* This function realigns a possibly wrapping http message at the beginning |
| 1964 | * of its buffer. The function may only be used when the buffer's tail is |
| 1965 | * empty. |
| 1966 | */ |
| 1967 | void http_message_realign(struct http_msg *msg) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1968 | { |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 1969 | struct buffer *buf = msg->buf; |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1970 | int off = buf->data + buf->size - buf->p; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1971 | |
| 1972 | /* two possible cases : |
| 1973 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1974 | * - 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] | 1975 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 1976 | if (buf->i) { |
| 1977 | int block1 = buf->i; |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1978 | int block2 = 0; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1979 | if (buf->p + buf->i > buf->data + buf->size) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1980 | /* non-contiguous block */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1981 | block1 = buf->data + buf->size - buf->p; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1982 | block2 = buf->p + buf->i - (buf->data + buf->size); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1983 | } |
| 1984 | if (block2) |
| 1985 | memcpy(swap_buffer, buf->data, block2); |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1986 | memmove(buf->data, buf->p, block1); |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 1987 | if (block2) |
| 1988 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1989 | } |
| 1990 | |
| 1991 | /* adjust all known pointers */ |
Willy Tarreau | 89fa706 | 2012-03-02 16:13:16 +0100 | [diff] [blame] | 1992 | buf->p = buf->data; |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1993 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 1994 | if (msg->err_pos >= 0) { |
| 1995 | msg->err_pos += off; |
| 1996 | if (msg->err_pos >= buf->size) |
| 1997 | msg->err_pos -= buf->size; |
| 1998 | } |
| 1999 | |
| 2000 | buf->flags &= ~BF_FULL; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 2001 | if (bi_full(buf)) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2002 | buf->flags |= BF_FULL; |
| 2003 | } |
| 2004 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2005 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2006 | * processing can continue on next analysers, or zero if it either needs more |
| 2007 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2008 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2009 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2010 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2011 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2012 | 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] | 2013 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2014 | /* |
| 2015 | * We will parse the partial (or complete) lines. |
| 2016 | * We will check the request syntax, and also join multi-line |
| 2017 | * headers. An index of all the lines will be elaborated while |
| 2018 | * parsing. |
| 2019 | * |
| 2020 | * For the parsing, we use a 28 states FSM. |
| 2021 | * |
| 2022 | * Here is the information we currently have : |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2023 | * req->p + msg->som = beginning of request |
| 2024 | * req->p + msg->eoh = end of processed headers / start of current one |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2025 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2026 | * msg->next = first non-visited byte |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2027 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2028 | * |
| 2029 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2030 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2031 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2032 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2033 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2034 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2035 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2036 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2037 | struct http_txn *txn = &s->txn; |
| 2038 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2039 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2040 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2041 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2042 | now_ms, __FUNCTION__, |
| 2043 | s, |
| 2044 | req, |
| 2045 | req->rex, req->wex, |
| 2046 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2047 | req->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2048 | req->analysers); |
| 2049 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2050 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2051 | s->srv_error = http_return_srv_error; |
| 2052 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2053 | /* There's a protected area at the end of the buffer for rewriting |
| 2054 | * purposes. We don't want to start to parse the request if the |
| 2055 | * protected area is affected, because we may have to move processed |
| 2056 | * data later, which is much more complicated. |
| 2057 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2058 | if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2059 | if ((txn->flags & TX_NOT_FIRST) && |
| 2060 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2061 | bi_end(req) < b_ptr(req, msg->next) || |
| 2062 | bi_end(req) > req->data + req->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2063 | if (req->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2064 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2065 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2066 | /* some data has still not left the buffer, wake us once that's done */ |
| 2067 | buffer_dont_connect(req); |
| 2068 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2069 | return 0; |
| 2070 | } |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2071 | if (bi_end(req) < b_ptr(req, msg->next) || |
| 2072 | bi_end(req) > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 2073 | http_message_realign(msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2074 | } |
| 2075 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2076 | /* Note that we have the same problem with the response ; we |
| 2077 | * may want to send a redirect, error or anything which requires |
| 2078 | * some spare space. So we'll ensure that we have at least |
| 2079 | * maxrewrite bytes available in the response buffer before |
| 2080 | * processing that one. This will only affect pipelined |
| 2081 | * keep-alive requests. |
| 2082 | */ |
| 2083 | if ((txn->flags & TX_NOT_FIRST) && |
| 2084 | unlikely((s->rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 2085 | bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) || |
| 2086 | bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 2087 | if (s->rep->o) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2088 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2089 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2090 | /* don't let a connection request be initiated */ |
| 2091 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2092 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2093 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2094 | return 0; |
| 2095 | } |
| 2096 | } |
| 2097 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2098 | if (likely(msg->next < req->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2099 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2102 | /* 1: we might have to print this header in debug mode */ |
| 2103 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2104 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2105 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2106 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2107 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2108 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2109 | sol = req->p + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2110 | eol = sol + msg->sl.rq.l; |
| 2111 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2112 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2113 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2114 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2115 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2116 | while (cur_idx) { |
| 2117 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2118 | debug_hdr("clihdr", s, sol, eol); |
| 2119 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2120 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2121 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2122 | } |
| 2123 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2124 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2125 | /* |
| 2126 | * Now we quickly check if we have found a full valid request. |
| 2127 | * If not so, we check the FD and buffer states before leaving. |
| 2128 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2129 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2130 | * requests are checked first. When waiting for a second request |
| 2131 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2132 | * we note the error in the session flags but don't set any state. |
| 2133 | * Since the error will be noted there, it will not be counted by |
| 2134 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2135 | * Last, we may increase some tracked counters' http request errors on |
| 2136 | * the cases that are deliberately the client's fault. For instance, |
| 2137 | * a timeout or connection reset is not counted as an error. However |
| 2138 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2139 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2140 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2141 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2142 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2143 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2144 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2145 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2146 | session_inc_http_req_ctr(s); |
| 2147 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2148 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2149 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2150 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2151 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2152 | /* 1: Since we are in header mode, if there's no space |
| 2153 | * left for headers, we won't be able to free more |
| 2154 | * later, so the session will never terminate. We |
| 2155 | * must terminate it now. |
| 2156 | */ |
| 2157 | if (unlikely(req->flags & BF_FULL)) { |
| 2158 | /* FIXME: check if URI is set and return Status |
| 2159 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2160 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2161 | session_inc_http_req_ctr(s); |
| 2162 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2163 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2164 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2165 | msg->err_pos = req->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2166 | goto return_bad_req; |
| 2167 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2168 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2169 | /* 2: have we encountered a read error ? */ |
| 2170 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2171 | if (!(s->flags & SN_ERR_MASK)) |
| 2172 | s->flags |= SN_ERR_CLICL; |
| 2173 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2174 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2175 | goto failed_keep_alive; |
| 2176 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2177 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2178 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2179 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2180 | session_inc_http_err_ctr(s); |
| 2181 | } |
| 2182 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2183 | msg->msg_state = HTTP_MSG_ERROR; |
| 2184 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2185 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2186 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2187 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2188 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2189 | if (s->listener->counters) |
| 2190 | s->listener->counters->failed_req++; |
| 2191 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2192 | if (!(s->flags & SN_FINST_MASK)) |
| 2193 | s->flags |= SN_FINST_R; |
| 2194 | return 0; |
| 2195 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2196 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2197 | /* 3: has the read timeout expired ? */ |
| 2198 | 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] | 2199 | if (!(s->flags & SN_ERR_MASK)) |
| 2200 | s->flags |= SN_ERR_CLITO; |
| 2201 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2202 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2203 | goto failed_keep_alive; |
| 2204 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2205 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2206 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2207 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2208 | session_inc_http_err_ctr(s); |
| 2209 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2210 | txn->status = 408; |
| 2211 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2212 | msg->msg_state = HTTP_MSG_ERROR; |
| 2213 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2214 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2215 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2216 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2217 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2218 | if (s->listener->counters) |
| 2219 | s->listener->counters->failed_req++; |
| 2220 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2221 | if (!(s->flags & SN_FINST_MASK)) |
| 2222 | s->flags |= SN_FINST_R; |
| 2223 | return 0; |
| 2224 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2225 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2226 | /* 4: have we encountered a close ? */ |
| 2227 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2228 | if (!(s->flags & SN_ERR_MASK)) |
| 2229 | s->flags |= SN_ERR_CLICL; |
| 2230 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2231 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2232 | goto failed_keep_alive; |
| 2233 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2234 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2235 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2236 | txn->status = 400; |
| 2237 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2238 | msg->msg_state = HTTP_MSG_ERROR; |
| 2239 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2240 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2241 | session_inc_http_err_ctr(s); |
| 2242 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2243 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2244 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2245 | if (s->listener->counters) |
| 2246 | s->listener->counters->failed_req++; |
| 2247 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2248 | if (!(s->flags & SN_FINST_MASK)) |
| 2249 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2250 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2253 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2254 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2255 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2256 | #ifdef TCP_QUICKACK |
| 2257 | if (s->listener->options & LI_O_NOQUICKACK) { |
| 2258 | /* We need more data, we have to re-enable quick-ack in case we |
| 2259 | * previously disabled it, otherwise we might cause the client |
| 2260 | * to delay next data. |
| 2261 | */ |
| 2262 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 2263 | } |
| 2264 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2265 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2266 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2267 | /* If the client starts to talk, let's fall back to |
| 2268 | * request timeout processing. |
| 2269 | */ |
| 2270 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2271 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2274 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2275 | if (!tick_isset(req->analyse_exp)) { |
| 2276 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2277 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2278 | tick_isset(s->be->timeout.httpka)) |
| 2279 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2280 | else |
| 2281 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2282 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2283 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2284 | /* we're not ready yet */ |
| 2285 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2286 | |
| 2287 | failed_keep_alive: |
| 2288 | /* Here we process low-level errors for keep-alive requests. In |
| 2289 | * short, if the request is not the first one and it experiences |
| 2290 | * a timeout, read error or shutdown, we just silently close so |
| 2291 | * that the client can try again. |
| 2292 | */ |
| 2293 | txn->status = 0; |
| 2294 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2295 | req->analysers = 0; |
| 2296 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2297 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2298 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2299 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2300 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2301 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2302 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2303 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2304 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2305 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 2306 | * points to the CRLF of the request line. msg->next points to the first |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 2307 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2308 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2309 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2310 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2311 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2312 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2313 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2314 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2315 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2316 | /* kill the pending keep-alive timeout */ |
| 2317 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2318 | req->analyse_exp = TICK_ETERNITY; |
| 2319 | } |
| 2320 | |
| 2321 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2322 | /* Maybe we found in invalid header name while we were configured not |
| 2323 | * to block on that, so we have to capture it now. |
| 2324 | */ |
| 2325 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2326 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2327 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2328 | /* |
| 2329 | * 1: identify the method |
| 2330 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2331 | txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2332 | |
| 2333 | /* we can make use of server redirect on GET and HEAD */ |
| 2334 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2335 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2336 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2337 | /* |
| 2338 | * 2: check if the URI matches the monitor_uri. |
| 2339 | * We have to do this for every request which gets in, because |
| 2340 | * the monitor-uri is defined by the frontend. |
| 2341 | */ |
| 2342 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2343 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2344 | !memcmp(req->p + msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2345 | s->fe->monitor_uri, |
| 2346 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2347 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2349 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2350 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2351 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2352 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2353 | s->fe->fe_counters.intercepted_req++; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2354 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2355 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2356 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2357 | 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] | 2358 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2359 | ret = acl_pass(ret); |
| 2360 | if (cond->pol == ACL_COND_UNLESS) |
| 2361 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2362 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2363 | if (ret) { |
| 2364 | /* we fail this request, let's return 503 service unavail */ |
| 2365 | txn->status = 503; |
| 2366 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2367 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2368 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2369 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2370 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2371 | /* nothing to fail, let's reply normaly */ |
| 2372 | txn->status = 200; |
Willy Tarreau | ae94d4d | 2011-05-11 16:28:49 +0200 | [diff] [blame] | 2373 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2374 | goto return_prx_cond; |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2379 | * Note: we cannot log anymore if the request has been |
| 2380 | * classified as invalid. |
| 2381 | */ |
| 2382 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2383 | /* we have a complete HTTP request that we must log */ |
| 2384 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2385 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2386 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2387 | if (urilen >= REQURI_LEN) |
| 2388 | urilen = REQURI_LEN - 1; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2389 | memcpy(txn->uri, &req->p[msg->som], urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2390 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2391 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2392 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2393 | s->do_log(s); |
| 2394 | } else { |
| 2395 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2396 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2397 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2398 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2399 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2400 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2401 | } |
| 2402 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2403 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 2404 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2405 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2406 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2407 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2408 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2409 | ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') || |
| 2410 | ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') && |
| 2411 | (req->p[msg->sol + msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2412 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2413 | |
| 2414 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2415 | 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] | 2416 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2417 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2418 | * we have what looks like a proxied connection instead of a connection, |
| 2419 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2420 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2421 | * happen to do that despite being non-standard :-( |
| 2422 | * We consider that a request not beginning with either '/' or '*' is |
| 2423 | * a proxied connection, which covers both "scheme://location" and |
| 2424 | * CONNECT ip:port. |
| 2425 | */ |
| 2426 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2427 | req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*') |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2428 | txn->flags |= TX_USE_PX_CONN; |
| 2429 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2430 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2431 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2432 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2433 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2434 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2435 | capture_headers(req->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2436 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2437 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2438 | /* 6: determine the transfer-length. |
| 2439 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2440 | * the presence of a message-body in a REQUEST and its transfer length |
| 2441 | * must be determined that way (in order of precedence) : |
| 2442 | * 1. The presence of a message-body in a request is signaled by the |
| 2443 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2444 | * in the request's header fields. When a request message contains |
| 2445 | * both a message-body of non-zero length and a method that does |
| 2446 | * not define any semantics for that request message-body, then an |
| 2447 | * origin server SHOULD either ignore the message-body or respond |
| 2448 | * with an appropriate error message (e.g., 413). A proxy or |
| 2449 | * gateway, when presented the same request, SHOULD either forward |
| 2450 | * the request inbound with the message- body or ignore the |
| 2451 | * message-body when determining a response. |
| 2452 | * |
| 2453 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2454 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2455 | * transfer-length is defined by the use of this transfer-coding. |
| 2456 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2457 | * transfer-coding is not present, the transfer-length is defined |
| 2458 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2459 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2460 | * 3. If a Content-Length header field is present, its decimal value in |
| 2461 | * OCTETs represents both the entity-length and the transfer-length. |
| 2462 | * If a message is received with both a Transfer-Encoding header |
| 2463 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2464 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2465 | * 4. By the server closing the connection. (Closing the connection |
| 2466 | * cannot be used to indicate the end of a request body, since that |
| 2467 | * would leave no possibility for the server to send back a response.) |
| 2468 | * |
| 2469 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2470 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2471 | * it is terminated by closing the connection. When the "chunked" |
| 2472 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2473 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2474 | */ |
| 2475 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2476 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2477 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2478 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2479 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2480 | http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2481 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2482 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2483 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2484 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2485 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2486 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2487 | break; |
| 2488 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2489 | } |
| 2490 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2491 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2492 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2493 | http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2494 | signed long long cl; |
| 2495 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2496 | if (!ctx.vlen) { |
| 2497 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2498 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2499 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2500 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2501 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 2502 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2503 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2504 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2505 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2506 | if (cl < 0) { |
| 2507 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2508 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2509 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2510 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2511 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2512 | msg->err_pos = ctx.line + ctx.val - req->data; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2513 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2514 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2515 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2516 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2517 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2518 | } |
| 2519 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2520 | /* bodyless requests have a known length */ |
| 2521 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2522 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2523 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2524 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2525 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2526 | req->analyse_exp = TICK_ETERNITY; |
| 2527 | return 1; |
| 2528 | |
| 2529 | return_bad_req: |
| 2530 | /* We centralize bad requests processing here */ |
| 2531 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2532 | /* we detected a parsing error. We want to archive this request |
| 2533 | * in the dedicated proxy area for later troubleshooting. |
| 2534 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2535 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2539 | txn->status = 400; |
| 2540 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2541 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2542 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2543 | if (s->listener->counters) |
| 2544 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2545 | |
| 2546 | return_prx_cond: |
| 2547 | if (!(s->flags & SN_ERR_MASK)) |
| 2548 | s->flags |= SN_ERR_PRXCOND; |
| 2549 | if (!(s->flags & SN_FINST_MASK)) |
| 2550 | s->flags |= SN_FINST_R; |
| 2551 | |
| 2552 | req->analysers = 0; |
| 2553 | req->analyse_exp = TICK_ETERNITY; |
| 2554 | return 0; |
| 2555 | } |
| 2556 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2557 | /* We reached the stats page through a POST request. |
| 2558 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2559 | * 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] | 2560 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2561 | 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] | 2562 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2563 | struct proxy *px = NULL; |
| 2564 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2565 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2566 | char key[LINESIZE]; |
| 2567 | int action = ST_ADM_ACTION_NONE; |
| 2568 | int reprocess = 0; |
| 2569 | |
| 2570 | int total_servers = 0; |
| 2571 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2572 | |
| 2573 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2574 | char *st_cur_param = NULL; |
| 2575 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2576 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 2577 | first_param = req->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2578 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2579 | |
| 2580 | cur_param = next_param = end_params; |
| 2581 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2582 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2583 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2584 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2585 | return 1; |
| 2586 | } |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 2587 | else if (end_params > req->p + req->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2588 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2589 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2590 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | *end_params = '\0'; |
| 2594 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2595 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2596 | |
| 2597 | /* |
| 2598 | * Parse the parameters in reverse order to only store the last value. |
| 2599 | * From the html form, the backend and the action are at the end. |
| 2600 | */ |
| 2601 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2602 | char *value; |
| 2603 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2604 | |
| 2605 | cur_param--; |
| 2606 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2607 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2608 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2609 | poffset = (cur_param != first_param ? 1 : 0); |
| 2610 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2611 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2612 | strncpy(key, cur_param + poffset, plen); |
| 2613 | key[plen - 1] = '\0'; |
| 2614 | } else { |
| 2615 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2616 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | /* Parse the value */ |
| 2620 | value = key; |
| 2621 | while (*value != '\0' && *value != '=') { |
| 2622 | value++; |
| 2623 | } |
| 2624 | if (*value == '=') { |
| 2625 | /* Ok, a value is found, we can mark the end of the key */ |
| 2626 | *value++ = '\0'; |
| 2627 | } |
| 2628 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2629 | if (!url_decode(key) || !url_decode(value)) |
| 2630 | break; |
| 2631 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2632 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2633 | if (!px && (strcmp(key, "b") == 0)) { |
| 2634 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2635 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2636 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2637 | goto out; |
| 2638 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2639 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2640 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2641 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2642 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2643 | } |
| 2644 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2645 | action = ST_ADM_ACTION_ENABLE; |
| 2646 | } |
| 2647 | else { |
| 2648 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2649 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2653 | if (!(px && action)) { |
| 2654 | /* |
| 2655 | * Indicates that we'll need to reprocess the parameters |
| 2656 | * as soon as backend and action are known |
| 2657 | */ |
| 2658 | if (!reprocess) { |
| 2659 | st_cur_param = cur_param; |
| 2660 | st_next_param = next_param; |
| 2661 | } |
| 2662 | reprocess = 1; |
| 2663 | } |
| 2664 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2665 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2666 | case ST_ADM_ACTION_DISABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2667 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2668 | /* Not already in maintenance, we can change the server state */ |
| 2669 | sv->state |= SRV_MAINTAIN; |
| 2670 | set_server_down(sv); |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2671 | altered_servers++; |
| 2672 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2673 | } |
| 2674 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2675 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2676 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2677 | /* Already in maintenance, we can change the server state */ |
| 2678 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2679 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2680 | altered_servers++; |
| 2681 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2682 | } |
| 2683 | break; |
| 2684 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2685 | } else { |
| 2686 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2687 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2688 | } |
| 2689 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2690 | if (reprocess && px && action) { |
| 2691 | /* Now, we know the backend and the action chosen by the user. |
| 2692 | * We can safely restart from the first server parameter |
| 2693 | * to reprocess them |
| 2694 | */ |
| 2695 | cur_param = st_cur_param; |
| 2696 | next_param = st_next_param; |
| 2697 | reprocess = 0; |
| 2698 | goto reprocess_servers; |
| 2699 | } |
| 2700 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2701 | next_param = cur_param; |
| 2702 | } |
| 2703 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2704 | |
| 2705 | if (total_servers == 0) { |
| 2706 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2707 | } |
| 2708 | else if (altered_servers == 0) { |
| 2709 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2710 | } |
| 2711 | else if (altered_servers == total_servers) { |
| 2712 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2713 | } |
| 2714 | else { |
| 2715 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2716 | } |
| 2717 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2718 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2719 | } |
| 2720 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2721 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2722 | * or NULL if everything's OK. |
| 2723 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2724 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2725 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2726 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2727 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2728 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2729 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2730 | int ret = 1; |
| 2731 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2732 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2733 | continue; |
| 2734 | |
| 2735 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2736 | if (rule->cond) { |
| 2737 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2738 | ret = acl_pass(ret); |
| 2739 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2740 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2741 | ret = !ret; |
| 2742 | } |
| 2743 | |
| 2744 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2745 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2746 | return NULL; /* no problem */ |
| 2747 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2748 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | return NULL; |
| 2752 | } |
| 2753 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2754 | /* This stream analyser runs all HTTP request processing which is common to |
| 2755 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2756 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2757 | * 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] | 2758 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2759 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2760 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2761 | 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] | 2762 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2763 | struct http_txn *txn = &s->txn; |
| 2764 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2766 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2767 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2768 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2769 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2770 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2771 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2772 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2773 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2774 | return 0; |
| 2775 | } |
| 2776 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2777 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2778 | req->analyse_exp = TICK_ETERNITY; |
| 2779 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2780 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2781 | now_ms, __FUNCTION__, |
| 2782 | s, |
| 2783 | req, |
| 2784 | req->rex, req->wex, |
| 2785 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2786 | req->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2787 | req->analysers); |
| 2788 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2789 | /* first check whether we have some ACLs set to block this request */ |
| 2790 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2791 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2792 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2793 | ret = acl_pass(ret); |
| 2794 | if (cond->pol == ACL_COND_UNLESS) |
| 2795 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2796 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2797 | if (ret) { |
| 2798 | txn->status = 403; |
| 2799 | /* let's log the request time */ |
| 2800 | s->logs.tv_request = now; |
| 2801 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2802 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2803 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2804 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2805 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2806 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2807 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2808 | 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] | 2809 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2810 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2811 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2812 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 2813 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2814 | 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] | 2815 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2816 | else |
| 2817 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2818 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2819 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2820 | 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] | 2821 | txn->status = 403; |
| 2822 | s->logs.tv_request = now; |
| 2823 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2824 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 2825 | s->fe->fe_counters.denied_req++; |
| 2826 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 2827 | s->be->be_counters.denied_req++; |
| 2828 | if (s->listener->counters) |
| 2829 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2830 | goto return_prx_cond; |
| 2831 | } |
| 2832 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | /* try headers filters */ |
| 2834 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2835 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2836 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2837 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2838 | /* has the request been denied ? */ |
| 2839 | if (txn->flags & TX_CLDENY) { |
| 2840 | /* no need to go further */ |
| 2841 | txn->status = 403; |
| 2842 | /* let's log the request time */ |
| 2843 | s->logs.tv_request = now; |
| 2844 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2845 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2846 | goto return_prx_cond; |
| 2847 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2848 | |
| 2849 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2850 | * which may be the same as the connect timeout if unspecified. |
| 2851 | * If unset, then set it to zero because we really want it to |
| 2852 | * eventually expire. We build the tarpit as an analyser. |
| 2853 | */ |
| 2854 | if (txn->flags & TX_CLTARPIT) { |
| 2855 | buffer_erase(s->req); |
| 2856 | /* wipe the request out so that we can drop the connection early |
| 2857 | * if the client closes first. |
| 2858 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2859 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2860 | req->analysers = 0; /* remove switching rules etc... */ |
| 2861 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2862 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2863 | if (!req->analyse_exp) |
| 2864 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2865 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2866 | return 1; |
| 2867 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2868 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2869 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2870 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2871 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2872 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2873 | * 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] | 2874 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2875 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2876 | * The method consists in checking if options changed between the two calls |
| 2877 | * (implying that either one is non-null, or one of them is non-null and we |
| 2878 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2879 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2880 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2881 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2882 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2883 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2884 | (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] | 2885 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2886 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2887 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 2888 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2889 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2890 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2891 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2892 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2893 | tmp = TX_CON_WANT_CLO; |
| 2894 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2895 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2896 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2897 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2898 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2899 | /* parse the Connection header and possibly clean it */ |
| 2900 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2901 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2902 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2903 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2904 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2905 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2906 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2907 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2908 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2909 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2910 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2911 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2912 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2913 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2914 | (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 2915 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2916 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2917 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2918 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2919 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2921 | /* we can be blocked here because the request needs to be authenticated, |
| 2922 | * either to pass or to access stats. |
| 2923 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2924 | 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] | 2925 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2926 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2927 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2928 | if (!realm) |
| 2929 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2930 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2931 | 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] | 2932 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2933 | txn->status = 401; |
| 2934 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2935 | /* on 401 we still count one error, because normal browsing |
| 2936 | * won't significantly increase the counter but brute force |
| 2937 | * attempts will. |
| 2938 | */ |
| 2939 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2940 | goto return_prx_cond; |
| 2941 | } |
| 2942 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2943 | /* add request headers from the rule sets in the same order */ |
| 2944 | list_for_each_entry(wl, &px->req_add, list) { |
| 2945 | if (wl->cond) { |
| 2946 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2947 | ret = acl_pass(ret); |
| 2948 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2949 | ret = !ret; |
| 2950 | if (!ret) |
| 2951 | continue; |
| 2952 | } |
| 2953 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 2954 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2955 | goto return_bad_req; |
| 2956 | } |
| 2957 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2958 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2959 | struct stats_admin_rule *stats_admin_rule; |
| 2960 | |
| 2961 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2962 | * FIXME!!! that one is rather dangerous, we want to |
| 2963 | * make it follow standard rules (eg: clear req->analysers). |
| 2964 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2965 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2966 | /* now check whether we have some admin rules for this request */ |
| 2967 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 2968 | int ret = 1; |
| 2969 | |
| 2970 | if (stats_admin_rule->cond) { |
| 2971 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 2972 | ret = acl_pass(ret); |
| 2973 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 2974 | ret = !ret; |
| 2975 | } |
| 2976 | |
| 2977 | if (ret) { |
| 2978 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2979 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 2980 | break; |
| 2981 | } |
| 2982 | } |
| 2983 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2984 | /* Was the status page requested with a POST ? */ |
| 2985 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2986 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2987 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 2988 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 2989 | * send an HTTP/1.1 100 Continue intermediate response. |
| 2990 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2991 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2992 | struct hdr_ctx ctx; |
| 2993 | ctx.idx = 0; |
| 2994 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 2995 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2996 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 2997 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2998 | } |
| 2999 | } |
| 3000 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3001 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3002 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3003 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3004 | /* we need more data */ |
| 3005 | req->analysers |= an_bit; |
| 3006 | buffer_dont_connect(req); |
| 3007 | return 0; |
| 3008 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3009 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3010 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3011 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3012 | } |
| 3013 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3014 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3015 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3016 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3017 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3018 | s->rep->prod->applet.private = s; |
| 3019 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3020 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3021 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3022 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3023 | |
| 3024 | return 0; |
| 3025 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3026 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3027 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3028 | /* check whether we have some ACLs set to redirect this request */ |
| 3029 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3030 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3031 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3032 | if (rule->cond) { |
| 3033 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3034 | ret = acl_pass(ret); |
| 3035 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3036 | ret = !ret; |
| 3037 | } |
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 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3040 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3041 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3042 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3043 | /* build redirect message */ |
| 3044 | switch(rule->code) { |
| 3045 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3046 | msg_fmt = HTTP_303; |
| 3047 | break; |
| 3048 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3049 | msg_fmt = HTTP_301; |
| 3050 | break; |
| 3051 | case 302: |
| 3052 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3053 | msg_fmt = HTTP_302; |
| 3054 | break; |
| 3055 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3057 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3058 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3059 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3060 | switch(rule->type) { |
| 3061 | case REDIRECT_TYPE_PREFIX: { |
| 3062 | const char *path; |
| 3063 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3064 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3065 | path = http_get_path(txn); |
| 3066 | /* build message using path */ |
| 3067 | if (path) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3068 | pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3069 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3070 | int qs = 0; |
| 3071 | while (qs < pathlen) { |
| 3072 | if (path[qs] == '?') { |
| 3073 | pathlen = qs; |
| 3074 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3075 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3076 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3077 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3078 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3079 | } else { |
| 3080 | path = "/"; |
| 3081 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3082 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3083 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3084 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3085 | goto return_bad_req; |
| 3086 | |
| 3087 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3088 | * add anything, otherwise it makes it hard for the user to |
| 3089 | * configure a self-redirection. |
| 3090 | */ |
| 3091 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3092 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3093 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3094 | } |
| 3095 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3096 | /* add path */ |
| 3097 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3098 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3099 | |
| 3100 | /* append a slash at the end of the location is needed and missing */ |
| 3101 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3102 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3103 | if (rdr.len > rdr.size - 5) |
| 3104 | goto return_bad_req; |
| 3105 | rdr.str[rdr.len] = '/'; |
| 3106 | rdr.len++; |
| 3107 | } |
| 3108 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3109 | break; |
| 3110 | } |
| 3111 | case REDIRECT_TYPE_LOCATION: |
| 3112 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3113 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3114 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3115 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3116 | /* add location */ |
| 3117 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3118 | rdr.len += rule->rdr_len; |
| 3119 | break; |
| 3120 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3121 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3122 | if (rule->cookie_len) { |
| 3123 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3124 | rdr.len += 14; |
| 3125 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3126 | rdr.len += rule->cookie_len; |
| 3127 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3128 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3129 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3130 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3131 | /* add end of headers and the keep-alive/close status. |
| 3132 | * We may choose to set keep-alive if the Location begins |
| 3133 | * with a slash, because the client will come back to the |
| 3134 | * same server. |
| 3135 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3136 | txn->status = rule->code; |
| 3137 | /* let's log the request time */ |
| 3138 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3139 | |
| 3140 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3141 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3142 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3143 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3144 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3145 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3146 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3147 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3148 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3149 | rdr.len += 30; |
| 3150 | } else { |
| 3151 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3152 | rdr.len += 24; |
| 3153 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3154 | } |
| 3155 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3156 | rdr.len += 4; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3157 | bo_inject(req->prod->ob, rdr.str, rdr.len); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3158 | /* "eat" the request */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3159 | bi_fast_delete(req, msg->sov - msg->som); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3160 | msg->som = msg->sov; |
| 3161 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3162 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3163 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3164 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3165 | break; |
| 3166 | } else { |
| 3167 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3168 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3169 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3170 | rdr.len += 29; |
| 3171 | } else { |
| 3172 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3173 | rdr.len += 23; |
| 3174 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3175 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3176 | goto return_prx_cond; |
| 3177 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3178 | } |
| 3179 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3180 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3181 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3182 | * If this happens, then the data will not come immediately, so we must |
| 3183 | * send all what we have without waiting. Note that due to the small gain |
| 3184 | * in waiting for the body of the request, it's easier to simply put the |
| 3185 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3186 | * itself once used. |
| 3187 | */ |
| 3188 | req->flags |= BF_SEND_DONTWAIT; |
| 3189 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3190 | /* that's OK for us now, let's move on to next analysers */ |
| 3191 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3192 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3193 | return_bad_req: |
| 3194 | /* We centralize bad requests processing here */ |
| 3195 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3196 | /* we detected a parsing error. We want to archive this request |
| 3197 | * in the dedicated proxy area for later troubleshooting. |
| 3198 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3199 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3200 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3202 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3203 | txn->status = 400; |
| 3204 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3206 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3207 | if (s->listener->counters) |
| 3208 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3209 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3210 | return_prx_cond: |
| 3211 | if (!(s->flags & SN_ERR_MASK)) |
| 3212 | s->flags |= SN_ERR_PRXCOND; |
| 3213 | if (!(s->flags & SN_FINST_MASK)) |
| 3214 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3215 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3216 | req->analysers = 0; |
| 3217 | req->analyse_exp = TICK_ETERNITY; |
| 3218 | return 0; |
| 3219 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3220 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3221 | /* This function performs all the processing enabled for the current request. |
| 3222 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3223 | * needs more data, encounters an error, or wants to immediately abort the |
| 3224 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3225 | */ |
| 3226 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3227 | { |
| 3228 | struct http_txn *txn = &s->txn; |
| 3229 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3230 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3231 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3232 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3233 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3234 | return 0; |
| 3235 | } |
| 3236 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3237 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3238 | now_ms, __FUNCTION__, |
| 3239 | s, |
| 3240 | req, |
| 3241 | req->rex, req->wex, |
| 3242 | req->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3243 | req->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3244 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3245 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3246 | /* |
| 3247 | * Right now, we know that we have processed the entire headers |
| 3248 | * and that unwanted requests have been filtered out. We can do |
| 3249 | * whatever we want with the remaining request. Also, now we |
| 3250 | * may have separate values for ->fe, ->be. |
| 3251 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3252 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3253 | /* |
| 3254 | * If HTTP PROXY is set we simply get remote server address |
| 3255 | * parsing incoming request. |
| 3256 | */ |
| 3257 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3258 | url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3259 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3260 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3261 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3262 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3263 | * Note that doing so might move headers in the request, but |
| 3264 | * the fields will stay coherent and the URI will not move. |
| 3265 | * This should only be performed in the backend. |
| 3266 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3267 | 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] | 3268 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3269 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3270 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3271 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3272 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3273 | * so let's do the same now. |
| 3274 | */ |
| 3275 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3276 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3277 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3278 | get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3279 | } |
| 3280 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3281 | /* add unique-id if "header-unique-id" is specified */ |
| 3282 | |
| 3283 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3284 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3285 | |
| 3286 | if (s->fe->header_unique_id && s->unique_id) { |
| 3287 | int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3288 | if (ret < 0 || ret > global.tune.bufsize) |
| 3289 | goto return_bad_req; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3290 | if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0)) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3291 | goto return_bad_req; |
| 3292 | } |
| 3293 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3294 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3295 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3296 | * asks for it. |
| 3297 | */ |
| 3298 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3299 | struct hdr_ctx ctx = { .idx = 0 }; |
| 3300 | |
| 3301 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3302 | http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3303 | /* The header is set to be added only if none is present |
| 3304 | * and we found it, so don't do anything. |
| 3305 | */ |
| 3306 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3307 | else if (s->req->prod->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3308 | /* Add an X-Forwarded-For header unless the source IP is |
| 3309 | * in the 'except' network range. |
| 3310 | */ |
| 3311 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3312 | (((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] | 3313 | != s->fe->except_net.s_addr) && |
| 3314 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3315 | (((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] | 3316 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3317 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3318 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3319 | 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] | 3320 | |
| 3321 | /* Note: we rely on the backend to get the header name to be used for |
| 3322 | * x-forwarded-for, because the header is really meant for the backends. |
| 3323 | * However, if the backend did not specify any option, we have to rely |
| 3324 | * on the frontend's header name. |
| 3325 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3326 | if (s->be->fwdfor_hdr_len) { |
| 3327 | len = s->be->fwdfor_hdr_len; |
| 3328 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3329 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3330 | len = s->fe->fwdfor_hdr_len; |
| 3331 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3332 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3333 | 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] | 3334 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3335 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3336 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3337 | } |
| 3338 | } |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3339 | else if (s->req->prod->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3340 | /* FIXME: for the sake of completeness, we should also support |
| 3341 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3342 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3343 | int len; |
| 3344 | char pn[INET6_ADDRSTRLEN]; |
| 3345 | inet_ntop(AF_INET6, |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3346 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3347 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3348 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3349 | /* Note: we rely on the backend to get the header name to be used for |
| 3350 | * x-forwarded-for, because the header is really meant for the backends. |
| 3351 | * However, if the backend did not specify any option, we have to rely |
| 3352 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3353 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3354 | if (s->be->fwdfor_hdr_len) { |
| 3355 | len = s->be->fwdfor_hdr_len; |
| 3356 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3357 | } else { |
| 3358 | len = s->fe->fwdfor_hdr_len; |
| 3359 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3360 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3361 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3362 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3363 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3364 | goto return_bad_req; |
| 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3369 | * 10: add X-Original-To if either the frontend or the backend |
| 3370 | * asks for it. |
| 3371 | */ |
| 3372 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3373 | |
| 3374 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3375 | if (s->req->prod->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3376 | /* Add an X-Original-To header unless the destination IP is |
| 3377 | * in the 'except' network range. |
| 3378 | */ |
Willy Tarreau | 9b061e3 | 2012-04-07 18:03:52 +0200 | [diff] [blame] | 3379 | stream_sock_get_to_addr(s->req->prod); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3380 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3381 | if (s->req->prod->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3382 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3383 | (((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] | 3384 | != s->fe->except_to.s_addr) && |
| 3385 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3386 | (((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] | 3387 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3388 | int len; |
| 3389 | unsigned char *pn; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 3390 | 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] | 3391 | |
| 3392 | /* Note: we rely on the backend to get the header name to be used for |
| 3393 | * x-original-to, because the header is really meant for the backends. |
| 3394 | * However, if the backend did not specify any option, we have to rely |
| 3395 | * on the frontend's header name. |
| 3396 | */ |
| 3397 | if (s->be->orgto_hdr_len) { |
| 3398 | len = s->be->orgto_hdr_len; |
| 3399 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3400 | } else { |
| 3401 | len = s->fe->orgto_hdr_len; |
| 3402 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3403 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3404 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3405 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3406 | if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3407 | goto return_bad_req; |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3412 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3413 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3414 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3415 | unsigned int want_flags = 0; |
| 3416 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3417 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3418 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3419 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3420 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3421 | want_flags |= TX_CON_CLO_SET; |
| 3422 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3423 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3424 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3425 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3426 | want_flags |= TX_CON_KAL_SET; |
| 3427 | } |
| 3428 | |
| 3429 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3430 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3431 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3432 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3433 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3434 | /* If we have no server assigned yet and we're balancing on url_param |
| 3435 | * with a POST request, we may be interested in checking the body for |
| 3436 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3437 | */ |
| 3438 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3439 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3440 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3441 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3442 | buffer_dont_connect(req); |
| 3443 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3444 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3445 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3446 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3447 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3448 | #ifdef TCP_QUICKACK |
| 3449 | /* We expect some data from the client. Unless we know for sure |
| 3450 | * we already have a full request, we have to re-enable quick-ack |
| 3451 | * in case we previously disabled it, otherwise we might cause |
| 3452 | * the client to delay further data. |
| 3453 | */ |
| 3454 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3455 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3456 | (msg->body_len > req->i - txn->req.eoh - 2))) |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3457 | setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); |
| 3458 | #endif |
| 3459 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3460 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3461 | /************************************************************* |
| 3462 | * OK, that's finished for the headers. We have done what we * |
| 3463 | * could. Let's switch to the DATA state. * |
| 3464 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3465 | req->analyse_exp = TICK_ETERNITY; |
| 3466 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3467 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3468 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3469 | /* OK let's go on with the BODY now */ |
| 3470 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3472 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3473 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3474 | /* we detected a parsing error. We want to archive this request |
| 3475 | * in the dedicated proxy area for later troubleshooting. |
| 3476 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3477 | http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3478 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3479 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3480 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3481 | txn->status = 400; |
| 3482 | req->analysers = 0; |
| 3483 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3484 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3485 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3486 | if (s->listener->counters) |
| 3487 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3488 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3489 | if (!(s->flags & SN_ERR_MASK)) |
| 3490 | s->flags |= SN_ERR_PRXCOND; |
| 3491 | if (!(s->flags & SN_FINST_MASK)) |
| 3492 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3493 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3494 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3495 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3496 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3497 | * returns zero, at the beginning because it prevents any other processing |
| 3498 | * from occurring, and at the end because it terminates the request. |
| 3499 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3500 | 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] | 3501 | { |
| 3502 | struct http_txn *txn = &s->txn; |
| 3503 | |
| 3504 | /* This connection is being tarpitted. The CLIENT side has |
| 3505 | * already set the connect expiration date to the right |
| 3506 | * timeout. We just have to check that the client is still |
| 3507 | * there and that the timeout has not expired. |
| 3508 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3509 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3510 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3511 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3512 | return 0; |
| 3513 | |
| 3514 | /* We will set the queue timer to the time spent, just for |
| 3515 | * logging purposes. We fake a 500 server error, so that the |
| 3516 | * attacker will not suspect his connection has been tarpitted. |
| 3517 | * It will not cause trouble to the logs because we can exclude |
| 3518 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3519 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3520 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3521 | |
| 3522 | txn->status = 500; |
| 3523 | if (req->flags != BF_READ_ERROR) |
| 3524 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3525 | |
| 3526 | req->analysers = 0; |
| 3527 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3528 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3529 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3530 | if (s->listener->counters) |
| 3531 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3532 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3533 | if (!(s->flags & SN_ERR_MASK)) |
| 3534 | s->flags |= SN_ERR_PRXCOND; |
| 3535 | if (!(s->flags & SN_FINST_MASK)) |
| 3536 | s->flags |= SN_FINST_T; |
| 3537 | return 0; |
| 3538 | } |
| 3539 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3540 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3541 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3542 | * must only be called after the standard HTTP request processing has occurred, |
| 3543 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3544 | * read more data, or 1 once it has completed its analysis. |
| 3545 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3546 | 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] | 3547 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3548 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3549 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3550 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3551 | |
| 3552 | /* We have to parse the HTTP request body to find any required data. |
| 3553 | * "balance url_param check_post" should have been the only way to get |
| 3554 | * into this. We were brought here after HTTP header analysis, so all |
| 3555 | * related structures are ready. |
| 3556 | */ |
| 3557 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3558 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3559 | goto missing_data; |
| 3560 | |
| 3561 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3562 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3563 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3564 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3565 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3566 | struct hdr_ctx ctx; |
| 3567 | ctx.idx = 0; |
| 3568 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 3569 | if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3570 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 3571 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3572 | } |
| 3573 | } |
| 3574 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3575 | } |
| 3576 | |
| 3577 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3578 | /* we have msg->sov which points to the first byte of message body. |
| 3579 | * msg->som still points to the beginning of the message. We must |
| 3580 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3581 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3582 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3583 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3584 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3585 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3586 | else |
| 3587 | msg->msg_state = HTTP_MSG_DATA; |
| 3588 | } |
| 3589 | |
| 3590 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3591 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3592 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3593 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3594 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3595 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3596 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3597 | if (!ret) |
| 3598 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3599 | else if (ret < 0) { |
| 3600 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3601 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3602 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3603 | } |
| 3604 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3605 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3606 | * We have the first data byte is in msg->sov. We're waiting for at |
| 3607 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3608 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3609 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3610 | if (msg->body_len < limit) |
| 3611 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3612 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3613 | if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3614 | goto http_end; |
| 3615 | |
| 3616 | missing_data: |
| 3617 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3618 | if (req->flags & BF_FULL) { |
| 3619 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3620 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3621 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3622 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3623 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3624 | txn->status = 408; |
| 3625 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3626 | |
| 3627 | if (!(s->flags & SN_ERR_MASK)) |
| 3628 | s->flags |= SN_ERR_CLITO; |
| 3629 | if (!(s->flags & SN_FINST_MASK)) |
| 3630 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3631 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3632 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3633 | |
| 3634 | /* we get here if we need to wait for more data */ |
| 3635 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3636 | /* Not enough data. We'll re-use the http-request |
| 3637 | * timeout here. Ideally, we should set the timeout |
| 3638 | * relative to the accept() date. We just set the |
| 3639 | * request timeout once at the beginning of the |
| 3640 | * request. |
| 3641 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3642 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3643 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3644 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3645 | return 0; |
| 3646 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3647 | |
| 3648 | http_end: |
| 3649 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3650 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3651 | req->analysers &= ~an_bit; |
| 3652 | req->analyse_exp = TICK_ETERNITY; |
| 3653 | return 1; |
| 3654 | |
| 3655 | return_bad_req: /* let's centralize all bad requests */ |
| 3656 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3657 | txn->status = 400; |
| 3658 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3659 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3660 | if (!(s->flags & SN_ERR_MASK)) |
| 3661 | s->flags |= SN_ERR_PRXCOND; |
| 3662 | if (!(s->flags & SN_FINST_MASK)) |
| 3663 | s->flags |= SN_FINST_R; |
| 3664 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3665 | return_err_msg: |
| 3666 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3667 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3668 | if (s->listener->counters) |
| 3669 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3670 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3671 | } |
| 3672 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3673 | /* send a server's name with an outgoing request over an established connection */ |
| 3674 | int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3675 | |
| 3676 | struct hdr_ctx ctx; |
| 3677 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3678 | char *hdr_name = be->server_id_hdr_name; |
| 3679 | int hdr_name_len = be->server_id_hdr_len; |
| 3680 | |
| 3681 | char *hdr_val; |
| 3682 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3683 | ctx.idx = 0; |
| 3684 | |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3685 | while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3686 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3687 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3688 | } |
| 3689 | |
| 3690 | /* Add the new header requested with the server value */ |
| 3691 | hdr_val = trash; |
| 3692 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3693 | hdr_val += hdr_name_len; |
| 3694 | *hdr_val++ = ':'; |
| 3695 | *hdr_val++ = ' '; |
| 3696 | hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val); |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3697 | http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3698 | |
| 3699 | return 0; |
| 3700 | } |
| 3701 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3702 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3703 | * right now but it works. |
| 3704 | */ |
| 3705 | void http_end_txn_clean_session(struct session *s) |
| 3706 | { |
| 3707 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3708 | * server's connections. We need a safer way to reinitialize buffer |
| 3709 | * flags. We also need a more accurate method for computing per-request |
| 3710 | * data. |
| 3711 | */ |
| 3712 | http_silent_debug(__LINE__, s); |
| 3713 | |
| 3714 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3715 | s->req->cons->shutr(s->req->cons); |
| 3716 | s->req->cons->shutw(s->req->cons); |
| 3717 | |
| 3718 | http_silent_debug(__LINE__, s); |
| 3719 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3720 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3721 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 3722 | if (unlikely(s->srv_conn)) |
| 3723 | sess_change_server(s, NULL); |
| 3724 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3725 | |
| 3726 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3727 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3728 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3729 | |
| 3730 | if (s->txn.status) { |
| 3731 | int n; |
| 3732 | |
| 3733 | n = s->txn.status / 100; |
| 3734 | if (n < 1 || n > 5) |
| 3735 | n = 0; |
| 3736 | |
| 3737 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3738 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3739 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3740 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3741 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3742 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3743 | } |
| 3744 | |
| 3745 | /* don't count other requests' data */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3746 | s->logs.bytes_in -= s->req->i; |
| 3747 | s->logs.bytes_out -= s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3748 | |
| 3749 | /* let's do a final log if we need it */ |
| 3750 | if (s->logs.logwait && |
| 3751 | !(s->flags & SN_MONITOR) && |
| 3752 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3753 | s->do_log(s); |
| 3754 | } |
| 3755 | |
| 3756 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3757 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3758 | tv_zero(&s->logs.tv_request); |
| 3759 | s->logs.t_queue = -1; |
| 3760 | s->logs.t_connect = -1; |
| 3761 | s->logs.t_data = -1; |
| 3762 | s->logs.t_close = 0; |
| 3763 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3764 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3765 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3766 | s->logs.bytes_in = s->req->total = s->req->i; |
| 3767 | s->logs.bytes_out = s->rep->total = s->rep->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3768 | |
| 3769 | if (s->pend_pos) |
| 3770 | pendconn_free(s->pend_pos); |
| 3771 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3772 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3773 | if (s->flags & SN_CURR_SESS) { |
| 3774 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3775 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3776 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3777 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3778 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3781 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3782 | |
| 3783 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3784 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3785 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3786 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3787 | s->req->cons->err_loc = NULL; |
| 3788 | s->req->cons->exp = TICK_ETERNITY; |
| 3789 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3790 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT); |
| 3791 | 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] | 3792 | 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] | 3793 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3794 | s->txn.meth = 0; |
| 3795 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3796 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3797 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3798 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3799 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 3800 | if (s->fe->options2 & PR_O2_NODELAY) { |
| 3801 | s->req->flags |= BF_NEVER_WAIT; |
| 3802 | s->rep->flags |= BF_NEVER_WAIT; |
| 3803 | } |
| 3804 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3805 | /* if the request buffer is not empty, it means we're |
| 3806 | * about to process another request, so send pending |
| 3807 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3808 | * Just don't do this if the buffer is close to be full, |
| 3809 | * because the request will wait for it to flush a little |
| 3810 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3811 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3812 | if (s->req->i) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 3813 | if (s->rep->o && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3814 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 3815 | bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3816 | s->rep->flags |= BF_EXPECT_MORE; |
| 3817 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3818 | |
| 3819 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3820 | buffer_auto_read(s->req); |
| 3821 | buffer_auto_close(s->req); |
| 3822 | buffer_auto_read(s->rep); |
| 3823 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3824 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 3825 | s->req->analysers = s->listener->analysers; |
| 3826 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3827 | s->rep->analysers = 0; |
| 3828 | |
| 3829 | http_silent_debug(__LINE__, s); |
| 3830 | } |
| 3831 | |
| 3832 | |
| 3833 | /* This function updates the request state machine according to the response |
| 3834 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3835 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3836 | * it is only used to find when a request/response couple is complete. Both |
| 3837 | * this function and its equivalent should loop until both return zero. It |
| 3838 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3839 | */ |
| 3840 | int http_sync_req_state(struct session *s) |
| 3841 | { |
| 3842 | struct buffer *buf = s->req; |
| 3843 | struct http_txn *txn = &s->txn; |
| 3844 | unsigned int old_flags = buf->flags; |
| 3845 | unsigned int old_state = txn->req.msg_state; |
| 3846 | |
| 3847 | http_silent_debug(__LINE__, s); |
| 3848 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3849 | return 0; |
| 3850 | |
| 3851 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3852 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3853 | * We can shut the read side unless we want to abort_on_close, |
| 3854 | * or we have a POST request. The issue with POST requests is |
| 3855 | * that some browsers still send a CRLF after the request, and |
| 3856 | * this CRLF must be read so that it does not remain in the kernel |
| 3857 | * buffers, otherwise a close could cause an RST on some systems |
| 3858 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3859 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 3860 | 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] | 3861 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3862 | |
| 3863 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3864 | goto wait_other_side; |
| 3865 | |
| 3866 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3867 | /* The server has not finished to respond, so we |
| 3868 | * don't want to move in order not to upset it. |
| 3869 | */ |
| 3870 | goto wait_other_side; |
| 3871 | } |
| 3872 | |
| 3873 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3874 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3875 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3876 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3877 | goto wait_other_side; |
| 3878 | } |
| 3879 | |
| 3880 | /* When we get here, it means that both the request and the |
| 3881 | * response have finished receiving. Depending on the connection |
| 3882 | * mode, we'll have to wait for the last bytes to leave in either |
| 3883 | * direction, and sometimes for a close to be effective. |
| 3884 | */ |
| 3885 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3886 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3887 | /* Server-close mode : queue a connection close to the server */ |
| 3888 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3889 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3890 | } |
| 3891 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3892 | /* Option forceclose is set, or either side wants to close, |
| 3893 | * let's enforce it now that we're not expecting any new |
| 3894 | * data to come. The caller knows the session is complete |
| 3895 | * once both states are CLOSED. |
| 3896 | */ |
| 3897 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3898 | buffer_shutr_now(buf); |
| 3899 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3900 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3901 | } |
| 3902 | else { |
| 3903 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3904 | * mode does not set the body analyser, we can't reach this place |
| 3905 | * in tunnel mode, so we're left with keep-alive only. |
| 3906 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3907 | */ |
| 3908 | buffer_auto_read(buf); |
| 3909 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
| 3912 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3913 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3914 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3915 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3916 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3917 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3918 | goto http_msg_closing; |
| 3919 | } |
| 3920 | else { |
| 3921 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3922 | goto http_msg_closed; |
| 3923 | } |
| 3924 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3925 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3929 | http_msg_closing: |
| 3930 | /* nothing else to forward, just waiting for the output buffer |
| 3931 | * to be empty and for the shutw_now to take effect. |
| 3932 | */ |
| 3933 | if (buf->flags & BF_OUT_EMPTY) { |
| 3934 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3935 | goto http_msg_closed; |
| 3936 | } |
| 3937 | else if (buf->flags & BF_SHUTW) { |
| 3938 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3939 | goto wait_other_side; |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3944 | http_msg_closed: |
| 3945 | goto wait_other_side; |
| 3946 | } |
| 3947 | |
| 3948 | wait_other_side: |
| 3949 | http_silent_debug(__LINE__, s); |
| 3950 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3951 | } |
| 3952 | |
| 3953 | |
| 3954 | /* This function updates the response state machine according to the request |
| 3955 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3956 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3957 | * it is only used to find when a request/response couple is complete. Both |
| 3958 | * this function and its equivalent should loop until both return zero. It |
| 3959 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3960 | */ |
| 3961 | int http_sync_res_state(struct session *s) |
| 3962 | { |
| 3963 | struct buffer *buf = s->rep; |
| 3964 | struct http_txn *txn = &s->txn; |
| 3965 | unsigned int old_flags = buf->flags; |
| 3966 | unsigned int old_state = txn->rsp.msg_state; |
| 3967 | |
| 3968 | http_silent_debug(__LINE__, s); |
| 3969 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3970 | return 0; |
| 3971 | |
| 3972 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3973 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3974 | * still monitor the server connection for a possible close |
| 3975 | * while the request is being uploaded, so we don't disable |
| 3976 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3977 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3978 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3979 | |
| 3980 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3981 | goto wait_other_side; |
| 3982 | |
| 3983 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3984 | /* The client seems to still be sending data, probably |
| 3985 | * because we got an error response during an upload. |
| 3986 | * We have the choice of either breaking the connection |
| 3987 | * or letting it pass through. Let's do the later. |
| 3988 | */ |
| 3989 | goto wait_other_side; |
| 3990 | } |
| 3991 | |
| 3992 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3993 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3994 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3995 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3996 | goto wait_other_side; |
| 3997 | } |
| 3998 | |
| 3999 | /* When we get here, it means that both the request and the |
| 4000 | * response have finished receiving. Depending on the connection |
| 4001 | * mode, we'll have to wait for the last bytes to leave in either |
| 4002 | * direction, and sometimes for a close to be effective. |
| 4003 | */ |
| 4004 | |
| 4005 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4006 | /* Server-close mode : shut read and wait for the request |
| 4007 | * side to close its output buffer. The caller will detect |
| 4008 | * when we're in DONE and the other is in CLOSED and will |
| 4009 | * catch that for the final cleanup. |
| 4010 | */ |
| 4011 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4012 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4013 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4014 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4015 | /* Option forceclose is set, or either side wants to close, |
| 4016 | * let's enforce it now that we're not expecting any new |
| 4017 | * data to come. The caller knows the session is complete |
| 4018 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4019 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4020 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4021 | buffer_shutr_now(buf); |
| 4022 | buffer_shutw_now(buf); |
| 4023 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4024 | } |
| 4025 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4026 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4027 | * mode does not set the body analyser, we can't reach this place |
| 4028 | * in tunnel mode, so we're left with keep-alive only. |
| 4029 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4030 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4031 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4032 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4036 | /* if we've just closed an output, let's switch */ |
| 4037 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4038 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4039 | goto http_msg_closing; |
| 4040 | } |
| 4041 | else { |
| 4042 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4043 | goto http_msg_closed; |
| 4044 | } |
| 4045 | } |
| 4046 | goto wait_other_side; |
| 4047 | } |
| 4048 | |
| 4049 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4050 | http_msg_closing: |
| 4051 | /* nothing else to forward, just waiting for the output buffer |
| 4052 | * to be empty and for the shutw_now to take effect. |
| 4053 | */ |
| 4054 | if (buf->flags & BF_OUT_EMPTY) { |
| 4055 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4056 | goto http_msg_closed; |
| 4057 | } |
| 4058 | else if (buf->flags & BF_SHUTW) { |
| 4059 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4060 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4061 | if (target_srv(&s->target)) |
| 4062 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4063 | goto wait_other_side; |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4068 | http_msg_closed: |
| 4069 | /* drop any pending data */ |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4070 | bi_erase(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4071 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4072 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4073 | goto wait_other_side; |
| 4074 | } |
| 4075 | |
| 4076 | wait_other_side: |
| 4077 | http_silent_debug(__LINE__, s); |
| 4078 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4079 | } |
| 4080 | |
| 4081 | |
| 4082 | /* Resync the request and response state machines. Return 1 if either state |
| 4083 | * changes. |
| 4084 | */ |
| 4085 | int http_resync_states(struct session *s) |
| 4086 | { |
| 4087 | struct http_txn *txn = &s->txn; |
| 4088 | int old_req_state = txn->req.msg_state; |
| 4089 | int old_res_state = txn->rsp.msg_state; |
| 4090 | |
| 4091 | http_silent_debug(__LINE__, s); |
| 4092 | http_sync_req_state(s); |
| 4093 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4094 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4095 | if (!http_sync_res_state(s)) |
| 4096 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4097 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4098 | if (!http_sync_req_state(s)) |
| 4099 | break; |
| 4100 | } |
| 4101 | http_silent_debug(__LINE__, s); |
| 4102 | /* OK, both state machines agree on a compatible state. |
| 4103 | * There are a few cases we're interested in : |
| 4104 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4105 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4106 | * directions, so let's simply disable both analysers. |
| 4107 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4108 | * request. |
| 4109 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4110 | * with server-close mode means we've completed one request and we |
| 4111 | * must re-initialize the server connection. |
| 4112 | */ |
| 4113 | |
| 4114 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4115 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4116 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4117 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4118 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4119 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4120 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4121 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4122 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4123 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4124 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4125 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4126 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4127 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4128 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4129 | s->rep->analysers = 0; |
| 4130 | buffer_auto_close(s->rep); |
| 4131 | buffer_auto_read(s->rep); |
| 4132 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4133 | buffer_abort(s->req); |
| 4134 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4135 | buffer_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4136 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4137 | } |
| 4138 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4139 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4140 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4141 | /* server-close: terminate this server connection and |
| 4142 | * reinitialize a fresh-new transaction. |
| 4143 | */ |
| 4144 | http_end_txn_clean_session(s); |
| 4145 | } |
| 4146 | |
| 4147 | http_silent_debug(__LINE__, s); |
| 4148 | return txn->req.msg_state != old_req_state || |
| 4149 | txn->rsp.msg_state != old_res_state; |
| 4150 | } |
| 4151 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4152 | /* This function is an analyser which forwards request body (including chunk |
| 4153 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4154 | * zero byte. The only situation where it must not be called is when we're in |
| 4155 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4156 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4157 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4158 | * 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] | 4159 | * 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] | 4160 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4161 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4162 | */ |
| 4163 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4164 | { |
| 4165 | struct http_txn *txn = &s->txn; |
| 4166 | struct http_msg *msg = &s->txn.req; |
| 4167 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4168 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4169 | return 0; |
| 4170 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4171 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4172 | ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4173 | /* Output closed while we were sending data. We must abort and |
| 4174 | * wake the other side up. |
| 4175 | */ |
| 4176 | msg->msg_state = HTTP_MSG_ERROR; |
| 4177 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4178 | return 1; |
| 4179 | } |
| 4180 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4181 | /* in most states, we should abort in case of early close */ |
| 4182 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4183 | |
| 4184 | /* Note that we don't have to send 100-continue back because we don't |
| 4185 | * need the data to complete our job, and it's up to the server to |
| 4186 | * decide whether to return 100, 417 or anything else in return of |
| 4187 | * an "Expect: 100-continue" header. |
| 4188 | */ |
| 4189 | |
| 4190 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4191 | /* we have msg->sov which points to the first byte of message body. |
| 4192 | * msg->som still points to the beginning of the message. We must |
| 4193 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4194 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4195 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4196 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4197 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4198 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4199 | else { |
| 4200 | msg->msg_state = HTTP_MSG_DATA; |
| 4201 | } |
| 4202 | } |
| 4203 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4204 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4205 | int bytes; |
| 4206 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4207 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4208 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4209 | bytes = msg->sov - msg->som; |
| 4210 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4211 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4212 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4213 | bytes += req->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4214 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4215 | msg->chunk_len += (unsigned int)bytes; |
| 4216 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4217 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4218 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4219 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4220 | /* must still forward */ |
| 4221 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4222 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4223 | |
| 4224 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4225 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4226 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4227 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4228 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4229 | } |
| 4230 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4231 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4232 | * set ->sov and ->next to point to the body and switch to DATA or |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4233 | * TRAILERS state. |
| 4234 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4235 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4236 | |
| 4237 | if (!ret) |
| 4238 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4239 | else if (ret < 0) { |
| 4240 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4241 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4242 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4243 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4244 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4245 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4246 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4247 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4248 | /* we want the CRLF after the data */ |
| 4249 | int ret; |
| 4250 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4251 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4252 | |
| 4253 | if (ret == 0) |
| 4254 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4255 | else if (ret < 0) { |
| 4256 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4257 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4258 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4259 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4260 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4261 | /* we're in MSG_CHUNK_SIZE now */ |
| 4262 | } |
| 4263 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4264 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4265 | |
| 4266 | if (ret == 0) |
| 4267 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4268 | else if (ret < 0) { |
| 4269 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4270 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4271 | http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4272 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4273 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4274 | /* we're in HTTP_MSG_DONE now */ |
| 4275 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4276 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4277 | int old_state = msg->msg_state; |
| 4278 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4279 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4280 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4281 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4282 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4283 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4284 | if (http_resync_states(s)) { |
| 4285 | /* some state changes occurred, maybe the analyser |
| 4286 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4287 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4288 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4289 | if (req->flags & BF_SHUTW) { |
| 4290 | /* request errors are most likely due to |
| 4291 | * the server aborting the transfer. |
| 4292 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4293 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4294 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4295 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4296 | http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4297 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4298 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4299 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4300 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4301 | |
| 4302 | /* If "option abortonclose" is set on the backend, we |
| 4303 | * want to monitor the client's connection and forward |
| 4304 | * any shutdown notification to the server, which will |
| 4305 | * decide whether to close or to go on processing the |
| 4306 | * request. |
| 4307 | */ |
| 4308 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4309 | buffer_auto_read(req); |
| 4310 | buffer_auto_close(req); |
| 4311 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4312 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4313 | /* POST requests may require to read extra CRLF |
| 4314 | * sent by broken browsers and which could cause |
| 4315 | * an RST to be sent upon close on some systems |
| 4316 | * (eg: Linux). |
| 4317 | */ |
| 4318 | buffer_auto_read(req); |
| 4319 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4320 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4321 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4322 | } |
| 4323 | } |
| 4324 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4325 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4326 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4327 | if (req->flags & BF_SHUTR) { |
| 4328 | if (!(s->flags & SN_ERR_MASK)) |
| 4329 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4330 | if (!(s->flags & SN_FINST_MASK)) { |
| 4331 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4332 | s->flags |= SN_FINST_H; |
| 4333 | else |
| 4334 | s->flags |= SN_FINST_D; |
| 4335 | } |
| 4336 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4337 | s->fe->fe_counters.cli_aborts++; |
| 4338 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4339 | if (target_srv(&s->target)) |
| 4340 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4341 | |
| 4342 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4343 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4344 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4345 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4346 | if (req->flags & BF_SHUTW) |
| 4347 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4348 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4349 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4350 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4351 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4352 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4353 | buffer_dont_close(req); |
| 4354 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4355 | /* 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] | 4356 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 4357 | * 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] | 4358 | * modes are already handled by the stream sock layer. We must not do |
| 4359 | * this in content-length mode because it could present the MSG_MORE |
| 4360 | * flag with the last block of forwarded data, which would cause an |
| 4361 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4362 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4363 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 4364 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4365 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4366 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4367 | return 0; |
| 4368 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4369 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4370 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4371 | if (s->listener->counters) |
| 4372 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4373 | return_bad_req_stats_ok: |
| 4374 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4375 | if (txn->status) { |
| 4376 | /* Note: we don't send any error if some data were already sent */ |
| 4377 | stream_int_retnclose(req->prod, NULL); |
| 4378 | } else { |
| 4379 | txn->status = 400; |
| 4380 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4381 | } |
| 4382 | req->analysers = 0; |
| 4383 | 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] | 4384 | |
| 4385 | if (!(s->flags & SN_ERR_MASK)) |
| 4386 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4387 | if (!(s->flags & SN_FINST_MASK)) { |
| 4388 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4389 | s->flags |= SN_FINST_H; |
| 4390 | else |
| 4391 | s->flags |= SN_FINST_D; |
| 4392 | } |
| 4393 | return 0; |
| 4394 | |
| 4395 | aborted_xfer: |
| 4396 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4397 | if (txn->status) { |
| 4398 | /* Note: we don't send any error if some data were already sent */ |
| 4399 | stream_int_retnclose(req->prod, NULL); |
| 4400 | } else { |
| 4401 | txn->status = 502; |
| 4402 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4403 | } |
| 4404 | req->analysers = 0; |
| 4405 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4406 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4407 | s->fe->fe_counters.srv_aborts++; |
| 4408 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4409 | if (target_srv(&s->target)) |
| 4410 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4411 | |
| 4412 | if (!(s->flags & SN_ERR_MASK)) |
| 4413 | s->flags |= SN_ERR_SRVCL; |
| 4414 | if (!(s->flags & SN_FINST_MASK)) { |
| 4415 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4416 | s->flags |= SN_FINST_H; |
| 4417 | else |
| 4418 | s->flags |= SN_FINST_D; |
| 4419 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4420 | return 0; |
| 4421 | } |
| 4422 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4423 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4424 | * processing can continue on next analysers, or zero if it either needs more |
| 4425 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4426 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4427 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4428 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4429 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4430 | 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] | 4431 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4432 | struct http_txn *txn = &s->txn; |
| 4433 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4434 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4435 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4436 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4437 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4438 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4439 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4440 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4441 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4442 | rep, |
| 4443 | rep->rex, rep->wex, |
| 4444 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4445 | rep->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4446 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4447 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4448 | /* |
| 4449 | * Now parse the partial (or complete) lines. |
| 4450 | * We will check the response syntax, and also join multi-line |
| 4451 | * headers. An index of all the lines will be elaborated while |
| 4452 | * parsing. |
| 4453 | * |
| 4454 | * For the parsing, we use a 28 states FSM. |
| 4455 | * |
| 4456 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4457 | * rep->data + msg->som = beginning of response |
| 4458 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4459 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4460 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4461 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4462 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4463 | */ |
| 4464 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4465 | /* There's a protected area at the end of the buffer for rewriting |
| 4466 | * purposes. We don't want to start to parse the request if the |
| 4467 | * protected area is affected, because we may have to move processed |
| 4468 | * data later, which is much more complicated. |
| 4469 | */ |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4470 | if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4471 | if (unlikely((rep->flags & BF_FULL) || |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 4472 | bi_end(rep) < b_ptr(rep, msg->next) || |
| 4473 | bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) { |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 4474 | if (rep->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4475 | /* 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] | 4476 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4477 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4478 | buffer_dont_close(rep); |
| 4479 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4480 | return 0; |
| 4481 | } |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4482 | if (rep->i <= rep->size - global.tune.maxrewrite) |
Willy Tarreau | 21710ff | 2012-03-09 13:58:04 +0100 | [diff] [blame] | 4483 | http_message_realign(msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4484 | } |
| 4485 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4486 | if (likely(msg->next < rep->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4487 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4488 | } |
| 4489 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4490 | /* 1: we might have to print this header in debug mode */ |
| 4491 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4492 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4493 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4494 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4495 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4496 | |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4497 | sol = rep->p + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4498 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4499 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4500 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4501 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4502 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4503 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4504 | while (cur_idx) { |
| 4505 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4506 | debug_hdr("srvhdr", s, sol, eol); |
| 4507 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4508 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4509 | } |
| 4510 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4511 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4512 | /* |
| 4513 | * Now we quickly check if we have found a full valid response. |
| 4514 | * If not so, we check the FD and buffer states before leaving. |
| 4515 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4516 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4517 | * responses are checked first. |
| 4518 | * |
| 4519 | * Depending on whether the client is still there or not, we |
| 4520 | * may send an error response back or not. Note that normally |
| 4521 | * we should only check for HTTP status there, and check I/O |
| 4522 | * errors somewhere else. |
| 4523 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4524 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4525 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4526 | /* Invalid response */ |
| 4527 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4528 | /* we detected a parsing error. We want to archive this response |
| 4529 | * in the dedicated proxy area for later troubleshooting. |
| 4530 | */ |
| 4531 | hdr_response_bad: |
| 4532 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4533 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4534 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4535 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4536 | if (target_srv(&s->target)) { |
| 4537 | target_srv(&s->target)->counters.failed_resp++; |
| 4538 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4539 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4540 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4541 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4542 | rep->analysers = 0; |
| 4543 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4544 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4545 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4546 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4547 | |
| 4548 | if (!(s->flags & SN_ERR_MASK)) |
| 4549 | s->flags |= SN_ERR_PRXCOND; |
| 4550 | if (!(s->flags & SN_FINST_MASK)) |
| 4551 | s->flags |= SN_FINST_H; |
| 4552 | |
| 4553 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4554 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4555 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4556 | /* too large response does not fit in buffer. */ |
| 4557 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4558 | if (msg->err_pos < 0) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4559 | msg->err_pos = rep->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4560 | goto hdr_response_bad; |
| 4561 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4562 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4563 | /* read error */ |
| 4564 | else if (rep->flags & BF_READ_ERROR) { |
| 4565 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4566 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4567 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4568 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4569 | if (target_srv(&s->target)) { |
| 4570 | target_srv(&s->target)->counters.failed_resp++; |
| 4571 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4572 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4573 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4574 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4575 | rep->analysers = 0; |
| 4576 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4577 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4578 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4579 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4580 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4581 | if (!(s->flags & SN_ERR_MASK)) |
| 4582 | s->flags |= SN_ERR_SRVCL; |
| 4583 | if (!(s->flags & SN_FINST_MASK)) |
| 4584 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4585 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4586 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4587 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4588 | /* read timeout : return a 504 to the client. */ |
| 4589 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4590 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4591 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4592 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4593 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4594 | if (target_srv(&s->target)) { |
| 4595 | target_srv(&s->target)->counters.failed_resp++; |
| 4596 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4597 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4598 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4599 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4600 | rep->analysers = 0; |
| 4601 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4602 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4603 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4604 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4605 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4606 | if (!(s->flags & SN_ERR_MASK)) |
| 4607 | s->flags |= SN_ERR_SRVTO; |
| 4608 | if (!(s->flags & SN_FINST_MASK)) |
| 4609 | s->flags |= SN_FINST_H; |
| 4610 | return 0; |
| 4611 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4612 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4613 | /* 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] | 4614 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4615 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4616 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4617 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4618 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4619 | if (target_srv(&s->target)) { |
| 4620 | target_srv(&s->target)->counters.failed_resp++; |
| 4621 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4622 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4623 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4624 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4625 | rep->analysers = 0; |
| 4626 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4627 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4628 | bi_erase(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4629 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4630 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4631 | if (!(s->flags & SN_ERR_MASK)) |
| 4632 | s->flags |= SN_ERR_SRVCL; |
| 4633 | if (!(s->flags & SN_FINST_MASK)) |
| 4634 | s->flags |= SN_FINST_H; |
| 4635 | return 0; |
| 4636 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4637 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4638 | /* write error to client (we don't send any message then) */ |
| 4639 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4640 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4641 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4642 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4643 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4644 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4645 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4646 | |
| 4647 | if (!(s->flags & SN_ERR_MASK)) |
| 4648 | s->flags |= SN_ERR_CLICL; |
| 4649 | if (!(s->flags & SN_FINST_MASK)) |
| 4650 | s->flags |= SN_FINST_H; |
| 4651 | |
| 4652 | /* process_session() will take care of the error */ |
| 4653 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4654 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4655 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4656 | buffer_dont_close(rep); |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
| 4660 | /* More interesting part now : we know that we have a complete |
| 4661 | * response which at least looks like HTTP. We have an indicator |
| 4662 | * of each header's length, so we can parse them quickly. |
| 4663 | */ |
| 4664 | |
| 4665 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4666 | http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4667 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4668 | /* |
| 4669 | * 1: get the status code |
| 4670 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4671 | n = rep->p[msg->sol + msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4672 | if (n < 1 || n > 5) |
| 4673 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4674 | /* when the client triggers a 4xx from the server, it's most often due |
| 4675 | * to a missing object or permission. These events should be tracked |
| 4676 | * because if they happen often, it may indicate a brute force or a |
| 4677 | * vulnerability scan. |
| 4678 | */ |
| 4679 | if (n == 4) |
| 4680 | session_inc_http_err_ctr(s); |
| 4681 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4682 | if (target_srv(&s->target)) |
| 4683 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4684 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4685 | /* check if the response is HTTP/1.1 or above */ |
| 4686 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4687 | ((rep->p[msg->sol + 5] > '1') || |
| 4688 | ((rep->p[msg->sol + 5] == '1') && |
| 4689 | (rep->p[msg->sol + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4690 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4691 | |
| 4692 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4693 | 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] | 4694 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4695 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4696 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4697 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4698 | txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4699 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4700 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4701 | * and 505 are triggered on demand by client request, so we must not |
| 4702 | * count them as server failures. |
| 4703 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4704 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4705 | 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] | 4706 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4707 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4708 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4709 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4710 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4711 | /* |
| 4712 | * 2: check for cacheability. |
| 4713 | */ |
| 4714 | |
| 4715 | switch (txn->status) { |
| 4716 | case 200: |
| 4717 | case 203: |
| 4718 | case 206: |
| 4719 | case 300: |
| 4720 | case 301: |
| 4721 | case 410: |
| 4722 | /* RFC2616 @13.4: |
| 4723 | * "A response received with a status code of |
| 4724 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4725 | * by a cache (...) unless a cache-control |
| 4726 | * directive prohibits caching." |
| 4727 | * |
| 4728 | * RFC2616 @9.5: POST method : |
| 4729 | * "Responses to this method are not cacheable, |
| 4730 | * unless the response includes appropriate |
| 4731 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4732 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4733 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4734 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4735 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4736 | break; |
| 4737 | default: |
| 4738 | break; |
| 4739 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4740 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4741 | /* |
| 4742 | * 3: we may need to capture headers |
| 4743 | */ |
| 4744 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 4745 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4746 | capture_headers(rep->p + msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4747 | txn->rsp.cap, s->fe->rsp_cap); |
| 4748 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4749 | /* 4: determine the transfer-length. |
| 4750 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4751 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4752 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4753 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4754 | * All responses to the HEAD request method MUST NOT include a |
| 4755 | * message-body, even though the presence of entity-header fields |
| 4756 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4757 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4758 | * message-body. All other responses do include a message-body, |
| 4759 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4760 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4761 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4762 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4763 | * always terminated by the first empty line after the header fields, |
| 4764 | * regardless of the entity-header fields present in the message. |
| 4765 | * |
| 4766 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4767 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4768 | * transfer-length is defined by the use of this transfer-coding. |
| 4769 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4770 | * transfer-coding is not present, the transfer-length is defined by |
| 4771 | * the sender closing the connection. |
| 4772 | * |
| 4773 | * 3. If a Content-Length header field is present, its decimal value in |
| 4774 | * OCTETs represents both the entity-length and the transfer-length. |
| 4775 | * If a message is received with both a Transfer-Encoding header |
| 4776 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4777 | * |
| 4778 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4779 | * the transfer-length is not otherwise specified, then this self- |
| 4780 | * delimiting media type defines the transfer-length. This media |
| 4781 | * type MUST NOT be used unless the sender knows that the recipient |
| 4782 | * can parse it; the presence in a request of a Range header with |
| 4783 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4784 | * client can parse multipart/byteranges responses. |
| 4785 | * |
| 4786 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4787 | */ |
| 4788 | |
| 4789 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4790 | * 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] | 4791 | * 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] | 4792 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4793 | */ |
| 4794 | if (txn->meth == HTTP_METH_HEAD || |
| 4795 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4796 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4797 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4798 | goto skip_content_length; |
| 4799 | } |
| 4800 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4801 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4802 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4803 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4804 | http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4805 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4806 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 4807 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4808 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4809 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4810 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4811 | break; |
| 4812 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4813 | } |
| 4814 | |
| 4815 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4816 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4817 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 4818 | http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4819 | signed long long cl; |
| 4820 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4821 | if (!ctx.vlen) { |
| 4822 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4823 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4824 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4825 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4826 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
| 4827 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4828 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4829 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4830 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4831 | if (cl < 0) { |
| 4832 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4833 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4834 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4835 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4836 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4837 | msg->err_pos = ctx.line + ctx.val - rep->data; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4838 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 4839 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4840 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4841 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4842 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4845 | /* FIXME: we should also implement the multipart/byterange method. |
| 4846 | * For now on, we resort to close mode in this case (unknown length). |
| 4847 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4848 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4849 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4850 | /* end of job, return OK */ |
| 4851 | rep->analysers &= ~an_bit; |
| 4852 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4853 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4854 | return 1; |
| 4855 | } |
| 4856 | |
| 4857 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4858 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4859 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4860 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4861 | */ |
| 4862 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4863 | { |
| 4864 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4865 | struct http_msg *msg = &txn->rsp; |
| 4866 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4867 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4868 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4869 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n", |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4870 | now_ms, __FUNCTION__, |
| 4871 | t, |
| 4872 | rep, |
| 4873 | rep->rex, rep->wex, |
| 4874 | rep->flags, |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4875 | rep->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4876 | rep->analysers); |
| 4877 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4878 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4879 | return 0; |
| 4880 | |
| 4881 | rep->analysers &= ~an_bit; |
| 4882 | rep->analyse_exp = TICK_ETERNITY; |
| 4883 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4884 | /* Now we have to check if we need to modify the Connection header. |
| 4885 | * This is more difficult on the response than it is on the request, |
| 4886 | * because we can have two different HTTP versions and we don't know |
| 4887 | * how the client will interprete a response. For instance, let's say |
| 4888 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4889 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4890 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4891 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4892 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4893 | * indicating how a request MAY be understood by the client. In case |
| 4894 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4895 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4896 | * will fall back to explicit close. Note that we won't take risks with |
| 4897 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4898 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4899 | */ |
| 4900 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4901 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4902 | txn->status == 101)) { |
| 4903 | /* Either we've established an explicit tunnel, or we're |
| 4904 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4905 | * to understand the next protocols. We have to switch to tunnel |
| 4906 | * mode, so that we transfer the request and responses then let |
| 4907 | * this protocol pass unmodified. When we later implement specific |
| 4908 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4909 | * header which contains information about that protocol for |
| 4910 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4911 | */ |
| 4912 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4913 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4914 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4915 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4916 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4917 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4918 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4919 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4920 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4921 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4922 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4923 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4924 | /* now adjust header transformations depending on current state */ |
| 4925 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4926 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4927 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4928 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4929 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4930 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4931 | else { /* SCL / KAL */ |
| 4932 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4933 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4934 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4935 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4936 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4937 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 4938 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4939 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4940 | /* Some keep-alive responses are converted to Server-close if |
| 4941 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4942 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4943 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4944 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4945 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4946 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4947 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4948 | } |
| 4949 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4950 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4951 | /* |
| 4952 | * 3: we will have to evaluate the filters. |
| 4953 | * As opposed to version 1.2, now they will be evaluated in the |
| 4954 | * filters order and not in the header order. This means that |
| 4955 | * each filter has to be validated among all headers. |
| 4956 | * |
| 4957 | * Filters are tried with ->be first, then with ->fe if it is |
| 4958 | * different from ->be. |
| 4959 | */ |
| 4960 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4961 | cur_proxy = t->be; |
| 4962 | while (1) { |
| 4963 | struct proxy *rule_set = cur_proxy; |
| 4964 | |
| 4965 | /* try headers filters */ |
| 4966 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4967 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4968 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4969 | if (target_srv(&t->target)) { |
| 4970 | target_srv(&t->target)->counters.failed_resp++; |
| 4971 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4972 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4973 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4974 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4975 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4976 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4977 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4978 | bi_erase(rep); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4979 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4980 | if (!(t->flags & SN_ERR_MASK)) |
| 4981 | t->flags |= SN_ERR_PRXCOND; |
| 4982 | if (!(t->flags & SN_FINST_MASK)) |
| 4983 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4984 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4985 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4986 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4987 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4988 | /* has the response been denied ? */ |
| 4989 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4990 | if (target_srv(&t->target)) |
| 4991 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4992 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4993 | t->be->be_counters.denied_resp++; |
| 4994 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4995 | if (t->listener->counters) |
| 4996 | t->listener->counters->denied_resp++; |
| 4997 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4998 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4999 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5000 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5001 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5002 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5003 | if (txn->status < 200) |
| 5004 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5005 | if (wl->cond) { |
| 5006 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5007 | ret = acl_pass(ret); |
| 5008 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5009 | ret = !ret; |
| 5010 | if (!ret) |
| 5011 | continue; |
| 5012 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5013 | if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5014 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5015 | } |
| 5016 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5017 | /* check whether we're already working on the frontend */ |
| 5018 | if (cur_proxy == t->fe) |
| 5019 | break; |
| 5020 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5021 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5022 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5023 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5024 | * We may be facing a 100-continue response, in which case this |
| 5025 | * is not the right response, and we're waiting for the next one. |
| 5026 | * 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] | 5027 | * next one. |
| 5028 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5029 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5030 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5031 | msg->next -= buffer_forward(rep, msg->next - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5032 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5033 | txn->status = 0; |
| 5034 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5035 | return 1; |
| 5036 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5037 | else if (unlikely(txn->status < 200)) |
| 5038 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5039 | |
| 5040 | /* we don't have any 1xx status code now */ |
| 5041 | |
| 5042 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5043 | * 4: check for server cookie. |
| 5044 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5045 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5046 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5047 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5048 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5049 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5050 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5051 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5052 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5053 | 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] | 5054 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5055 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5056 | /* |
| 5057 | * 6: add server cookie in the response if needed |
| 5058 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5059 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5060 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5061 | (!(t->flags & SN_DIRECT) || |
| 5062 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5063 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5064 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5065 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5066 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5067 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5068 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5069 | /* the server is known, it's not the one the client requested, or the |
| 5070 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5071 | * insert a set-cookie here, except if we want to insert only on POST |
| 5072 | * requests and this one isn't. Note that servers which don't have cookies |
| 5073 | * (eg: some backup servers) will return a full cookie removal request. |
| 5074 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5075 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5076 | len = sprintf(trash, |
| 5077 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5078 | t->be->cookie_name); |
| 5079 | } |
| 5080 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5081 | 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] | 5082 | |
| 5083 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5084 | /* emit last_date, which is mandatory */ |
| 5085 | trash[len++] = COOKIE_DELIM_DATE; |
| 5086 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5087 | if (t->be->cookie_maxlife) { |
| 5088 | /* emit first_date, which is either the original one or |
| 5089 | * the current date. |
| 5090 | */ |
| 5091 | trash[len++] = COOKIE_DELIM_DATE; |
| 5092 | s30tob64(txn->cookie_first_date ? |
| 5093 | txn->cookie_first_date >> 2 : |
| 5094 | (date.tv_sec+3) >> 2, trash + len); |
| 5095 | len += 5; |
| 5096 | } |
| 5097 | } |
| 5098 | len += sprintf(trash + len, "; path=/"); |
| 5099 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5100 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5101 | if (t->be->cookie_domain) |
| 5102 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5103 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5104 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5123 | if (unlikely(http_header_add_tail2(&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 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5170 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 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 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5177 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5178 | want_flags |= TX_CON_CLO_SET; |
| 5179 | } |
| 5180 | |
| 5181 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5182 | http_change_connection_header(txn, msg, 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 | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5186 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 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 | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 5238 | ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) || |
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) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5252 | /* we have msg->sov which points to the first byte of message body. |
| 5253 | * msg->som still points to the beginning of the message. We must |
| 5254 | * save the body in msg->next because it survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5255 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 5256 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5257 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5258 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 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; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5275 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5276 | msg->chunk_len += (unsigned int)bytes; |
| 5277 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5278 | } |
| 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 */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5286 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 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 | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5293 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5294 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5295 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5296 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5297 | |
| 5298 | if (!ret) |
| 5299 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5300 | else if (ret < 0) { |
| 5301 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5302 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5303 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5304 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5305 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5306 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5307 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5308 | /* we want the CRLF after the data */ |
| 5309 | int ret; |
| 5310 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5311 | ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5312 | |
| 5313 | if (!ret) |
| 5314 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5315 | else if (ret < 0) { |
| 5316 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5317 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5318 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5319 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5320 | /* we're in MSG_CHUNK_SIZE now */ |
| 5321 | } |
| 5322 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5323 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5324 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5325 | if (ret == 0) |
| 5326 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5327 | else if (ret < 0) { |
| 5328 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5329 | http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5330 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5331 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5332 | /* we're in HTTP_MSG_DONE now */ |
| 5333 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5334 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5335 | int old_state = msg->msg_state; |
| 5336 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5337 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5338 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5339 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5340 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5341 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5342 | if (http_resync_states(s)) { |
| 5343 | http_silent_debug(__LINE__, s); |
| 5344 | /* some state changes occurred, maybe the analyser |
| 5345 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5346 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5347 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5348 | if (res->flags & BF_SHUTW) { |
| 5349 | /* response errors are most likely due to |
| 5350 | * the client aborting the transfer. |
| 5351 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5352 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5353 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5354 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5355 | http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5356 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5357 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5358 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5359 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5360 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5361 | } |
| 5362 | } |
| 5363 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5364 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5365 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5366 | if (res->flags & BF_SHUTR) { |
| 5367 | if (!(s->flags & SN_ERR_MASK)) |
| 5368 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5369 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5370 | if (target_srv(&s->target)) |
| 5371 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5372 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5373 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5374 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5375 | if (res->flags & BF_SHUTW) |
| 5376 | goto aborted_xfer; |
| 5377 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5378 | /* 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] | 5379 | if (!s->req->analysers) |
| 5380 | goto return_bad_res; |
| 5381 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5382 | /* forward any pending data */ |
| 5383 | bytes = msg->sov - msg->som; |
| 5384 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5385 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5386 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5387 | bytes += res->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5388 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5389 | msg->chunk_len += (unsigned int)bytes; |
| 5390 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5391 | } |
| 5392 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5393 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5394 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5395 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5396 | * close. |
| 5397 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5398 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5399 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5400 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5401 | buffer_dont_close(res); |
| 5402 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5403 | /* 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] | 5404 | * what we did. So we always set the BF_EXPECT_MORE flag so that the |
| 5405 | * 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] | 5406 | * modes are already handled by the stream sock layer. We must not do |
| 5407 | * this in content-length mode because it could present the MSG_MORE |
| 5408 | * flag with the last block of forwarded data, which would cause an |
| 5409 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5410 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5411 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 869fc1e | 2012-03-05 08:29:20 +0100 | [diff] [blame] | 5412 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5413 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5414 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5415 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5416 | return 0; |
| 5417 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5418 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5419 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5420 | if (target_srv(&s->target)) |
| 5421 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5422 | |
| 5423 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5424 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5425 | /* don't send any error message as we're in the body */ |
| 5426 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5427 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5428 | 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] | 5429 | if (target_srv(&s->target)) |
| 5430 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5431 | |
| 5432 | if (!(s->flags & SN_ERR_MASK)) |
| 5433 | s->flags |= SN_ERR_PRXCOND; |
| 5434 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5435 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5436 | return 0; |
| 5437 | |
| 5438 | aborted_xfer: |
| 5439 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5440 | /* don't send any error message as we're in the body */ |
| 5441 | stream_int_retnclose(res->cons, NULL); |
| 5442 | res->analysers = 0; |
| 5443 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5444 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5445 | s->fe->fe_counters.cli_aborts++; |
| 5446 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5447 | if (target_srv(&s->target)) |
| 5448 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5449 | |
| 5450 | if (!(s->flags & SN_ERR_MASK)) |
| 5451 | s->flags |= SN_ERR_CLICL; |
| 5452 | if (!(s->flags & SN_FINST_MASK)) |
| 5453 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5454 | return 0; |
| 5455 | } |
| 5456 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5457 | /* Iterate the same filter through all request headers. |
| 5458 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5459 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5460 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5461 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5462 | 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] | 5463 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5464 | char term; |
| 5465 | char *cur_ptr, *cur_end, *cur_next; |
| 5466 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5467 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5468 | struct hdr_idx_elem *cur_hdr; |
| 5469 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5471 | last_hdr = 0; |
| 5472 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5473 | cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5474 | old_idx = 0; |
| 5475 | |
| 5476 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5477 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5478 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5479 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5480 | (exp->action == ACT_ALLOW || |
| 5481 | exp->action == ACT_DENY || |
| 5482 | exp->action == ACT_TARPIT)) |
| 5483 | return 0; |
| 5484 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5485 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5486 | if (!cur_idx) |
| 5487 | break; |
| 5488 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5489 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5490 | cur_ptr = cur_next; |
| 5491 | cur_end = cur_ptr + cur_hdr->len; |
| 5492 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5493 | |
| 5494 | /* Now we have one header between cur_ptr and cur_end, |
| 5495 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5496 | */ |
| 5497 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5498 | /* The annoying part is that pattern matching needs |
| 5499 | * that we modify the contents to null-terminate all |
| 5500 | * strings before testing them. |
| 5501 | */ |
| 5502 | |
| 5503 | term = *cur_end; |
| 5504 | *cur_end = '\0'; |
| 5505 | |
| 5506 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5507 | switch (exp->action) { |
| 5508 | case ACT_SETBE: |
| 5509 | /* It is not possible to jump a second time. |
| 5510 | * FIXME: should we return an HTTP/500 here so that |
| 5511 | * the admin knows there's a problem ? |
| 5512 | */ |
| 5513 | if (t->be != t->fe) |
| 5514 | break; |
| 5515 | |
| 5516 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5517 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5518 | last_hdr = 1; |
| 5519 | break; |
| 5520 | |
| 5521 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5522 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5523 | last_hdr = 1; |
| 5524 | break; |
| 5525 | |
| 5526 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5527 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5528 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5529 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5530 | t->fe->fe_counters.denied_req++; |
| 5531 | if (t->fe != t->be) |
| 5532 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5533 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5534 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5535 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5536 | break; |
| 5537 | |
| 5538 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5539 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5540 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5541 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5542 | t->fe->fe_counters.denied_req++; |
| 5543 | if (t->fe != t->be) |
| 5544 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5545 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5546 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5547 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5548 | break; |
| 5549 | |
| 5550 | case ACT_REPLACE: |
| 5551 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5552 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5553 | /* FIXME: if the user adds a newline in the replacement, the |
| 5554 | * index will not be recalculated for now, and the new line |
| 5555 | * will not be counted as a new header. |
| 5556 | */ |
| 5557 | |
| 5558 | cur_end += delta; |
| 5559 | cur_next += delta; |
| 5560 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5561 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5562 | break; |
| 5563 | |
| 5564 | case ACT_REMOVE: |
| 5565 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5566 | cur_next += delta; |
| 5567 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5568 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5569 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5570 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5571 | cur_hdr->len = 0; |
| 5572 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5573 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5574 | break; |
| 5575 | |
| 5576 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5577 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5578 | if (cur_end) |
| 5579 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5581 | /* keep the link from this header to next one in case of later |
| 5582 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5583 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5584 | old_idx = cur_idx; |
| 5585 | } |
| 5586 | return 0; |
| 5587 | } |
| 5588 | |
| 5589 | |
| 5590 | /* Apply the filter to the request line. |
| 5591 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5592 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5593 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5594 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5595 | */ |
| 5596 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5597 | { |
| 5598 | char term; |
| 5599 | char *cur_ptr, *cur_end; |
| 5600 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5601 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5602 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5603 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5604 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5605 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5606 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5607 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5608 | (exp->action == ACT_ALLOW || |
| 5609 | exp->action == ACT_DENY || |
| 5610 | exp->action == ACT_TARPIT)) |
| 5611 | return 0; |
| 5612 | else if (exp->action == ACT_REMOVE) |
| 5613 | return 0; |
| 5614 | |
| 5615 | done = 0; |
| 5616 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5617 | cur_ptr = req->p + txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5618 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5619 | |
| 5620 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5621 | |
| 5622 | /* The annoying part is that pattern matching needs |
| 5623 | * that we modify the contents to null-terminate all |
| 5624 | * strings before testing them. |
| 5625 | */ |
| 5626 | |
| 5627 | term = *cur_end; |
| 5628 | *cur_end = '\0'; |
| 5629 | |
| 5630 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5631 | switch (exp->action) { |
| 5632 | case ACT_SETBE: |
| 5633 | /* It is not possible to jump a second time. |
| 5634 | * FIXME: should we return an HTTP/500 here so that |
| 5635 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5636 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5637 | if (t->be != t->fe) |
| 5638 | break; |
| 5639 | |
| 5640 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5641 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5642 | done = 1; |
| 5643 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5645 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5646 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5647 | done = 1; |
| 5648 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5649 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5650 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5651 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5652 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5653 | t->fe->fe_counters.denied_req++; |
| 5654 | if (t->fe != t->be) |
| 5655 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5656 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5657 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5659 | done = 1; |
| 5660 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5662 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5663 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5664 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5665 | t->fe->fe_counters.denied_req++; |
| 5666 | if (t->fe != t->be) |
| 5667 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5668 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5669 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5671 | done = 1; |
| 5672 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5674 | case ACT_REPLACE: |
| 5675 | *cur_end = term; /* restore the string terminator */ |
| 5676 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5677 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5678 | /* FIXME: if the user adds a newline in the replacement, the |
| 5679 | * index will not be recalculated for now, and the new line |
| 5680 | * will not be counted as a new header. |
| 5681 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5682 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5683 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5684 | cur_end += delta; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 5685 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5686 | HTTP_MSG_RQMETH, |
| 5687 | cur_ptr, cur_end + 1, |
| 5688 | NULL, NULL); |
| 5689 | if (unlikely(!cur_end)) |
| 5690 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5692 | /* we have a full request and we know that we have either a CR |
| 5693 | * or an LF at <ptr>. |
| 5694 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5695 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5696 | 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] | 5697 | /* there is no point trying this regex on headers */ |
| 5698 | return 1; |
| 5699 | } |
| 5700 | } |
| 5701 | *cur_end = term; /* restore the string terminator */ |
| 5702 | return done; |
| 5703 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5704 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5705 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5706 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5707 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5708 | * 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] | 5709 | * 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] | 5710 | * unparsable request. Since it can manage the switch to another backend, it |
| 5711 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5712 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5713 | 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] | 5714 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5715 | struct http_txn *txn = &s->txn; |
| 5716 | struct hdr_exp *exp; |
| 5717 | |
| 5718 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5719 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5721 | /* |
| 5722 | * The interleaving of transformations and verdicts |
| 5723 | * makes it difficult to decide to continue or stop |
| 5724 | * the evaluation. |
| 5725 | */ |
| 5726 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5727 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5728 | break; |
| 5729 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5730 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5731 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5732 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5733 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5734 | |
| 5735 | /* if this filter had a condition, evaluate it now and skip to |
| 5736 | * next filter if the condition does not match. |
| 5737 | */ |
| 5738 | if (exp->cond) { |
| 5739 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5740 | ret = acl_pass(ret); |
| 5741 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5742 | ret = !ret; |
| 5743 | |
| 5744 | if (!ret) |
| 5745 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5749 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5750 | if (unlikely(ret < 0)) |
| 5751 | return -1; |
| 5752 | |
| 5753 | if (likely(ret == 0)) { |
| 5754 | /* The filter did not match the request, it can be |
| 5755 | * iterated through all headers. |
| 5756 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5757 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5758 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5759 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5760 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5761 | } |
| 5762 | |
| 5763 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5764 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5765 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5766 | * Try to retrieve the server associated to the appsession. |
| 5767 | * If the server is found, it's assigned to the session. |
| 5768 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5769 | 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] | 5770 | struct http_txn *txn = &t->txn; |
| 5771 | appsess *asession = NULL; |
| 5772 | char *sessid_temp = NULL; |
| 5773 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5774 | if (len > t->be->appsession_len) { |
| 5775 | len = t->be->appsession_len; |
| 5776 | } |
| 5777 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5778 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5779 | /* 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] | 5780 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5781 | /* 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] | 5782 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5783 | } |
| 5784 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5785 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5786 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5787 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5788 | return; |
| 5789 | } |
| 5790 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5791 | memcpy(txn->sessid, buf, len); |
| 5792 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5796 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5797 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5798 | return; |
| 5799 | } |
| 5800 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5801 | memcpy(sessid_temp, buf, len); |
| 5802 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5803 | |
| 5804 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5805 | /* free previously allocated memory */ |
| 5806 | pool_free2(apools.sessid, sessid_temp); |
| 5807 | |
| 5808 | if (asession != NULL) { |
| 5809 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5810 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5811 | asession->request_count++; |
| 5812 | |
| 5813 | if (asession->serverid != NULL) { |
| 5814 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5815 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5816 | while (srv) { |
| 5817 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5818 | if ((srv->state & SRV_RUNNING) || |
| 5819 | (t->be->options & PR_O_PERSIST) || |
| 5820 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5821 | /* we found the server and it's usable */ |
| 5822 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5823 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5824 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 5825 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 5826 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5827 | break; |
| 5828 | } else { |
| 5829 | txn->flags &= ~TX_CK_MASK; |
| 5830 | txn->flags |= TX_CK_DOWN; |
| 5831 | } |
| 5832 | } |
| 5833 | srv = srv->next; |
| 5834 | } |
| 5835 | } |
| 5836 | } |
| 5837 | } |
| 5838 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5839 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 5840 | * same way as with headers above except that the semi-colon also ends a token. |
| 5841 | * See RFC2965 for more information. Note that it requires a valid header to |
| 5842 | * return a valid result. |
| 5843 | */ |
| 5844 | char *find_cookie_value_end(char *s, const char *e) |
| 5845 | { |
| 5846 | int quoted, qdpair; |
| 5847 | |
| 5848 | quoted = qdpair = 0; |
| 5849 | for (; s < e; s++) { |
| 5850 | if (qdpair) qdpair = 0; |
| 5851 | else if (quoted) { |
| 5852 | if (*s == '\\') qdpair = 1; |
| 5853 | else if (*s == '"') quoted = 0; |
| 5854 | } |
| 5855 | else if (*s == '"') quoted = 1; |
| 5856 | else if (*s == ',' || *s == ';') return s; |
| 5857 | } |
| 5858 | return s; |
| 5859 | } |
| 5860 | |
| 5861 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 5862 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 5863 | * the first delimiter is updated if required. The function tries as much as |
| 5864 | * possible to respect the following principles : |
| 5865 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 5866 | * colon, in which case <next> is simply removed |
| 5867 | * - set exactly one space character after the new first delimiter, unless |
| 5868 | * there are not enough characters in the block being moved to do so. |
| 5869 | * - remove unneeded spaces before the previous delimiter and after the new |
| 5870 | * one. |
| 5871 | * |
| 5872 | * It is the caller's responsibility to ensure that : |
| 5873 | * - <from> points to a valid delimiter or the colon ; |
| 5874 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 5875 | * - there are non-space chars before <from> ; |
| 5876 | * - there is a CR/LF at or after <next>. |
| 5877 | */ |
| 5878 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 5879 | { |
| 5880 | char *prev = *from; |
| 5881 | |
| 5882 | if (*prev == ':') { |
| 5883 | /* We're removing the first value, preserve the colon and add a |
| 5884 | * space if possible. |
| 5885 | */ |
| 5886 | if (!http_is_crlf[(unsigned char)*next]) |
| 5887 | next++; |
| 5888 | prev++; |
| 5889 | if (prev < next) |
| 5890 | *prev++ = ' '; |
| 5891 | |
| 5892 | while (http_is_spht[(unsigned char)*next]) |
| 5893 | next++; |
| 5894 | } else { |
| 5895 | /* Remove useless spaces before the old delimiter. */ |
| 5896 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 5897 | prev--; |
| 5898 | *from = prev; |
| 5899 | |
| 5900 | /* copy the delimiter and if possible a space if we're |
| 5901 | * not at the end of the line. |
| 5902 | */ |
| 5903 | if (!http_is_crlf[(unsigned char)*next]) { |
| 5904 | *prev++ = *next++; |
| 5905 | if (prev + 1 < next) |
| 5906 | *prev++ = ' '; |
| 5907 | while (http_is_spht[(unsigned char)*next]) |
| 5908 | next++; |
| 5909 | } |
| 5910 | } |
| 5911 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 5912 | } |
| 5913 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5914 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5915 | * 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] | 5916 | * desirable to call it only when needed. This code is quite complex because |
| 5917 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 5918 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5919 | */ |
| 5920 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5921 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5922 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5923 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5924 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5925 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 5926 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5927 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5928 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5929 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 5930 | hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5931 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5932 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5933 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5934 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5935 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5936 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5937 | hdr_beg = hdr_next; |
| 5938 | hdr_end = hdr_beg + cur_hdr->len; |
| 5939 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5940 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5941 | /* We have one full header between hdr_beg and hdr_end, and the |
| 5942 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5943 | * "Cookie:" headers. |
| 5944 | */ |
| 5945 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5946 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5947 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5948 | old_idx = cur_idx; |
| 5949 | continue; |
| 5950 | } |
| 5951 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5952 | del_from = NULL; /* nothing to be deleted */ |
| 5953 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 5954 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5955 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5956 | * attributes whose name begin with a '$', and associate them with |
| 5957 | * the right cookie, if we want to delete this cookie. |
| 5958 | * So there are 3 cases for each cookie read : |
| 5959 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5960 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5961 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5962 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5963 | * "special" cookie. |
| 5964 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5965 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 5966 | * *MUST* delete it. |
| 5967 | * |
| 5968 | * Note: RFC2965 is unclear about the processing of spaces around |
| 5969 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 5970 | * the RFC explicitly allows spaces before it, and not within the |
| 5971 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 5972 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 5973 | * after the equal sign too, resulting in some (rare) buggy |
| 5974 | * implementations trying to do that. So let's do what servers do. |
| 5975 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 5976 | * allowed quoted strings in values, with any possible character |
| 5977 | * after a backslash, including control chars and delimitors, which |
| 5978 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 5979 | * within values even without quotes. |
| 5980 | * |
| 5981 | * We have to keep multiple pointers in order to support cookie |
| 5982 | * removal at the beginning, middle or end of header without |
| 5983 | * corrupting the header. All of these headers are valid : |
| 5984 | * |
| 5985 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 5986 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 5987 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 5988 | * | | | | | | | | | |
| 5989 | * | | | | | | | | hdr_end <--+ |
| 5990 | * | | | | | | | +--> next |
| 5991 | * | | | | | | +----> val_end |
| 5992 | * | | | | | +-----------> val_beg |
| 5993 | * | | | | +--------------> equal |
| 5994 | * | | | +----------------> att_end |
| 5995 | * | | +---------------------> att_beg |
| 5996 | * | +--------------------------> prev |
| 5997 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5998 | */ |
| 5999 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6000 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6001 | /* Iterate through all cookies on this line */ |
| 6002 | |
| 6003 | /* find att_beg */ |
| 6004 | att_beg = prev + 1; |
| 6005 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6006 | att_beg++; |
| 6007 | |
| 6008 | /* find att_end : this is the first character after the last non |
| 6009 | * space before the equal. It may be equal to hdr_end. |
| 6010 | */ |
| 6011 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6012 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6013 | while (equal < hdr_end) { |
| 6014 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6015 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6016 | if (http_is_spht[(unsigned char)*equal++]) |
| 6017 | continue; |
| 6018 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6019 | } |
| 6020 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6021 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6022 | * is between <att_beg> and <equal>, both may be identical. |
| 6023 | */ |
| 6024 | |
| 6025 | /* look for end of cookie if there is an equal sign */ |
| 6026 | if (equal < hdr_end && *equal == '=') { |
| 6027 | /* look for the beginning of the value */ |
| 6028 | val_beg = equal + 1; |
| 6029 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6030 | val_beg++; |
| 6031 | |
| 6032 | /* find the end of the value, respecting quotes */ |
| 6033 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6034 | |
| 6035 | /* make val_end point to the first white space or delimitor after the value */ |
| 6036 | val_end = next; |
| 6037 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6038 | val_end--; |
| 6039 | } else { |
| 6040 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6041 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6042 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6043 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6044 | * they will automatically be removed if a header before them is removed, |
| 6045 | * since they're supposed to be linked together. |
| 6046 | */ |
| 6047 | if (*att_beg == '$') |
| 6048 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6049 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6050 | /* Ignore cookies with no equal sign */ |
| 6051 | if (equal == next) { |
| 6052 | /* This is not our cookie, so we must preserve it. But if we already |
| 6053 | * scheduled another cookie for removal, we cannot remove the |
| 6054 | * complete header, but we can remove the previous block itself. |
| 6055 | */ |
| 6056 | preserve_hdr = 1; |
| 6057 | if (del_from != NULL) { |
| 6058 | int delta = del_hdr_value(req, &del_from, prev); |
| 6059 | val_end += delta; |
| 6060 | next += delta; |
| 6061 | hdr_end += delta; |
| 6062 | hdr_next += delta; |
| 6063 | cur_hdr->len += delta; |
| 6064 | http_msg_move_end(&txn->req, delta); |
| 6065 | prev = del_from; |
| 6066 | del_from = NULL; |
| 6067 | } |
| 6068 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6069 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6070 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6071 | /* if there are spaces around the equal sign, we need to |
| 6072 | * strip them otherwise we'll get trouble for cookie captures, |
| 6073 | * or even for rewrites. Since this happens extremely rarely, |
| 6074 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6075 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6076 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6077 | int stripped_before = 0; |
| 6078 | int stripped_after = 0; |
| 6079 | |
| 6080 | if (att_end != equal) { |
| 6081 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6082 | equal += stripped_before; |
| 6083 | val_beg += stripped_before; |
| 6084 | } |
| 6085 | |
| 6086 | if (val_beg > equal + 1) { |
| 6087 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6088 | val_beg += stripped_after; |
| 6089 | stripped_before += stripped_after; |
| 6090 | } |
| 6091 | |
| 6092 | val_end += stripped_before; |
| 6093 | next += stripped_before; |
| 6094 | hdr_end += stripped_before; |
| 6095 | hdr_next += stripped_before; |
| 6096 | cur_hdr->len += stripped_before; |
| 6097 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6098 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6099 | /* now everything is as on the diagram above */ |
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 | /* First, let's see if we want to capture this cookie. We check |
| 6102 | * that we don't already have a client side cookie, because we |
| 6103 | * can only capture one. Also as an optimisation, we ignore |
| 6104 | * cookies shorter than the declared name. |
| 6105 | */ |
| 6106 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6107 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6108 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6109 | int log_len = val_end - att_beg; |
| 6110 | |
| 6111 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6112 | Alert("HTTP logging : out of memory.\n"); |
| 6113 | } else { |
| 6114 | if (log_len > t->fe->capture_len) |
| 6115 | log_len = t->fe->capture_len; |
| 6116 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6117 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6118 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6119 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6120 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6121 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6122 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6123 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6124 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
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 | * For cookies in prefix mode, the form is : |
| 6127 | * |
| 6128 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6129 | */ |
| 6130 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6131 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6132 | struct server *srv = t->be->srv; |
| 6133 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6134 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6135 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6136 | * have the server ID between val_beg and delim, and the original cookie between |
| 6137 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6138 | * |
| 6139 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6140 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6141 | * | || || | |+-> next |
| 6142 | * | || || | +--> val_end |
| 6143 | * | || || +---------> delim |
| 6144 | * | || |+------------> val_beg |
| 6145 | * | || +-------------> att_end = equal |
| 6146 | * | |+-----------------> att_beg |
| 6147 | * | +------------------> prev |
| 6148 | * +-------------------------> hdr_beg |
| 6149 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6150 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6151 | if (t->be->options & PR_O_COOK_PFX) { |
| 6152 | for (delim = val_beg; delim < val_end; delim++) |
| 6153 | if (*delim == COOKIE_DELIM) |
| 6154 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6155 | } else { |
| 6156 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6157 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6158 | /* Now check if the cookie contains a date field, which would |
| 6159 | * appear after a vertical bar ('|') just after the server name |
| 6160 | * and before the delimiter. |
| 6161 | */ |
| 6162 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6163 | if (vbar1) { |
| 6164 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6165 | * right is the last seen date. It is a base64 encoded |
| 6166 | * 30-bit value representing the UNIX date since the |
| 6167 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6168 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6169 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6170 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6171 | if (val_end - vbar1 >= 5) { |
| 6172 | val = b64tos30(vbar1); |
| 6173 | if (val > 0) |
| 6174 | txn->cookie_last_date = val << 2; |
| 6175 | } |
| 6176 | /* look for a second vertical bar */ |
| 6177 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6178 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6179 | val = b64tos30(vbar1 + 1); |
| 6180 | if (val > 0) |
| 6181 | txn->cookie_first_date = val << 2; |
| 6182 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6183 | } |
| 6184 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6185 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6186 | /* if the cookie has an expiration date and the proxy wants to check |
| 6187 | * it, then we do that now. We first check if the cookie is too old, |
| 6188 | * then only if it has expired. We detect strict overflow because the |
| 6189 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6190 | * in the future are ignored if their offset is beyond one day. This |
| 6191 | * allows an admin to fix timezone issues without expiring everyone |
| 6192 | * and at the same time avoids keeping unwanted side effects for too |
| 6193 | * long. |
| 6194 | */ |
| 6195 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6196 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6197 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6198 | txn->flags &= ~TX_CK_MASK; |
| 6199 | txn->flags |= TX_CK_OLD; |
| 6200 | delim = val_beg; // let's pretend we have not found the cookie |
| 6201 | txn->cookie_first_date = 0; |
| 6202 | txn->cookie_last_date = 0; |
| 6203 | } |
| 6204 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6205 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6206 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6207 | txn->flags &= ~TX_CK_MASK; |
| 6208 | txn->flags |= TX_CK_EXPIRED; |
| 6209 | delim = val_beg; // let's pretend we have not found the cookie |
| 6210 | txn->cookie_first_date = 0; |
| 6211 | txn->cookie_last_date = 0; |
| 6212 | } |
| 6213 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6214 | /* Here, we'll look for the first running server which supports the cookie. |
| 6215 | * This allows to share a same cookie between several servers, for example |
| 6216 | * to dedicate backup servers to specific servers only. |
| 6217 | * However, to prevent clients from sticking to cookie-less backup server |
| 6218 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6219 | * empty cookies and mark them as invalid. |
| 6220 | * The same behaviour is applied when persistence must be ignored. |
| 6221 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6222 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6223 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6224 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6225 | while (srv) { |
| 6226 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6227 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6228 | if ((srv->state & SRV_RUNNING) || |
| 6229 | (t->be->options & PR_O_PERSIST) || |
| 6230 | (t->flags & SN_FORCE_PRST)) { |
| 6231 | /* we found the server and we can use it */ |
| 6232 | txn->flags &= ~TX_CK_MASK; |
| 6233 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6234 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6235 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6236 | break; |
| 6237 | } else { |
| 6238 | /* we found a server, but it's down, |
| 6239 | * mark it as such and go on in case |
| 6240 | * another one is available. |
| 6241 | */ |
| 6242 | txn->flags &= ~TX_CK_MASK; |
| 6243 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6244 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6245 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6246 | srv = srv->next; |
| 6247 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6248 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6249 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6250 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6251 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6252 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6253 | txn->flags |= TX_CK_UNUSED; |
| 6254 | else |
| 6255 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6256 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6258 | /* depending on the cookie mode, we may have to either : |
| 6259 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6260 | * the server never sees it ; |
| 6261 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6262 | * application cookie so that it does not get accidentely removed later, |
| 6263 | * if we're in cookie prefix mode |
| 6264 | */ |
| 6265 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6266 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6267 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6268 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6269 | val_end += delta; |
| 6270 | next += delta; |
| 6271 | hdr_end += delta; |
| 6272 | hdr_next += delta; |
| 6273 | cur_hdr->len += delta; |
| 6274 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6275 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6276 | del_from = NULL; |
| 6277 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6278 | } |
| 6279 | else if (del_from == NULL && |
| 6280 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6281 | del_from = prev; |
| 6282 | } |
| 6283 | } else { |
| 6284 | /* This is not our cookie, so we must preserve it. But if we already |
| 6285 | * scheduled another cookie for removal, we cannot remove the |
| 6286 | * complete header, but we can remove the previous block itself. |
| 6287 | */ |
| 6288 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6289 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6290 | if (del_from != NULL) { |
| 6291 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6292 | if (att_beg >= del_from) |
| 6293 | att_beg += delta; |
| 6294 | if (att_end >= del_from) |
| 6295 | att_end += delta; |
| 6296 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6297 | val_end += delta; |
| 6298 | next += delta; |
| 6299 | hdr_end += delta; |
| 6300 | hdr_next += delta; |
| 6301 | cur_hdr->len += delta; |
| 6302 | http_msg_move_end(&txn->req, delta); |
| 6303 | prev = del_from; |
| 6304 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6305 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6306 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6307 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6308 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6309 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6310 | int cmp_len, value_len; |
| 6311 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6312 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6313 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6314 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6315 | value_begin = att_beg + t->be->appsession_name_len; |
| 6316 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6317 | } else { |
| 6318 | cmp_len = att_end - att_beg; |
| 6319 | value_begin = val_beg; |
| 6320 | value_len = val_end - val_beg; |
| 6321 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6322 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | /* let's see if the cookie is our appcookie */ |
| 6324 | if (cmp_len == t->be->appsession_name_len && |
| 6325 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6326 | manage_client_side_appsession(t, value_begin, value_len); |
| 6327 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6328 | } |
| 6329 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6330 | /* continue with next cookie on this header line */ |
| 6331 | att_beg = next; |
| 6332 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6333 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6334 | /* There are no more cookies on this line. |
| 6335 | * We may still have one (or several) marked for deletion at the |
| 6336 | * end of the line. We must do this now in two ways : |
| 6337 | * - if some cookies must be preserved, we only delete from the |
| 6338 | * mark to the end of line ; |
| 6339 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6340 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6341 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6342 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6343 | if (preserve_hdr) { |
| 6344 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6345 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6346 | cur_hdr->len += delta; |
| 6347 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6348 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6349 | |
| 6350 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6351 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6352 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6353 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6354 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6355 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6356 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6357 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6358 | } |
| 6359 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6360 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6361 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6362 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6366 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6367 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6368 | */ |
| 6369 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6370 | { |
| 6371 | char term; |
| 6372 | char *cur_ptr, *cur_end, *cur_next; |
| 6373 | int cur_idx, old_idx, last_hdr; |
| 6374 | struct http_txn *txn = &t->txn; |
| 6375 | struct hdr_idx_elem *cur_hdr; |
| 6376 | int len, delta; |
| 6377 | |
| 6378 | last_hdr = 0; |
| 6379 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6380 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6381 | old_idx = 0; |
| 6382 | |
| 6383 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6384 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6385 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6386 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6387 | (exp->action == ACT_ALLOW || |
| 6388 | exp->action == ACT_DENY)) |
| 6389 | return 0; |
| 6390 | |
| 6391 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6392 | if (!cur_idx) |
| 6393 | break; |
| 6394 | |
| 6395 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6396 | cur_ptr = cur_next; |
| 6397 | cur_end = cur_ptr + cur_hdr->len; |
| 6398 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6399 | |
| 6400 | /* Now we have one header between cur_ptr and cur_end, |
| 6401 | * and the next header starts at cur_next. |
| 6402 | */ |
| 6403 | |
| 6404 | /* The annoying part is that pattern matching needs |
| 6405 | * that we modify the contents to null-terminate all |
| 6406 | * strings before testing them. |
| 6407 | */ |
| 6408 | |
| 6409 | term = *cur_end; |
| 6410 | *cur_end = '\0'; |
| 6411 | |
| 6412 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6413 | switch (exp->action) { |
| 6414 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6415 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6416 | last_hdr = 1; |
| 6417 | break; |
| 6418 | |
| 6419 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6420 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6421 | last_hdr = 1; |
| 6422 | break; |
| 6423 | |
| 6424 | case ACT_REPLACE: |
| 6425 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6426 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6427 | /* FIXME: if the user adds a newline in the replacement, the |
| 6428 | * index will not be recalculated for now, and the new line |
| 6429 | * will not be counted as a new header. |
| 6430 | */ |
| 6431 | |
| 6432 | cur_end += delta; |
| 6433 | cur_next += delta; |
| 6434 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6435 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6436 | break; |
| 6437 | |
| 6438 | case ACT_REMOVE: |
| 6439 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6440 | cur_next += delta; |
| 6441 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6442 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6443 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6444 | txn->hdr_idx.used--; |
| 6445 | cur_hdr->len = 0; |
| 6446 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6447 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6448 | break; |
| 6449 | |
| 6450 | } |
| 6451 | } |
| 6452 | if (cur_end) |
| 6453 | *cur_end = term; /* restore the string terminator */ |
| 6454 | |
| 6455 | /* keep the link from this header to next one in case of later |
| 6456 | * removal of next header. |
| 6457 | */ |
| 6458 | old_idx = cur_idx; |
| 6459 | } |
| 6460 | return 0; |
| 6461 | } |
| 6462 | |
| 6463 | |
| 6464 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6465 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6466 | * or -1 if a replacement resulted in an invalid status line. |
| 6467 | */ |
| 6468 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6469 | { |
| 6470 | char term; |
| 6471 | char *cur_ptr, *cur_end; |
| 6472 | int done; |
| 6473 | struct http_txn *txn = &t->txn; |
| 6474 | int len, delta; |
| 6475 | |
| 6476 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6477 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6478 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6479 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6480 | (exp->action == ACT_ALLOW || |
| 6481 | exp->action == ACT_DENY)) |
| 6482 | return 0; |
| 6483 | else if (exp->action == ACT_REMOVE) |
| 6484 | return 0; |
| 6485 | |
| 6486 | done = 0; |
| 6487 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6488 | cur_ptr = rtr->p + txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6489 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6490 | |
| 6491 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6492 | |
| 6493 | /* The annoying part is that pattern matching needs |
| 6494 | * that we modify the contents to null-terminate all |
| 6495 | * strings before testing them. |
| 6496 | */ |
| 6497 | |
| 6498 | term = *cur_end; |
| 6499 | *cur_end = '\0'; |
| 6500 | |
| 6501 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6502 | switch (exp->action) { |
| 6503 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6504 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6505 | done = 1; |
| 6506 | break; |
| 6507 | |
| 6508 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6509 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6510 | done = 1; |
| 6511 | break; |
| 6512 | |
| 6513 | case ACT_REPLACE: |
| 6514 | *cur_end = term; /* restore the string terminator */ |
| 6515 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6516 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6517 | /* FIXME: if the user adds a newline in the replacement, the |
| 6518 | * index will not be recalculated for now, and the new line |
| 6519 | * will not be counted as a new header. |
| 6520 | */ |
| 6521 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6522 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6523 | cur_end += delta; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 6524 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6525 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6526 | cur_ptr, cur_end + 1, |
| 6527 | NULL, NULL); |
| 6528 | if (unlikely(!cur_end)) |
| 6529 | return -1; |
| 6530 | |
| 6531 | /* we have a full respnse and we know that we have either a CR |
| 6532 | * or an LF at <ptr>. |
| 6533 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6534 | txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6535 | 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] | 6536 | /* there is no point trying this regex on headers */ |
| 6537 | return 1; |
| 6538 | } |
| 6539 | } |
| 6540 | *cur_end = term; /* restore the string terminator */ |
| 6541 | return done; |
| 6542 | } |
| 6543 | |
| 6544 | |
| 6545 | |
| 6546 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6547 | * 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] | 6548 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6549 | * unparsable response. |
| 6550 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6551 | 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] | 6552 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6553 | struct http_txn *txn = &s->txn; |
| 6554 | struct hdr_exp *exp; |
| 6555 | |
| 6556 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6557 | int ret; |
| 6558 | |
| 6559 | /* |
| 6560 | * The interleaving of transformations and verdicts |
| 6561 | * makes it difficult to decide to continue or stop |
| 6562 | * the evaluation. |
| 6563 | */ |
| 6564 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6565 | if (txn->flags & TX_SVDENY) |
| 6566 | break; |
| 6567 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6568 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6569 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6570 | exp->action == ACT_PASS)) { |
| 6571 | exp = exp->next; |
| 6572 | continue; |
| 6573 | } |
| 6574 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6575 | /* if this filter had a condition, evaluate it now and skip to |
| 6576 | * next filter if the condition does not match. |
| 6577 | */ |
| 6578 | if (exp->cond) { |
| 6579 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6580 | ret = acl_pass(ret); |
| 6581 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6582 | ret = !ret; |
| 6583 | if (!ret) |
| 6584 | continue; |
| 6585 | } |
| 6586 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6587 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6588 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6589 | if (unlikely(ret < 0)) |
| 6590 | return -1; |
| 6591 | |
| 6592 | if (likely(ret == 0)) { |
| 6593 | /* The filter did not match the response, it can be |
| 6594 | * iterated through all headers. |
| 6595 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6596 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6597 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6598 | } |
| 6599 | return 0; |
| 6600 | } |
| 6601 | |
| 6602 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6603 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6604 | * 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] | 6605 | * desirable to call it only when needed. This function is also used when we |
| 6606 | * 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] | 6607 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6608 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6609 | { |
| 6610 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6611 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6612 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6613 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6614 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6615 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6616 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6617 | /* Iterate through the headers. |
| 6618 | * we start with the start line. |
| 6619 | */ |
| 6620 | old_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6621 | hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6622 | |
| 6623 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6624 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6625 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6626 | |
| 6627 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6628 | hdr_beg = hdr_next; |
| 6629 | hdr_end = hdr_beg + cur_hdr->len; |
| 6630 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6631 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6632 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6633 | * next header starts at hdr_next. We're only interested in |
| 6634 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6635 | */ |
| 6636 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6637 | is_cookie2 = 0; |
| 6638 | prev = hdr_beg + 10; |
| 6639 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6640 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6641 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6642 | if (!val) { |
| 6643 | old_idx = cur_idx; |
| 6644 | continue; |
| 6645 | } |
| 6646 | is_cookie2 = 1; |
| 6647 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6648 | } |
| 6649 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6650 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6651 | * <prev> points to the colon. |
| 6652 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6653 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6654 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6655 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6656 | * check-cache is enabled) and we are not interested in checking |
| 6657 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6658 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6659 | if (t->be->cookie_name == NULL && |
| 6660 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6661 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6662 | return; |
| 6663 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6664 | /* OK so now we know we have to process this response cookie. |
| 6665 | * The format of the Set-Cookie header is slightly different |
| 6666 | * from the format of the Cookie header in that it does not |
| 6667 | * support the comma as a cookie delimiter (thus the header |
| 6668 | * cannot be folded) because the Expires attribute described in |
| 6669 | * the original Netscape's spec may contain an unquoted date |
| 6670 | * with a comma inside. We have to live with this because |
| 6671 | * many browsers don't support Max-Age and some browsers don't |
| 6672 | * support quoted strings. However the Set-Cookie2 header is |
| 6673 | * clean. |
| 6674 | * |
| 6675 | * We have to keep multiple pointers in order to support cookie |
| 6676 | * removal at the beginning, middle or end of header without |
| 6677 | * corrupting the header (in case of set-cookie2). A special |
| 6678 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6679 | * fields after the first semi-colon. The <next> pointer points |
| 6680 | * either to the end of line (set-cookie) or next unquoted comma |
| 6681 | * (set-cookie2). All of these headers are valid : |
| 6682 | * |
| 6683 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6684 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6685 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6686 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6687 | * | | | | | | | | | | |
| 6688 | * | | | | | | | | +-> next hdr_end <--+ |
| 6689 | * | | | | | | | +------------> scav |
| 6690 | * | | | | | | +--------------> val_end |
| 6691 | * | | | | | +--------------------> val_beg |
| 6692 | * | | | | +----------------------> equal |
| 6693 | * | | | +------------------------> att_end |
| 6694 | * | | +----------------------------> att_beg |
| 6695 | * | +------------------------------> prev |
| 6696 | * +-----------------------------------------> hdr_beg |
| 6697 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6698 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6699 | for (; prev < hdr_end; prev = next) { |
| 6700 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6701 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6702 | /* find att_beg */ |
| 6703 | att_beg = prev + 1; |
| 6704 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6705 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6706 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6707 | /* find att_end : this is the first character after the last non |
| 6708 | * space before the equal. It may be equal to hdr_end. |
| 6709 | */ |
| 6710 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6711 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6712 | while (equal < hdr_end) { |
| 6713 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6714 | break; |
| 6715 | if (http_is_spht[(unsigned char)*equal++]) |
| 6716 | continue; |
| 6717 | att_end = equal; |
| 6718 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6719 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6720 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6721 | * is between <att_beg> and <equal>, both may be identical. |
| 6722 | */ |
| 6723 | |
| 6724 | /* look for end of cookie if there is an equal sign */ |
| 6725 | if (equal < hdr_end && *equal == '=') { |
| 6726 | /* look for the beginning of the value */ |
| 6727 | val_beg = equal + 1; |
| 6728 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6729 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6730 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6731 | /* find the end of the value, respecting quotes */ |
| 6732 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6733 | |
| 6734 | /* make val_end point to the first white space or delimitor after the value */ |
| 6735 | val_end = next; |
| 6736 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6737 | val_end--; |
| 6738 | } else { |
| 6739 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6740 | val_beg = val_end = next = equal; |
| 6741 | } |
| 6742 | |
| 6743 | if (next < hdr_end) { |
| 6744 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6745 | * a colon or semi-colon before the end. So skip all attr-value |
| 6746 | * pairs and look for the next comma. For Set-Cookie, since |
| 6747 | * commas are permitted in values, skip to the end. |
| 6748 | */ |
| 6749 | if (is_cookie2) |
| 6750 | next = find_hdr_value_end(next, hdr_end); |
| 6751 | else |
| 6752 | next = hdr_end; |
| 6753 | } |
| 6754 | |
| 6755 | /* Now everything is as on the diagram above */ |
| 6756 | |
| 6757 | /* Ignore cookies with no equal sign */ |
| 6758 | if (equal == val_end) |
| 6759 | continue; |
| 6760 | |
| 6761 | /* If there are spaces around the equal sign, we need to |
| 6762 | * strip them otherwise we'll get trouble for cookie captures, |
| 6763 | * or even for rewrites. Since this happens extremely rarely, |
| 6764 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6765 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6766 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6767 | int stripped_before = 0; |
| 6768 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6769 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6770 | if (att_end != equal) { |
| 6771 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6772 | equal += stripped_before; |
| 6773 | val_beg += stripped_before; |
| 6774 | } |
| 6775 | |
| 6776 | if (val_beg > equal + 1) { |
| 6777 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6778 | val_beg += stripped_after; |
| 6779 | stripped_before += stripped_after; |
| 6780 | } |
| 6781 | |
| 6782 | val_end += stripped_before; |
| 6783 | next += stripped_before; |
| 6784 | hdr_end += stripped_before; |
| 6785 | hdr_next += stripped_before; |
| 6786 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6787 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6788 | } |
| 6789 | |
| 6790 | /* First, let's see if we want to capture this cookie. We check |
| 6791 | * that we don't already have a server side cookie, because we |
| 6792 | * can only capture one. Also as an optimisation, we ignore |
| 6793 | * cookies shorter than the declared name. |
| 6794 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6795 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6796 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6797 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6798 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6799 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6800 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6801 | Alert("HTTP logging : out of memory.\n"); |
| 6802 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6803 | else { |
| 6804 | if (log_len > t->fe->capture_len) |
| 6805 | log_len = t->fe->capture_len; |
| 6806 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6807 | txn->srv_cookie[log_len] = 0; |
| 6808 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6809 | } |
| 6810 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6811 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6812 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6813 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6814 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6815 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6816 | /* assume passive cookie by default */ |
| 6817 | txn->flags &= ~TX_SCK_MASK; |
| 6818 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6819 | |
| 6820 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6821 | * this occurrence because we'll insert another one later. |
| 6822 | * 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] | 6823 | * a direct access. |
| 6824 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 6825 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 6826 | /* The "preserve" flag was set, we don't want to touch the |
| 6827 | * server's cookie. |
| 6828 | */ |
| 6829 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6830 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6831 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6832 | /* this cookie must be deleted */ |
| 6833 | if (*prev == ':' && next == hdr_end) { |
| 6834 | /* whole header */ |
| 6835 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 6836 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6837 | txn->hdr_idx.used--; |
| 6838 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6839 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6840 | hdr_next += delta; |
| 6841 | http_msg_move_end(&txn->rsp, delta); |
| 6842 | /* note: while both invalid now, <next> and <hdr_end> |
| 6843 | * are still equal, so the for() will stop as expected. |
| 6844 | */ |
| 6845 | } else { |
| 6846 | /* just remove the value */ |
| 6847 | int delta = del_hdr_value(res, &prev, next); |
| 6848 | next = prev; |
| 6849 | hdr_end += delta; |
| 6850 | hdr_next += delta; |
| 6851 | cur_hdr->len += delta; |
| 6852 | http_msg_move_end(&txn->rsp, delta); |
| 6853 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6854 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6855 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6856 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6857 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6858 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6859 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6860 | * with this server since we know it. |
| 6861 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6862 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6863 | next += delta; |
| 6864 | hdr_end += delta; |
| 6865 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6866 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6867 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6868 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6869 | txn->flags &= ~TX_SCK_MASK; |
| 6870 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6871 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6872 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6873 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6874 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6875 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6876 | 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] | 6877 | next += delta; |
| 6878 | hdr_end += delta; |
| 6879 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6880 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6881 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6882 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6883 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6884 | txn->flags &= ~TX_SCK_MASK; |
| 6885 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6886 | } |
| 6887 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6888 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6889 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6890 | int cmp_len, value_len; |
| 6891 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6892 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6893 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6894 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6895 | value_begin = att_beg + t->be->appsession_name_len; |
| 6896 | 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] | 6897 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6898 | cmp_len = att_end - att_beg; |
| 6899 | value_begin = val_beg; |
| 6900 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6901 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6902 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6903 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6904 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6905 | /* free a possibly previously allocated memory */ |
| 6906 | pool_free2(apools.sessid, txn->sessid); |
| 6907 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6908 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6909 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6910 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6911 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6912 | return; |
| 6913 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6914 | memcpy(txn->sessid, value_begin, value_len); |
| 6915 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6916 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6917 | } |
| 6918 | /* that's done for this cookie, check the next one on the same |
| 6919 | * line when next != hdr_end (only if is_cookie2). |
| 6920 | */ |
| 6921 | } |
| 6922 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6923 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6924 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6925 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6926 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6927 | appsess *asession = NULL; |
| 6928 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6929 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6930 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6931 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6932 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6933 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6934 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6935 | return; |
| 6936 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6937 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 6938 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6939 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6940 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6941 | 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] | 6942 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6943 | return; |
| 6944 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6945 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6946 | asession->sessid[t->be->appsession_len] = 0; |
| 6947 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6948 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6949 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 6950 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6951 | 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] | 6952 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6953 | return; |
| 6954 | } |
| 6955 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6956 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6957 | |
| 6958 | asession->request_count = 0; |
| 6959 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6960 | } |
| 6961 | |
| 6962 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6963 | asession->request_count++; |
| 6964 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6968 | /* |
| 6969 | * Check if response is cacheable or not. Updates t->flags. |
| 6970 | */ |
| 6971 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6972 | { |
| 6973 | struct http_txn *txn = &t->txn; |
| 6974 | char *p1, *p2; |
| 6975 | |
| 6976 | char *cur_ptr, *cur_end, *cur_next; |
| 6977 | int cur_idx; |
| 6978 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6979 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6980 | return; |
| 6981 | |
| 6982 | /* Iterate through the headers. |
| 6983 | * we start with the start line. |
| 6984 | */ |
| 6985 | cur_idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 6986 | cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6987 | |
| 6988 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6989 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6990 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6991 | |
| 6992 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6993 | cur_ptr = cur_next; |
| 6994 | cur_end = cur_ptr + cur_hdr->len; |
| 6995 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6996 | |
| 6997 | /* We have one full header between cur_ptr and cur_end, and the |
| 6998 | * next header starts at cur_next. We're only interested in |
| 6999 | * "Cookie:" headers. |
| 7000 | */ |
| 7001 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7002 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7003 | if (val) { |
| 7004 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7005 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7006 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7007 | return; |
| 7008 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7009 | } |
| 7010 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7011 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7012 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7013 | continue; |
| 7014 | |
| 7015 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7016 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7017 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7018 | |
| 7019 | if (p1 >= cur_end) /* no more info */ |
| 7020 | continue; |
| 7021 | |
| 7022 | /* p1 is at the beginning of the value */ |
| 7023 | p2 = p1; |
| 7024 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7025 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7026 | p2++; |
| 7027 | |
| 7028 | /* we have a complete value between p1 and p2 */ |
| 7029 | if (p2 < cur_end && *p2 == '=') { |
| 7030 | /* we have something of the form no-cache="set-cookie" */ |
| 7031 | if ((cur_end - p1 >= 21) && |
| 7032 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7033 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7034 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7035 | continue; |
| 7036 | } |
| 7037 | |
| 7038 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7039 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7040 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7041 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7042 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7043 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7044 | return; |
| 7045 | } |
| 7046 | |
| 7047 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7048 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7049 | continue; |
| 7050 | } |
| 7051 | } |
| 7052 | } |
| 7053 | |
| 7054 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7055 | /* |
| 7056 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7057 | * If the server is found, it's assigned to the session. |
| 7058 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7059 | 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] | 7060 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7061 | char *end_params, *first_param, *cur_param, *next_param; |
| 7062 | char separator; |
| 7063 | int value_len; |
| 7064 | |
| 7065 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7066 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7067 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7068 | (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] | 7069 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7070 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7071 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7072 | first_param = NULL; |
| 7073 | switch (mode) { |
| 7074 | case PR_O2_AS_M_PP: |
| 7075 | first_param = memchr(begin, ';', len); |
| 7076 | break; |
| 7077 | case PR_O2_AS_M_QS: |
| 7078 | first_param = memchr(begin, '?', len); |
| 7079 | break; |
| 7080 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7081 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7082 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7083 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7084 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7085 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7086 | switch (mode) { |
| 7087 | case PR_O2_AS_M_PP: |
| 7088 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7089 | end_params = (char *) begin + len; |
| 7090 | } |
| 7091 | separator = ';'; |
| 7092 | break; |
| 7093 | case PR_O2_AS_M_QS: |
| 7094 | end_params = (char *) begin + len; |
| 7095 | separator = '&'; |
| 7096 | break; |
| 7097 | default: |
| 7098 | /* unknown mode, shouldn't happen */ |
| 7099 | return; |
| 7100 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7101 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7102 | cur_param = next_param = end_params; |
| 7103 | while (cur_param > first_param) { |
| 7104 | cur_param--; |
| 7105 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7106 | /* let's see if this is the appsession parameter */ |
| 7107 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7108 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7109 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7110 | /* Cool... it's the right one */ |
| 7111 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7112 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7113 | if (value_len > 0) { |
| 7114 | manage_client_side_appsession(t, cur_param, value_len); |
| 7115 | } |
| 7116 | break; |
| 7117 | } |
| 7118 | next_param = cur_param; |
| 7119 | } |
| 7120 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7121 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7122 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7123 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7124 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7125 | } |
| 7126 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7127 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7128 | * 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] | 7129 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7130 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7131 | * 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] | 7132 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7133 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7134 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7135 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7136 | 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] | 7137 | { |
| 7138 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7139 | struct http_msg *msg = &txn->req; |
| 7140 | const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u; |
| 7141 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7142 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7143 | if (!uri_auth) |
| 7144 | return 0; |
| 7145 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7146 | 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] | 7147 | return 0; |
| 7148 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7149 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7150 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7151 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7152 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7153 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7154 | return 0; |
| 7155 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7156 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7157 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7158 | return 0; |
| 7159 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7160 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7161 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7162 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7163 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7164 | break; |
| 7165 | } |
| 7166 | h++; |
| 7167 | } |
| 7168 | |
| 7169 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7170 | h = uri + uri_auth->uri_len; |
| 7171 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7172 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7173 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7174 | break; |
| 7175 | } |
| 7176 | h++; |
| 7177 | } |
| 7178 | } |
| 7179 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7180 | h = uri + uri_auth->uri_len; |
| 7181 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7182 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7183 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7184 | break; |
| 7185 | } |
| 7186 | h++; |
| 7187 | } |
| 7188 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7189 | h = uri + uri_auth->uri_len; |
| 7190 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7191 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7192 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7193 | h += 4; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7194 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7195 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7196 | si->applet.ctx.stats.st_code = i; |
| 7197 | break; |
| 7198 | } |
| 7199 | } |
| 7200 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7201 | break; |
| 7202 | } |
| 7203 | h++; |
| 7204 | } |
| 7205 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7206 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7207 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7208 | return 1; |
| 7209 | } |
| 7210 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7211 | /* |
| 7212 | * 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] | 7213 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7214 | * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests |
| 7215 | * or response messages cannot wrap, this function may also be used with chunks |
| 7216 | * which may wrap. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7217 | */ |
| 7218 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7219 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7220 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7221 | { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7222 | struct buffer *buf = msg->buf; |
| 7223 | |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7224 | if (bi_end(buf) <= (buf->p + msg->som)) { /* message wraps */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7225 | int len1 = buf->size - msg->som - (buf->p - buf->data); |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7226 | es->len = bi_end(buf) - (buf->p + msg->som) + buf->size; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7227 | memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7228 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7229 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7230 | } |
| 7231 | else { |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7232 | es->len = bi_end(buf) - (buf->p + msg->som); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7233 | memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf))); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7234 | } |
| 7235 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7236 | if (msg->err_pos >= 0) |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 7237 | es->pos = msg->err_pos - msg->som - (buf->p - buf->data); |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7238 | else if (b_ptr(buf, msg->next) >= (buf->p + msg->som)) |
| 7239 | es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7240 | else |
Willy Tarreau | cc5cfcb | 2012-05-04 21:35:27 +0200 | [diff] [blame] | 7241 | es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som) + buf->size; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7242 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7243 | es->when = date; // user-visible date |
| 7244 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7245 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7246 | es->oe = other_end; |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 7247 | es->src = s->req->prod->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7248 | es->state = state; |
| 7249 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7250 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7251 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7252 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7253 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7254 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7255 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7256 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7257 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7258 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7259 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7260 | * -1. |
| 7261 | * 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] | 7262 | */ |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7263 | unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen, |
| 7264 | struct hdr_idx *idx, int occ, |
| 7265 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7266 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7267 | struct hdr_ctx local_ctx; |
| 7268 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7269 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7270 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7271 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7272 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7273 | if (!ctx) { |
| 7274 | local_ctx.idx = 0; |
| 7275 | ctx = &local_ctx; |
| 7276 | } |
| 7277 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7278 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7279 | /* search from the beginning */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7280 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7281 | occ--; |
| 7282 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7283 | *vptr = ctx->line + ctx->val; |
| 7284 | *vlen = ctx->vlen; |
| 7285 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7286 | } |
| 7287 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7288 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7289 | } |
| 7290 | |
| 7291 | /* negative occurrence, we scan all the list then walk back */ |
| 7292 | if (-occ > MAX_HDR_HISTORY) |
| 7293 | return 0; |
| 7294 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7295 | found = hist_ptr = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7296 | while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7297 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7298 | len_hist[hist_ptr] = ctx->vlen; |
| 7299 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7300 | hist_ptr = 0; |
| 7301 | found++; |
| 7302 | } |
| 7303 | if (-occ > found) |
| 7304 | return 0; |
| 7305 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7306 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7307 | */ |
| 7308 | hist_ptr += occ; |
| 7309 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7310 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7311 | *vptr = ptr_hist[hist_ptr]; |
| 7312 | *vlen = len_hist[hist_ptr]; |
| 7313 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7314 | } |
| 7315 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7316 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7317 | * Print a debug line with a header |
| 7318 | */ |
| 7319 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7320 | { |
| 7321 | int len, max; |
| 7322 | 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] | 7323 | 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] | 7324 | max = end - start; |
| 7325 | UBOUND(max, sizeof(trash) - len - 1); |
| 7326 | len += strlcpy2(trash + len, start, max + 1); |
| 7327 | trash[len++] = '\n'; |
Willy Tarreau | 2133782 | 2012-04-29 14:11:38 +0200 | [diff] [blame] | 7328 | if (write(1, trash, len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7329 | } |
| 7330 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7331 | /* |
| 7332 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7333 | * the required fields are properly allocated and that we only need to (re)init |
| 7334 | * them. This should be used before processing any new request. |
| 7335 | */ |
| 7336 | void http_init_txn(struct session *s) |
| 7337 | { |
| 7338 | struct http_txn *txn = &s->txn; |
| 7339 | struct proxy *fe = s->fe; |
| 7340 | |
| 7341 | txn->flags = 0; |
| 7342 | txn->status = -1; |
| 7343 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7344 | global.req_count++; |
| 7345 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7346 | txn->cookie_first_date = 0; |
| 7347 | txn->cookie_last_date = 0; |
| 7348 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7349 | txn->req.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7350 | txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7351 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7352 | txn->rsp.flags = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7353 | txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 7354 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7355 | txn->req.chunk_len = 0LL; |
| 7356 | txn->req.body_len = 0LL; |
| 7357 | txn->rsp.chunk_len = 0LL; |
| 7358 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7359 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7360 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 7361 | txn->req.buf = s->req; |
| 7362 | txn->rsp.buf = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7363 | |
| 7364 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7365 | |
| 7366 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7367 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7368 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7369 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7370 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7371 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7372 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7373 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7374 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7375 | |
| 7376 | if (txn->hdr_idx.v) |
| 7377 | hdr_idx_init(&txn->hdr_idx); |
| 7378 | } |
| 7379 | |
| 7380 | /* to be used at the end of a transaction */ |
| 7381 | void http_end_txn(struct session *s) |
| 7382 | { |
| 7383 | struct http_txn *txn = &s->txn; |
| 7384 | |
| 7385 | /* these ones will have been dynamically allocated */ |
| 7386 | pool_free2(pool2_requri, txn->uri); |
| 7387 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7388 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7389 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7390 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7391 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7392 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7393 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7394 | txn->uri = NULL; |
| 7395 | txn->srv_cookie = NULL; |
| 7396 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7397 | |
| 7398 | if (txn->req.cap) { |
| 7399 | struct cap_hdr *h; |
| 7400 | for (h = s->fe->req_cap; h; h = h->next) |
| 7401 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7402 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7403 | } |
| 7404 | |
| 7405 | if (txn->rsp.cap) { |
| 7406 | struct cap_hdr *h; |
| 7407 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7408 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7409 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7410 | } |
| 7411 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7412 | } |
| 7413 | |
| 7414 | /* to be used at the end of a transaction to prepare a new one */ |
| 7415 | void http_reset_txn(struct session *s) |
| 7416 | { |
| 7417 | http_end_txn(s); |
| 7418 | http_init_txn(s); |
| 7419 | |
| 7420 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7421 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7422 | session_del_srv_conn(s); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7423 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7424 | /* re-init store persistence */ |
| 7425 | s->store_count = 0; |
| 7426 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7427 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7428 | |
| 7429 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7430 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7431 | /* We must trim any excess data from the response buffer, because we |
| 7432 | * may have blocked an invalid response from a server that we don't |
| 7433 | * want to accidentely forward once we disable the analysers, nor do |
| 7434 | * we want those data to come along with next response. A typical |
| 7435 | * example of such data would be from a buggy server responding to |
| 7436 | * a HEAD with some data, or sending more than the advertised |
| 7437 | * content-length. |
| 7438 | */ |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 7439 | if (unlikely(s->rep->i)) |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 7440 | s->rep->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7441 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7442 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7443 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7444 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7445 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7446 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7447 | |
| 7448 | s->req->rex = TICK_ETERNITY; |
| 7449 | s->req->wex = TICK_ETERNITY; |
| 7450 | s->req->analyse_exp = TICK_ETERNITY; |
| 7451 | s->rep->rex = TICK_ETERNITY; |
| 7452 | s->rep->wex = TICK_ETERNITY; |
| 7453 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7454 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7455 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7456 | void free_http_req_rules(struct list *r) { |
| 7457 | struct http_req_rule *tr, *pr; |
| 7458 | |
| 7459 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7460 | LIST_DEL(&pr->list); |
| 7461 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7462 | free(pr->http_auth.realm); |
| 7463 | |
| 7464 | free(pr); |
| 7465 | } |
| 7466 | } |
| 7467 | |
| 7468 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7469 | { |
| 7470 | struct http_req_rule *rule; |
| 7471 | int cur_arg; |
| 7472 | |
| 7473 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7474 | if (!rule) { |
| 7475 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7476 | return NULL; |
| 7477 | } |
| 7478 | |
| 7479 | if (!*args[0]) { |
| 7480 | goto req_error_parsing; |
| 7481 | } else if (!strcmp(args[0], "allow")) { |
| 7482 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7483 | cur_arg = 1; |
| 7484 | } else if (!strcmp(args[0], "deny")) { |
| 7485 | rule->action = HTTP_REQ_ACT_DENY; |
| 7486 | cur_arg = 1; |
| 7487 | } else if (!strcmp(args[0], "auth")) { |
| 7488 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7489 | cur_arg = 1; |
| 7490 | |
| 7491 | while(*args[cur_arg]) { |
| 7492 | if (!strcmp(args[cur_arg], "realm")) { |
| 7493 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7494 | cur_arg+=2; |
| 7495 | continue; |
| 7496 | } else |
| 7497 | break; |
| 7498 | } |
| 7499 | } else { |
| 7500 | req_error_parsing: |
| 7501 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7502 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7503 | return NULL; |
| 7504 | } |
| 7505 | |
| 7506 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7507 | struct acl_cond *cond; |
| 7508 | |
| 7509 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7510 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7511 | file, linenum, args[0]); |
| 7512 | return NULL; |
| 7513 | } |
| 7514 | rule->cond = cond; |
| 7515 | } |
| 7516 | else if (*args[cur_arg]) { |
| 7517 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7518 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7519 | file, linenum, args[0], args[cur_arg]); |
| 7520 | return NULL; |
| 7521 | } |
| 7522 | |
| 7523 | return rule; |
| 7524 | } |
| 7525 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7526 | /************************************************************************/ |
| 7527 | /* The code below is dedicated to ACL parsing and matching */ |
| 7528 | /************************************************************************/ |
| 7529 | |
| 7530 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7531 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 7532 | * which means that a request or response is ready. If some data is missing, |
| 7533 | * a parsing attempt is made. This is useful in TCP-based ACLs which are able |
| 7534 | * to extract data from L7. |
| 7535 | * |
| 7536 | * The function returns : |
| 7537 | * 0 if some data is missing or if the requested data cannot be fetched |
| 7538 | * -1 if it is certain that we'll never have any HTTP message there |
| 7539 | * 1 if an HTTP message is ready |
| 7540 | */ |
| 7541 | static int |
| 7542 | acl_prefetch_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7543 | struct acl_expr *expr, struct acl_test *test) |
| 7544 | { |
| 7545 | struct http_txn *txn = l7; |
| 7546 | struct http_msg *msg = &txn->req; |
| 7547 | |
| 7548 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7549 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7550 | */ |
| 7551 | |
| 7552 | if (unlikely(!s || !txn)) |
| 7553 | return 0; |
| 7554 | |
| 7555 | /* Check for a dependency on a request */ |
| 7556 | |
| 7557 | if (expr->kw->requires & ACL_USE_L7REQ_ANY) { |
| 7558 | if (unlikely(!s->req)) |
| 7559 | return 0; |
| 7560 | |
| 7561 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
| 7562 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
| 7563 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7564 | return -1; |
| 7565 | } |
| 7566 | |
| 7567 | /* Try to decode HTTP request */ |
| 7568 | if (likely(msg->next < s->req->i)) |
| 7569 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 7570 | |
| 7571 | /* Still no valid request ? */ |
| 7572 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
| 7573 | if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) { |
| 7574 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7575 | return -1; |
| 7576 | } |
| 7577 | /* wait for final state */ |
| 7578 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7579 | return 0; |
| 7580 | } |
| 7581 | |
| 7582 | /* OK we just got a valid HTTP request. We have some minor |
| 7583 | * preparation to perform so that further checks can rely |
| 7584 | * on HTTP tests. |
| 7585 | */ |
| 7586 | txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l); |
| 7587 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7588 | s->flags |= SN_REDIRECTABLE; |
| 7589 | |
| 7590 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) { |
| 7591 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7592 | return -1; |
| 7593 | } |
| 7594 | } |
| 7595 | |
| 7596 | if ((expr->kw->requires & ACL_USE_L7REQ_VOLATILE) && |
| 7597 | txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7598 | return 0; /* data might have moved and indexes changed */ |
| 7599 | |
| 7600 | /* otherwise everything's ready for the request */ |
| 7601 | } |
| 7602 | |
| 7603 | /* Check for a dependency on a response */ |
| 7604 | if (expr->kw->requires & ACL_USE_L7RTR_ANY) { |
| 7605 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 7606 | return 0; |
| 7607 | } |
| 7608 | |
| 7609 | /* everything's OK */ |
| 7610 | return 1; |
| 7611 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7612 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7613 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
| 7614 | do { int r = acl_prefetch_http(px, l4, l7, dir, expr, test); if (r <= 0) return r; } while (0) |
| 7615 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7616 | |
| 7617 | /* 1. Check on METHOD |
| 7618 | * We use the pre-parsed method if it is known, and store its number as an |
| 7619 | * integer. If it is unknown, we use the pointer and the length. |
| 7620 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7621 | 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] | 7622 | { |
| 7623 | int len, meth; |
| 7624 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7625 | len = strlen(*text); |
| 7626 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7627 | |
| 7628 | pattern->val.i = meth; |
| 7629 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7630 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7631 | if (!pattern->ptr.str) |
| 7632 | return 0; |
| 7633 | pattern->len = len; |
| 7634 | } |
| 7635 | return 1; |
| 7636 | } |
| 7637 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7638 | /* This function fetches the method of current HTTP request and stores |
| 7639 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 7640 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 7641 | * in <len> and <ptr> is NULL ; |
| 7642 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 7643 | * <len> to its length. |
| 7644 | * This is intended to be used with acl_match_meth() only. |
| 7645 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7646 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7647 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7648 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7649 | { |
| 7650 | int meth; |
| 7651 | struct http_txn *txn = l7; |
| 7652 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7653 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7654 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7655 | meth = txn->meth; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7656 | temp_pattern.data.str.len = meth; |
| 7657 | temp_pattern.data.str.str = NULL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7658 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7659 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7660 | /* ensure the indexes are not affected */ |
| 7661 | return 0; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7662 | temp_pattern.data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7663 | temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7664 | } |
| 7665 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7666 | return 1; |
| 7667 | } |
| 7668 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7669 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7670 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7671 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7672 | int icase; |
| 7673 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7674 | |
| 7675 | if (temp_pattern.data.str.str == NULL) { |
| 7676 | /* well-known method */ |
| 7677 | if (temp_pattern.data.str.len == pattern->val.i) |
| 7678 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7679 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7680 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7681 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7682 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 7683 | if (pattern->val.i != HTTP_METH_OTHER) |
| 7684 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7685 | |
| 7686 | /* Other method, we must compare the strings */ |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7687 | if (pattern->len != temp_pattern.data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7688 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7689 | |
| 7690 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 7691 | if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) || |
| 7692 | (!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] | 7693 | return ACL_PAT_FAIL; |
| 7694 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7695 | } |
| 7696 | |
| 7697 | /* 2. Check on Request/Status Version |
| 7698 | * We simply compare strings here. |
| 7699 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7700 | 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] | 7701 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7702 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7703 | if (!pattern->ptr.str) |
| 7704 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7705 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7706 | return 1; |
| 7707 | } |
| 7708 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7709 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7710 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7711 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7712 | { |
| 7713 | struct http_txn *txn = l7; |
| 7714 | char *ptr; |
| 7715 | int len; |
| 7716 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7717 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7718 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7719 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7720 | ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7721 | |
| 7722 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7723 | if (len <= 0) |
| 7724 | return 0; |
| 7725 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7726 | temp_pattern.data.str.str = ptr; |
| 7727 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7728 | |
| 7729 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7730 | return 1; |
| 7731 | } |
| 7732 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7733 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7734 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7735 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7736 | { |
| 7737 | struct http_txn *txn = l7; |
| 7738 | char *ptr; |
| 7739 | int len; |
| 7740 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7741 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7742 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7743 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7744 | ptr = txn->rsp.buf->p + txn->rsp.sol; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7745 | |
| 7746 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7747 | if (len <= 0) |
| 7748 | return 0; |
| 7749 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7750 | temp_pattern.data.str.str = ptr; |
| 7751 | temp_pattern.data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7752 | |
| 7753 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7754 | return 1; |
| 7755 | } |
| 7756 | |
| 7757 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7758 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7759 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7760 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7761 | { |
| 7762 | struct http_txn *txn = l7; |
| 7763 | char *ptr; |
| 7764 | int len; |
| 7765 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7766 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7767 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7768 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7769 | ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7770 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7771 | temp_pattern.data.integer = __strl2ui(ptr, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7772 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7773 | return 1; |
| 7774 | } |
| 7775 | |
| 7776 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7777 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7778 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7779 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7780 | { |
| 7781 | struct http_txn *txn = l7; |
| 7782 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7783 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7784 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7785 | temp_pattern.data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7786 | temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7787 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7788 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7789 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7790 | return 1; |
| 7791 | } |
| 7792 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7793 | static int |
| 7794 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7795 | struct acl_expr *expr, struct acl_test *test) |
| 7796 | { |
| 7797 | struct http_txn *txn = l7; |
| 7798 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7799 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7800 | |
| 7801 | /* Parse HTTP request */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7802 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7803 | if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET) |
| 7804 | return 0; |
| 7805 | temp_pattern.type = PATTERN_TYPE_IP; |
| 7806 | 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] | 7807 | |
| 7808 | /* |
| 7809 | * If we are parsing url in frontend space, we prepare backend stage |
| 7810 | * to not parse again the same url ! optimization lazyness... |
| 7811 | */ |
| 7812 | if (px->options & PR_O_HTTP_PROXY) |
| 7813 | l4->flags |= SN_ADDR_SET; |
| 7814 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7815 | test->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7816 | return 1; |
| 7817 | } |
| 7818 | |
| 7819 | static int |
| 7820 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7821 | struct acl_expr *expr, struct acl_test *test) |
| 7822 | { |
| 7823 | struct http_txn *txn = l7; |
| 7824 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7825 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7826 | |
| 7827 | /* Same optimization as url_ip */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7828 | url2sa(txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to); |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7829 | 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] | 7830 | |
| 7831 | if (px->options & PR_O_HTTP_PROXY) |
| 7832 | l4->flags |= SN_ADDR_SET; |
| 7833 | |
| 7834 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7835 | return 1; |
| 7836 | } |
| 7837 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7838 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7839 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7840 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7841 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7842 | 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] | 7843 | struct acl_expr *expr, struct acl_test *test) |
| 7844 | { |
| 7845 | struct http_txn *txn = l7; |
| 7846 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7847 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7848 | |
| 7849 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7850 | /* search for header from the beginning */ |
| 7851 | ctx->idx = 0; |
| 7852 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7853 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7854 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7855 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 7856 | temp_pattern.data.str.str = (char *)ctx->line + ctx->val; |
| 7857 | temp_pattern.data.str.len = ctx->vlen; |
| 7858 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7859 | return 1; |
| 7860 | } |
| 7861 | |
| 7862 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7863 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7864 | return 0; |
| 7865 | } |
| 7866 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7867 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7868 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7869 | struct acl_expr *expr, struct acl_test *test) |
| 7870 | { |
| 7871 | struct http_txn *txn = l7; |
| 7872 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7873 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7874 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7875 | return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7876 | } |
| 7877 | |
| 7878 | static int |
| 7879 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7880 | struct acl_expr *expr, struct acl_test *test) |
| 7881 | { |
| 7882 | struct http_txn *txn = l7; |
| 7883 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7884 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7885 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7886 | return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7887 | } |
| 7888 | |
| 7889 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7890 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7891 | */ |
| 7892 | static int |
| 7893 | 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] | 7894 | struct acl_expr *expr, struct acl_test *test) |
| 7895 | { |
| 7896 | struct http_txn *txn = l7; |
| 7897 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7898 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7899 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7900 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7901 | ctx.idx = 0; |
| 7902 | cnt = 0; |
| 7903 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7904 | cnt++; |
| 7905 | |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7906 | temp_pattern.data.integer = cnt; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7907 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7908 | return 1; |
| 7909 | } |
| 7910 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7911 | static int |
| 7912 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7913 | struct acl_expr *expr, struct acl_test *test) |
| 7914 | { |
| 7915 | struct http_txn *txn = l7; |
| 7916 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7917 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7918 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7919 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | static int |
| 7923 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7924 | struct acl_expr *expr, struct acl_test *test) |
| 7925 | { |
| 7926 | struct http_txn *txn = l7; |
| 7927 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7928 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7929 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7930 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7931 | } |
| 7932 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7933 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7934 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7935 | * 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] | 7936 | */ |
| 7937 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7938 | 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] | 7939 | struct acl_expr *expr, struct acl_test *test) |
| 7940 | { |
| 7941 | struct http_txn *txn = l7; |
| 7942 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7943 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7944 | |
| 7945 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7946 | /* search for header from the beginning */ |
| 7947 | ctx->idx = 0; |
| 7948 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7949 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7950 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7951 | test->flags |= ACL_TEST_F_VOL_HDR; |
Willy Tarreau | a5e3756 | 2011-12-16 17:06:15 +0100 | [diff] [blame] | 7952 | temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7953 | return 1; |
| 7954 | } |
| 7955 | |
| 7956 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7957 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7958 | return 0; |
| 7959 | } |
| 7960 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7961 | static int |
| 7962 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7963 | struct acl_expr *expr, struct acl_test *test) |
| 7964 | { |
| 7965 | struct http_txn *txn = l7; |
| 7966 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7967 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7968 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7969 | return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7970 | } |
| 7971 | |
| 7972 | static int |
| 7973 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7974 | struct acl_expr *expr, struct acl_test *test) |
| 7975 | { |
| 7976 | struct http_txn *txn = l7; |
| 7977 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 7978 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7979 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7980 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7981 | } |
| 7982 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7983 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7984 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7985 | */ |
| 7986 | static int |
| 7987 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7988 | struct acl_expr *expr, struct acl_test *test) |
| 7989 | { |
| 7990 | struct http_txn *txn = l7; |
| 7991 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7992 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7993 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7994 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7995 | /* search for header from the beginning */ |
| 7996 | ctx->idx = 0; |
| 7997 | |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 7998 | 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] | 7999 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8000 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8001 | /* Same optimization as url_ip */ |
Willy Tarreau | f4362b3 | 2011-12-16 17:49:52 +0100 | [diff] [blame] | 8002 | temp_pattern.type = PATTERN_TYPE_IP; |
| 8003 | if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip)) |
| 8004 | return 1; |
| 8005 | /* Dods not look like an IP address, let's fetch next one */ |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8006 | } |
| 8007 | |
| 8008 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8009 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8010 | return 0; |
| 8011 | } |
| 8012 | |
| 8013 | static int |
| 8014 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8015 | struct acl_expr *expr, struct acl_test *test) |
| 8016 | { |
| 8017 | struct http_txn *txn = l7; |
| 8018 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8019 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8020 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8021 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test); |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8022 | } |
| 8023 | |
| 8024 | static int |
| 8025 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8026 | struct acl_expr *expr, struct acl_test *test) |
| 8027 | { |
| 8028 | struct http_txn *txn = l7; |
| 8029 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8030 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8031 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8032 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test); |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8033 | } |
| 8034 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8035 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8036 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8037 | */ |
| 8038 | static int |
| 8039 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8040 | struct acl_expr *expr, struct acl_test *test) |
| 8041 | { |
| 8042 | struct http_txn *txn = l7; |
| 8043 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8044 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8045 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8046 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8047 | end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 8048 | ptr = http_get_path(txn); |
| 8049 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8050 | return 0; |
| 8051 | |
| 8052 | /* OK, we got the '/' ! */ |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8053 | temp_pattern.data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8054 | |
| 8055 | while (ptr < end && *ptr != '?') |
| 8056 | ptr++; |
| 8057 | |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8058 | temp_pattern.data.str.len = ptr - temp_pattern.data.str.str; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8059 | |
| 8060 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8061 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8062 | return 1; |
| 8063 | } |
| 8064 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8065 | static int |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8066 | acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, int dir, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8067 | struct acl_expr *expr, struct acl_test *test) |
| 8068 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8069 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8070 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8071 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8072 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8073 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8074 | |
| 8075 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8076 | return 1; |
| 8077 | } |
| 8078 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8079 | /* return a valid test if the current request is the first one on the connection */ |
| 8080 | static int |
| 8081 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8082 | struct acl_expr *expr, struct acl_test *test) |
| 8083 | { |
| 8084 | if (!s) |
| 8085 | return 0; |
| 8086 | |
| 8087 | if (s->txn.flags & TX_NOT_FIRST) |
| 8088 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8089 | else |
| 8090 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8091 | |
| 8092 | return 1; |
| 8093 | } |
| 8094 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8095 | static int |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8096 | acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, int dir, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8097 | struct acl_expr *expr, struct acl_test *test) |
| 8098 | { |
| 8099 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8100 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8101 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8102 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8103 | return 0; |
| 8104 | |
| 8105 | test->ctx.a[0] = expr->arg.ul; |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8106 | test->ctx.a[1] = l4->txn.auth.user; |
| 8107 | test->ctx.a[2] = l4->txn.auth.pass; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8108 | |
| 8109 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8110 | |
| 8111 | return 1; |
| 8112 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8113 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8114 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8115 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8116 | * the cookie value is returned into *value and *value_l, and the function |
| 8117 | * returns a pointer to the next pointer to search from if the value was found. |
| 8118 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8119 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8120 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8121 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8122 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8123 | * list is expected. |
| 8124 | */ |
| 8125 | static char * |
| 8126 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8127 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8128 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8129 | { |
| 8130 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8131 | char *next; |
| 8132 | |
| 8133 | /* we search at least a cookie name followed by an equal, and more |
| 8134 | * generally something like this : |
| 8135 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8136 | */ |
| 8137 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8138 | /* Iterate through all cookies on this line */ |
| 8139 | |
| 8140 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8141 | att_beg++; |
| 8142 | |
| 8143 | /* find att_end : this is the first character after the last non |
| 8144 | * space before the equal. It may be equal to hdr_end. |
| 8145 | */ |
| 8146 | equal = att_end = att_beg; |
| 8147 | |
| 8148 | while (equal < hdr_end) { |
| 8149 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8150 | break; |
| 8151 | if (http_is_spht[(unsigned char)*equal++]) |
| 8152 | continue; |
| 8153 | att_end = equal; |
| 8154 | } |
| 8155 | |
| 8156 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8157 | * is between <att_beg> and <equal>, both may be identical. |
| 8158 | */ |
| 8159 | |
| 8160 | /* look for end of cookie if there is an equal sign */ |
| 8161 | if (equal < hdr_end && *equal == '=') { |
| 8162 | /* look for the beginning of the value */ |
| 8163 | val_beg = equal + 1; |
| 8164 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8165 | val_beg++; |
| 8166 | |
| 8167 | /* find the end of the value, respecting quotes */ |
| 8168 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8169 | |
| 8170 | /* make val_end point to the first white space or delimitor after the value */ |
| 8171 | val_end = next; |
| 8172 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8173 | val_end--; |
| 8174 | } else { |
| 8175 | val_beg = val_end = next = equal; |
| 8176 | } |
| 8177 | |
| 8178 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8179 | * they will automatically be removed if a header before them is removed, |
| 8180 | * since they're supposed to be linked together. |
| 8181 | */ |
| 8182 | if (*att_beg == '$') |
| 8183 | continue; |
| 8184 | |
| 8185 | /* Ignore cookies with no equal sign */ |
| 8186 | if (equal == next) |
| 8187 | continue; |
| 8188 | |
| 8189 | /* Now we have the cookie name between att_beg and att_end, and |
| 8190 | * its value between val_beg and val_end. |
| 8191 | */ |
| 8192 | |
| 8193 | if (att_end - att_beg == cookie_name_l && |
| 8194 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8195 | /* let's return this value and indicate where to go on from */ |
| 8196 | *value = val_beg; |
| 8197 | *value_l = val_end - val_beg; |
| 8198 | return next + 1; |
| 8199 | } |
| 8200 | |
| 8201 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8202 | if (!list) |
| 8203 | break; |
| 8204 | } |
| 8205 | |
| 8206 | return NULL; |
| 8207 | } |
| 8208 | |
| 8209 | /* Iterate over all cookies present in a request. The context is stored in |
| 8210 | * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the |
| 8211 | * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is |
| 8212 | * non-null, then multiple cookies may be parsed on the same line. |
| 8213 | * The cookie name is in expr->arg and the name length in expr->arg_len. |
| 8214 | */ |
| 8215 | static int |
| 8216 | acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8217 | const char *hdr_name, int hdr_name_len, int multi, |
| 8218 | struct acl_expr *expr, struct acl_test *test) |
| 8219 | { |
| 8220 | struct http_txn *txn = l7; |
| 8221 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8222 | struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2]; |
| 8223 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8224 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) { |
| 8225 | /* search for the header from the beginning, we must first initialize |
| 8226 | * the search parameters. |
| 8227 | */ |
| 8228 | test->ctx.a[0] = NULL; |
| 8229 | ctx->idx = 0; |
| 8230 | } |
| 8231 | |
| 8232 | while (1) { |
| 8233 | /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8234 | if (!test->ctx.a[0]) { |
| 8235 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8236 | goto out; |
| 8237 | |
| 8238 | if (ctx->vlen < expr->arg_len + 1) |
| 8239 | continue; |
| 8240 | |
| 8241 | test->ctx.a[0] = ctx->line + ctx->val; |
| 8242 | test->ctx.a[1] = test->ctx.a[0] + ctx->vlen; |
| 8243 | } |
| 8244 | |
| 8245 | test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1], |
| 8246 | expr->arg.str, expr->arg_len, multi, |
| 8247 | &temp_pattern.data.str.str, |
| 8248 | &temp_pattern.data.str.len); |
| 8249 | if (test->ctx.a[0]) { |
| 8250 | /* one value was returned into temp_pattern.data.str.{str,len} */ |
| 8251 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8252 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8253 | return 1; |
| 8254 | } |
| 8255 | } |
| 8256 | |
| 8257 | out: |
| 8258 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8259 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8260 | return 0; |
| 8261 | } |
| 8262 | |
| 8263 | static int |
| 8264 | acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8265 | struct acl_expr *expr, struct acl_test *test) |
| 8266 | { |
| 8267 | struct http_txn *txn = l7; |
| 8268 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8269 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8270 | |
| 8271 | /* The Cookie header allows multiple cookies on the same line */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8272 | return acl_fetch_any_cookie_value(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8273 | } |
| 8274 | |
| 8275 | static int |
| 8276 | acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8277 | struct acl_expr *expr, struct acl_test *test) |
| 8278 | { |
| 8279 | struct http_txn *txn = l7; |
| 8280 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8281 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8282 | |
| 8283 | /* The Set-Cookie header allows only one cookie on the same line */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8284 | return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8285 | } |
| 8286 | |
| 8287 | /* Iterate over all cookies present in a request to count how many occurrences |
| 8288 | * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then |
| 8289 | * multiple cookies may be parsed on the same line. |
| 8290 | */ |
| 8291 | static int |
| 8292 | acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8293 | const char *hdr_name, int hdr_name_len, int multi, |
| 8294 | struct acl_expr *expr, struct acl_test *test) |
| 8295 | { |
| 8296 | struct http_txn *txn = l7; |
| 8297 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8298 | struct hdr_ctx ctx; |
| 8299 | int cnt; |
| 8300 | char *val_beg, *val_end; |
| 8301 | |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8302 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8303 | ctx.idx = 0; |
| 8304 | cnt = 0; |
| 8305 | |
| 8306 | while (1) { |
| 8307 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8308 | if (!val_beg) { |
| 8309 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8310 | break; |
| 8311 | |
| 8312 | if (ctx.vlen < expr->arg_len + 1) |
| 8313 | continue; |
| 8314 | |
| 8315 | val_beg = ctx.line + ctx.val; |
| 8316 | val_end = val_beg + ctx.vlen; |
| 8317 | } |
| 8318 | |
| 8319 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
| 8320 | expr->arg.str, expr->arg_len, multi, |
| 8321 | &temp_pattern.data.str.str, |
| 8322 | &temp_pattern.data.str.len))) { |
| 8323 | cnt++; |
| 8324 | } |
| 8325 | } |
| 8326 | |
| 8327 | temp_pattern.data.integer = cnt; |
| 8328 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8329 | return 1; |
| 8330 | } |
| 8331 | |
| 8332 | static int |
| 8333 | acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8334 | struct acl_expr *expr, struct acl_test *test) |
| 8335 | { |
| 8336 | struct http_txn *txn = l7; |
| 8337 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8338 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8339 | |
| 8340 | /* The Cookie header allows multiple cookies on the same line */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8341 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8342 | } |
| 8343 | |
| 8344 | static int |
| 8345 | acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8346 | struct acl_expr *expr, struct acl_test *test) |
| 8347 | { |
| 8348 | struct http_txn *txn = l7; |
| 8349 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8350 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8351 | |
| 8352 | /* The Set-Cookie header allows only one cookie on the same line */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8353 | return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8354 | } |
| 8355 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8356 | /************************************************************************/ |
| 8357 | /* All supported keywords must be declared here. */ |
| 8358 | /************************************************************************/ |
| 8359 | |
| 8360 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8361 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8362 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8363 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8364 | { "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] | 8365 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8366 | { "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] | 8367 | { "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] | 8368 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8369 | { "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] | 8370 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8371 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8372 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8373 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8374 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8375 | { "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] | 8376 | { "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] | 8377 | { "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] | 8378 | { "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] | 8379 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8380 | /* 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] | 8381 | { "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] | 8382 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8383 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8384 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8385 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8386 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8387 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8388 | { "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] | 8389 | { "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] | 8390 | { "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] | 8391 | { "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] | 8392 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8393 | { "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] | 8394 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8395 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8396 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8397 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8398 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8399 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8400 | { "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] | 8401 | { "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] | 8402 | { "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] | 8403 | { "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] | 8404 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8405 | { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8406 | { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8407 | { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8408 | { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8409 | { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8410 | { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8411 | { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8412 | { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE }, |
| 8413 | { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8414 | |
| 8415 | { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
| 8416 | { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8417 | { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8418 | { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8419 | { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8420 | { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8421 | { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8422 | { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE }, |
| 8423 | { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8424 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8425 | { "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] | 8426 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8427 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8428 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8429 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8430 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8431 | { "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] | 8432 | { "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] | 8433 | |
Willy Tarreau | 7744e0c | 2012-04-16 15:00:51 +0200 | [diff] [blame^] | 8434 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE }, |
| 8435 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8436 | { "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] | 8437 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8438 | }}; |
| 8439 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8440 | /************************************************************************/ |
| 8441 | /* The code below is dedicated to pattern fetching and matching */ |
| 8442 | /************************************************************************/ |
| 8443 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8444 | /* Returns the last occurrence of specified header. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8445 | static int |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8446 | pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8447 | 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] | 8448 | { |
| 8449 | struct http_txn *txn = l7; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 8450 | |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8451 | return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, |
| 8452 | -1, NULL, &data->str.str, &data->str.len); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8453 | } |
| 8454 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8455 | /* |
| 8456 | * Given a path string and its length, find the position of beginning of the |
| 8457 | * query string. Returns NULL if no query string is found in the path. |
| 8458 | * |
| 8459 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8460 | * |
| 8461 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8462 | */ |
| 8463 | static inline char *find_query_string(char *path, size_t path_l) |
| 8464 | { |
| 8465 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8466 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8467 | p = memchr(path, '?', path_l); |
| 8468 | return p ? p + 1 : NULL; |
| 8469 | } |
| 8470 | |
| 8471 | static inline int is_param_delimiter(char c) |
| 8472 | { |
| 8473 | return c == '&' || c == ';'; |
| 8474 | } |
| 8475 | |
| 8476 | /* |
| 8477 | * Given a url parameter, find the starting position of the first occurence, |
| 8478 | * or NULL if the parameter is not found. |
| 8479 | * |
| 8480 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8481 | * the function will return query_string+8. |
| 8482 | */ |
| 8483 | static char* |
| 8484 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8485 | char* url_param_name, size_t url_param_name_l) |
| 8486 | { |
| 8487 | char *pos, *last; |
| 8488 | |
| 8489 | pos = query_string; |
| 8490 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8491 | |
| 8492 | while (pos <= last) { |
| 8493 | if (pos[url_param_name_l] == '=') { |
| 8494 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8495 | return pos; |
| 8496 | pos += url_param_name_l + 1; |
| 8497 | } |
| 8498 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8499 | pos++; |
| 8500 | pos++; |
| 8501 | } |
| 8502 | return NULL; |
| 8503 | } |
| 8504 | |
| 8505 | /* |
| 8506 | * Given a url parameter name, returns its value and size into *value and |
| 8507 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8508 | * zero is returned and value/value_l are not touched. |
| 8509 | */ |
| 8510 | static int |
| 8511 | find_url_param_value(char* path, size_t path_l, |
| 8512 | char* url_param_name, size_t url_param_name_l, |
| 8513 | char** value, size_t* value_l) |
| 8514 | { |
| 8515 | char *query_string, *qs_end; |
| 8516 | char *arg_start; |
| 8517 | char *value_start, *value_end; |
| 8518 | |
| 8519 | query_string = find_query_string(path, path_l); |
| 8520 | if (!query_string) |
| 8521 | return 0; |
| 8522 | |
| 8523 | qs_end = path + path_l; |
| 8524 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8525 | url_param_name, url_param_name_l); |
| 8526 | if (!arg_start) |
| 8527 | return 0; |
| 8528 | |
| 8529 | value_start = arg_start + url_param_name_l + 1; |
| 8530 | value_end = value_start; |
| 8531 | |
| 8532 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8533 | value_end++; |
| 8534 | |
| 8535 | *value = value_start; |
| 8536 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8537 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8538 | } |
| 8539 | |
| 8540 | static int |
| 8541 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8542 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8543 | { |
| 8544 | struct http_txn *txn = l7; |
| 8545 | struct http_msg *msg = &txn->req; |
| 8546 | char *url_param_value; |
| 8547 | size_t url_param_value_l; |
| 8548 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8549 | if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8550 | arg_p->data.str.str, arg_p->data.str.len, |
| 8551 | &url_param_value, &url_param_value_l)) |
| 8552 | return 0; |
| 8553 | |
| 8554 | data->str.str = url_param_value; |
| 8555 | data->str.len = url_param_value_l; |
| 8556 | return 1; |
| 8557 | } |
| 8558 | |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8559 | /* Try to find in request or response message is in <msg> and whose transaction |
| 8560 | * is in <txn> the last occurrence of a cookie name in all cookie header values |
| 8561 | * whose header name is <hdr_name> with name of length <hdr_name_len>. The |
| 8562 | * pointer and size of the last occurrence of the cookie value is returned into |
| 8563 | * <value> and <value_l>, and the function returns non-zero if the value was |
| 8564 | * found. Otherwise if the cookie was not found, zero is returned and neither |
| 8565 | * value nor value_l are touched. The input hdr string should begin at the |
| 8566 | * header's value, and its size should be in hdr_l. <list> must be non-zero if |
| 8567 | * value may represent a list of values (cookie headers). |
| 8568 | */ |
| 8569 | |
| 8570 | static int |
| 8571 | find_cookie_value(struct http_msg *msg, struct http_txn *txn, |
| 8572 | const char *hdr_name, int hdr_name_len, |
| 8573 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8574 | char **value, int *value_l) |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8575 | { |
| 8576 | struct hdr_ctx ctx; |
| 8577 | int found = 0; |
| 8578 | |
| 8579 | ctx.idx = 0; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 8580 | while (http_find_header2(hdr_name, hdr_name_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8581 | char *hdr, *end; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8582 | if (ctx.vlen < cookie_name_l + 1) |
| 8583 | continue; |
| 8584 | |
Willy Tarreau | 4573af9 | 2012-04-06 18:20:06 +0200 | [diff] [blame] | 8585 | hdr = ctx.line + ctx.val; |
| 8586 | end = hdr + ctx.vlen; |
| 8587 | while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l))) |
| 8588 | found = 1; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8589 | } |
| 8590 | return found; |
| 8591 | } |
| 8592 | |
| 8593 | static int |
| 8594 | pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8595 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8596 | { |
| 8597 | struct http_txn *txn = l7; |
| 8598 | struct http_msg *msg = &txn->req; |
| 8599 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8600 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8601 | int found = 0; |
| 8602 | |
| 8603 | found = find_cookie_value(msg, txn, "Cookie", 6, |
| 8604 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8605 | &cookie_value, &cookie_value_l); |
| 8606 | if (found) { |
| 8607 | data->str.str = cookie_value; |
| 8608 | data->str.len = cookie_value_l; |
| 8609 | } |
| 8610 | |
| 8611 | return found; |
| 8612 | } |
| 8613 | |
| 8614 | |
| 8615 | static int |
| 8616 | pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8617 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8618 | { |
| 8619 | struct http_txn *txn = l7; |
| 8620 | struct http_msg *msg = &txn->rsp; |
| 8621 | char *cookie_value; |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8622 | int cookie_value_l; |
Willy Tarreau | b3eb221 | 2011-07-01 16:16:17 +0200 | [diff] [blame] | 8623 | int found = 0; |
| 8624 | |
| 8625 | found = find_cookie_value(msg, txn, "Set-Cookie", 10, |
| 8626 | arg_p->data.str.str, arg_p->data.str.len, 1, |
| 8627 | &cookie_value, &cookie_value_l); |
| 8628 | if (found) { |
| 8629 | data->str.str = cookie_value; |
| 8630 | data->str.len = cookie_value_l; |
| 8631 | } |
| 8632 | |
| 8633 | return found; |
| 8634 | } |
| 8635 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8636 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8637 | /************************************************************************/ |
| 8638 | /* All supported keywords must be declared here. */ |
| 8639 | /************************************************************************/ |
| 8640 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8641 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Willy Tarreau | e428fb7 | 2011-12-16 21:50:30 +0100 | [diff] [blame] | 8642 | { "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] | 8643 | { "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] | 8644 | { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
| 8645 | { "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] | 8646 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8647 | }}; |
| 8648 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8649 | |
| 8650 | __attribute__((constructor)) |
| 8651 | static void __http_protocol_init(void) |
| 8652 | { |
| 8653 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8654 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8655 | } |
| 8656 | |
| 8657 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8658 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8659 | * Local variables: |
| 8660 | * c-indent-level: 8 |
| 8661 | * c-basic-offset: 8 |
| 8662 | * End: |
| 8663 | */ |