Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 49 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 60 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/task.h> |
| 62 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 63 | const char HTTP_100[] = |
| 64 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 65 | |
| 66 | const struct chunk http_100_chunk = { |
| 67 | .str = (char *)&HTTP_100, |
| 68 | .len = sizeof(HTTP_100)-1 |
| 69 | }; |
| 70 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 71 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 72 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | "HTTP/1.0 200 OK\r\n" |
| 74 | "Cache-Control: no-cache\r\n" |
| 75 | "Connection: close\r\n" |
| 76 | "Content-Type: text/html\r\n" |
| 77 | "\r\n" |
| 78 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const struct chunk http_200_chunk = { |
| 81 | .str = (char *)&HTTP_200, |
| 82 | .len = sizeof(HTTP_200)-1 |
| 83 | }; |
| 84 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 85 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 86 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 95 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 96 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 99 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 102 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 106 | const char *HTTP_401_fmt = |
| 107 | "HTTP/1.0 401 Unauthorized\r\n" |
| 108 | "Cache-Control: no-cache\r\n" |
| 109 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 110 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 112 | "\r\n" |
| 113 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 114 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 115 | const char *HTTP_407_fmt = |
| 116 | "HTTP/1.0 407 Unauthorized\r\n" |
| 117 | "Cache-Control: no-cache\r\n" |
| 118 | "Connection: close\r\n" |
| 119 | "Content-Type: text/html\r\n" |
| 120 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 121 | "\r\n" |
| 122 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 123 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | |
| 125 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 126 | [HTTP_ERR_400] = 400, |
| 127 | [HTTP_ERR_403] = 403, |
| 128 | [HTTP_ERR_408] = 408, |
| 129 | [HTTP_ERR_500] = 500, |
| 130 | [HTTP_ERR_502] = 502, |
| 131 | [HTTP_ERR_503] = 503, |
| 132 | [HTTP_ERR_504] = 504, |
| 133 | }; |
| 134 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 137 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_403] = |
| 145 | "HTTP/1.0 403 Forbidden\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_408] = |
| 153 | "HTTP/1.0 408 Request Time-out\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_500] = |
| 161 | "HTTP/1.0 500 Server Error\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_502] = |
| 169 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_503] = |
| 177 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 183 | |
| 184 | [HTTP_ERR_504] = |
| 185 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 191 | |
| 192 | }; |
| 193 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 194 | /* We must put the messages here since GCC cannot initialize consts depending |
| 195 | * on strlen(). |
| 196 | */ |
| 197 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 198 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 199 | #define FD_SETS_ARE_BITFIELDS |
| 200 | #ifdef FD_SETS_ARE_BITFIELDS |
| 201 | /* |
| 202 | * This map is used with all the FD_* macros to check whether a particular bit |
| 203 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 204 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 205 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 206 | * exclusively to the macros. |
| 207 | */ |
| 208 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 210 | |
| 211 | #else |
| 212 | #error "Check if your OS uses bitfields for fd_sets" |
| 213 | #endif |
| 214 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 215 | void init_proto_http() |
| 216 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | int i; |
| 218 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 221 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 222 | if (!http_err_msgs[msg]) { |
| 223 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 224 | abort(); |
| 225 | } |
| 226 | |
| 227 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 228 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 229 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
| 231 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 232 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 233 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 234 | * printable characters above. |
| 235 | */ |
| 236 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 237 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 238 | for (i = 0; i < 32; i++) { |
| 239 | FD_SET(i, hdr_encode_map); |
| 240 | FD_SET(i, url_encode_map); |
| 241 | } |
| 242 | for (i = 127; i < 256; i++) { |
| 243 | FD_SET(i, hdr_encode_map); |
| 244 | FD_SET(i, url_encode_map); |
| 245 | } |
| 246 | |
| 247 | tmp = "\"#{|}"; |
| 248 | while (*tmp) { |
| 249 | FD_SET(*tmp, hdr_encode_map); |
| 250 | tmp++; |
| 251 | } |
| 252 | |
| 253 | tmp = "\"#"; |
| 254 | while (*tmp) { |
| 255 | FD_SET(*tmp, url_encode_map); |
| 256 | tmp++; |
| 257 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 258 | |
| 259 | /* memory allocations */ |
| 260 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 261 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 262 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 264 | /* |
| 265 | * We have 26 list of methods (1 per first letter), each of which can have |
| 266 | * up to 3 entries (2 valid, 1 null). |
| 267 | */ |
| 268 | struct http_method_desc { |
| 269 | http_meth_t meth; |
| 270 | int len; |
| 271 | const char text[8]; |
| 272 | }; |
| 273 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 274 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | ['C' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 277 | }, |
| 278 | ['D' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 280 | }, |
| 281 | ['G' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 283 | }, |
| 284 | ['H' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 286 | }, |
| 287 | ['P' - 'A'] = { |
| 288 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 289 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 290 | }, |
| 291 | ['T' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 293 | }, |
| 294 | /* rest is empty like this : |
| 295 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 296 | */ |
| 297 | }; |
| 298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 299 | /* 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] | 300 | * 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] | 301 | * RFC2616 for those chars. |
| 302 | */ |
| 303 | |
| 304 | const char http_is_spht[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | }; |
| 307 | |
| 308 | const char http_is_crlf[256] = { |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_lws[256] = { |
| 313 | [' '] = 1, ['\t'] = 1, |
| 314 | ['\r'] = 1, ['\n'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_sep[256] = { |
| 318 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 319 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 320 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 321 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 322 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 323 | }; |
| 324 | |
| 325 | const char http_is_ctl[256] = { |
| 326 | [0 ... 31] = 1, |
| 327 | [127] = 1, |
| 328 | }; |
| 329 | |
| 330 | /* |
| 331 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 332 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 333 | * them correctly. Instead, define every non-CTL char's status. |
| 334 | */ |
| 335 | const char http_is_token[256] = { |
| 336 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 337 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 338 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 339 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 340 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 341 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 342 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 343 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 344 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 345 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 346 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 347 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 348 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 349 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 350 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 351 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 352 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 353 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 354 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 355 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 356 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 357 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 358 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 359 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 363 | /* |
| 364 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 365 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 366 | */ |
| 367 | const char http_is_ver_token[256] = { |
| 368 | ['.'] = 1, ['/'] = 1, |
| 369 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 370 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 371 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 372 | }; |
| 373 | |
| 374 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 375 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 376 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 377 | */ |
| 378 | #if defined(DEBUG_FSM) |
| 379 | static void http_silent_debug(int line, struct session *s) |
| 380 | { |
| 381 | int size = 0; |
| 382 | size += snprintf(trash + size, sizeof(trash) - size, |
| 383 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 384 | line, |
| 385 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 386 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 387 | write(-1, trash, size); |
| 388 | size = 0; |
| 389 | size += snprintf(trash + size, sizeof(trash) - size, |
| 390 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 391 | line, |
| 392 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 393 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 394 | |
| 395 | write(-1, trash, size); |
| 396 | } |
| 397 | #else |
| 398 | #define http_silent_debug(l,s) do { } while (0) |
| 399 | #endif |
| 400 | |
| 401 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 403 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 404 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 405 | * of headers is automatically adjusted. The number of bytes added is returned |
| 406 | * on success, otherwise <0 is returned indicating an error. |
| 407 | */ |
| 408 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 409 | struct hdr_idx *hdr_idx, const char *text) |
| 410 | { |
| 411 | int bytes, len; |
| 412 | |
| 413 | len = strlen(text); |
| 414 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 415 | if (!bytes) |
| 416 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 417 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 418 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 423 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 424 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 425 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 426 | * of headers is automatically adjusted. The number of bytes added is returned |
| 427 | * on success, otherwise <0 is returned indicating an error. |
| 428 | */ |
| 429 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 430 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 431 | { |
| 432 | int bytes; |
| 433 | |
| 434 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 435 | if (!bytes) |
| 436 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 437 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 438 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 439 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | |
| 441 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 442 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 443 | * If so, returns the position of the first non-space character relative to |
| 444 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 445 | * to return a pointer to the place after the first space. Returns 0 if the |
| 446 | * header name does not match. Checks are case-insensitive. |
| 447 | */ |
| 448 | int http_header_match2(const char *hdr, const char *end, |
| 449 | const char *name, int len) |
| 450 | { |
| 451 | const char *val; |
| 452 | |
| 453 | if (hdr + len >= end) |
| 454 | return 0; |
| 455 | if (hdr[len] != ':') |
| 456 | return 0; |
| 457 | if (strncasecmp(hdr, name, len) != 0) |
| 458 | return 0; |
| 459 | val = hdr + len + 1; |
| 460 | while (val < end && HTTP_IS_SPHT(*val)) |
| 461 | val++; |
| 462 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 463 | return len + 2; /* we may replace starting from second space */ |
| 464 | return val - hdr; |
| 465 | } |
| 466 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 467 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 468 | * par 2.2 for more information. Note that it requires a valid header to return |
| 469 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 470 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 471 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 472 | { |
| 473 | int quoted, qdpair; |
| 474 | |
| 475 | quoted = qdpair = 0; |
| 476 | for (; s < e; s++) { |
| 477 | if (qdpair) qdpair = 0; |
| 478 | else if (quoted && *s == '\\') qdpair = 1; |
| 479 | else if (quoted && *s == '"') quoted = 0; |
| 480 | else if (*s == '"') quoted = 1; |
| 481 | else if (*s == ',') return s; |
| 482 | } |
| 483 | return s; |
| 484 | } |
| 485 | |
| 486 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 487 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 488 | * structure holds everything necessary to use the header and find next |
| 489 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 490 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 491 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 492 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 493 | * is NULL when searching for a new values of a header, the current header is |
| 494 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 495 | */ |
| 496 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 497 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 498 | struct hdr_ctx *ctx) |
| 499 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 500 | char *eol, *sov; |
| 501 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 502 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 503 | cur_idx = ctx->idx; |
| 504 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 505 | /* We have previously returned a value, let's search |
| 506 | * another one on the same line. |
| 507 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 509 | ctx->del = ctx->val + ctx->vlen; |
| 510 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 511 | eol = sol + idx->v[cur_idx].len; |
| 512 | |
| 513 | if (sov >= eol) |
| 514 | /* no more values in this header */ |
| 515 | goto next_hdr; |
| 516 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 517 | /* values remaining for this header, skip the comma but save it |
| 518 | * for later use (eg: for header deletion). |
| 519 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 520 | sov++; |
| 521 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 522 | sov++; |
| 523 | |
| 524 | goto return_hdr; |
| 525 | } |
| 526 | |
| 527 | /* first request for this header */ |
| 528 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 529 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 531 | while (cur_idx) { |
| 532 | eol = sol + idx->v[cur_idx].len; |
| 533 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 534 | if (len == 0) { |
| 535 | /* No argument was passed, we want any header. |
| 536 | * To achieve this, we simply build a fake request. */ |
| 537 | while (sol + len < eol && sol[len] != ':') |
| 538 | len++; |
| 539 | name = sol; |
| 540 | } |
| 541 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 542 | if ((len < eol - sol) && |
| 543 | (sol[len] == ':') && |
| 544 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 545 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 546 | sov = sol + len + 1; |
| 547 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 548 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 549 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 550 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 551 | ctx->prev = old_idx; |
| 552 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 553 | ctx->idx = cur_idx; |
| 554 | ctx->val = sov - sol; |
| 555 | |
| 556 | eol = find_hdr_value_end(sov, eol); |
| 557 | ctx->vlen = eol - sov; |
| 558 | return 1; |
| 559 | } |
| 560 | next_hdr: |
| 561 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 562 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 563 | cur_idx = idx->v[cur_idx].next; |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 569 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 570 | struct hdr_ctx *ctx) |
| 571 | { |
| 572 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 573 | } |
| 574 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 575 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 576 | * the http_find_header functions. The value is removed, as well as surrounding |
| 577 | * commas if any. If the removed value was alone, the whole header is removed. |
| 578 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 579 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 580 | * no more header, so any processing may stop. The ctx is always left in a form |
| 581 | * that can be handled by http_find_header2() to find next occurrence. |
| 582 | */ |
| 583 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 584 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 585 | { |
| 586 | int cur_idx = ctx->idx; |
| 587 | char *sol = ctx->line; |
| 588 | struct hdr_idx_elem *hdr; |
| 589 | int delta, skip_comma; |
| 590 | |
| 591 | if (!cur_idx) |
| 592 | return 0; |
| 593 | |
| 594 | hdr = &idx->v[cur_idx]; |
| 595 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 596 | /* This was the only value of the header, we must now remove it entirely. */ |
| 597 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 598 | http_msg_move_end(msg, delta); |
| 599 | idx->used--; |
| 600 | hdr->len = 0; /* unused entry */ |
| 601 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| 602 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 603 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 604 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 605 | ctx->vlen = 0; |
| 606 | return ctx->idx; |
| 607 | } |
| 608 | |
| 609 | /* This was not the only value of this header. We have to remove between |
| 610 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 611 | * of the list, we remove the last separator. |
| 612 | */ |
| 613 | |
| 614 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 615 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 616 | sol + ctx->val + ctx->vlen + skip_comma, |
| 617 | NULL, 0); |
| 618 | hdr->len += delta; |
| 619 | http_msg_move_end(msg, delta); |
| 620 | ctx->val = ctx->del; |
| 621 | ctx->vlen = 0; |
| 622 | return ctx->idx; |
| 623 | } |
| 624 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 625 | /* This function handles a server error at the stream interface level. The |
| 626 | * stream interface is assumed to be already in a closed state. An optional |
| 627 | * message is copied into the input buffer, and an HTTP status code stored. |
| 628 | * 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] | 629 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 630 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 631 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 632 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 633 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 634 | buffer_auto_read(si->ob); |
| 635 | buffer_abort(si->ob); |
| 636 | buffer_auto_close(si->ob); |
| 637 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 638 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 639 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 640 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 641 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 642 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 643 | } |
| 644 | if (!(t->flags & SN_ERR_MASK)) |
| 645 | t->flags |= err; |
| 646 | if (!(t->flags & SN_FINST_MASK)) |
| 647 | t->flags |= finst; |
| 648 | } |
| 649 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 650 | /* This function returns the appropriate error location for the given session |
| 651 | * and message. |
| 652 | */ |
| 653 | |
| 654 | struct chunk *error_message(struct session *s, int msgnum) |
| 655 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 656 | if (s->be->errmsg[msgnum].str) |
| 657 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 658 | else if (s->fe->errmsg[msgnum].str) |
| 659 | return &s->fe->errmsg[msgnum]; |
| 660 | else |
| 661 | return &http_err_chunks[msgnum]; |
| 662 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 663 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 664 | /* |
| 665 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 666 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 667 | */ |
| 668 | static http_meth_t find_http_meth(const char *str, const int len) |
| 669 | { |
| 670 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 671 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 672 | |
| 673 | m = ((unsigned)*str - 'A'); |
| 674 | |
| 675 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 676 | for (h = http_methods[m]; h->len > 0; h++) { |
| 677 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 678 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 679 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 681 | }; |
| 682 | return HTTP_METH_OTHER; |
| 683 | } |
| 684 | return HTTP_METH_NONE; |
| 685 | |
| 686 | } |
| 687 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 688 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 689 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 690 | * It is returned otherwise. |
| 691 | */ |
| 692 | static char * |
| 693 | http_get_path(struct http_txn *txn) |
| 694 | { |
| 695 | char *ptr, *end; |
| 696 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 697 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 698 | end = ptr + txn->req.sl.rq.u_l; |
| 699 | |
| 700 | if (ptr >= end) |
| 701 | return NULL; |
| 702 | |
| 703 | /* RFC2616, par. 5.1.2 : |
| 704 | * Request-URI = "*" | absuri | abspath | authority |
| 705 | */ |
| 706 | |
| 707 | if (*ptr == '*') |
| 708 | return NULL; |
| 709 | |
| 710 | if (isalpha((unsigned char)*ptr)) { |
| 711 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 712 | ptr++; |
| 713 | while (ptr < end && |
| 714 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 715 | ptr++; |
| 716 | /* skip '://' */ |
| 717 | if (ptr == end || *ptr++ != ':') |
| 718 | return NULL; |
| 719 | if (ptr == end || *ptr++ != '/') |
| 720 | return NULL; |
| 721 | if (ptr == end || *ptr++ != '/') |
| 722 | return NULL; |
| 723 | } |
| 724 | /* skip [user[:passwd]@]host[:[port]] */ |
| 725 | |
| 726 | while (ptr < end && *ptr != '/') |
| 727 | ptr++; |
| 728 | |
| 729 | if (ptr == end) |
| 730 | return NULL; |
| 731 | |
| 732 | /* OK, we got the '/' ! */ |
| 733 | return ptr; |
| 734 | } |
| 735 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 736 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 737 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 738 | * left unchanged and will follow normal proxy processing. |
| 739 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 740 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 741 | { |
| 742 | struct http_txn *txn; |
| 743 | struct chunk rdr; |
| 744 | char *path; |
| 745 | int len; |
| 746 | |
| 747 | /* 1: create the response header */ |
| 748 | rdr.len = strlen(HTTP_302); |
| 749 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 750 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 751 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 752 | |
| 753 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 754 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 755 | return; |
| 756 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 757 | /* special prefix "/" means don't change URL */ |
| 758 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 759 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 760 | rdr.len += s->srv->rdr_len; |
| 761 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | |
| 763 | /* 3: add the request URI */ |
| 764 | txn = &s->txn; |
| 765 | path = http_get_path(txn); |
| 766 | if (!path) |
| 767 | return; |
| 768 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 769 | len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 770 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 771 | return; |
| 772 | |
| 773 | memcpy(rdr.str + rdr.len, path, len); |
| 774 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 775 | |
| 776 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 777 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 778 | rdr.len += 29; |
| 779 | } else { |
| 780 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 781 | rdr.len += 23; |
| 782 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 783 | |
| 784 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 785 | si->shutr(si); |
| 786 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 787 | si->err_type = SI_ET_NONE; |
| 788 | si->err_loc = NULL; |
| 789 | si->state = SI_ST_CLO; |
| 790 | |
| 791 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 792 | 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] | 793 | |
| 794 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 795 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 796 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 799 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 800 | * that the server side is closed. Note that err_type is actually a |
| 801 | * bitmask, where almost only aborts may be cumulated with other |
| 802 | * values. We consider that aborted operations are more important |
| 803 | * than timeouts or errors due to the fact that nobody else in the |
| 804 | * logs might explain incomplete retries. All others should avoid |
| 805 | * being cumulated. It should normally not be possible to have multiple |
| 806 | * aborts at once, but just in case, the first one in sequence is reported. |
| 807 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 808 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 809 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 810 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 811 | |
| 812 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 813 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 814 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 815 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 816 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 817 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 819 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 820 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 821 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 822 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 823 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 824 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 825 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 826 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 827 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 828 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 829 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 830 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 831 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 832 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 833 | } |
| 834 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 835 | extern const char sess_term_cond[8]; |
| 836 | extern const char sess_fin_state[8]; |
| 837 | extern const char *monthname[12]; |
| 838 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 839 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 840 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 841 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 842 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 843 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 844 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 845 | void http_sess_clflog(struct session *s) |
| 846 | { |
| 847 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 848 | struct proxy *fe = s->fe; |
| 849 | struct proxy *be = s->be; |
| 850 | struct proxy *prx_log; |
| 851 | struct http_txn *txn = &s->txn; |
| 852 | int tolog, level, err; |
| 853 | char *uri, *h; |
| 854 | char *svid; |
| 855 | struct tm tm; |
| 856 | static char tmpline[MAX_SYSLOG_LEN]; |
| 857 | int hdr; |
| 858 | size_t w; |
| 859 | int t_request; |
| 860 | |
| 861 | prx_log = fe; |
| 862 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 863 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 864 | txn->status >= 500; |
| 865 | |
| 866 | if (s->cli_addr.ss_family == AF_INET) |
| 867 | inet_ntop(AF_INET, |
| 868 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 869 | pn, sizeof(pn)); |
| 870 | else |
| 871 | inet_ntop(AF_INET6, |
| 872 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 873 | pn, sizeof(pn)); |
| 874 | |
| 875 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 876 | |
| 877 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 878 | tolog = fe->to_log; |
| 879 | |
| 880 | h = tmpline; |
| 881 | |
| 882 | w = snprintf(h, sizeof(tmpline), |
| 883 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 884 | pn, |
| 885 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 886 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 887 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 888 | goto trunc; |
| 889 | h += w; |
| 890 | |
| 891 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 892 | goto trunc; |
| 893 | |
| 894 | *(h++) = ' '; |
| 895 | *(h++) = '\"'; |
| 896 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 897 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 898 | '#', url_encode_map, uri); |
| 899 | *(h++) = '\"'; |
| 900 | |
| 901 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 902 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 903 | goto trunc; |
| 904 | h += w; |
| 905 | |
| 906 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 907 | goto trunc; |
| 908 | memcpy(h, " \"-\" \"-\"", 8); |
| 909 | h += 8; |
| 910 | |
| 911 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 912 | " %d %03d", |
| 913 | (s->cli_addr.ss_family == AF_INET) ? |
| 914 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 915 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 916 | (int)s->logs.accept_date.tv_usec/1000); |
| 917 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 918 | goto trunc; |
| 919 | h += w; |
| 920 | |
| 921 | w = strlen(fe->id); |
| 922 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 923 | goto trunc; |
| 924 | *(h++) = ' '; |
| 925 | *(h++) = '\"'; |
| 926 | memcpy(h, fe->id, w); |
| 927 | h += w; |
| 928 | *(h++) = '\"'; |
| 929 | |
| 930 | w = strlen(be->id); |
| 931 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 932 | goto trunc; |
| 933 | *(h++) = ' '; |
| 934 | *(h++) = '\"'; |
| 935 | memcpy(h, be->id, w); |
| 936 | h += w; |
| 937 | *(h++) = '\"'; |
| 938 | |
| 939 | svid = (tolog & LW_SVID) ? |
| 940 | (s->data_source != DATA_SRC_STATS) ? |
| 941 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 942 | |
| 943 | w = strlen(svid); |
| 944 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 945 | goto trunc; |
| 946 | *(h++) = ' '; |
| 947 | *(h++) = '\"'; |
| 948 | memcpy(h, svid, w); |
| 949 | h += w; |
| 950 | *(h++) = '\"'; |
| 951 | |
| 952 | t_request = -1; |
| 953 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 954 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 955 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 956 | " %d %ld %ld %ld %ld", |
| 957 | t_request, |
| 958 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 959 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 960 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 961 | s->logs.t_close); |
| 962 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 963 | goto trunc; |
| 964 | h += w; |
| 965 | |
| 966 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 967 | goto trunc; |
| 968 | *(h++) = ' '; |
| 969 | *(h++) = '\"'; |
| 970 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 971 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 972 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 973 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 974 | *(h++) = '\"'; |
| 975 | |
| 976 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 977 | " %d %d %d %d %d %ld %ld", |
| 978 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 979 | (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 980 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 981 | |
| 982 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 983 | goto trunc; |
| 984 | h += w; |
| 985 | |
| 986 | if (txn->cli_cookie) { |
| 987 | w = strlen(txn->cli_cookie); |
| 988 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 989 | goto trunc; |
| 990 | *(h++) = ' '; |
| 991 | *(h++) = '\"'; |
| 992 | memcpy(h, txn->cli_cookie, w); |
| 993 | h += w; |
| 994 | *(h++) = '\"'; |
| 995 | } else { |
| 996 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 997 | goto trunc; |
| 998 | memcpy(h, " \"-\"", 4); |
| 999 | h += 4; |
| 1000 | } |
| 1001 | |
| 1002 | if (txn->srv_cookie) { |
| 1003 | w = strlen(txn->srv_cookie); |
| 1004 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1005 | goto trunc; |
| 1006 | *(h++) = ' '; |
| 1007 | *(h++) = '\"'; |
| 1008 | memcpy(h, txn->srv_cookie, w); |
| 1009 | h += w; |
| 1010 | *(h++) = '\"'; |
| 1011 | } else { |
| 1012 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1013 | goto trunc; |
| 1014 | memcpy(h, " \"-\"", 4); |
| 1015 | h += 4; |
| 1016 | } |
| 1017 | |
| 1018 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1019 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1020 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1021 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1022 | if (txn->req.cap[hdr] != NULL) { |
| 1023 | *(h++) = ' '; |
| 1024 | *(h++) = '\"'; |
| 1025 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1026 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1027 | *(h++) = '\"'; |
| 1028 | } else { |
| 1029 | memcpy(h, " \"-\"", 4); |
| 1030 | h += 4; |
| 1031 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1036 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1037 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1038 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1039 | if (txn->rsp.cap[hdr] != NULL) { |
| 1040 | *(h++) = ' '; |
| 1041 | *(h++) = '\"'; |
| 1042 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1043 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1044 | *(h++) = '\"'; |
| 1045 | } else { |
| 1046 | memcpy(h, " \"-\"", 4); |
| 1047 | h += 4; |
| 1048 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | trunc: |
| 1053 | *h = '\0'; |
| 1054 | |
| 1055 | level = LOG_INFO; |
| 1056 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1057 | level = LOG_ERR; |
| 1058 | |
| 1059 | send_log(prx_log, level, "%s\n", tmpline); |
| 1060 | |
| 1061 | s->logs.logwait = 0; |
| 1062 | } |
| 1063 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1064 | /* |
| 1065 | * send a log for the session when we have enough info about it. |
| 1066 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1067 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1068 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1069 | { |
| 1070 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1071 | struct proxy *fe = s->fe; |
| 1072 | struct proxy *be = s->be; |
| 1073 | struct proxy *prx_log; |
| 1074 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1075 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1076 | char *uri, *h; |
| 1077 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1078 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1079 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1080 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | int hdr; |
| 1082 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1083 | /* if we don't want to log normal traffic, return now */ |
| 1084 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1085 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1086 | txn->status >= 500; |
| 1087 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1088 | return; |
| 1089 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1090 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1091 | return; |
| 1092 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1093 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1094 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1095 | return http_sess_clflog(s); |
| 1096 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1097 | if (s->cli_addr.ss_family == AF_INET) |
| 1098 | inet_ntop(AF_INET, |
| 1099 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1100 | pn, sizeof(pn)); |
| 1101 | else |
| 1102 | inet_ntop(AF_INET6, |
| 1103 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1104 | pn, sizeof(pn)); |
| 1105 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1106 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1107 | |
| 1108 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1109 | tolog = fe->to_log; |
| 1110 | |
| 1111 | h = tmpline; |
| 1112 | if (fe->to_log & LW_REQHDR && |
| 1113 | txn->req.cap && |
| 1114 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1115 | *(h++) = ' '; |
| 1116 | *(h++) = '{'; |
| 1117 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1118 | if (hdr) |
| 1119 | *(h++) = '|'; |
| 1120 | if (txn->req.cap[hdr] != NULL) |
| 1121 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1122 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1123 | } |
| 1124 | *(h++) = '}'; |
| 1125 | } |
| 1126 | |
| 1127 | if (fe->to_log & LW_RSPHDR && |
| 1128 | txn->rsp.cap && |
| 1129 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1130 | *(h++) = ' '; |
| 1131 | *(h++) = '{'; |
| 1132 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1133 | if (hdr) |
| 1134 | *(h++) = '|'; |
| 1135 | if (txn->rsp.cap[hdr] != NULL) |
| 1136 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1137 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1138 | } |
| 1139 | *(h++) = '}'; |
| 1140 | } |
| 1141 | |
| 1142 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1143 | *(h++) = ' '; |
| 1144 | *(h++) = '"'; |
| 1145 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1146 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1147 | '#', url_encode_map, uri); |
| 1148 | *(h++) = '"'; |
| 1149 | } |
| 1150 | *h = '\0'; |
| 1151 | |
| 1152 | svid = (tolog & LW_SVID) ? |
| 1153 | (s->data_source != DATA_SRC_STATS) ? |
| 1154 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1155 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1156 | t_request = -1; |
| 1157 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1158 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1159 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1160 | level = LOG_INFO; |
| 1161 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1162 | level = LOG_ERR; |
| 1163 | |
| 1164 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1165 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1166 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1167 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1168 | pn, |
| 1169 | (s->cli_addr.ss_family == AF_INET) ? |
| 1170 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1171 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1172 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1173 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1174 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1175 | t_request, |
| 1176 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1177 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1178 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1179 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1180 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1181 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1182 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1183 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1184 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1185 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1186 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1187 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1188 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1189 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1190 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1191 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1192 | |
| 1193 | s->logs.logwait = 0; |
| 1194 | } |
| 1195 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1196 | |
| 1197 | /* |
| 1198 | * Capture headers from message starting at <som> according to header list |
| 1199 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1200 | */ |
| 1201 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1202 | char **cap, struct cap_hdr *cap_hdr) |
| 1203 | { |
| 1204 | char *eol, *sol, *col, *sov; |
| 1205 | int cur_idx; |
| 1206 | struct cap_hdr *h; |
| 1207 | int len; |
| 1208 | |
| 1209 | sol = som + hdr_idx_first_pos(idx); |
| 1210 | cur_idx = hdr_idx_first_idx(idx); |
| 1211 | |
| 1212 | while (cur_idx) { |
| 1213 | eol = sol + idx->v[cur_idx].len; |
| 1214 | |
| 1215 | col = sol; |
| 1216 | while (col < eol && *col != ':') |
| 1217 | col++; |
| 1218 | |
| 1219 | sov = col + 1; |
| 1220 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1221 | sov++; |
| 1222 | |
| 1223 | for (h = cap_hdr; h; h = h->next) { |
| 1224 | if ((h->namelen == col - sol) && |
| 1225 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1226 | if (cap[h->index] == NULL) |
| 1227 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1228 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (cap[h->index] == NULL) { |
| 1231 | Alert("HTTP capture : out of memory.\n"); |
| 1232 | continue; |
| 1233 | } |
| 1234 | |
| 1235 | len = eol - sov; |
| 1236 | if (len > h->len) |
| 1237 | len = h->len; |
| 1238 | |
| 1239 | memcpy(cap[h->index], sov, len); |
| 1240 | cap[h->index][len]=0; |
| 1241 | } |
| 1242 | } |
| 1243 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1244 | cur_idx = idx->v[cur_idx].next; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1249 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1250 | */ |
| 1251 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1252 | |
| 1253 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1254 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1255 | */ |
| 1256 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1257 | ptr++; \ |
| 1258 | if (likely(ptr < end)) \ |
| 1259 | goto good; \ |
| 1260 | else { \ |
| 1261 | state = (st); \ |
| 1262 | goto http_msg_ood; \ |
| 1263 | } \ |
| 1264 | } while (0) |
| 1265 | |
| 1266 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1267 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1268 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1269 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1270 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1271 | * will give undefined results. |
| 1272 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1273 | * and that msg->sol points to the beginning of the response. |
| 1274 | * If a complete line is found (which implies that at least one CR or LF is |
| 1275 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1276 | * returned indicating an incomplete line (which does not mean that parts have |
| 1277 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1278 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1279 | * upon next call. |
| 1280 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1281 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1282 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1283 | * 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] | 1284 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1286 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1287 | unsigned int state, const char *ptr, const char *end, |
| 1288 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1289 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1290 | switch (state) { |
| 1291 | http_msg_rpver: |
| 1292 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1293 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1294 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1295 | |
| 1296 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1297 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1298 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1299 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1300 | state = HTTP_MSG_ERROR; |
| 1301 | break; |
| 1302 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | http_msg_rpver_sp: |
| 1304 | case HTTP_MSG_RPVER_SP: |
| 1305 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1306 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1307 | goto http_msg_rpcode; |
| 1308 | } |
| 1309 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1310 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1311 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1312 | state = HTTP_MSG_ERROR; |
| 1313 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1314 | |
| 1315 | http_msg_rpcode: |
| 1316 | case HTTP_MSG_RPCODE: |
| 1317 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1318 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1319 | |
| 1320 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1321 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1323 | } |
| 1324 | |
| 1325 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1326 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1327 | http_msg_rsp_reason: |
| 1328 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1329 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1330 | msg->sl.st.r_l = 0; |
| 1331 | goto http_msg_rpline_eol; |
| 1332 | |
| 1333 | http_msg_rpcode_sp: |
| 1334 | case HTTP_MSG_RPCODE_SP: |
| 1335 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1336 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | goto http_msg_rpreason; |
| 1338 | } |
| 1339 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1340 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1341 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1342 | goto http_msg_rsp_reason; |
| 1343 | |
| 1344 | http_msg_rpreason: |
| 1345 | case HTTP_MSG_RPREASON: |
| 1346 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1347 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1348 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | http_msg_rpline_eol: |
| 1350 | /* We have seen the end of line. Note that we do not |
| 1351 | * necessarily have the \n yet, but at least we know that we |
| 1352 | * have EITHER \r OR \n, otherwise the response would not be |
| 1353 | * complete. We can then record the response length and return |
| 1354 | * to the caller which will be able to register it. |
| 1355 | */ |
| 1356 | msg->sl.st.l = ptr - msg->sol; |
| 1357 | return ptr; |
| 1358 | |
| 1359 | #ifdef DEBUG_FULL |
| 1360 | default: |
| 1361 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1362 | exit(1); |
| 1363 | #endif |
| 1364 | } |
| 1365 | |
| 1366 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1367 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1368 | if (ret_state) |
| 1369 | *ret_state = state; |
| 1370 | if (ret_ptr) |
| 1371 | *ret_ptr = (char *)ptr; |
| 1372 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1373 | } |
| 1374 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1376 | * This function parses a request line between <ptr> and <end>, starting with |
| 1377 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1378 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1379 | * will give undefined results. |
| 1380 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1381 | * and that msg->sol points to the beginning of the request. |
| 1382 | * If a complete line is found (which implies that at least one CR or LF is |
| 1383 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1384 | * returned indicating an incomplete line (which does not mean that parts have |
| 1385 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1386 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1387 | * upon next call. |
| 1388 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1389 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1391 | * 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] | 1392 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1393 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1394 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1395 | unsigned int state, const char *ptr, const char *end, |
| 1396 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1397 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | switch (state) { |
| 1399 | http_msg_rqmeth: |
| 1400 | case HTTP_MSG_RQMETH: |
| 1401 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1402 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1405 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1407 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1408 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1410 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1411 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1413 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1415 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1417 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | msg->sl.rq.v_l = 0; |
| 1419 | goto http_msg_rqline_eol; |
| 1420 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1421 | state = HTTP_MSG_ERROR; |
| 1422 | break; |
| 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | http_msg_rqmeth_sp: |
| 1425 | case HTTP_MSG_RQMETH_SP: |
| 1426 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1427 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | goto http_msg_rquri; |
| 1429 | } |
| 1430 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1431 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1432 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1433 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | http_msg_rquri: |
| 1436 | case HTTP_MSG_RQURI: |
| 1437 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1438 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1441 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1443 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1444 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1446 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | http_msg_rquri_sp: |
| 1449 | case HTTP_MSG_RQURI_SP: |
| 1450 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1451 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1452 | goto http_msg_rqver; |
| 1453 | } |
| 1454 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1455 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1456 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1457 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1458 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | http_msg_rqver: |
| 1460 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1461 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1463 | |
| 1464 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1465 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1466 | http_msg_rqline_eol: |
| 1467 | /* We have seen the end of line. Note that we do not |
| 1468 | * necessarily have the \n yet, but at least we know that we |
| 1469 | * have EITHER \r OR \n, otherwise the request would not be |
| 1470 | * complete. We can then record the request length and return |
| 1471 | * to the caller which will be able to register it. |
| 1472 | */ |
| 1473 | msg->sl.rq.l = ptr - msg->sol; |
| 1474 | return ptr; |
| 1475 | } |
| 1476 | |
| 1477 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1478 | state = HTTP_MSG_ERROR; |
| 1479 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1480 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | #ifdef DEBUG_FULL |
| 1482 | default: |
| 1483 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1484 | exit(1); |
| 1485 | #endif |
| 1486 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1487 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1489 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | if (ret_state) |
| 1491 | *ret_state = state; |
| 1492 | if (ret_ptr) |
| 1493 | *ret_ptr = (char *)ptr; |
| 1494 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1496 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1497 | /* |
| 1498 | * Returns the data from Authorization header. Function may be called more |
| 1499 | * than once so data is stored in txn->auth_data. When no header is found |
| 1500 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1501 | * searching again for something we are unable to find anyway. |
| 1502 | */ |
| 1503 | |
| 1504 | char get_http_auth_buff[BUFSIZE]; |
| 1505 | |
| 1506 | int |
| 1507 | get_http_auth(struct session *s) |
| 1508 | { |
| 1509 | |
| 1510 | struct http_txn *txn = &s->txn; |
| 1511 | struct chunk auth_method; |
| 1512 | struct hdr_ctx ctx; |
| 1513 | char *h, *p; |
| 1514 | int len; |
| 1515 | |
| 1516 | #ifdef DEBUG_AUTH |
| 1517 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1518 | #endif |
| 1519 | |
| 1520 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1521 | return 0; |
| 1522 | |
| 1523 | if (txn->auth.method) |
| 1524 | return 1; |
| 1525 | |
| 1526 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1527 | |
| 1528 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1529 | |
| 1530 | if (txn->flags & TX_USE_PX_CONN) { |
| 1531 | h = "Proxy-Authorization"; |
| 1532 | len = strlen(h); |
| 1533 | } else { |
| 1534 | h = "Authorization"; |
| 1535 | len = strlen(h); |
| 1536 | } |
| 1537 | |
| 1538 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1539 | return 0; |
| 1540 | |
| 1541 | h = ctx.line + ctx.val; |
| 1542 | |
| 1543 | p = memchr(h, ' ', ctx.vlen); |
| 1544 | if (!p || p == h) |
| 1545 | return 0; |
| 1546 | |
| 1547 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1548 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1549 | |
| 1550 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1551 | |
| 1552 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1553 | get_http_auth_buff, BUFSIZE - 1); |
| 1554 | |
| 1555 | if (len < 0) |
| 1556 | return 0; |
| 1557 | |
| 1558 | |
| 1559 | get_http_auth_buff[len] = '\0'; |
| 1560 | |
| 1561 | p = strchr(get_http_auth_buff, ':'); |
| 1562 | |
| 1563 | if (!p) |
| 1564 | return 0; |
| 1565 | |
| 1566 | txn->auth.user = get_http_auth_buff; |
| 1567 | *p = '\0'; |
| 1568 | txn->auth.pass = p+1; |
| 1569 | |
| 1570 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1571 | return 1; |
| 1572 | } |
| 1573 | |
| 1574 | return 0; |
| 1575 | } |
| 1576 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1577 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1580 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1581 | * when data are missing and recalled at the exact same location with no |
| 1582 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1583 | * 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] | 1584 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1585 | * the first state, so that none of the msg pointers has to be initialized |
| 1586 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1587 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1589 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1590 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1591 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1593 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | ptr = buf->lr; |
| 1595 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | if (unlikely(ptr >= end)) |
| 1598 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1601 | /* |
| 1602 | * First, states that are specific to the response only. |
| 1603 | * We check them first so that request and headers are |
| 1604 | * closer to each other (accessed more often). |
| 1605 | */ |
| 1606 | http_msg_rpbefore: |
| 1607 | case HTTP_MSG_RPBEFORE: |
| 1608 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1609 | /* we have a start of message, but we have to check |
| 1610 | * first if we need to remove some CRLF. We can only |
| 1611 | * do this when send_max=0. |
| 1612 | */ |
| 1613 | char *beg = buf->w + buf->send_max; |
| 1614 | if (beg >= buf->data + buf->size) |
| 1615 | beg -= buf->size; |
| 1616 | if (unlikely(ptr != beg)) { |
| 1617 | if (buf->send_max) |
| 1618 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1619 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1620 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1621 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1622 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1623 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1624 | hdr_idx_init(idx); |
| 1625 | state = HTTP_MSG_RPVER; |
| 1626 | goto http_msg_rpver; |
| 1627 | } |
| 1628 | |
| 1629 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1630 | goto http_msg_invalid; |
| 1631 | |
| 1632 | if (unlikely(*ptr == '\n')) |
| 1633 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1634 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1635 | /* stop here */ |
| 1636 | |
| 1637 | http_msg_rpbefore_cr: |
| 1638 | case HTTP_MSG_RPBEFORE_CR: |
| 1639 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1640 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1641 | /* stop here */ |
| 1642 | |
| 1643 | http_msg_rpver: |
| 1644 | case HTTP_MSG_RPVER: |
| 1645 | case HTTP_MSG_RPVER_SP: |
| 1646 | case HTTP_MSG_RPCODE: |
| 1647 | case HTTP_MSG_RPCODE_SP: |
| 1648 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1649 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1650 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1651 | if (unlikely(!ptr)) |
| 1652 | return; |
| 1653 | |
| 1654 | /* we have a full response and we know that we have either a CR |
| 1655 | * or an LF at <ptr>. |
| 1656 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1657 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1658 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1659 | |
| 1660 | msg->sol = ptr; |
| 1661 | if (likely(*ptr == '\r')) |
| 1662 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1663 | goto http_msg_rpline_end; |
| 1664 | |
| 1665 | http_msg_rpline_end: |
| 1666 | case HTTP_MSG_RPLINE_END: |
| 1667 | /* msg->sol must point to the first of CR or LF. */ |
| 1668 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1669 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1670 | /* stop here */ |
| 1671 | |
| 1672 | /* |
| 1673 | * Second, states that are specific to the request only |
| 1674 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | http_msg_rqbefore: |
| 1676 | case HTTP_MSG_RQBEFORE: |
| 1677 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1678 | /* we have a start of message, but we have to check |
| 1679 | * first if we need to remove some CRLF. We can only |
| 1680 | * do this when send_max=0. |
| 1681 | */ |
| 1682 | char *beg = buf->w + buf->send_max; |
| 1683 | if (beg >= buf->data + buf->size) |
| 1684 | beg -= buf->size; |
| 1685 | if (likely(ptr != beg)) { |
| 1686 | if (buf->send_max) |
| 1687 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1688 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1689 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1691 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1692 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1693 | /* we will need this when keep-alive will be supported |
| 1694 | hdr_idx_init(idx); |
| 1695 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1696 | state = HTTP_MSG_RQMETH; |
| 1697 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1700 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1701 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | if (unlikely(*ptr == '\n')) |
| 1704 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1705 | 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] | 1706 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1707 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | http_msg_rqbefore_cr: |
| 1709 | case HTTP_MSG_RQBEFORE_CR: |
| 1710 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1711 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1712 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | http_msg_rqmeth: |
| 1715 | case HTTP_MSG_RQMETH: |
| 1716 | case HTTP_MSG_RQMETH_SP: |
| 1717 | case HTTP_MSG_RQURI: |
| 1718 | case HTTP_MSG_RQURI_SP: |
| 1719 | case HTTP_MSG_RQVER: |
| 1720 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1721 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | if (unlikely(!ptr)) |
| 1723 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1724 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | /* we have a full request and we know that we have either a CR |
| 1726 | * or an LF at <ptr>. |
| 1727 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1728 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1730 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | msg->sol = ptr; |
| 1732 | if (likely(*ptr == '\r')) |
| 1733 | 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] | 1734 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1735 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | http_msg_rqline_end: |
| 1737 | case HTTP_MSG_RQLINE_END: |
| 1738 | /* check for HTTP/0.9 request : no version information available. |
| 1739 | * msg->sol must point to the first of CR or LF. |
| 1740 | */ |
| 1741 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1742 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1743 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1744 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1745 | 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] | 1746 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1747 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1748 | /* |
| 1749 | * Common states below |
| 1750 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | http_msg_hdr_first: |
| 1752 | case HTTP_MSG_HDR_FIRST: |
| 1753 | msg->sol = ptr; |
| 1754 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1755 | goto http_msg_hdr_name; |
| 1756 | } |
| 1757 | |
| 1758 | if (likely(*ptr == '\r')) |
| 1759 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1760 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | http_msg_hdr_name: |
| 1763 | case HTTP_MSG_HDR_NAME: |
| 1764 | /* assumes msg->sol points to the first char */ |
| 1765 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1766 | 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] | 1767 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | if (likely(*ptr == ':')) { |
| 1769 | msg->col = ptr - buf->data; |
| 1770 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1771 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1772 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1773 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1774 | goto http_msg_invalid; |
| 1775 | |
| 1776 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1777 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1778 | |
| 1779 | /* and we still accept this non-token character */ |
| 1780 | 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] | 1781 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | http_msg_hdr_l1_sp: |
| 1783 | case HTTP_MSG_HDR_L1_SP: |
| 1784 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1785 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1786 | 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] | 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | /* header value can be basically anything except CR/LF */ |
| 1789 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1790 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1792 | goto http_msg_hdr_val; |
| 1793 | } |
| 1794 | |
| 1795 | if (likely(*ptr == '\r')) |
| 1796 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1797 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1798 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | http_msg_hdr_l1_lf: |
| 1800 | case HTTP_MSG_HDR_L1_LF: |
| 1801 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1802 | 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] | 1803 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1804 | http_msg_hdr_l1_lws: |
| 1805 | case HTTP_MSG_HDR_L1_LWS: |
| 1806 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1807 | /* replace HT,CR,LF with spaces */ |
| 1808 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1809 | buf->data[msg->sov] = ' '; |
| 1810 | goto http_msg_hdr_l1_sp; |
| 1811 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1812 | /* we had a header consisting only in spaces ! */ |
| 1813 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | goto http_msg_complete_header; |
| 1815 | |
| 1816 | http_msg_hdr_val: |
| 1817 | case HTTP_MSG_HDR_VAL: |
| 1818 | /* assumes msg->sol points to the first char, msg->col to the |
| 1819 | * colon, and msg->sov points to the first character of the |
| 1820 | * value. |
| 1821 | */ |
| 1822 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1823 | 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] | 1824 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | msg->eol = ptr; |
| 1826 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1827 | * real header end in case it ends with lots of LWS, but is this |
| 1828 | * really needed ? |
| 1829 | */ |
| 1830 | if (likely(*ptr == '\r')) |
| 1831 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1832 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1833 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1834 | http_msg_hdr_l2_lf: |
| 1835 | case HTTP_MSG_HDR_L2_LF: |
| 1836 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1837 | 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] | 1838 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1839 | http_msg_hdr_l2_lws: |
| 1840 | case HTTP_MSG_HDR_L2_LWS: |
| 1841 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1842 | /* LWS: replace HT,CR,LF with spaces */ |
| 1843 | for (; msg->eol < ptr; msg->eol++) |
| 1844 | *msg->eol = ' '; |
| 1845 | goto http_msg_hdr_val; |
| 1846 | } |
| 1847 | http_msg_complete_header: |
| 1848 | /* |
| 1849 | * It was a new header, so the last one is finished. |
| 1850 | * Assumes msg->sol points to the first char, msg->col to the |
| 1851 | * colon, msg->sov points to the first character of the value |
| 1852 | * and msg->eol to the first CR or LF so we know how the line |
| 1853 | * ends. We insert last header into the index. |
| 1854 | */ |
| 1855 | /* |
| 1856 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1857 | write(2, msg->sol, msg->eol-msg->sol); |
| 1858 | fprintf(stderr,"\n"); |
| 1859 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1860 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1861 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1862 | idx, idx->tail) < 0)) |
| 1863 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1864 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | msg->sol = ptr; |
| 1866 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1867 | goto http_msg_hdr_name; |
| 1868 | } |
| 1869 | |
| 1870 | if (likely(*ptr == '\r')) |
| 1871 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1872 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1873 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1874 | http_msg_last_lf: |
| 1875 | case HTTP_MSG_LAST_LF: |
| 1876 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1877 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1878 | ptr++; |
| 1879 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1880 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1881 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1882 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1883 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1884 | return; |
| 1885 | #ifdef DEBUG_FULL |
| 1886 | default: |
| 1887 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1888 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1889 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | } |
| 1891 | http_msg_ood: |
| 1892 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1893 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1894 | buf->lr = ptr; |
| 1895 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1896 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1897 | http_msg_invalid: |
| 1898 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1899 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1900 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1901 | return; |
| 1902 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1903 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1904 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1905 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1906 | * nothing is done and 1 is returned. |
| 1907 | */ |
| 1908 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1909 | { |
| 1910 | int delta; |
| 1911 | char *cur_end; |
| 1912 | |
| 1913 | if (msg->sl.rq.v_l != 0) |
| 1914 | return 1; |
| 1915 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1916 | cur_end = msg->sol + msg->sl.rq.l; |
| 1917 | delta = 0; |
| 1918 | |
| 1919 | if (msg->sl.rq.u_l == 0) { |
| 1920 | /* if no URI was set, add "/" */ |
| 1921 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1922 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1923 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1924 | } |
| 1925 | /* add HTTP version */ |
| 1926 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1927 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1928 | cur_end += delta; |
| 1929 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1930 | HTTP_MSG_RQMETH, |
| 1931 | msg->sol, cur_end + 1, |
| 1932 | NULL, NULL); |
| 1933 | if (unlikely(!cur_end)) |
| 1934 | return 0; |
| 1935 | |
| 1936 | /* we have a full HTTP/1.0 request now and we know that |
| 1937 | * we have either a CR or an LF at <ptr>. |
| 1938 | */ |
| 1939 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1940 | return 1; |
| 1941 | } |
| 1942 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1943 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1944 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1945 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1946 | * are used for that : |
| 1947 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1948 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1949 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1950 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1951 | * harmless combinations may be removed. Do not call that after changes have |
| 1952 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1953 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1954 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1955 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1956 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1957 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1958 | const char *hdr_val = "Connection"; |
| 1959 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1960 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1961 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1962 | return; |
| 1963 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1964 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1965 | hdr_val = "Proxy-Connection"; |
| 1966 | hdr_len = 16; |
| 1967 | } |
| 1968 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1969 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1970 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1971 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1972 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1973 | txn->flags |= TX_HDR_CONN_KAL; |
| 1974 | if ((to_del & 2) && buf) |
| 1975 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1976 | else |
| 1977 | txn->flags |= TX_CON_KAL_SET; |
| 1978 | } |
| 1979 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1980 | txn->flags |= TX_HDR_CONN_CLO; |
| 1981 | if ((to_del & 1) && buf) |
| 1982 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1983 | else |
| 1984 | txn->flags |= TX_CON_CLO_SET; |
| 1985 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1986 | } |
| 1987 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1988 | txn->flags |= TX_HDR_CONN_PRS; |
| 1989 | return; |
| 1990 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1991 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1992 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1993 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1994 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1995 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1996 | */ |
| 1997 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1998 | { |
| 1999 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2000 | const char *hdr_val = "Connection"; |
| 2001 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2002 | |
| 2003 | ctx.idx = 0; |
| 2004 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2005 | |
| 2006 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2007 | hdr_val = "Proxy-Connection"; |
| 2008 | hdr_len = 16; |
| 2009 | } |
| 2010 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2011 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2012 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2013 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2014 | if (wanted & TX_CON_KAL_SET) |
| 2015 | txn->flags |= TX_CON_KAL_SET; |
| 2016 | else |
| 2017 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2018 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2019 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2020 | if (wanted & TX_CON_CLO_SET) |
| 2021 | txn->flags |= TX_CON_CLO_SET; |
| 2022 | else |
| 2023 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2024 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2025 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2026 | |
| 2027 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2028 | return; |
| 2029 | |
| 2030 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2031 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2032 | hdr_val = "Connection: close"; |
| 2033 | hdr_len = 17; |
| 2034 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2035 | hdr_val = "Proxy-Connection: close"; |
| 2036 | hdr_len = 23; |
| 2037 | } |
| 2038 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2042 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2043 | hdr_val = "Connection: keep-alive"; |
| 2044 | hdr_len = 22; |
| 2045 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2046 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2047 | hdr_len = 28; |
| 2048 | } |
| 2049 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2050 | } |
| 2051 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2052 | } |
| 2053 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2054 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2055 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2056 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2057 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2058 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2059 | * 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] | 2060 | * 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] | 2061 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2062 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2063 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2064 | char *ptr = buf->lr; |
| 2065 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2066 | unsigned int chunk = 0; |
| 2067 | |
| 2068 | /* The chunk size is in the following form, though we are only |
| 2069 | * interested in the size and CRLF : |
| 2070 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2071 | */ |
| 2072 | while (1) { |
| 2073 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2074 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2075 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2076 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2077 | if (c < 0) /* not a hex digit anymore */ |
| 2078 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2079 | if (++ptr >= end) |
| 2080 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2081 | if (chunk & 0xF000000) /* overflow will occur */ |
| 2082 | return -1; |
| 2083 | chunk = (chunk << 4) + c; |
| 2084 | } |
| 2085 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2086 | /* empty size not allowed */ |
| 2087 | if (ptr == buf->lr) |
| 2088 | return -1; |
| 2089 | |
| 2090 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2091 | if (++ptr >= end) |
| 2092 | ptr = buf->data; |
| 2093 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2094 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2095 | } |
| 2096 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2097 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2098 | * for the end of chunk size. |
| 2099 | */ |
| 2100 | while (1) { |
| 2101 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2102 | /* we now have a CR or an LF at ptr */ |
| 2103 | if (likely(*ptr == '\r')) { |
| 2104 | if (++ptr >= end) |
| 2105 | ptr = buf->data; |
| 2106 | if (ptr == buf->r) |
| 2107 | return 0; |
| 2108 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2109 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | if (*ptr != '\n') |
| 2111 | return -1; |
| 2112 | if (++ptr >= end) |
| 2113 | ptr = buf->data; |
| 2114 | /* done */ |
| 2115 | break; |
| 2116 | } |
| 2117 | else if (*ptr == ';') { |
| 2118 | /* chunk extension, ends at next CRLF */ |
| 2119 | if (++ptr >= end) |
| 2120 | ptr = buf->data; |
| 2121 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2122 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2123 | |
| 2124 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2125 | if (++ptr >= end) |
| 2126 | ptr = buf->data; |
| 2127 | if (ptr == buf->r) |
| 2128 | return 0; |
| 2129 | } |
| 2130 | /* we have a CRLF now, loop above */ |
| 2131 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2132 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2133 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2134 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2135 | } |
| 2136 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2137 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2138 | * which may or may not be present. We save that into ->lr and |
| 2139 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2140 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2141 | msg->sov += ptr - buf->lr; |
| 2142 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2143 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2144 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2145 | return 1; |
| 2146 | } |
| 2147 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2148 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2149 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2150 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2151 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2152 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2153 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2154 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2155 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2156 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2157 | * which implies that all non-trailers data have already been scheduled for |
| 2158 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2159 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2160 | * important to note that this function is designed to be able to parse wrapped |
| 2161 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2162 | */ |
| 2163 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2164 | { |
| 2165 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2166 | while (1) { |
| 2167 | char *p1 = NULL, *p2 = NULL; |
| 2168 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2169 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* scan current line and stop at LF or CRLF */ |
| 2172 | while (1) { |
| 2173 | if (ptr == buf->r) |
| 2174 | return 0; |
| 2175 | |
| 2176 | if (*ptr == '\n') { |
| 2177 | if (!p1) |
| 2178 | p1 = ptr; |
| 2179 | p2 = ptr; |
| 2180 | break; |
| 2181 | } |
| 2182 | |
| 2183 | if (*ptr == '\r') { |
| 2184 | if (p1) |
| 2185 | return -1; |
| 2186 | p1 = ptr; |
| 2187 | } |
| 2188 | |
| 2189 | ptr++; |
| 2190 | if (ptr >= buf->data + buf->size) |
| 2191 | ptr = buf->data; |
| 2192 | } |
| 2193 | |
| 2194 | /* after LF; point to beginning of next line */ |
| 2195 | p2++; |
| 2196 | if (p2 >= buf->data + buf->size) |
| 2197 | p2 = buf->data; |
| 2198 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2199 | bytes = p2 - buf->lr; |
| 2200 | if (bytes < 0) |
| 2201 | bytes += buf->size; |
| 2202 | |
| 2203 | /* schedule this line for forwarding */ |
| 2204 | msg->sov += bytes; |
| 2205 | if (msg->sov >= buf->size) |
| 2206 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2207 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2208 | if (p1 == buf->lr) { |
| 2209 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2210 | * Everything was scheduled for forwarding, there's nothing |
| 2211 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2212 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2213 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2214 | msg->msg_state = HTTP_MSG_DONE; |
| 2215 | return 1; |
| 2216 | } |
| 2217 | /* OK, next line then */ |
| 2218 | buf->lr = p2; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2223 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2224 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2225 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2226 | * not enough data are available, the function does not change anything and |
| 2227 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2228 | * does not change anything. Note: this function is designed to parse wrapped |
| 2229 | * CRLF at the end of the buffer. |
| 2230 | */ |
| 2231 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2232 | { |
| 2233 | char *ptr; |
| 2234 | int bytes; |
| 2235 | |
| 2236 | /* NB: we'll check data availabilty at the end. It's not a |
| 2237 | * problem because whatever we match first will be checked |
| 2238 | * against the correct length. |
| 2239 | */ |
| 2240 | bytes = 1; |
| 2241 | ptr = buf->lr; |
| 2242 | if (*ptr == '\r') { |
| 2243 | bytes++; |
| 2244 | ptr++; |
| 2245 | if (ptr >= buf->data + buf->size) |
| 2246 | ptr = buf->data; |
| 2247 | } |
| 2248 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2249 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2250 | return 0; |
| 2251 | |
| 2252 | if (*ptr != '\n') |
| 2253 | return -1; |
| 2254 | |
| 2255 | ptr++; |
| 2256 | if (ptr >= buf->data + buf->size) |
| 2257 | ptr = buf->data; |
| 2258 | buf->lr = ptr; |
| 2259 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2260 | msg->sov = ptr - buf->data; |
| 2261 | msg->som = msg->sov - bytes; |
| 2262 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2263 | return 1; |
| 2264 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2265 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2266 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2267 | { |
| 2268 | char *end = buf->data + buf->size; |
| 2269 | int off = buf->data + buf->size - buf->w; |
| 2270 | |
| 2271 | /* two possible cases : |
| 2272 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2273 | * - 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] | 2274 | */ |
| 2275 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2276 | int block1 = buf->l; |
| 2277 | int block2 = 0; |
| 2278 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2279 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2280 | block1 = buf->data + buf->size - buf->w; |
| 2281 | block2 = buf->r - buf->data; |
| 2282 | } |
| 2283 | if (block2) |
| 2284 | memcpy(swap_buffer, buf->data, block2); |
| 2285 | memmove(buf->data, buf->w, block1); |
| 2286 | if (block2) |
| 2287 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | /* adjust all known pointers */ |
| 2291 | buf->w = buf->data; |
| 2292 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2293 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2294 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2295 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2296 | |
| 2297 | /* adjust relative pointers */ |
| 2298 | msg->som = 0; |
| 2299 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2300 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2301 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2302 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2303 | if (msg->err_pos >= 0) { |
| 2304 | msg->err_pos += off; |
| 2305 | if (msg->err_pos >= buf->size) |
| 2306 | msg->err_pos -= buf->size; |
| 2307 | } |
| 2308 | |
| 2309 | buf->flags &= ~BF_FULL; |
| 2310 | if (buf->l >= buffer_max_len(buf)) |
| 2311 | buf->flags |= BF_FULL; |
| 2312 | } |
| 2313 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2314 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2315 | * processing can continue on next analysers, or zero if it either needs more |
| 2316 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2317 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2318 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2319 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2320 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2321 | 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] | 2322 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2323 | /* |
| 2324 | * We will parse the partial (or complete) lines. |
| 2325 | * We will check the request syntax, and also join multi-line |
| 2326 | * headers. An index of all the lines will be elaborated while |
| 2327 | * parsing. |
| 2328 | * |
| 2329 | * For the parsing, we use a 28 states FSM. |
| 2330 | * |
| 2331 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2332 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2333 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2334 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2335 | * req->lr = first non-visited byte |
| 2336 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2337 | * |
| 2338 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2339 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2340 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2341 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2342 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2343 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2344 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2345 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | struct http_txn *txn = &s->txn; |
| 2347 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2348 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2349 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2350 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2351 | now_ms, __FUNCTION__, |
| 2352 | s, |
| 2353 | req, |
| 2354 | req->rex, req->wex, |
| 2355 | req->flags, |
| 2356 | req->l, |
| 2357 | req->analysers); |
| 2358 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2359 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2360 | s->srv_error = http_return_srv_error; |
| 2361 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2362 | /* There's a protected area at the end of the buffer for rewriting |
| 2363 | * purposes. We don't want to start to parse the request if the |
| 2364 | * protected area is affected, because we may have to move processed |
| 2365 | * data later, which is much more complicated. |
| 2366 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2367 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2368 | if ((txn->flags & TX_NOT_FIRST) && |
| 2369 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2370 | req->r < req->lr || |
| 2371 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2372 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2373 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2374 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2375 | /* some data has still not left the buffer, wake us once that's done */ |
| 2376 | buffer_dont_connect(req); |
| 2377 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2378 | return 0; |
| 2379 | } |
| 2380 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2381 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2384 | /* Note that we have the same problem with the response ; we |
| 2385 | * may want to send a redirect, error or anything which requires |
| 2386 | * some spare space. So we'll ensure that we have at least |
| 2387 | * maxrewrite bytes available in the response buffer before |
| 2388 | * processing that one. This will only affect pipelined |
| 2389 | * keep-alive requests. |
| 2390 | */ |
| 2391 | if ((txn->flags & TX_NOT_FIRST) && |
| 2392 | unlikely((s->rep->flags & BF_FULL) || |
| 2393 | s->rep->r < s->rep->lr || |
| 2394 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2395 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2396 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2397 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2398 | /* don't let a connection request be initiated */ |
| 2399 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2400 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2401 | return 0; |
| 2402 | } |
| 2403 | } |
| 2404 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2405 | if (likely(req->lr < req->r)) |
| 2406 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2409 | /* 1: we might have to print this header in debug mode */ |
| 2410 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2411 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2412 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2413 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2414 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2415 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2416 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2417 | eol = sol + msg->sl.rq.l; |
| 2418 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2419 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2420 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2421 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2422 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2423 | while (cur_idx) { |
| 2424 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2425 | debug_hdr("clihdr", s, sol, eol); |
| 2426 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2427 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2428 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2429 | } |
| 2430 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2431 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2432 | /* |
| 2433 | * Now we quickly check if we have found a full valid request. |
| 2434 | * If not so, we check the FD and buffer states before leaving. |
| 2435 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2436 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2437 | * requests are checked first. When waiting for a second request |
| 2438 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2439 | * we note the error in the session flags but don't set any state. |
| 2440 | * Since the error will be noted there, it will not be counted by |
| 2441 | * process_session() as a frontend error. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2442 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2443 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2444 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2445 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2446 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2447 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2448 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2449 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2450 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2451 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2452 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2453 | /* 1: Since we are in header mode, if there's no space |
| 2454 | * left for headers, we won't be able to free more |
| 2455 | * later, so the session will never terminate. We |
| 2456 | * must terminate it now. |
| 2457 | */ |
| 2458 | if (unlikely(req->flags & BF_FULL)) { |
| 2459 | /* FIXME: check if URI is set and return Status |
| 2460 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2461 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2462 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2463 | goto return_bad_req; |
| 2464 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2465 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2466 | /* 2: have we encountered a read error ? */ |
| 2467 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2468 | if (!(s->flags & SN_ERR_MASK)) |
| 2469 | s->flags |= SN_ERR_CLICL; |
| 2470 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2471 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2472 | goto failed_keep_alive; |
| 2473 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2474 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2475 | if (msg->err_pos >= 0) |
| 2476 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2477 | msg->msg_state = HTTP_MSG_ERROR; |
| 2478 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2479 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2480 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2481 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2482 | if (s->listener->counters) |
| 2483 | s->listener->counters->failed_req++; |
| 2484 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2485 | if (!(s->flags & SN_FINST_MASK)) |
| 2486 | s->flags |= SN_FINST_R; |
| 2487 | return 0; |
| 2488 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2489 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2490 | /* 3: has the read timeout expired ? */ |
| 2491 | 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] | 2492 | if (!(s->flags & SN_ERR_MASK)) |
| 2493 | s->flags |= SN_ERR_CLITO; |
| 2494 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2495 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2496 | goto failed_keep_alive; |
| 2497 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2498 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2499 | if (msg->err_pos >= 0) |
| 2500 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2501 | txn->status = 408; |
| 2502 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2503 | msg->msg_state = HTTP_MSG_ERROR; |
| 2504 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2505 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2506 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2507 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2508 | if (s->listener->counters) |
| 2509 | s->listener->counters->failed_req++; |
| 2510 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2511 | if (!(s->flags & SN_FINST_MASK)) |
| 2512 | s->flags |= SN_FINST_R; |
| 2513 | return 0; |
| 2514 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2515 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2516 | /* 4: have we encountered a close ? */ |
| 2517 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2518 | if (!(s->flags & SN_ERR_MASK)) |
| 2519 | s->flags |= SN_ERR_CLICL; |
| 2520 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2521 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2522 | goto failed_keep_alive; |
| 2523 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2524 | if (msg->err_pos >= 0) |
| 2525 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | txn->status = 400; |
| 2527 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2528 | msg->msg_state = HTTP_MSG_ERROR; |
| 2529 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2530 | |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2531 | proxy_inc_fe_req_ctr(s->fe); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2532 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2533 | if (s->listener->counters) |
| 2534 | s->listener->counters->failed_req++; |
| 2535 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2536 | if (!(s->flags & SN_FINST_MASK)) |
| 2537 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2538 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2539 | } |
| 2540 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2541 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2542 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2543 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2544 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2545 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2546 | /* If the client starts to talk, let's fall back to |
| 2547 | * request timeout processing. |
| 2548 | */ |
| 2549 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2550 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2553 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2554 | if (!tick_isset(req->analyse_exp)) { |
| 2555 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2556 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2557 | tick_isset(s->be->timeout.httpka)) |
| 2558 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2559 | else |
| 2560 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2561 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2563 | /* we're not ready yet */ |
| 2564 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2565 | |
| 2566 | failed_keep_alive: |
| 2567 | /* Here we process low-level errors for keep-alive requests. In |
| 2568 | * short, if the request is not the first one and it experiences |
| 2569 | * a timeout, read error or shutdown, we just silently close so |
| 2570 | * that the client can try again. |
| 2571 | */ |
| 2572 | txn->status = 0; |
| 2573 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2574 | req->analysers = 0; |
| 2575 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2576 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2577 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2578 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2579 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2580 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2581 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2582 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2583 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2584 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2585 | * points to the CRLF of the request line. req->lr points to the first |
| 2586 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2587 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2588 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2589 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2590 | |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2591 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2592 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2593 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2594 | /* kill the pending keep-alive timeout */ |
| 2595 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2596 | req->analyse_exp = TICK_ETERNITY; |
| 2597 | } |
| 2598 | |
| 2599 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2600 | /* Maybe we found in invalid header name while we were configured not |
| 2601 | * to block on that, so we have to capture it now. |
| 2602 | */ |
| 2603 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2604 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2605 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2606 | /* |
| 2607 | * 1: identify the method |
| 2608 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2609 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | |
| 2611 | /* we can make use of server redirect on GET and HEAD */ |
| 2612 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2613 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2614 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | /* |
| 2616 | * 2: check if the URI matches the monitor_uri. |
| 2617 | * We have to do this for every request which gets in, because |
| 2618 | * the monitor-uri is defined by the frontend. |
| 2619 | */ |
| 2620 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2621 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2622 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2623 | s->fe->monitor_uri, |
| 2624 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2625 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2626 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2627 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2628 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2629 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2630 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2631 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2632 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2633 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2634 | 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] | 2635 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | ret = acl_pass(ret); |
| 2637 | if (cond->pol == ACL_COND_UNLESS) |
| 2638 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2640 | if (ret) { |
| 2641 | /* we fail this request, let's return 503 service unavail */ |
| 2642 | txn->status = 503; |
| 2643 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2644 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2645 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2646 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2647 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2648 | /* nothing to fail, let's reply normaly */ |
| 2649 | txn->status = 200; |
| 2650 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2651 | goto return_prx_cond; |
| 2652 | } |
| 2653 | |
| 2654 | /* |
| 2655 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2656 | * Note: we cannot log anymore if the request has been |
| 2657 | * classified as invalid. |
| 2658 | */ |
| 2659 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2660 | /* we have a complete HTTP request that we must log */ |
| 2661 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2662 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2663 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2664 | if (urilen >= REQURI_LEN) |
| 2665 | urilen = REQURI_LEN - 1; |
| 2666 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2667 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2668 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2669 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2670 | s->do_log(s); |
| 2671 | } else { |
| 2672 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2673 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2674 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2675 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2676 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2677 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2678 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2679 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2680 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2681 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2682 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2683 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2684 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2685 | txn->flags |= TX_REQ_VER_11; |
| 2686 | |
| 2687 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2688 | 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] | 2689 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2690 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2691 | * we have what looks like a proxied connection instead of a connection, |
| 2692 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2693 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2694 | * happen to do that despite being non-standard :-( |
| 2695 | * We consider that a request not beginning with either '/' or '*' is |
| 2696 | * a proxied connection, which covers both "scheme://location" and |
| 2697 | * CONNECT ip:port. |
| 2698 | */ |
| 2699 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2700 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2701 | txn->flags |= TX_USE_PX_CONN; |
| 2702 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2703 | /* transfer length unknown*/ |
| 2704 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2705 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2706 | /* 5: we may need to capture headers */ |
| 2707 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2708 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2709 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2710 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2711 | /* 6: determine the transfer-length. |
| 2712 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2713 | * the presence of a message-body in a REQUEST and its transfer length |
| 2714 | * must be determined that way (in order of precedence) : |
| 2715 | * 1. The presence of a message-body in a request is signaled by the |
| 2716 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2717 | * in the request's header fields. When a request message contains |
| 2718 | * both a message-body of non-zero length and a method that does |
| 2719 | * not define any semantics for that request message-body, then an |
| 2720 | * origin server SHOULD either ignore the message-body or respond |
| 2721 | * with an appropriate error message (e.g., 413). A proxy or |
| 2722 | * gateway, when presented the same request, SHOULD either forward |
| 2723 | * the request inbound with the message- body or ignore the |
| 2724 | * message-body when determining a response. |
| 2725 | * |
| 2726 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2727 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2728 | * transfer-length is defined by the use of this transfer-coding. |
| 2729 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2730 | * transfer-coding is not present, the transfer-length is defined |
| 2731 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2732 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2733 | * 3. If a Content-Length header field is present, its decimal value in |
| 2734 | * OCTETs represents both the entity-length and the transfer-length. |
| 2735 | * If a message is received with both a Transfer-Encoding header |
| 2736 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2737 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2738 | * 4. By the server closing the connection. (Closing the connection |
| 2739 | * cannot be used to indicate the end of a request body, since that |
| 2740 | * would leave no possibility for the server to send back a response.) |
| 2741 | * |
| 2742 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2743 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2744 | * it is terminated by closing the connection. When the "chunked" |
| 2745 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2746 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2747 | */ |
| 2748 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2749 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2750 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2751 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2752 | while ((txn->flags & TX_REQ_VER_11) && |
| 2753 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2754 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2755 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2756 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2757 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2758 | use_close_only = 1; |
| 2759 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2760 | break; |
| 2761 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2762 | } |
| 2763 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2764 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2765 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2766 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2767 | signed long long cl; |
| 2768 | |
| 2769 | if (!ctx.vlen) |
| 2770 | goto return_bad_req; |
| 2771 | |
| 2772 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2773 | goto return_bad_req; /* parse failure */ |
| 2774 | |
| 2775 | if (cl < 0) |
| 2776 | goto return_bad_req; |
| 2777 | |
| 2778 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2779 | goto return_bad_req; /* already specified, was different */ |
| 2780 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2781 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2782 | msg->hdr_content_len = cl; |
| 2783 | } |
| 2784 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2785 | /* bodyless requests have a known length */ |
| 2786 | if (!use_close_only) |
| 2787 | txn->flags |= TX_REQ_XFER_LEN; |
| 2788 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2789 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2790 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2791 | req->analyse_exp = TICK_ETERNITY; |
| 2792 | return 1; |
| 2793 | |
| 2794 | return_bad_req: |
| 2795 | /* We centralize bad requests processing here */ |
| 2796 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2797 | /* we detected a parsing error. We want to archive this request |
| 2798 | * in the dedicated proxy area for later troubleshooting. |
| 2799 | */ |
| 2800 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2801 | } |
| 2802 | |
| 2803 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2804 | txn->status = 400; |
| 2805 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2806 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2807 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2808 | if (s->listener->counters) |
| 2809 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2810 | |
| 2811 | return_prx_cond: |
| 2812 | if (!(s->flags & SN_ERR_MASK)) |
| 2813 | s->flags |= SN_ERR_PRXCOND; |
| 2814 | if (!(s->flags & SN_FINST_MASK)) |
| 2815 | s->flags |= SN_FINST_R; |
| 2816 | |
| 2817 | req->analysers = 0; |
| 2818 | req->analyse_exp = TICK_ETERNITY; |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2822 | /* This stream analyser runs all HTTP request processing which is common to |
| 2823 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2824 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2825 | * 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] | 2826 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2827 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2828 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2829 | 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] | 2830 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2831 | struct http_txn *txn = &s->txn; |
| 2832 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2833 | struct acl_cond *cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2834 | struct req_acl_rule *req_acl, *req_acl_final = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2835 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2836 | struct cond_wordlist *wl; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2837 | int del_ka, del_cl, do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2838 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2839 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2840 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2841 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2842 | return 0; |
| 2843 | } |
| 2844 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2845 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2846 | req->analyse_exp = TICK_ETERNITY; |
| 2847 | |
| 2848 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2849 | now_ms, __FUNCTION__, |
| 2850 | s, |
| 2851 | req, |
| 2852 | req->rex, req->wex, |
| 2853 | req->flags, |
| 2854 | req->l, |
| 2855 | req->analysers); |
| 2856 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2857 | /* first check whether we have some ACLs set to block this request */ |
| 2858 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2859 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2860 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2861 | ret = acl_pass(ret); |
| 2862 | if (cond->pol == ACL_COND_UNLESS) |
| 2863 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2864 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2865 | if (ret) { |
| 2866 | txn->status = 403; |
| 2867 | /* let's log the request time */ |
| 2868 | s->logs.tv_request = now; |
| 2869 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2870 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2871 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2872 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2873 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2874 | do_stats = stats_check_uri(s, px); |
| 2875 | |
| 2876 | list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2877 | int ret = 1; |
| 2878 | |
| 2879 | if (req_acl->action >= PR_REQ_ACL_ACT_MAX) |
| 2880 | continue; |
| 2881 | |
| 2882 | /* check condition, but only if attached */ |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2883 | if (req_acl->cond) { |
| 2884 | ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ); |
| 2885 | ret = acl_pass(ret); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2886 | |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2887 | if (req_acl->cond->pol == ACL_COND_UNLESS) |
| 2888 | ret = !ret; |
| 2889 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2890 | |
| 2891 | if (ret) { |
| 2892 | req_acl_final = req_acl; |
| 2893 | break; |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) { |
| 2898 | txn->status = 403; |
| 2899 | s->logs.tv_request = now; |
| 2900 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2901 | goto return_prx_cond; |
| 2902 | } |
| 2903 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2904 | /* try headers filters */ |
| 2905 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2906 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2907 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2908 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2909 | /* has the request been denied ? */ |
| 2910 | if (txn->flags & TX_CLDENY) { |
| 2911 | /* no need to go further */ |
| 2912 | txn->status = 403; |
| 2913 | /* let's log the request time */ |
| 2914 | s->logs.tv_request = now; |
| 2915 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2916 | goto return_prx_cond; |
| 2917 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2918 | |
| 2919 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2920 | * which may be the same as the connect timeout if unspecified. |
| 2921 | * If unset, then set it to zero because we really want it to |
| 2922 | * eventually expire. We build the tarpit as an analyser. |
| 2923 | */ |
| 2924 | if (txn->flags & TX_CLTARPIT) { |
| 2925 | buffer_erase(s->req); |
| 2926 | /* wipe the request out so that we can drop the connection early |
| 2927 | * if the client closes first. |
| 2928 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2929 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2930 | req->analysers = 0; /* remove switching rules etc... */ |
| 2931 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2932 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2933 | if (!req->analyse_exp) |
| 2934 | req->analyse_exp = tick_add(now_ms, 0); |
| 2935 | return 1; |
| 2936 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2937 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2938 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2939 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2940 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2941 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2942 | * 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] | 2943 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2944 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2945 | * The method consists in checking if options changed between the two calls |
| 2946 | * (implying that either one is non-null, or one of them is non-null and we |
| 2947 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2948 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2949 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2950 | del_cl = del_ka = 0; |
| 2951 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2952 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2953 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2954 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2955 | (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] | 2956 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2957 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2958 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2959 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2960 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2961 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2962 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2963 | tmp = TX_CON_WANT_CLO; |
| 2964 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2965 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2966 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2967 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2968 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2969 | /* parse the Connection header and possibly clean it */ |
| 2970 | int to_del = 0; |
| 2971 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 2972 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 2973 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2974 | to_del |= 2; /* remove "keep-alive" */ |
| 2975 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2976 | to_del |= 1; /* remove "close" */ |
| 2977 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2978 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2979 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2980 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2981 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2982 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2983 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2984 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
| 2985 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 2986 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 2987 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2988 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2989 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2990 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2991 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2992 | list_for_each_entry(wl, &px->req_add, list) { |
Willy Tarreau | 8abd4cd | 2010-01-31 14:30:44 +0100 | [diff] [blame] | 2993 | if (wl->cond) { |
| 2994 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2995 | ret = acl_pass(ret); |
| 2996 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2997 | ret = !ret; |
| 2998 | if (!ret) |
| 2999 | continue; |
| 3000 | } |
| 3001 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 3002 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3003 | goto return_bad_req; |
| 3004 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3005 | |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3006 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) { |
| 3007 | struct chunk msg; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3008 | char *realm = req_acl->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3009 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3010 | if (!realm) |
| 3011 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3012 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3013 | 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] | 3014 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3015 | txn->status = 401; |
| 3016 | stream_int_retnclose(req->prod, &msg); |
| 3017 | goto return_prx_cond; |
| 3018 | } |
| 3019 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3020 | if (do_stats) { |
| 3021 | /* We need to provied stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3022 | * FIXME!!! that one is rather dangerous, we want to |
| 3023 | * make it follow standard rules (eg: clear req->analysers). |
| 3024 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3025 | |
| 3026 | s->logs.tv_request = now; |
| 3027 | s->data_source = DATA_SRC_STATS; |
| 3028 | s->data_state = DATA_ST_INIT; |
| 3029 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3030 | stream_int_register_handler(s->rep->prod, http_stats_io_handler); |
| 3031 | s->rep->prod->private = s; |
| 3032 | s->rep->prod->st0 = s->rep->prod->st1 = 0; |
| 3033 | req->analysers = 0; |
| 3034 | |
| 3035 | return 0; |
| 3036 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3037 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3038 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3039 | /* check whether we have some ACLs set to redirect this request */ |
| 3040 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3041 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3042 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3043 | if (rule->cond) { |
| 3044 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3045 | ret = acl_pass(ret); |
| 3046 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3047 | ret = !ret; |
| 3048 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3049 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3050 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3051 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3052 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3053 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3054 | /* build redirect message */ |
| 3055 | switch(rule->code) { |
| 3056 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3057 | msg_fmt = HTTP_303; |
| 3058 | break; |
| 3059 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3060 | msg_fmt = HTTP_301; |
| 3061 | break; |
| 3062 | case 302: |
| 3063 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3064 | msg_fmt = HTTP_302; |
| 3065 | break; |
| 3066 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3067 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3068 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3069 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3070 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3071 | switch(rule->type) { |
| 3072 | case REDIRECT_TYPE_PREFIX: { |
| 3073 | const char *path; |
| 3074 | int pathlen; |
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 | path = http_get_path(txn); |
| 3077 | /* build message using path */ |
| 3078 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3079 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3081 | int qs = 0; |
| 3082 | while (qs < pathlen) { |
| 3083 | if (path[qs] == '?') { |
| 3084 | pathlen = qs; |
| 3085 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3086 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3087 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3088 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3089 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3090 | } else { |
| 3091 | path = "/"; |
| 3092 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3093 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3094 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3095 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3096 | goto return_bad_req; |
| 3097 | |
| 3098 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3099 | * add anything, otherwise it makes it hard for the user to |
| 3100 | * configure a self-redirection. |
| 3101 | */ |
| 3102 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3103 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3104 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3105 | } |
| 3106 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3107 | /* add path */ |
| 3108 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3109 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3110 | |
| 3111 | /* append a slash at the end of the location is needed and missing */ |
| 3112 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3113 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3114 | if (rdr.len > rdr.size - 5) |
| 3115 | goto return_bad_req; |
| 3116 | rdr.str[rdr.len] = '/'; |
| 3117 | rdr.len++; |
| 3118 | } |
| 3119 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3120 | break; |
| 3121 | } |
| 3122 | case REDIRECT_TYPE_LOCATION: |
| 3123 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3124 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3125 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3126 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3127 | /* add location */ |
| 3128 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3129 | rdr.len += rule->rdr_len; |
| 3130 | break; |
| 3131 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3132 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3133 | if (rule->cookie_len) { |
| 3134 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3135 | rdr.len += 14; |
| 3136 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3137 | rdr.len += rule->cookie_len; |
| 3138 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3139 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3140 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3141 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3142 | /* add end of headers and the keep-alive/close status. |
| 3143 | * We may choose to set keep-alive if the Location begins |
| 3144 | * with a slash, because the client will come back to the |
| 3145 | * same server. |
| 3146 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3147 | txn->status = rule->code; |
| 3148 | /* let's log the request time */ |
| 3149 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3150 | |
| 3151 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3152 | (txn->flags & TX_REQ_XFER_LEN) && |
| 3153 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 3154 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3155 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3156 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3157 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3158 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3159 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3160 | rdr.len += 30; |
| 3161 | } else { |
| 3162 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3163 | rdr.len += 24; |
| 3164 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3165 | } |
| 3166 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3167 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3168 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3169 | /* "eat" the request */ |
| 3170 | buffer_ignore(req, msg->sov - msg->som); |
| 3171 | msg->som = msg->sov; |
| 3172 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3173 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3174 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3175 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3176 | break; |
| 3177 | } else { |
| 3178 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3179 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3180 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3181 | rdr.len += 29; |
| 3182 | } else { |
| 3183 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3184 | rdr.len += 23; |
| 3185 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3186 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3187 | goto return_prx_cond; |
| 3188 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3189 | } |
| 3190 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3191 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3192 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3193 | * If this happens, then the data will not come immediately, so we must |
| 3194 | * send all what we have without waiting. Note that due to the small gain |
| 3195 | * in waiting for the body of the request, it's easier to simply put the |
| 3196 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3197 | * itself once used. |
| 3198 | */ |
| 3199 | req->flags |= BF_SEND_DONTWAIT; |
| 3200 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3201 | /* that's OK for us now, let's move on to next analysers */ |
| 3202 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3203 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3204 | return_bad_req: |
| 3205 | /* We centralize bad requests processing here */ |
| 3206 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3207 | /* we detected a parsing error. We want to archive this request |
| 3208 | * in the dedicated proxy area for later troubleshooting. |
| 3209 | */ |
| 3210 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 3211 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3212 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3213 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3214 | txn->status = 400; |
| 3215 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3216 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3217 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3218 | if (s->listener->counters) |
| 3219 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3220 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3221 | return_prx_cond: |
| 3222 | if (!(s->flags & SN_ERR_MASK)) |
| 3223 | s->flags |= SN_ERR_PRXCOND; |
| 3224 | if (!(s->flags & SN_FINST_MASK)) |
| 3225 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3226 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3227 | req->analysers = 0; |
| 3228 | req->analyse_exp = TICK_ETERNITY; |
| 3229 | return 0; |
| 3230 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3231 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3232 | /* This function performs all the processing enabled for the current request. |
| 3233 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3234 | * needs more data, encounters an error, or wants to immediately abort the |
| 3235 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3236 | */ |
| 3237 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3238 | { |
| 3239 | struct http_txn *txn = &s->txn; |
| 3240 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3242 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3243 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3244 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3248 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3249 | now_ms, __FUNCTION__, |
| 3250 | s, |
| 3251 | req, |
| 3252 | req->rex, req->wex, |
| 3253 | req->flags, |
| 3254 | req->l, |
| 3255 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3256 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3257 | /* |
| 3258 | * Right now, we know that we have processed the entire headers |
| 3259 | * and that unwanted requests have been filtered out. We can do |
| 3260 | * whatever we want with the remaining request. Also, now we |
| 3261 | * may have separate values for ->fe, ->be. |
| 3262 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3263 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3264 | /* |
| 3265 | * If HTTP PROXY is set we simply get remote server address |
| 3266 | * parsing incoming request. |
| 3267 | */ |
| 3268 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3269 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3270 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3271 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3273 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3274 | * Note that doing so might move headers in the request, but |
| 3275 | * the fields will stay coherent and the URI will not move. |
| 3276 | * This should only be performed in the backend. |
| 3277 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3278 | 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] | 3279 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3280 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3281 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3282 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3283 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3284 | * so let's do the same now. |
| 3285 | */ |
| 3286 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3287 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3288 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3289 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3293 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3294 | * asks for it. |
| 3295 | */ |
| 3296 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3297 | if (s->cli_addr.ss_family == AF_INET) { |
| 3298 | /* Add an X-Forwarded-For header unless the source IP is |
| 3299 | * in the 'except' network range. |
| 3300 | */ |
| 3301 | if ((!s->fe->except_mask.s_addr || |
| 3302 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3303 | != s->fe->except_net.s_addr) && |
| 3304 | (!s->be->except_mask.s_addr || |
| 3305 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3306 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3307 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3308 | unsigned char *pn; |
| 3309 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3310 | |
| 3311 | /* Note: we rely on the backend to get the header name to be used for |
| 3312 | * x-forwarded-for, because the header is really meant for the backends. |
| 3313 | * However, if the backend did not specify any option, we have to rely |
| 3314 | * on the frontend's header name. |
| 3315 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3316 | if (s->be->fwdfor_hdr_len) { |
| 3317 | len = s->be->fwdfor_hdr_len; |
| 3318 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3319 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3320 | len = s->fe->fwdfor_hdr_len; |
| 3321 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3322 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3323 | 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] | 3324 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3325 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3326 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3327 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3328 | } |
| 3329 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3330 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3331 | /* FIXME: for the sake of completeness, we should also support |
| 3332 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3333 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3334 | int len; |
| 3335 | char pn[INET6_ADDRSTRLEN]; |
| 3336 | inet_ntop(AF_INET6, |
| 3337 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3338 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3339 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3340 | /* Note: we rely on the backend to get the header name to be used for |
| 3341 | * x-forwarded-for, because the header is really meant for the backends. |
| 3342 | * However, if the backend did not specify any option, we have to rely |
| 3343 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3344 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3345 | if (s->be->fwdfor_hdr_len) { |
| 3346 | len = s->be->fwdfor_hdr_len; |
| 3347 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3348 | } else { |
| 3349 | len = s->fe->fwdfor_hdr_len; |
| 3350 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3351 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3352 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3353 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3354 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3355 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3356 | goto return_bad_req; |
| 3357 | } |
| 3358 | } |
| 3359 | |
| 3360 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3361 | * 10: add X-Original-To if either the frontend or the backend |
| 3362 | * asks for it. |
| 3363 | */ |
| 3364 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3365 | |
| 3366 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3367 | if (s->cli_addr.ss_family == AF_INET) { |
| 3368 | /* Add an X-Original-To header unless the destination IP is |
| 3369 | * in the 'except' network range. |
| 3370 | */ |
| 3371 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3372 | get_frt_addr(s); |
| 3373 | |
| 3374 | if ((!s->fe->except_mask_to.s_addr || |
| 3375 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3376 | != s->fe->except_to.s_addr) && |
| 3377 | (!s->be->except_mask_to.s_addr || |
| 3378 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3379 | != s->be->except_to.s_addr)) { |
| 3380 | int len; |
| 3381 | unsigned char *pn; |
| 3382 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3383 | |
| 3384 | /* Note: we rely on the backend to get the header name to be used for |
| 3385 | * x-original-to, because the header is really meant for the backends. |
| 3386 | * However, if the backend did not specify any option, we have to rely |
| 3387 | * on the frontend's header name. |
| 3388 | */ |
| 3389 | if (s->be->orgto_hdr_len) { |
| 3390 | len = s->be->orgto_hdr_len; |
| 3391 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3392 | } else { |
| 3393 | len = s->fe->orgto_hdr_len; |
| 3394 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3395 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3396 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3397 | |
| 3398 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3399 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3400 | goto return_bad_req; |
| 3401 | } |
| 3402 | } |
| 3403 | } |
| 3404 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3405 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3406 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3407 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
| 3408 | unsigned int want_flags = 0; |
| 3409 | |
| 3410 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3411 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3412 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) || |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3413 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) |
| 3414 | want_flags |= TX_CON_CLO_SET; |
| 3415 | } else { |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3416 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3417 | (((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA) && |
| 3418 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3419 | want_flags |= TX_CON_KAL_SET; |
| 3420 | } |
| 3421 | |
| 3422 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3423 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3424 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3425 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3426 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3427 | /* If we have no server assigned yet and we're balancing on url_param |
| 3428 | * with a POST request, we may be interested in checking the body for |
| 3429 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3430 | */ |
| 3431 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3432 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3433 | s->be->url_param_post_limit != 0 && |
| 3434 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3435 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3436 | buffer_dont_connect(req); |
| 3437 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3439 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3440 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3441 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3442 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3443 | /************************************************************* |
| 3444 | * OK, that's finished for the headers. We have done what we * |
| 3445 | * could. Let's switch to the DATA state. * |
| 3446 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3447 | req->analyse_exp = TICK_ETERNITY; |
| 3448 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3449 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3450 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3451 | /* OK let's go on with the BODY now */ |
| 3452 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3453 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3454 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3455 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3456 | /* we detected a parsing error. We want to archive this request |
| 3457 | * in the dedicated proxy area for later troubleshooting. |
| 3458 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3459 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3460 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3461 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3462 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3463 | txn->status = 400; |
| 3464 | req->analysers = 0; |
| 3465 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3466 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3467 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3468 | if (s->listener->counters) |
| 3469 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3470 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3471 | if (!(s->flags & SN_ERR_MASK)) |
| 3472 | s->flags |= SN_ERR_PRXCOND; |
| 3473 | if (!(s->flags & SN_FINST_MASK)) |
| 3474 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3475 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3476 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3477 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3478 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3479 | * returns zero, at the beginning because it prevents any other processing |
| 3480 | * from occurring, and at the end because it terminates the request. |
| 3481 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3482 | 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] | 3483 | { |
| 3484 | struct http_txn *txn = &s->txn; |
| 3485 | |
| 3486 | /* This connection is being tarpitted. The CLIENT side has |
| 3487 | * already set the connect expiration date to the right |
| 3488 | * timeout. We just have to check that the client is still |
| 3489 | * there and that the timeout has not expired. |
| 3490 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3491 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3492 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3493 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3494 | return 0; |
| 3495 | |
| 3496 | /* We will set the queue timer to the time spent, just for |
| 3497 | * logging purposes. We fake a 500 server error, so that the |
| 3498 | * attacker will not suspect his connection has been tarpitted. |
| 3499 | * It will not cause trouble to the logs because we can exclude |
| 3500 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3501 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3502 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3503 | |
| 3504 | txn->status = 500; |
| 3505 | if (req->flags != BF_READ_ERROR) |
| 3506 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3507 | |
| 3508 | req->analysers = 0; |
| 3509 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3510 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3511 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3512 | if (s->listener->counters) |
| 3513 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3514 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3515 | if (!(s->flags & SN_ERR_MASK)) |
| 3516 | s->flags |= SN_ERR_PRXCOND; |
| 3517 | if (!(s->flags & SN_FINST_MASK)) |
| 3518 | s->flags |= SN_FINST_T; |
| 3519 | return 0; |
| 3520 | } |
| 3521 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3522 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3523 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3524 | * must only be called after the standard HTTP request processing has occurred, |
| 3525 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3526 | * read more data, or 1 once it has completed its analysis. |
| 3527 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3528 | 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] | 3529 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3530 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3531 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3532 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3533 | |
| 3534 | /* We have to parse the HTTP request body to find any required data. |
| 3535 | * "balance url_param check_post" should have been the only way to get |
| 3536 | * into this. We were brought here after HTTP header analysis, so all |
| 3537 | * related structures are ready. |
| 3538 | */ |
| 3539 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3540 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3541 | goto missing_data; |
| 3542 | |
| 3543 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3544 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3545 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3546 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3547 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3548 | struct hdr_ctx ctx; |
| 3549 | ctx.idx = 0; |
| 3550 | /* Expect is allowed in 1.1, look for it */ |
| 3551 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3552 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3553 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3554 | } |
| 3555 | } |
| 3556 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3557 | } |
| 3558 | |
| 3559 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3560 | /* we have msg->col and msg->sov which both point to the first |
| 3561 | * byte of message body. msg->som still points to the beginning |
| 3562 | * of the message. We must save the body in req->lr because it |
| 3563 | * survives buffer re-alignments. |
| 3564 | */ |
| 3565 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3566 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3567 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3568 | else |
| 3569 | msg->msg_state = HTTP_MSG_DATA; |
| 3570 | } |
| 3571 | |
| 3572 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3573 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3574 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3575 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3576 | */ |
| 3577 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3578 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3579 | if (!ret) |
| 3580 | goto missing_data; |
| 3581 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3582 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3583 | } |
| 3584 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3585 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3586 | * We have the first non-header byte in msg->col, which is either the |
| 3587 | * beginning of the chunk size or of the data. The first data byte is in |
| 3588 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3589 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3590 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3591 | |
| 3592 | if (msg->hdr_content_len < limit) |
| 3593 | limit = msg->hdr_content_len; |
| 3594 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3595 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3596 | goto http_end; |
| 3597 | |
| 3598 | missing_data: |
| 3599 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3600 | if (req->flags & BF_FULL) |
| 3601 | goto return_bad_req; |
| 3602 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3603 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3604 | txn->status = 408; |
| 3605 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3606 | |
| 3607 | if (!(s->flags & SN_ERR_MASK)) |
| 3608 | s->flags |= SN_ERR_CLITO; |
| 3609 | if (!(s->flags & SN_FINST_MASK)) |
| 3610 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3611 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3612 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3613 | |
| 3614 | /* we get here if we need to wait for more data */ |
| 3615 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3616 | /* Not enough data. We'll re-use the http-request |
| 3617 | * timeout here. Ideally, we should set the timeout |
| 3618 | * relative to the accept() date. We just set the |
| 3619 | * request timeout once at the beginning of the |
| 3620 | * request. |
| 3621 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3622 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3623 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3624 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3625 | return 0; |
| 3626 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3627 | |
| 3628 | http_end: |
| 3629 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3630 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3631 | req->analysers &= ~an_bit; |
| 3632 | req->analyse_exp = TICK_ETERNITY; |
| 3633 | return 1; |
| 3634 | |
| 3635 | return_bad_req: /* let's centralize all bad requests */ |
| 3636 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3637 | txn->status = 400; |
| 3638 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3639 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3640 | if (!(s->flags & SN_ERR_MASK)) |
| 3641 | s->flags |= SN_ERR_PRXCOND; |
| 3642 | if (!(s->flags & SN_FINST_MASK)) |
| 3643 | s->flags |= SN_FINST_R; |
| 3644 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3645 | return_err_msg: |
| 3646 | req->analysers = 0; |
| 3647 | s->fe->counters.failed_req++; |
| 3648 | if (s->listener->counters) |
| 3649 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3650 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3651 | } |
| 3652 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3653 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3654 | * right now but it works. |
| 3655 | */ |
| 3656 | void http_end_txn_clean_session(struct session *s) |
| 3657 | { |
| 3658 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3659 | * server's connections. We need a safer way to reinitialize buffer |
| 3660 | * flags. We also need a more accurate method for computing per-request |
| 3661 | * data. |
| 3662 | */ |
| 3663 | http_silent_debug(__LINE__, s); |
| 3664 | |
| 3665 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3666 | s->req->cons->shutr(s->req->cons); |
| 3667 | s->req->cons->shutw(s->req->cons); |
| 3668 | |
| 3669 | http_silent_debug(__LINE__, s); |
| 3670 | |
| 3671 | if (s->flags & SN_BE_ASSIGNED) |
| 3672 | s->be->beconn--; |
| 3673 | |
| 3674 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3675 | session_process_counters(s); |
| 3676 | |
| 3677 | if (s->txn.status) { |
| 3678 | int n; |
| 3679 | |
| 3680 | n = s->txn.status / 100; |
| 3681 | if (n < 1 || n > 5) |
| 3682 | n = 0; |
| 3683 | |
| 3684 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3685 | s->fe->counters.fe.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3686 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3687 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3688 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3689 | s->be->counters.be.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /* don't count other requests' data */ |
| 3693 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3694 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3695 | |
| 3696 | /* let's do a final log if we need it */ |
| 3697 | if (s->logs.logwait && |
| 3698 | !(s->flags & SN_MONITOR) && |
| 3699 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3700 | s->do_log(s); |
| 3701 | } |
| 3702 | |
| 3703 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3704 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3705 | tv_zero(&s->logs.tv_request); |
| 3706 | s->logs.t_queue = -1; |
| 3707 | s->logs.t_connect = -1; |
| 3708 | s->logs.t_data = -1; |
| 3709 | s->logs.t_close = 0; |
| 3710 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3711 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3712 | |
| 3713 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3714 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3715 | |
| 3716 | if (s->pend_pos) |
| 3717 | pendconn_free(s->pend_pos); |
| 3718 | |
| 3719 | if (s->srv) { |
| 3720 | if (s->flags & SN_CURR_SESS) { |
| 3721 | s->flags &= ~SN_CURR_SESS; |
| 3722 | s->srv->cur_sess--; |
| 3723 | } |
| 3724 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3725 | process_srv_queue(s->srv); |
| 3726 | } |
| 3727 | |
| 3728 | if (unlikely(s->srv_conn)) |
| 3729 | sess_change_server(s, NULL); |
| 3730 | s->srv = NULL; |
| 3731 | |
| 3732 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3733 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3734 | s->req->cons->err_type = SI_ET_NONE; |
| 3735 | s->req->cons->err_loc = NULL; |
| 3736 | s->req->cons->exp = TICK_ETERNITY; |
| 3737 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3738 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3739 | 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); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3740 | 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] | 3741 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3742 | s->txn.meth = 0; |
| 3743 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3744 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 3745 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3746 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3747 | |
| 3748 | /* if the request buffer is not empty, it means we're |
| 3749 | * about to process another request, so send pending |
| 3750 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3751 | * Just don't do this if the buffer is close to be full, |
| 3752 | * because the request will wait for it to flush a little |
| 3753 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3754 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3755 | if (s->req->l > s->req->send_max) { |
| 3756 | if (s->rep->send_max && |
| 3757 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3758 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3759 | s->rep->flags |= BF_EXPECT_MORE; |
| 3760 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3761 | |
| 3762 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3763 | buffer_auto_read(s->req); |
| 3764 | buffer_auto_close(s->req); |
| 3765 | buffer_auto_read(s->rep); |
| 3766 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3767 | |
| 3768 | /* make ->lr point to the first non-forwarded byte */ |
| 3769 | s->req->lr = s->req->w + s->req->send_max; |
| 3770 | if (s->req->lr >= s->req->data + s->req->size) |
| 3771 | s->req->lr -= s->req->size; |
| 3772 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3773 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3774 | s->rep->lr -= s->req->size; |
| 3775 | |
| 3776 | s->req->analysers |= s->fe->fe_req_ana; |
| 3777 | s->rep->analysers = 0; |
| 3778 | |
| 3779 | http_silent_debug(__LINE__, s); |
| 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | /* This function updates the request state machine according to the response |
| 3784 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3785 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3786 | * it is only used to find when a request/response couple is complete. Both |
| 3787 | * this function and its equivalent should loop until both return zero. It |
| 3788 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3789 | */ |
| 3790 | int http_sync_req_state(struct session *s) |
| 3791 | { |
| 3792 | struct buffer *buf = s->req; |
| 3793 | struct http_txn *txn = &s->txn; |
| 3794 | unsigned int old_flags = buf->flags; |
| 3795 | unsigned int old_state = txn->req.msg_state; |
| 3796 | |
| 3797 | http_silent_debug(__LINE__, s); |
| 3798 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3799 | return 0; |
| 3800 | |
| 3801 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3802 | /* No need to read anymore, the request was completely parsed. |
| 3803 | * We can shut the read side unless we want to abort_on_close. |
| 3804 | */ |
| 3805 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3806 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3807 | |
| 3808 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3809 | goto wait_other_side; |
| 3810 | |
| 3811 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3812 | /* The server has not finished to respond, so we |
| 3813 | * don't want to move in order not to upset it. |
| 3814 | */ |
| 3815 | goto wait_other_side; |
| 3816 | } |
| 3817 | |
| 3818 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3819 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3820 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3821 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3822 | goto wait_other_side; |
| 3823 | } |
| 3824 | |
| 3825 | /* When we get here, it means that both the request and the |
| 3826 | * response have finished receiving. Depending on the connection |
| 3827 | * mode, we'll have to wait for the last bytes to leave in either |
| 3828 | * direction, and sometimes for a close to be effective. |
| 3829 | */ |
| 3830 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3831 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3832 | /* Server-close mode : queue a connection close to the server */ |
| 3833 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3834 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3835 | } |
| 3836 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3837 | /* Option forceclose is set, or either side wants to close, |
| 3838 | * let's enforce it now that we're not expecting any new |
| 3839 | * data to come. The caller knows the session is complete |
| 3840 | * once both states are CLOSED. |
| 3841 | */ |
| 3842 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3843 | buffer_shutr_now(buf); |
| 3844 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3845 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3846 | } |
| 3847 | else { |
| 3848 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3849 | * mode does not set the body analyser, we can't reach this place |
| 3850 | * in tunnel mode, so we're left with keep-alive only. |
| 3851 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3852 | */ |
| 3853 | buffer_auto_read(buf); |
| 3854 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3855 | } |
| 3856 | |
| 3857 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3858 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3859 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3860 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3861 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3862 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3863 | goto http_msg_closing; |
| 3864 | } |
| 3865 | else { |
| 3866 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3867 | goto http_msg_closed; |
| 3868 | } |
| 3869 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3870 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3874 | http_msg_closing: |
| 3875 | /* nothing else to forward, just waiting for the output buffer |
| 3876 | * to be empty and for the shutw_now to take effect. |
| 3877 | */ |
| 3878 | if (buf->flags & BF_OUT_EMPTY) { |
| 3879 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3880 | goto http_msg_closed; |
| 3881 | } |
| 3882 | else if (buf->flags & BF_SHUTW) { |
| 3883 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3884 | goto wait_other_side; |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3889 | http_msg_closed: |
| 3890 | goto wait_other_side; |
| 3891 | } |
| 3892 | |
| 3893 | wait_other_side: |
| 3894 | http_silent_debug(__LINE__, s); |
| 3895 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3896 | } |
| 3897 | |
| 3898 | |
| 3899 | /* This function updates the response state machine according to the request |
| 3900 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3901 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3902 | * it is only used to find when a request/response couple is complete. Both |
| 3903 | * this function and its equivalent should loop until both return zero. It |
| 3904 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3905 | */ |
| 3906 | int http_sync_res_state(struct session *s) |
| 3907 | { |
| 3908 | struct buffer *buf = s->rep; |
| 3909 | struct http_txn *txn = &s->txn; |
| 3910 | unsigned int old_flags = buf->flags; |
| 3911 | unsigned int old_state = txn->rsp.msg_state; |
| 3912 | |
| 3913 | http_silent_debug(__LINE__, s); |
| 3914 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3915 | return 0; |
| 3916 | |
| 3917 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3918 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3919 | * still monitor the server connection for a possible close |
| 3920 | * while the request is being uploaded, so we don't disable |
| 3921 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3922 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3923 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3924 | |
| 3925 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3926 | goto wait_other_side; |
| 3927 | |
| 3928 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3929 | /* The client seems to still be sending data, probably |
| 3930 | * because we got an error response during an upload. |
| 3931 | * We have the choice of either breaking the connection |
| 3932 | * or letting it pass through. Let's do the later. |
| 3933 | */ |
| 3934 | goto wait_other_side; |
| 3935 | } |
| 3936 | |
| 3937 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3938 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3939 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3940 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3941 | goto wait_other_side; |
| 3942 | } |
| 3943 | |
| 3944 | /* When we get here, it means that both the request and the |
| 3945 | * response have finished receiving. Depending on the connection |
| 3946 | * mode, we'll have to wait for the last bytes to leave in either |
| 3947 | * direction, and sometimes for a close to be effective. |
| 3948 | */ |
| 3949 | |
| 3950 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3951 | /* Server-close mode : shut read and wait for the request |
| 3952 | * side to close its output buffer. The caller will detect |
| 3953 | * when we're in DONE and the other is in CLOSED and will |
| 3954 | * catch that for the final cleanup. |
| 3955 | */ |
| 3956 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3957 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3958 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3959 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3960 | /* Option forceclose is set, or either side wants to close, |
| 3961 | * let's enforce it now that we're not expecting any new |
| 3962 | * data to come. The caller knows the session is complete |
| 3963 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3964 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3965 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3966 | buffer_shutr_now(buf); |
| 3967 | buffer_shutw_now(buf); |
| 3968 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3969 | } |
| 3970 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3971 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3972 | * mode does not set the body analyser, we can't reach this place |
| 3973 | * in tunnel mode, so we're left with keep-alive only. |
| 3974 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3975 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3976 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3977 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3981 | /* if we've just closed an output, let's switch */ |
| 3982 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3983 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3984 | goto http_msg_closing; |
| 3985 | } |
| 3986 | else { |
| 3987 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3988 | goto http_msg_closed; |
| 3989 | } |
| 3990 | } |
| 3991 | goto wait_other_side; |
| 3992 | } |
| 3993 | |
| 3994 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3995 | http_msg_closing: |
| 3996 | /* nothing else to forward, just waiting for the output buffer |
| 3997 | * to be empty and for the shutw_now to take effect. |
| 3998 | */ |
| 3999 | if (buf->flags & BF_OUT_EMPTY) { |
| 4000 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4001 | goto http_msg_closed; |
| 4002 | } |
| 4003 | else if (buf->flags & BF_SHUTW) { |
| 4004 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 4005 | s->be->counters.cli_aborts++; |
| 4006 | if (s->srv) |
| 4007 | s->srv->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4008 | goto wait_other_side; |
| 4009 | } |
| 4010 | } |
| 4011 | |
| 4012 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4013 | http_msg_closed: |
| 4014 | /* drop any pending data */ |
| 4015 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4016 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4017 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4018 | goto wait_other_side; |
| 4019 | } |
| 4020 | |
| 4021 | wait_other_side: |
| 4022 | http_silent_debug(__LINE__, s); |
| 4023 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4024 | } |
| 4025 | |
| 4026 | |
| 4027 | /* Resync the request and response state machines. Return 1 if either state |
| 4028 | * changes. |
| 4029 | */ |
| 4030 | int http_resync_states(struct session *s) |
| 4031 | { |
| 4032 | struct http_txn *txn = &s->txn; |
| 4033 | int old_req_state = txn->req.msg_state; |
| 4034 | int old_res_state = txn->rsp.msg_state; |
| 4035 | |
| 4036 | http_silent_debug(__LINE__, s); |
| 4037 | http_sync_req_state(s); |
| 4038 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4039 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4040 | if (!http_sync_res_state(s)) |
| 4041 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4042 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4043 | if (!http_sync_req_state(s)) |
| 4044 | break; |
| 4045 | } |
| 4046 | http_silent_debug(__LINE__, s); |
| 4047 | /* OK, both state machines agree on a compatible state. |
| 4048 | * There are a few cases we're interested in : |
| 4049 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4050 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4051 | * directions, so let's simply disable both analysers. |
| 4052 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4053 | * request. |
| 4054 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4055 | * with server-close mode means we've completed one request and we |
| 4056 | * must re-initialize the server connection. |
| 4057 | */ |
| 4058 | |
| 4059 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4060 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4061 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4062 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4063 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4064 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4065 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4066 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4067 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4068 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4069 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4070 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4071 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4072 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4073 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4074 | s->rep->analysers = 0; |
| 4075 | buffer_auto_close(s->rep); |
| 4076 | buffer_auto_read(s->rep); |
| 4077 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4078 | buffer_abort(s->req); |
| 4079 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4080 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4081 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4082 | } |
| 4083 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4084 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4085 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4086 | /* server-close: terminate this server connection and |
| 4087 | * reinitialize a fresh-new transaction. |
| 4088 | */ |
| 4089 | http_end_txn_clean_session(s); |
| 4090 | } |
| 4091 | |
| 4092 | http_silent_debug(__LINE__, s); |
| 4093 | return txn->req.msg_state != old_req_state || |
| 4094 | txn->rsp.msg_state != old_res_state; |
| 4095 | } |
| 4096 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4097 | /* This function is an analyser which forwards request body (including chunk |
| 4098 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4099 | * zero byte. The only situation where it must not be called is when we're in |
| 4100 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4101 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4102 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4103 | * read more data, or 1 once we can go on with next request or end the session. |
| 4104 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4105 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4106 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4107 | */ |
| 4108 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4109 | { |
| 4110 | struct http_txn *txn = &s->txn; |
| 4111 | struct http_msg *msg = &s->txn.req; |
| 4112 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4113 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4114 | return 0; |
| 4115 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4116 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4117 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4118 | /* Output closed while we were sending data. We must abort and |
| 4119 | * wake the other side up. |
| 4120 | */ |
| 4121 | msg->msg_state = HTTP_MSG_ERROR; |
| 4122 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4123 | return 1; |
| 4124 | } |
| 4125 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4126 | /* in most states, we should abort in case of early close */ |
| 4127 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4128 | |
| 4129 | /* Note that we don't have to send 100-continue back because we don't |
| 4130 | * need the data to complete our job, and it's up to the server to |
| 4131 | * decide whether to return 100, 417 or anything else in return of |
| 4132 | * an "Expect: 100-continue" header. |
| 4133 | */ |
| 4134 | |
| 4135 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4136 | /* we have msg->col and msg->sov which both point to the first |
| 4137 | * byte of message body. msg->som still points to the beginning |
| 4138 | * of the message. We must save the body in req->lr because it |
| 4139 | * survives buffer re-alignments. |
| 4140 | */ |
| 4141 | req->lr = req->data + msg->sov; |
| 4142 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4143 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4144 | else { |
| 4145 | msg->msg_state = HTTP_MSG_DATA; |
| 4146 | } |
| 4147 | } |
| 4148 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4149 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4150 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4151 | /* we may have some data pending */ |
| 4152 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4153 | int bytes = msg->sov - msg->som; |
| 4154 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4155 | bytes += req->size; |
| 4156 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 4157 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4158 | msg->som = msg->sov; |
| 4159 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4160 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4161 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4162 | /* must still forward */ |
| 4163 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4164 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4165 | |
| 4166 | /* nothing left to forward */ |
| 4167 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4168 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4169 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4170 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4171 | } |
| 4172 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4173 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4174 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4175 | * TRAILERS state. |
| 4176 | */ |
| 4177 | int ret = http_parse_chunk_size(req, msg); |
| 4178 | |
| 4179 | if (!ret) |
| 4180 | goto missing_data; |
| 4181 | else if (ret < 0) |
| 4182 | goto return_bad_req; |
| 4183 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4184 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4185 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4186 | /* we want the CRLF after the data */ |
| 4187 | int ret; |
| 4188 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4189 | req->lr = req->w + req->send_max; |
| 4190 | if (req->lr >= req->data + req->size) |
| 4191 | req->lr -= req->size; |
| 4192 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4193 | ret = http_skip_chunk_crlf(req, msg); |
| 4194 | |
| 4195 | if (ret == 0) |
| 4196 | goto missing_data; |
| 4197 | else if (ret < 0) |
| 4198 | goto return_bad_req; |
| 4199 | /* we're in MSG_CHUNK_SIZE now */ |
| 4200 | } |
| 4201 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4202 | int ret = http_forward_trailers(req, msg); |
| 4203 | |
| 4204 | if (ret == 0) |
| 4205 | goto missing_data; |
| 4206 | else if (ret < 0) |
| 4207 | goto return_bad_req; |
| 4208 | /* we're in HTTP_MSG_DONE now */ |
| 4209 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4210 | else { |
| 4211 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4212 | /* for keep-alive we don't want to forward closes on DONE */ |
| 4213 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4214 | if (http_resync_states(s)) { |
| 4215 | /* some state changes occurred, maybe the analyser |
| 4216 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4217 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4218 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4219 | goto return_bad_req; |
| 4220 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4221 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4222 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4223 | } |
| 4224 | } |
| 4225 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4226 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4227 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4228 | if (req->flags & BF_SHUTR) { |
| 4229 | if (!(s->flags & SN_ERR_MASK)) |
| 4230 | s->flags |= SN_ERR_CLICL; |
| 4231 | if (!(s->flags & SN_FINST_MASK)) |
| 4232 | s->flags |= SN_FINST_D; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4233 | goto return_bad_req; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4234 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4235 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4236 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4237 | if (req->flags & BF_SHUTW) { |
| 4238 | if (!(s->flags & SN_ERR_MASK)) |
| 4239 | s->flags |= SN_ERR_SRVCL; |
| 4240 | if (!(s->flags & SN_FINST_MASK)) |
| 4241 | s->flags |= SN_FINST_D; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4242 | goto return_bad_req; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4243 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4244 | |
| 4245 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4246 | return 0; |
| 4247 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4248 | return_bad_req: /* let's centralize all bad requests */ |
| 4249 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4250 | txn->status = 400; |
| 4251 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 4252 | stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4253 | req->analysers = 0; |
| 4254 | s->fe->counters.failed_req++; |
| 4255 | if (s->listener->counters) |
| 4256 | s->listener->counters->failed_req++; |
| 4257 | |
| 4258 | if (!(s->flags & SN_ERR_MASK)) |
| 4259 | s->flags |= SN_ERR_PRXCOND; |
| 4260 | if (!(s->flags & SN_FINST_MASK)) |
| 4261 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4262 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4263 | return 0; |
| 4264 | } |
| 4265 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4266 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4267 | * processing can continue on next analysers, or zero if it either needs more |
| 4268 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4269 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4270 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4271 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4272 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4273 | 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] | 4274 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4275 | struct http_txn *txn = &s->txn; |
| 4276 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4277 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4278 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4279 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4280 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4281 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4282 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4283 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4284 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4285 | rep, |
| 4286 | rep->rex, rep->wex, |
| 4287 | rep->flags, |
| 4288 | rep->l, |
| 4289 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4290 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4291 | /* |
| 4292 | * Now parse the partial (or complete) lines. |
| 4293 | * We will check the response syntax, and also join multi-line |
| 4294 | * headers. An index of all the lines will be elaborated while |
| 4295 | * parsing. |
| 4296 | * |
| 4297 | * For the parsing, we use a 28 states FSM. |
| 4298 | * |
| 4299 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4300 | * rep->data + msg->som = beginning of response |
| 4301 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4302 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4303 | * rep->lr = first non-visited byte |
| 4304 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4305 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4306 | */ |
| 4307 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4308 | /* There's a protected area at the end of the buffer for rewriting |
| 4309 | * purposes. We don't want to start to parse the request if the |
| 4310 | * protected area is affected, because we may have to move processed |
| 4311 | * data later, which is much more complicated. |
| 4312 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4313 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4314 | if (unlikely((rep->flags & BF_FULL) || |
| 4315 | rep->r < rep->lr || |
| 4316 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4317 | if (rep->send_max) { |
| 4318 | /* 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] | 4319 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4320 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4321 | buffer_dont_close(rep); |
| 4322 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4323 | return 0; |
| 4324 | } |
| 4325 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4326 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4327 | } |
| 4328 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4329 | if (likely(rep->lr < rep->r)) |
| 4330 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4331 | } |
| 4332 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4333 | /* 1: we might have to print this header in debug mode */ |
| 4334 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4335 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4336 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4337 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4338 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4339 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4340 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4341 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4342 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4343 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4344 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4345 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4346 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4347 | while (cur_idx) { |
| 4348 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4349 | debug_hdr("srvhdr", s, sol, eol); |
| 4350 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4351 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4352 | } |
| 4353 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4354 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4355 | /* |
| 4356 | * Now we quickly check if we have found a full valid response. |
| 4357 | * If not so, we check the FD and buffer states before leaving. |
| 4358 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4359 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4360 | * responses are checked first. |
| 4361 | * |
| 4362 | * Depending on whether the client is still there or not, we |
| 4363 | * may send an error response back or not. Note that normally |
| 4364 | * we should only check for HTTP status there, and check I/O |
| 4365 | * errors somewhere else. |
| 4366 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4367 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4368 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4369 | /* Invalid response */ |
| 4370 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4371 | /* we detected a parsing error. We want to archive this response |
| 4372 | * in the dedicated proxy area for later troubleshooting. |
| 4373 | */ |
| 4374 | hdr_response_bad: |
| 4375 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4376 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4377 | |
| 4378 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4379 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4380 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4381 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4382 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4383 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4384 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4385 | rep->analysers = 0; |
| 4386 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4387 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4388 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4389 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4390 | |
| 4391 | if (!(s->flags & SN_ERR_MASK)) |
| 4392 | s->flags |= SN_ERR_PRXCOND; |
| 4393 | if (!(s->flags & SN_FINST_MASK)) |
| 4394 | s->flags |= SN_FINST_H; |
| 4395 | |
| 4396 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4397 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4398 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4399 | /* too large response does not fit in buffer. */ |
| 4400 | else if (rep->flags & BF_FULL) { |
| 4401 | goto hdr_response_bad; |
| 4402 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4403 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4404 | /* read error */ |
| 4405 | else if (rep->flags & BF_READ_ERROR) { |
| 4406 | if (msg->err_pos >= 0) |
| 4407 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4408 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4409 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4410 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4411 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4412 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4413 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4414 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4415 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4416 | rep->analysers = 0; |
| 4417 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4418 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4419 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4420 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4421 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4422 | if (!(s->flags & SN_ERR_MASK)) |
| 4423 | s->flags |= SN_ERR_SRVCL; |
| 4424 | if (!(s->flags & SN_FINST_MASK)) |
| 4425 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4426 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4427 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4428 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4429 | /* read timeout : return a 504 to the client. */ |
| 4430 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4431 | if (msg->err_pos >= 0) |
| 4432 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4433 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4434 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4435 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4436 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4437 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4438 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4439 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4440 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4441 | rep->analysers = 0; |
| 4442 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4443 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4444 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4445 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4446 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4447 | if (!(s->flags & SN_ERR_MASK)) |
| 4448 | s->flags |= SN_ERR_SRVTO; |
| 4449 | if (!(s->flags & SN_FINST_MASK)) |
| 4450 | s->flags |= SN_FINST_H; |
| 4451 | return 0; |
| 4452 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4453 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4454 | /* close from server */ |
| 4455 | else if (rep->flags & BF_SHUTR) { |
| 4456 | if (msg->err_pos >= 0) |
| 4457 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4458 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4459 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4460 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4461 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4462 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4463 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4464 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4465 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4466 | rep->analysers = 0; |
| 4467 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4468 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4469 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4470 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4471 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4472 | if (!(s->flags & SN_ERR_MASK)) |
| 4473 | s->flags |= SN_ERR_SRVCL; |
| 4474 | if (!(s->flags & SN_FINST_MASK)) |
| 4475 | s->flags |= SN_FINST_H; |
| 4476 | return 0; |
| 4477 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4478 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4479 | /* write error to client (we don't send any message then) */ |
| 4480 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4481 | if (msg->err_pos >= 0) |
| 4482 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4483 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4484 | s->be->counters.failed_resp++; |
| 4485 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4486 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4487 | |
| 4488 | if (!(s->flags & SN_ERR_MASK)) |
| 4489 | s->flags |= SN_ERR_CLICL; |
| 4490 | if (!(s->flags & SN_FINST_MASK)) |
| 4491 | s->flags |= SN_FINST_H; |
| 4492 | |
| 4493 | /* process_session() will take care of the error */ |
| 4494 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4495 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4496 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4497 | buffer_dont_close(rep); |
| 4498 | return 0; |
| 4499 | } |
| 4500 | |
| 4501 | /* More interesting part now : we know that we have a complete |
| 4502 | * response which at least looks like HTTP. We have an indicator |
| 4503 | * of each header's length, so we can parse them quickly. |
| 4504 | */ |
| 4505 | |
| 4506 | if (unlikely(msg->err_pos >= 0)) |
| 4507 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4508 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4509 | /* |
| 4510 | * 1: get the status code |
| 4511 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4512 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4513 | if (n < 1 || n > 5) |
| 4514 | n = 0; |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4515 | if (s->srv) |
| 4516 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4517 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4518 | /* check if the response is HTTP/1.1 or above */ |
| 4519 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4520 | ((msg->sol[5] > '1') || |
| 4521 | ((msg->sol[5] == '1') && |
| 4522 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4523 | txn->flags |= TX_RES_VER_11; |
| 4524 | |
| 4525 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4526 | 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] | 4527 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4528 | /* transfer length unknown*/ |
| 4529 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4530 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4531 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4532 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4533 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4534 | * and 505 are triggered on demand by client request, so we must not |
| 4535 | * count them as server failures. |
| 4536 | */ |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4537 | if (s->srv) { |
| 4538 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
| 4539 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4540 | else |
| 4541 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4542 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4543 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4544 | /* |
| 4545 | * 2: check for cacheability. |
| 4546 | */ |
| 4547 | |
| 4548 | switch (txn->status) { |
| 4549 | case 200: |
| 4550 | case 203: |
| 4551 | case 206: |
| 4552 | case 300: |
| 4553 | case 301: |
| 4554 | case 410: |
| 4555 | /* RFC2616 @13.4: |
| 4556 | * "A response received with a status code of |
| 4557 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4558 | * by a cache (...) unless a cache-control |
| 4559 | * directive prohibits caching." |
| 4560 | * |
| 4561 | * RFC2616 @9.5: POST method : |
| 4562 | * "Responses to this method are not cacheable, |
| 4563 | * unless the response includes appropriate |
| 4564 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4565 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4566 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4567 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4568 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4569 | break; |
| 4570 | default: |
| 4571 | break; |
| 4572 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4573 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4574 | /* |
| 4575 | * 3: we may need to capture headers |
| 4576 | */ |
| 4577 | s->logs.logwait &= ~LW_RESP; |
| 4578 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4579 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4580 | txn->rsp.cap, s->fe->rsp_cap); |
| 4581 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4582 | /* 4: determine the transfer-length. |
| 4583 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4584 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4585 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4586 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4587 | * All responses to the HEAD request method MUST NOT include a |
| 4588 | * message-body, even though the presence of entity-header fields |
| 4589 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4590 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4591 | * message-body. All other responses do include a message-body, |
| 4592 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4593 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4594 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4595 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4596 | * always terminated by the first empty line after the header fields, |
| 4597 | * regardless of the entity-header fields present in the message. |
| 4598 | * |
| 4599 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4600 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4601 | * transfer-length is defined by the use of this transfer-coding. |
| 4602 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4603 | * transfer-coding is not present, the transfer-length is defined by |
| 4604 | * the sender closing the connection. |
| 4605 | * |
| 4606 | * 3. If a Content-Length header field is present, its decimal value in |
| 4607 | * OCTETs represents both the entity-length and the transfer-length. |
| 4608 | * If a message is received with both a Transfer-Encoding header |
| 4609 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4610 | * |
| 4611 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4612 | * the transfer-length is not otherwise specified, then this self- |
| 4613 | * delimiting media type defines the transfer-length. This media |
| 4614 | * type MUST NOT be used unless the sender knows that the recipient |
| 4615 | * can parse it; the presence in a request of a Range header with |
| 4616 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4617 | * client can parse multipart/byteranges responses. |
| 4618 | * |
| 4619 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4620 | */ |
| 4621 | |
| 4622 | /* Skip parsing if no content length is possible. The response flags |
| 4623 | * remain 0 as well as the hdr_content_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4624 | * 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] | 4625 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4626 | */ |
| 4627 | if (txn->meth == HTTP_METH_HEAD || |
| 4628 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4629 | txn->status == 204 || txn->status == 304) { |
| 4630 | txn->flags |= TX_RES_XFER_LEN; |
| 4631 | goto skip_content_length; |
| 4632 | } |
| 4633 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4634 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4635 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4636 | while ((txn->flags & TX_RES_VER_11) && |
| 4637 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4638 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4639 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4640 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4641 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4642 | use_close_only = 1; |
| 4643 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4644 | break; |
| 4645 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4649 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4650 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4651 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4652 | signed long long cl; |
| 4653 | |
| 4654 | if (!ctx.vlen) |
| 4655 | goto hdr_response_bad; |
| 4656 | |
| 4657 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4658 | goto hdr_response_bad; /* parse failure */ |
| 4659 | |
| 4660 | if (cl < 0) |
| 4661 | goto hdr_response_bad; |
| 4662 | |
| 4663 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4664 | goto hdr_response_bad; /* already specified, was different */ |
| 4665 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4666 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4667 | msg->hdr_content_len = cl; |
| 4668 | } |
| 4669 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4670 | /* FIXME: we should also implement the multipart/byterange method. |
| 4671 | * For now on, we resort to close mode in this case (unknown length). |
| 4672 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4673 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4674 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4675 | /* end of job, return OK */ |
| 4676 | rep->analysers &= ~an_bit; |
| 4677 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4678 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4679 | return 1; |
| 4680 | } |
| 4681 | |
| 4682 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4683 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4684 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4685 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4686 | */ |
| 4687 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4688 | { |
| 4689 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4690 | struct http_msg *msg = &txn->rsp; |
| 4691 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4692 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4693 | |
| 4694 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4695 | now_ms, __FUNCTION__, |
| 4696 | t, |
| 4697 | rep, |
| 4698 | rep->rex, rep->wex, |
| 4699 | rep->flags, |
| 4700 | rep->l, |
| 4701 | rep->analysers); |
| 4702 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4703 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4704 | return 0; |
| 4705 | |
| 4706 | rep->analysers &= ~an_bit; |
| 4707 | rep->analyse_exp = TICK_ETERNITY; |
| 4708 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4709 | /* Now we have to check if we need to modify the Connection header. |
| 4710 | * This is more difficult on the response than it is on the request, |
| 4711 | * because we can have two different HTTP versions and we don't know |
| 4712 | * how the client will interprete a response. For instance, let's say |
| 4713 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4714 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4715 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4716 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4717 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4718 | * indicating how a request MAY be understood by the client. In case |
| 4719 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4720 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4721 | * will fall back to explicit close. Note that we won't take risks with |
| 4722 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4723 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4724 | */ |
| 4725 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4726 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4727 | txn->status == 101)) { |
| 4728 | /* Either we've established an explicit tunnel, or we're |
| 4729 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4730 | * to understand the next protocols. We have to switch to tunnel |
| 4731 | * mode, so that we transfer the request and responses then let |
| 4732 | * this protocol pass unmodified. When we later implement specific |
| 4733 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4734 | * header which contains information about that protocol for |
| 4735 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4736 | */ |
| 4737 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4738 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4739 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4740 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4741 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4742 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4743 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4744 | /* on unknown transfer length, we must close */ |
| 4745 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4746 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4747 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4748 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4749 | /* now adjust header transformations depending on current state */ |
| 4750 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4751 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4752 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4753 | if (!(txn->flags & TX_RES_VER_11)) |
| 4754 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4755 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4756 | else { /* SCL / KAL */ |
| 4757 | to_del |= 1; /* remove "close" on any response */ |
| 4758 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4759 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4760 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4761 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4762 | /* Parse and remove some headers from the connection header */ |
| 4763 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4764 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4765 | /* Some keep-alive responses are converted to Server-close if |
| 4766 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4767 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4768 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4769 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4770 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4771 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4772 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4773 | } |
| 4774 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4775 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4776 | /* |
| 4777 | * 3: we will have to evaluate the filters. |
| 4778 | * As opposed to version 1.2, now they will be evaluated in the |
| 4779 | * filters order and not in the header order. This means that |
| 4780 | * each filter has to be validated among all headers. |
| 4781 | * |
| 4782 | * Filters are tried with ->be first, then with ->fe if it is |
| 4783 | * different from ->be. |
| 4784 | */ |
| 4785 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4786 | cur_proxy = t->be; |
| 4787 | while (1) { |
| 4788 | struct proxy *rule_set = cur_proxy; |
| 4789 | |
| 4790 | /* try headers filters */ |
| 4791 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4792 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4793 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4794 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4795 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4796 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4797 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4798 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4799 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4800 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4801 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4802 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4803 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4804 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4805 | if (!(t->flags & SN_ERR_MASK)) |
| 4806 | t->flags |= SN_ERR_PRXCOND; |
| 4807 | if (!(t->flags & SN_FINST_MASK)) |
| 4808 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4809 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4810 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4811 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4812 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4813 | /* has the response been denied ? */ |
| 4814 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4815 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4816 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4817 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4818 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4819 | if (t->listener->counters) |
| 4820 | t->listener->counters->denied_resp++; |
| 4821 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4822 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4823 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4824 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4825 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4826 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4827 | if (txn->status < 200) |
| 4828 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4829 | if (wl->cond) { |
| 4830 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 4831 | ret = acl_pass(ret); |
| 4832 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4833 | ret = !ret; |
| 4834 | if (!ret) |
| 4835 | continue; |
| 4836 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4837 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4838 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4839 | } |
| 4840 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4841 | /* check whether we're already working on the frontend */ |
| 4842 | if (cur_proxy == t->fe) |
| 4843 | break; |
| 4844 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4845 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4846 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4847 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4848 | * We may be facing a 100-continue response, in which case this |
| 4849 | * is not the right response, and we're waiting for the next one. |
| 4850 | * 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] | 4851 | * next one. |
| 4852 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4853 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4854 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4855 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4856 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4857 | txn->status = 0; |
| 4858 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4859 | return 1; |
| 4860 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4861 | else if (unlikely(txn->status < 200)) |
| 4862 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4863 | |
| 4864 | /* we don't have any 1xx status code now */ |
| 4865 | |
| 4866 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4867 | * 4: check for server cookie. |
| 4868 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4869 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4870 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4871 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4872 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4873 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4874 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4875 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4876 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4877 | 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] | 4878 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4879 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4880 | /* |
| 4881 | * 6: add server cookie in the response if needed |
| 4882 | */ |
| 4883 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 4884 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 4885 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4886 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4887 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4888 | /* the server is known, it's not the one the client requested, we have to |
| 4889 | * insert a set-cookie here, except if we want to insert only on POST |
| 4890 | * requests and this one isn't. Note that servers which don't have cookies |
| 4891 | * (eg: some backup servers) will return a full cookie removal request. |
| 4892 | */ |
| 4893 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4894 | t->be->cookie_name, |
| 4895 | t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4896 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4897 | if (t->be->cookie_domain) |
| 4898 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4899 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4900 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4901 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4902 | goto return_bad_resp; |
| 4903 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4904 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4905 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4906 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4907 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4908 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4909 | */ |
| 4910 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4911 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4912 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4913 | |
| 4914 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4915 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4916 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | } |
| 4918 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4919 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4920 | /* |
| 4921 | * 7: check if result will be cacheable with a cookie. |
| 4922 | * We'll block the response if security checks have caught |
| 4923 | * nasty things such as a cacheable cookie. |
| 4924 | */ |
| 4925 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4926 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4927 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4928 | |
| 4929 | /* we're in presence of a cacheable response containing |
| 4930 | * a set-cookie header. We'll block it as requested by |
| 4931 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4932 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4933 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4934 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4935 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4936 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4937 | if (t->listener->counters) |
| 4938 | t->listener->counters->denied_resp++; |
| 4939 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4940 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4941 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4942 | send_log(t->be, LOG_ALERT, |
| 4943 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4944 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4945 | goto return_srv_prx_502; |
| 4946 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4947 | |
| 4948 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4949 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4950 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4951 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4952 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 4953 | unsigned int want_flags = 0; |
| 4954 | |
| 4955 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4956 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4957 | /* we want a keep-alive response here. Keep-alive header |
| 4958 | * required if either side is not 1.1. |
| 4959 | */ |
| 4960 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 4961 | want_flags |= TX_CON_KAL_SET; |
| 4962 | } |
| 4963 | else { |
| 4964 | /* we want a close response here. Close header required if |
| 4965 | * the server is 1.1, regardless of the client. |
| 4966 | */ |
| 4967 | if (txn->flags & TX_RES_VER_11) |
| 4968 | want_flags |= TX_CON_CLO_SET; |
| 4969 | } |
| 4970 | |
| 4971 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 4972 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4973 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4974 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4975 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4976 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 4977 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4978 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4979 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4980 | /************************************************************* |
| 4981 | * OK, that's finished for the headers. We have done what we * |
| 4982 | * could. Let's switch to the DATA state. * |
| 4983 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4984 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4985 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4986 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4987 | /* if the user wants to log as soon as possible, without counting |
| 4988 | * bytes from the server, then this is the right moment. We have |
| 4989 | * to temporarily assign bytes_out to log what we currently have. |
| 4990 | */ |
| 4991 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4992 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4993 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4994 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4995 | t->logs.bytes_out = 0; |
| 4996 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4998 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4999 | * otherwise we would not let the client side wake up. |
| 5000 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5001 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5002 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5003 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5004 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5005 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5006 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5007 | /* This function is an analyser which forwards response body (including chunk |
| 5008 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5009 | * zero byte. The only situation where it must not be called is when we're in |
| 5010 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5011 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5012 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5013 | * read more data, or 1 once we can go on with next request or end the session. |
| 5014 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 5015 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5016 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5017 | */ |
| 5018 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5019 | { |
| 5020 | struct http_txn *txn = &s->txn; |
| 5021 | struct http_msg *msg = &s->txn.rsp; |
| 5022 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5023 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5024 | return 0; |
| 5025 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5026 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5027 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5028 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5029 | /* Output closed while we were sending data. We must abort and |
| 5030 | * wake the other side up. |
| 5031 | */ |
| 5032 | msg->msg_state = HTTP_MSG_ERROR; |
| 5033 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5034 | return 1; |
| 5035 | } |
| 5036 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5037 | /* in most states, we should abort in case of early close */ |
| 5038 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5039 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5040 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5041 | /* we have msg->col and msg->sov which both point to the first |
| 5042 | * byte of message body. msg->som still points to the beginning |
| 5043 | * of the message. We must save the body in req->lr because it |
| 5044 | * survives buffer re-alignments. |
| 5045 | */ |
| 5046 | res->lr = res->data + msg->sov; |
| 5047 | if (txn->flags & TX_RES_TE_CHNK) |
| 5048 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5049 | else { |
| 5050 | msg->msg_state = HTTP_MSG_DATA; |
| 5051 | } |
| 5052 | } |
| 5053 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5054 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5055 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5056 | /* we may have some data pending */ |
| 5057 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5058 | int bytes = msg->sov - msg->som; |
| 5059 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 5060 | bytes += res->size; |
| 5061 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 5062 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5063 | msg->som = msg->sov; |
| 5064 | } |
| 5065 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5066 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5067 | /* must still forward */ |
| 5068 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5069 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5070 | |
| 5071 | /* nothing left to forward */ |
| 5072 | if (txn->flags & TX_RES_TE_CHNK) |
| 5073 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5074 | else |
| 5075 | msg->msg_state = HTTP_MSG_DONE; |
| 5076 | } |
| 5077 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5078 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 5079 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5080 | */ |
| 5081 | int ret = http_parse_chunk_size(res, msg); |
| 5082 | |
| 5083 | if (!ret) |
| 5084 | goto missing_data; |
| 5085 | else if (ret < 0) |
| 5086 | goto return_bad_res; |
| 5087 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5088 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5089 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5090 | /* we want the CRLF after the data */ |
| 5091 | int ret; |
| 5092 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5093 | res->lr = res->w + res->send_max; |
| 5094 | if (res->lr >= res->data + res->size) |
| 5095 | res->lr -= res->size; |
| 5096 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5097 | ret = http_skip_chunk_crlf(res, msg); |
| 5098 | |
| 5099 | if (!ret) |
| 5100 | goto missing_data; |
| 5101 | else if (ret < 0) |
| 5102 | goto return_bad_res; |
| 5103 | /* we're in MSG_CHUNK_SIZE now */ |
| 5104 | } |
| 5105 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5106 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5107 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5108 | if (ret == 0) |
| 5109 | goto missing_data; |
| 5110 | else if (ret < 0) |
| 5111 | goto return_bad_res; |
| 5112 | /* we're in HTTP_MSG_DONE now */ |
| 5113 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5114 | else { |
| 5115 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5116 | /* for keep-alive we don't want to forward closes on DONE */ |
| 5117 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5118 | if (http_resync_states(s)) { |
| 5119 | http_silent_debug(__LINE__, s); |
| 5120 | /* some state changes occurred, maybe the analyser |
| 5121 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5122 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5123 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 5124 | goto return_bad_res; |
| 5125 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5126 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5127 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5128 | } |
| 5129 | } |
| 5130 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5131 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5132 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5133 | if (res->flags & BF_SHUTR) { |
| 5134 | if (!(s->flags & SN_ERR_MASK)) |
| 5135 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | ae52678 | 2010-03-04 20:34:23 +0100 | [diff] [blame] | 5136 | s->be->counters.srv_aborts++; |
| 5137 | if (s->srv) |
| 5138 | s->srv->counters.srv_aborts++; |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5139 | goto return_bad_res; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5140 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5141 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5142 | /* 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] | 5143 | if (!s->req->analysers) |
| 5144 | goto return_bad_res; |
| 5145 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5146 | /* forward the chunk size as well as any pending data */ |
| 5147 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5148 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 5149 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5150 | msg->som = msg->sov; |
| 5151 | } |
| 5152 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5153 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5154 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5155 | return 0; |
| 5156 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5157 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5158 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5159 | /* don't send any error message as we're in the body */ |
| 5160 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5161 | res->analysers = 0; |
| 5162 | s->be->counters.failed_resp++; |
| 5163 | if (s->srv) { |
| 5164 | s->srv->counters.failed_resp++; |
| 5165 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 5166 | } |
| 5167 | |
| 5168 | if (!(s->flags & SN_ERR_MASK)) |
| 5169 | s->flags |= SN_ERR_PRXCOND; |
| 5170 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5171 | s->flags |= SN_FINST_D; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5172 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5173 | return 0; |
| 5174 | } |
| 5175 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5176 | /* Iterate the same filter through all request headers. |
| 5177 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5178 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5179 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5180 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5181 | 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] | 5182 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5183 | char term; |
| 5184 | char *cur_ptr, *cur_end, *cur_next; |
| 5185 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5186 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5187 | struct hdr_idx_elem *cur_hdr; |
| 5188 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5190 | last_hdr = 0; |
| 5191 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5192 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5193 | old_idx = 0; |
| 5194 | |
| 5195 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5196 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5197 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5198 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5199 | (exp->action == ACT_ALLOW || |
| 5200 | exp->action == ACT_DENY || |
| 5201 | exp->action == ACT_TARPIT)) |
| 5202 | return 0; |
| 5203 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5204 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5205 | if (!cur_idx) |
| 5206 | break; |
| 5207 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5208 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5209 | cur_ptr = cur_next; |
| 5210 | cur_end = cur_ptr + cur_hdr->len; |
| 5211 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5212 | |
| 5213 | /* Now we have one header between cur_ptr and cur_end, |
| 5214 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5215 | */ |
| 5216 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5217 | /* The annoying part is that pattern matching needs |
| 5218 | * that we modify the contents to null-terminate all |
| 5219 | * strings before testing them. |
| 5220 | */ |
| 5221 | |
| 5222 | term = *cur_end; |
| 5223 | *cur_end = '\0'; |
| 5224 | |
| 5225 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5226 | switch (exp->action) { |
| 5227 | case ACT_SETBE: |
| 5228 | /* It is not possible to jump a second time. |
| 5229 | * FIXME: should we return an HTTP/500 here so that |
| 5230 | * the admin knows there's a problem ? |
| 5231 | */ |
| 5232 | if (t->be != t->fe) |
| 5233 | break; |
| 5234 | |
| 5235 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5236 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5237 | last_hdr = 1; |
| 5238 | break; |
| 5239 | |
| 5240 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5241 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5242 | last_hdr = 1; |
| 5243 | break; |
| 5244 | |
| 5245 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5246 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5247 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5248 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5249 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5250 | if (t->listener->counters) |
| 5251 | t->listener->counters->denied_resp++; |
| 5252 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5253 | break; |
| 5254 | |
| 5255 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5256 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5257 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5258 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5259 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5260 | if (t->listener->counters) |
| 5261 | t->listener->counters->denied_resp++; |
| 5262 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5263 | break; |
| 5264 | |
| 5265 | case ACT_REPLACE: |
| 5266 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5267 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5268 | /* FIXME: if the user adds a newline in the replacement, the |
| 5269 | * index will not be recalculated for now, and the new line |
| 5270 | * will not be counted as a new header. |
| 5271 | */ |
| 5272 | |
| 5273 | cur_end += delta; |
| 5274 | cur_next += delta; |
| 5275 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5276 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5277 | break; |
| 5278 | |
| 5279 | case ACT_REMOVE: |
| 5280 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5281 | cur_next += delta; |
| 5282 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5283 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5284 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5285 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5286 | cur_hdr->len = 0; |
| 5287 | cur_end = NULL; /* null-term has been rewritten */ |
| 5288 | break; |
| 5289 | |
| 5290 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5291 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5292 | if (cur_end) |
| 5293 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5294 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5295 | /* keep the link from this header to next one in case of later |
| 5296 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5297 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5298 | old_idx = cur_idx; |
| 5299 | } |
| 5300 | return 0; |
| 5301 | } |
| 5302 | |
| 5303 | |
| 5304 | /* Apply the filter to the request line. |
| 5305 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5306 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5307 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5308 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5309 | */ |
| 5310 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5311 | { |
| 5312 | char term; |
| 5313 | char *cur_ptr, *cur_end; |
| 5314 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5315 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5316 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5317 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5318 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5319 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5320 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5321 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5322 | (exp->action == ACT_ALLOW || |
| 5323 | exp->action == ACT_DENY || |
| 5324 | exp->action == ACT_TARPIT)) |
| 5325 | return 0; |
| 5326 | else if (exp->action == ACT_REMOVE) |
| 5327 | return 0; |
| 5328 | |
| 5329 | done = 0; |
| 5330 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5331 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5332 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5333 | |
| 5334 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5335 | |
| 5336 | /* The annoying part is that pattern matching needs |
| 5337 | * that we modify the contents to null-terminate all |
| 5338 | * strings before testing them. |
| 5339 | */ |
| 5340 | |
| 5341 | term = *cur_end; |
| 5342 | *cur_end = '\0'; |
| 5343 | |
| 5344 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5345 | switch (exp->action) { |
| 5346 | case ACT_SETBE: |
| 5347 | /* It is not possible to jump a second time. |
| 5348 | * FIXME: should we return an HTTP/500 here so that |
| 5349 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5350 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5351 | if (t->be != t->fe) |
| 5352 | break; |
| 5353 | |
| 5354 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5355 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5356 | done = 1; |
| 5357 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5358 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5359 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5360 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5361 | done = 1; |
| 5362 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5364 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5365 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5366 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5367 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5368 | if (t->listener->counters) |
| 5369 | t->listener->counters->denied_resp++; |
| 5370 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5371 | done = 1; |
| 5372 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5373 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5374 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5375 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5376 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5377 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5378 | if (t->listener->counters) |
| 5379 | t->listener->counters->denied_resp++; |
| 5380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5381 | done = 1; |
| 5382 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5383 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5384 | case ACT_REPLACE: |
| 5385 | *cur_end = term; /* restore the string terminator */ |
| 5386 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5387 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5388 | /* FIXME: if the user adds a newline in the replacement, the |
| 5389 | * index will not be recalculated for now, and the new line |
| 5390 | * will not be counted as a new header. |
| 5391 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5392 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5393 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5394 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5395 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5396 | HTTP_MSG_RQMETH, |
| 5397 | cur_ptr, cur_end + 1, |
| 5398 | NULL, NULL); |
| 5399 | if (unlikely(!cur_end)) |
| 5400 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5402 | /* we have a full request and we know that we have either a CR |
| 5403 | * or an LF at <ptr>. |
| 5404 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5405 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5406 | 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] | 5407 | /* there is no point trying this regex on headers */ |
| 5408 | return 1; |
| 5409 | } |
| 5410 | } |
| 5411 | *cur_end = term; /* restore the string terminator */ |
| 5412 | return done; |
| 5413 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5414 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5415 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5416 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5417 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5418 | * 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] | 5419 | * 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] | 5420 | * unparsable request. Since it can manage the switch to another backend, it |
| 5421 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5422 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5423 | 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] | 5424 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5425 | struct http_txn *txn = &s->txn; |
| 5426 | struct hdr_exp *exp; |
| 5427 | |
| 5428 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5429 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5431 | /* |
| 5432 | * The interleaving of transformations and verdicts |
| 5433 | * makes it difficult to decide to continue or stop |
| 5434 | * the evaluation. |
| 5435 | */ |
| 5436 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5437 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5438 | break; |
| 5439 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5440 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5441 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5442 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5443 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5444 | |
| 5445 | /* if this filter had a condition, evaluate it now and skip to |
| 5446 | * next filter if the condition does not match. |
| 5447 | */ |
| 5448 | if (exp->cond) { |
| 5449 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5450 | ret = acl_pass(ret); |
| 5451 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5452 | ret = !ret; |
| 5453 | |
| 5454 | if (!ret) |
| 5455 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5459 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5460 | if (unlikely(ret < 0)) |
| 5461 | return -1; |
| 5462 | |
| 5463 | if (likely(ret == 0)) { |
| 5464 | /* The filter did not match the request, it can be |
| 5465 | * iterated through all headers. |
| 5466 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5467 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5468 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5469 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5470 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5471 | } |
| 5472 | |
| 5473 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5474 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5475 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5476 | * Try to retrieve the server associated to the appsession. |
| 5477 | * If the server is found, it's assigned to the session. |
| 5478 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5479 | 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] | 5480 | struct http_txn *txn = &t->txn; |
| 5481 | appsess *asession = NULL; |
| 5482 | char *sessid_temp = NULL; |
| 5483 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5484 | if (len > t->be->appsession_len) { |
| 5485 | len = t->be->appsession_len; |
| 5486 | } |
| 5487 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5488 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5489 | /* 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] | 5490 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5491 | /* 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] | 5492 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5493 | } |
| 5494 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5495 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5496 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5497 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5498 | return; |
| 5499 | } |
| 5500 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5501 | memcpy(txn->sessid, buf, len); |
| 5502 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5506 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5507 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5508 | return; |
| 5509 | } |
| 5510 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5511 | memcpy(sessid_temp, buf, len); |
| 5512 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5513 | |
| 5514 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5515 | /* free previously allocated memory */ |
| 5516 | pool_free2(apools.sessid, sessid_temp); |
| 5517 | |
| 5518 | if (asession != NULL) { |
| 5519 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5520 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5521 | asession->request_count++; |
| 5522 | |
| 5523 | if (asession->serverid != NULL) { |
| 5524 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5525 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5526 | while (srv) { |
| 5527 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5528 | if ((srv->state & SRV_RUNNING) || |
| 5529 | (t->be->options & PR_O_PERSIST) || |
| 5530 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5531 | /* we found the server and it's usable */ |
| 5532 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5533 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5534 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5535 | t->srv = srv; |
| 5536 | break; |
| 5537 | } else { |
| 5538 | txn->flags &= ~TX_CK_MASK; |
| 5539 | txn->flags |= TX_CK_DOWN; |
| 5540 | } |
| 5541 | } |
| 5542 | srv = srv->next; |
| 5543 | } |
| 5544 | } |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5549 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5550 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5551 | */ |
| 5552 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5553 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5554 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5555 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5556 | char *del_colon, *del_cookie, *colon; |
| 5557 | int app_cookies; |
| 5558 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5559 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5560 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5561 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5562 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5563 | * we start with the start line. |
| 5564 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5565 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5566 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5567 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5568 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5569 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5570 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5571 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5572 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5573 | cur_ptr = cur_next; |
| 5574 | cur_end = cur_ptr + cur_hdr->len; |
| 5575 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5576 | |
| 5577 | /* We have one full header between cur_ptr and cur_end, and the |
| 5578 | * next header starts at cur_next. We're only interested in |
| 5579 | * "Cookie:" headers. |
| 5580 | */ |
| 5581 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5582 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5583 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5584 | old_idx = cur_idx; |
| 5585 | continue; |
| 5586 | } |
| 5587 | |
| 5588 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5589 | * attributes whose name begin with a '$', and associate them with |
| 5590 | * the right cookie, if we want to delete this cookie. |
| 5591 | * So there are 3 cases for each cookie read : |
| 5592 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5593 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5594 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5595 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5596 | * "special" cookie. |
| 5597 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5598 | * remove it. If no application cookie persists in the header, we |
| 5599 | * *MUST* delete it |
| 5600 | */ |
| 5601 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5602 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
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 | /* del_cookie == NULL => nothing to be deleted */ |
| 5605 | del_colon = del_cookie = NULL; |
| 5606 | app_cookies = 0; |
| 5607 | |
| 5608 | while (p1 < cur_end) { |
| 5609 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5610 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5611 | while (p1 < cur_end) { |
| 5612 | if (*p1 == ';' || *p1 == ',') |
| 5613 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5614 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5615 | break; |
| 5616 | p1++; |
| 5617 | } |
| 5618 | |
| 5619 | if (p1 == cur_end) |
| 5620 | break; |
| 5621 | |
| 5622 | /* p1 is at the beginning of the cookie name */ |
| 5623 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5624 | while (p2 < cur_end && *p2 != '=') { |
| 5625 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5626 | /* oops, the cookie name was truncated, resync */ |
| 5627 | p1 = p2; |
| 5628 | goto resync_name; |
| 5629 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5630 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5631 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5632 | |
| 5633 | if (p2 == cur_end) |
| 5634 | break; |
| 5635 | |
| 5636 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5637 | if (p3 == cur_end) |
| 5638 | break; |
| 5639 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5640 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5641 | p5 = p4 = p3; |
| 5642 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5643 | if (!isspace((unsigned char)*p5)) |
| 5644 | p4 = p5 + 1; |
| 5645 | p5++; |
| 5646 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5647 | |
| 5648 | /* here, we have the cookie name between p1 and p2, |
| 5649 | * and its value between p3 and p4. |
| 5650 | * we can process it : |
| 5651 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5652 | * Cookie: NAME=VALUE ; |
| 5653 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5654 | * | || || +--> p4 |
| 5655 | * | || |+-------> p3 |
| 5656 | * | || +--------> p2 |
| 5657 | * | |+------------> p1 |
| 5658 | * | +-------------> colon |
| 5659 | * +--------------------> cur_ptr |
| 5660 | */ |
| 5661 | |
| 5662 | if (*p1 == '$') { |
| 5663 | /* skip this one */ |
| 5664 | } |
| 5665 | else { |
| 5666 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5667 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5668 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5669 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5670 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5671 | int log_len = p4 - p1; |
| 5672 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5673 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5674 | Alert("HTTP logging : out of memory.\n"); |
| 5675 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5676 | if (log_len > t->fe->capture_len) |
| 5677 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5678 | memcpy(txn->cli_cookie, p1, log_len); |
| 5679 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5680 | } |
| 5681 | } |
| 5682 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5683 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5684 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5685 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5686 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5687 | char *delim; |
| 5688 | |
| 5689 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5690 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5691 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5692 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5693 | * Cookie: NAME=SRV~VALUE ; |
| 5694 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5695 | * | || || | +--> p4 |
| 5696 | * | || || +--------> delim |
| 5697 | * | || |+-----------> p3 |
| 5698 | * | || +------------> p2 |
| 5699 | * | |+----------------> p1 |
| 5700 | * | +-----------------> colon |
| 5701 | * +------------------------> cur_ptr |
| 5702 | */ |
| 5703 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5704 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5705 | for (delim = p3; delim < p4; delim++) |
| 5706 | if (*delim == COOKIE_DELIM) |
| 5707 | break; |
| 5708 | } |
| 5709 | else |
| 5710 | delim = p4; |
| 5711 | |
| 5712 | |
| 5713 | /* Here, we'll look for the first running server which supports the cookie. |
| 5714 | * This allows to share a same cookie between several servers, for example |
| 5715 | * to dedicate backup servers to specific servers only. |
| 5716 | * However, to prevent clients from sticking to cookie-less backup server |
| 5717 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5718 | * empty cookies and mark them as invalid. |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5719 | * The same behaviour is applied when persistence must be ignored. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5720 | */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5721 | if ((delim == p3) || (t->flags & SN_IGNORE_PRST)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5722 | srv = NULL; |
| 5723 | |
| 5724 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5725 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5726 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5727 | if ((srv->state & SRV_RUNNING) || |
| 5728 | (t->be->options & PR_O_PERSIST) || |
| 5729 | (t->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5730 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5731 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5732 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5733 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5734 | t->srv = srv; |
| 5735 | break; |
| 5736 | } else { |
| 5737 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5738 | txn->flags &= ~TX_CK_MASK; |
| 5739 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5740 | } |
| 5741 | } |
| 5742 | srv = srv->next; |
| 5743 | } |
| 5744 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5745 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5746 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5747 | txn->flags &= ~TX_CK_MASK; |
| 5748 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5749 | } |
| 5750 | |
| 5751 | /* depending on the cookie mode, we may have to either : |
| 5752 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5753 | * the server never sees it ; |
| 5754 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5755 | * application cookie so that it does not get accidentely removed later, |
| 5756 | * if we're in cookie prefix mode |
| 5757 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5758 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5759 | int delta; /* negative */ |
| 5760 | |
| 5761 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5762 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5763 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5764 | cur_end += delta; |
| 5765 | cur_next += delta; |
| 5766 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5767 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5768 | |
| 5769 | del_cookie = del_colon = NULL; |
| 5770 | app_cookies++; /* protect the header from deletion */ |
| 5771 | } |
| 5772 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5773 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5774 | del_cookie = p1; |
| 5775 | del_colon = colon; |
| 5776 | } |
| 5777 | } else { |
| 5778 | /* now we know that we must keep this cookie since it's |
| 5779 | * not ours. But if we wanted to delete our cookie |
| 5780 | * earlier, we cannot remove the complete header, but we |
| 5781 | * can remove the previous block itself. |
| 5782 | */ |
| 5783 | app_cookies++; |
| 5784 | |
| 5785 | if (del_cookie != NULL) { |
| 5786 | int delta; /* negative */ |
| 5787 | |
| 5788 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5789 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5790 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5791 | cur_end += delta; |
| 5792 | cur_next += delta; |
| 5793 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5794 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5795 | del_cookie = del_colon = NULL; |
| 5796 | } |
| 5797 | } |
| 5798 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5799 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 5800 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5801 | int cmp_len, value_len; |
| 5802 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5803 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5804 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5805 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5806 | value_begin = p1 + t->be->appsession_name_len; |
| 5807 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5808 | } else { |
| 5809 | cmp_len = p2 - p1; |
| 5810 | value_begin = p3; |
| 5811 | value_len = p4 - p3; |
| 5812 | } |
| 5813 | |
| 5814 | /* let's see if the cookie is our appcookie */ |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 5815 | if ((cmp_len == t->be->appsession_name_len) && |
| 5816 | (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5817 | /* Cool... it's the right one */ |
| 5818 | manage_client_side_appsession(t, value_begin, value_len); |
| 5819 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5820 | #if defined(DEBUG_HASH) |
| 5821 | Alert("manage_client_side_cookies\n"); |
| 5822 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5823 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5824 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5825 | } |
| 5826 | |
| 5827 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5828 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5829 | } /* while (p1 < cur_end) */ |
| 5830 | |
| 5831 | /* There's no more cookie on this line. |
| 5832 | * We may have marked the last one(s) for deletion. |
| 5833 | * We must do this now in two ways : |
| 5834 | * - if there is no app cookie, we simply delete the header ; |
| 5835 | * - if there are app cookies, we must delete the end of the |
| 5836 | * string properly, including the colon/semi-colon before |
| 5837 | * the cookie name. |
| 5838 | */ |
| 5839 | if (del_cookie != NULL) { |
| 5840 | int delta; |
| 5841 | if (app_cookies) { |
| 5842 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5843 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5844 | cur_hdr->len += delta; |
| 5845 | } else { |
| 5846 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5849 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5850 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5851 | cur_hdr->len = 0; |
| 5852 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5853 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5854 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5855 | } |
| 5856 | |
| 5857 | /* keep the link from this header to next one */ |
| 5858 | old_idx = cur_idx; |
| 5859 | } /* end of cookie processing on this header */ |
| 5860 | } |
| 5861 | |
| 5862 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5863 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5864 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5865 | */ |
| 5866 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5867 | { |
| 5868 | char term; |
| 5869 | char *cur_ptr, *cur_end, *cur_next; |
| 5870 | int cur_idx, old_idx, last_hdr; |
| 5871 | struct http_txn *txn = &t->txn; |
| 5872 | struct hdr_idx_elem *cur_hdr; |
| 5873 | int len, delta; |
| 5874 | |
| 5875 | last_hdr = 0; |
| 5876 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5877 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5878 | old_idx = 0; |
| 5879 | |
| 5880 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5881 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5882 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5883 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5884 | (exp->action == ACT_ALLOW || |
| 5885 | exp->action == ACT_DENY)) |
| 5886 | return 0; |
| 5887 | |
| 5888 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5889 | if (!cur_idx) |
| 5890 | break; |
| 5891 | |
| 5892 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5893 | cur_ptr = cur_next; |
| 5894 | cur_end = cur_ptr + cur_hdr->len; |
| 5895 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5896 | |
| 5897 | /* Now we have one header between cur_ptr and cur_end, |
| 5898 | * and the next header starts at cur_next. |
| 5899 | */ |
| 5900 | |
| 5901 | /* The annoying part is that pattern matching needs |
| 5902 | * that we modify the contents to null-terminate all |
| 5903 | * strings before testing them. |
| 5904 | */ |
| 5905 | |
| 5906 | term = *cur_end; |
| 5907 | *cur_end = '\0'; |
| 5908 | |
| 5909 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5910 | switch (exp->action) { |
| 5911 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5912 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5913 | last_hdr = 1; |
| 5914 | break; |
| 5915 | |
| 5916 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5917 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5918 | last_hdr = 1; |
| 5919 | break; |
| 5920 | |
| 5921 | case ACT_REPLACE: |
| 5922 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5923 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5924 | /* FIXME: if the user adds a newline in the replacement, the |
| 5925 | * index will not be recalculated for now, and the new line |
| 5926 | * will not be counted as a new header. |
| 5927 | */ |
| 5928 | |
| 5929 | cur_end += delta; |
| 5930 | cur_next += delta; |
| 5931 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5932 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5933 | break; |
| 5934 | |
| 5935 | case ACT_REMOVE: |
| 5936 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5937 | cur_next += delta; |
| 5938 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5939 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5940 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5941 | txn->hdr_idx.used--; |
| 5942 | cur_hdr->len = 0; |
| 5943 | cur_end = NULL; /* null-term has been rewritten */ |
| 5944 | break; |
| 5945 | |
| 5946 | } |
| 5947 | } |
| 5948 | if (cur_end) |
| 5949 | *cur_end = term; /* restore the string terminator */ |
| 5950 | |
| 5951 | /* keep the link from this header to next one in case of later |
| 5952 | * removal of next header. |
| 5953 | */ |
| 5954 | old_idx = cur_idx; |
| 5955 | } |
| 5956 | return 0; |
| 5957 | } |
| 5958 | |
| 5959 | |
| 5960 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5961 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5962 | * or -1 if a replacement resulted in an invalid status line. |
| 5963 | */ |
| 5964 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5965 | { |
| 5966 | char term; |
| 5967 | char *cur_ptr, *cur_end; |
| 5968 | int done; |
| 5969 | struct http_txn *txn = &t->txn; |
| 5970 | int len, delta; |
| 5971 | |
| 5972 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5973 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5974 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5975 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5976 | (exp->action == ACT_ALLOW || |
| 5977 | exp->action == ACT_DENY)) |
| 5978 | return 0; |
| 5979 | else if (exp->action == ACT_REMOVE) |
| 5980 | return 0; |
| 5981 | |
| 5982 | done = 0; |
| 5983 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5984 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 5985 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5986 | |
| 5987 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5988 | |
| 5989 | /* The annoying part is that pattern matching needs |
| 5990 | * that we modify the contents to null-terminate all |
| 5991 | * strings before testing them. |
| 5992 | */ |
| 5993 | |
| 5994 | term = *cur_end; |
| 5995 | *cur_end = '\0'; |
| 5996 | |
| 5997 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5998 | switch (exp->action) { |
| 5999 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6000 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6001 | done = 1; |
| 6002 | break; |
| 6003 | |
| 6004 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6005 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6006 | done = 1; |
| 6007 | break; |
| 6008 | |
| 6009 | case ACT_REPLACE: |
| 6010 | *cur_end = term; /* restore the string terminator */ |
| 6011 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6012 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6013 | /* FIXME: if the user adds a newline in the replacement, the |
| 6014 | * index will not be recalculated for now, and the new line |
| 6015 | * will not be counted as a new header. |
| 6016 | */ |
| 6017 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6018 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6019 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6020 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6021 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6022 | cur_ptr, cur_end + 1, |
| 6023 | NULL, NULL); |
| 6024 | if (unlikely(!cur_end)) |
| 6025 | return -1; |
| 6026 | |
| 6027 | /* we have a full respnse and we know that we have either a CR |
| 6028 | * or an LF at <ptr>. |
| 6029 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6030 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6031 | 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] | 6032 | /* there is no point trying this regex on headers */ |
| 6033 | return 1; |
| 6034 | } |
| 6035 | } |
| 6036 | *cur_end = term; /* restore the string terminator */ |
| 6037 | return done; |
| 6038 | } |
| 6039 | |
| 6040 | |
| 6041 | |
| 6042 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6043 | * 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] | 6044 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6045 | * unparsable response. |
| 6046 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6047 | 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] | 6048 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6049 | struct http_txn *txn = &s->txn; |
| 6050 | struct hdr_exp *exp; |
| 6051 | |
| 6052 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6053 | int ret; |
| 6054 | |
| 6055 | /* |
| 6056 | * The interleaving of transformations and verdicts |
| 6057 | * makes it difficult to decide to continue or stop |
| 6058 | * the evaluation. |
| 6059 | */ |
| 6060 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6061 | if (txn->flags & TX_SVDENY) |
| 6062 | break; |
| 6063 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6064 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6065 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6066 | exp->action == ACT_PASS)) { |
| 6067 | exp = exp->next; |
| 6068 | continue; |
| 6069 | } |
| 6070 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6071 | /* if this filter had a condition, evaluate it now and skip to |
| 6072 | * next filter if the condition does not match. |
| 6073 | */ |
| 6074 | if (exp->cond) { |
| 6075 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6076 | ret = acl_pass(ret); |
| 6077 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6078 | ret = !ret; |
| 6079 | if (!ret) |
| 6080 | continue; |
| 6081 | } |
| 6082 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6083 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6084 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6085 | if (unlikely(ret < 0)) |
| 6086 | return -1; |
| 6087 | |
| 6088 | if (likely(ret == 0)) { |
| 6089 | /* The filter did not match the response, it can be |
| 6090 | * iterated through all headers. |
| 6091 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6092 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6093 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6094 | } |
| 6095 | return 0; |
| 6096 | } |
| 6097 | |
| 6098 | |
| 6099 | |
| 6100 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6101 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 6102 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6103 | */ |
| 6104 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 6105 | { |
| 6106 | struct http_txn *txn = &t->txn; |
| 6107 | char *p1, *p2, *p3, *p4; |
| 6108 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6109 | char *cur_ptr, *cur_end, *cur_next; |
| 6110 | int cur_idx, old_idx, delta; |
| 6111 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6112 | /* Iterate through the headers. |
| 6113 | * we start with the start line. |
| 6114 | */ |
| 6115 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6116 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6117 | |
| 6118 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6119 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6120 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6121 | |
| 6122 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6123 | cur_ptr = cur_next; |
| 6124 | cur_end = cur_ptr + cur_hdr->len; |
| 6125 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6126 | |
| 6127 | /* We have one full header between cur_ptr and cur_end, and the |
| 6128 | * next header starts at cur_next. We're only interested in |
| 6129 | * "Cookie:" headers. |
| 6130 | */ |
| 6131 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6132 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 6133 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6134 | old_idx = cur_idx; |
| 6135 | continue; |
| 6136 | } |
| 6137 | |
| 6138 | /* OK, right now we know we have a set-cookie at cur_ptr */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6139 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6140 | |
| 6141 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6142 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 6143 | * the cookie capture is declared in the fronend. |
| 6144 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6145 | if (t->be->cookie_name == NULL && |
| 6146 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6147 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6148 | return; |
| 6149 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6150 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6151 | |
| 6152 | while (p1 < cur_end) { /* in fact, we'll break after the first cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6153 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 6154 | break; |
| 6155 | |
| 6156 | /* p1 is at the beginning of the cookie name */ |
| 6157 | p2 = p1; |
| 6158 | |
| 6159 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 6160 | p2++; |
| 6161 | |
| 6162 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 6163 | break; |
| 6164 | |
| 6165 | p3 = p2 + 1; /* skip the '=' sign */ |
| 6166 | if (p3 == cur_end) |
| 6167 | break; |
| 6168 | |
| 6169 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6170 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6171 | p4++; |
| 6172 | |
| 6173 | /* here, we have the cookie name between p1 and p2, |
| 6174 | * and its value between p3 and p4. |
| 6175 | * we can process it. |
| 6176 | */ |
| 6177 | |
| 6178 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6179 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6180 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6181 | (p4 - p1 >= t->fe->capture_namelen) && |
| 6182 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6183 | int log_len = p4 - p1; |
| 6184 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6185 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6186 | Alert("HTTP logging : out of memory.\n"); |
| 6187 | } |
| 6188 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6189 | if (log_len > t->fe->capture_len) |
| 6190 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6191 | memcpy(txn->srv_cookie, p1, log_len); |
| 6192 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | /* now check if we need to process it for persistence */ |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6196 | if (!(t->flags & SN_IGNORE_PRST) && (p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6197 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6198 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6199 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6200 | |
| 6201 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6202 | * this occurrence because we'll insert another one later. |
| 6203 | * We'll delete it too if the "indirect" option is set and we're in |
| 6204 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6205 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 6206 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6207 | /* this header must be deleted */ |
| 6208 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6209 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6210 | txn->hdr_idx.used--; |
| 6211 | cur_hdr->len = 0; |
| 6212 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6213 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6214 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6215 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6216 | } |
| 6217 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6218 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6219 | /* replace bytes p3->p4 with the cookie name associated |
| 6220 | * with this server since we know it. |
| 6221 | */ |
| 6222 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 6223 | cur_hdr->len += delta; |
| 6224 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6225 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6227 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6228 | } |
| 6229 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6230 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6231 | /* insert the cookie name associated with this server |
| 6232 | * before existing cookie, and insert a delimitor between them.. |
| 6233 | */ |
| 6234 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 6235 | cur_hdr->len += delta; |
| 6236 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6237 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6238 | |
| 6239 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6240 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6241 | } |
| 6242 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6243 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 6244 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6245 | int cmp_len, value_len; |
| 6246 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6247 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6248 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6249 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 6250 | value_begin = p1 + t->be->appsession_name_len; |
| 6251 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 6252 | } else { |
| 6253 | cmp_len = p2 - p1; |
| 6254 | value_begin = p3; |
| 6255 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6256 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6257 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6258 | if ((cmp_len == t->be->appsession_name_len) && |
| 6259 | (memcmp(p1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6260 | /* Cool... it's the right one */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6261 | if (txn->sessid != NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6262 | /* free previously allocated memory as we don't need it anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6263 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6264 | } |
| 6265 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6266 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6267 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6268 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6269 | return; |
| 6270 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6271 | memcpy(txn->sessid, value_begin, value_len); |
| 6272 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6273 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6274 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6275 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 6276 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6277 | /* keep the link from this header to next one */ |
| 6278 | old_idx = cur_idx; |
| 6279 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6280 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6281 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6282 | appsess *asession = NULL; |
| 6283 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6284 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6285 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6286 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6287 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6288 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6289 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6290 | return; |
| 6291 | } |
| 6292 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6293 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6294 | 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] | 6295 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6296 | return; |
| 6297 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6298 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6299 | asession->sessid[t->be->appsession_len] = 0; |
| 6300 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6301 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6302 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 6303 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6304 | 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] | 6305 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6306 | return; |
| 6307 | } |
| 6308 | asession->serverid[0] = '\0'; |
| 6309 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 6310 | |
| 6311 | asession->request_count = 0; |
| 6312 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6313 | } |
| 6314 | |
| 6315 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6316 | asession->request_count++; |
| 6317 | } |
| 6318 | |
| 6319 | #if defined(DEBUG_HASH) |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6320 | if (t->be->appsession_name) { |
| 6321 | Alert("manage_server_side_cookies\n"); |
| 6322 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 6323 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6324 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6325 | } |
| 6326 | |
| 6327 | |
| 6328 | |
| 6329 | /* |
| 6330 | * Check if response is cacheable or not. Updates t->flags. |
| 6331 | */ |
| 6332 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6333 | { |
| 6334 | struct http_txn *txn = &t->txn; |
| 6335 | char *p1, *p2; |
| 6336 | |
| 6337 | char *cur_ptr, *cur_end, *cur_next; |
| 6338 | int cur_idx; |
| 6339 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6340 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6341 | return; |
| 6342 | |
| 6343 | /* Iterate through the headers. |
| 6344 | * we start with the start line. |
| 6345 | */ |
| 6346 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6347 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6348 | |
| 6349 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6350 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6351 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6352 | |
| 6353 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6354 | cur_ptr = cur_next; |
| 6355 | cur_end = cur_ptr + cur_hdr->len; |
| 6356 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6357 | |
| 6358 | /* We have one full header between cur_ptr and cur_end, and the |
| 6359 | * next header starts at cur_next. We're only interested in |
| 6360 | * "Cookie:" headers. |
| 6361 | */ |
| 6362 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6363 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6364 | if (val) { |
| 6365 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6366 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6367 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6368 | return; |
| 6369 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6370 | } |
| 6371 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6372 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6373 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6374 | continue; |
| 6375 | |
| 6376 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6377 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6378 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6379 | |
| 6380 | if (p1 >= cur_end) /* no more info */ |
| 6381 | continue; |
| 6382 | |
| 6383 | /* p1 is at the beginning of the value */ |
| 6384 | p2 = p1; |
| 6385 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6386 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6387 | p2++; |
| 6388 | |
| 6389 | /* we have a complete value between p1 and p2 */ |
| 6390 | if (p2 < cur_end && *p2 == '=') { |
| 6391 | /* we have something of the form no-cache="set-cookie" */ |
| 6392 | if ((cur_end - p1 >= 21) && |
| 6393 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6394 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6395 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6396 | continue; |
| 6397 | } |
| 6398 | |
| 6399 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6400 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6401 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6402 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6403 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6404 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6405 | return; |
| 6406 | } |
| 6407 | |
| 6408 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6409 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6410 | continue; |
| 6411 | } |
| 6412 | } |
| 6413 | } |
| 6414 | |
| 6415 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6416 | /* |
| 6417 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6418 | * If the server is found, it's assigned to the session. |
| 6419 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6420 | 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] | 6421 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6422 | char *end_params, *first_param, *cur_param, *next_param; |
| 6423 | char separator; |
| 6424 | int value_len; |
| 6425 | |
| 6426 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6428 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 6429 | (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] | 6430 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6431 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6432 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6433 | first_param = NULL; |
| 6434 | switch (mode) { |
| 6435 | case PR_O2_AS_M_PP: |
| 6436 | first_param = memchr(begin, ';', len); |
| 6437 | break; |
| 6438 | case PR_O2_AS_M_QS: |
| 6439 | first_param = memchr(begin, '?', len); |
| 6440 | break; |
| 6441 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6442 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6443 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6444 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6446 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6447 | switch (mode) { |
| 6448 | case PR_O2_AS_M_PP: |
| 6449 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6450 | end_params = (char *) begin + len; |
| 6451 | } |
| 6452 | separator = ';'; |
| 6453 | break; |
| 6454 | case PR_O2_AS_M_QS: |
| 6455 | end_params = (char *) begin + len; |
| 6456 | separator = '&'; |
| 6457 | break; |
| 6458 | default: |
| 6459 | /* unknown mode, shouldn't happen */ |
| 6460 | return; |
| 6461 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6462 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6463 | cur_param = next_param = end_params; |
| 6464 | while (cur_param > first_param) { |
| 6465 | cur_param--; |
| 6466 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6467 | /* let's see if this is the appsession parameter */ |
| 6468 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6469 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6470 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6471 | /* Cool... it's the right one */ |
| 6472 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6473 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6474 | if (value_len > 0) { |
| 6475 | manage_client_side_appsession(t, cur_param, value_len); |
| 6476 | } |
| 6477 | break; |
| 6478 | } |
| 6479 | next_param = cur_param; |
| 6480 | } |
| 6481 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6482 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6483 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6484 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6485 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6486 | } |
| 6487 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6488 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6489 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6490 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6491 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6492 | * It is assumed that the request is either a HEAD or GET and that the |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6493 | * t->be->uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6494 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6495 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6496 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6497 | int stats_check_uri(struct session *t, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6498 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6499 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6500 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6501 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6502 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6503 | if (!uri_auth) |
| 6504 | return 0; |
| 6505 | |
| 6506 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD) |
| 6507 | return 0; |
| 6508 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6509 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6510 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6511 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6512 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6513 | return 0; |
| 6514 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6515 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6516 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6517 | /* the URI is in h */ |
| 6518 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6519 | return 0; |
| 6520 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6521 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6522 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6523 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6524 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6525 | break; |
| 6526 | } |
| 6527 | h++; |
| 6528 | } |
| 6529 | |
| 6530 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6531 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6532 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6533 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6534 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6535 | break; |
| 6536 | } |
| 6537 | h++; |
| 6538 | } |
| 6539 | } |
| 6540 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6541 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6542 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6543 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6544 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6545 | break; |
| 6546 | } |
| 6547 | h++; |
| 6548 | } |
| 6549 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6550 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6551 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6552 | return 1; |
| 6553 | } |
| 6554 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6555 | /* |
| 6556 | * 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] | 6557 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6558 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6559 | */ |
| 6560 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6561 | struct buffer *buf, struct http_msg *msg, |
| 6562 | struct proxy *other_end) |
| 6563 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6564 | es->len = buf->r - (buf->data + msg->som); |
| 6565 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6566 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6567 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6568 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6569 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6570 | es->when = date; // user-visible date |
| 6571 | es->sid = s->uniq_id; |
| 6572 | es->srv = s->srv; |
| 6573 | es->oe = other_end; |
| 6574 | es->src = s->cli_addr; |
| 6575 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6576 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 6577 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 6578 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 6579 | * occurrence number corresponding to this value is returned. If <occ> is |
| 6580 | * strictly negative, the occurrence number before the end corresponding to |
| 6581 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 6582 | * not recommended to use it that way. Negative occurrences are limited to |
| 6583 | * a small value because it is required to keep them in memory while scanning. |
| 6584 | * IP address 0.0.0.0 is returned if no match is found. |
| 6585 | */ |
| 6586 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 6587 | { |
| 6588 | struct hdr_ctx ctx; |
| 6589 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 6590 | unsigned int hist_ptr; |
| 6591 | int found = 0; |
| 6592 | |
| 6593 | ctx.idx = 0; |
| 6594 | if (occ >= 0) { |
| 6595 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 6596 | occ--; |
| 6597 | if (occ <= 0) { |
| 6598 | found = 1; |
| 6599 | break; |
| 6600 | } |
| 6601 | } |
| 6602 | if (!found) |
| 6603 | return 0; |
| 6604 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 6605 | } |
| 6606 | |
| 6607 | /* negative occurrence, we scan all the list then walk back */ |
| 6608 | if (-occ > MAX_HDR_HISTORY) |
| 6609 | return 0; |
| 6610 | |
| 6611 | hist_ptr = 0; |
| 6612 | hdr_hist[hist_ptr] = 0; |
| 6613 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 6614 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 6615 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 6616 | hist_ptr = 0; |
| 6617 | found++; |
| 6618 | } |
| 6619 | if (-occ > found) |
| 6620 | return 0; |
| 6621 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 6622 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 6623 | */ |
| 6624 | hist_ptr += occ; |
| 6625 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 6626 | hist_ptr -= MAX_HDR_HISTORY; |
| 6627 | return hdr_hist[hist_ptr]; |
| 6628 | } |
| 6629 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6630 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6631 | * Print a debug line with a header |
| 6632 | */ |
| 6633 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6634 | { |
| 6635 | int len, max; |
| 6636 | 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] | 6637 | 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] | 6638 | max = end - start; |
| 6639 | UBOUND(max, sizeof(trash) - len - 1); |
| 6640 | len += strlcpy2(trash + len, start, max + 1); |
| 6641 | trash[len++] = '\n'; |
| 6642 | write(1, trash, len); |
| 6643 | } |
| 6644 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6645 | /* |
| 6646 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6647 | * the required fields are properly allocated and that we only need to (re)init |
| 6648 | * them. This should be used before processing any new request. |
| 6649 | */ |
| 6650 | void http_init_txn(struct session *s) |
| 6651 | { |
| 6652 | struct http_txn *txn = &s->txn; |
| 6653 | struct proxy *fe = s->fe; |
| 6654 | |
| 6655 | txn->flags = 0; |
| 6656 | txn->status = -1; |
| 6657 | |
| 6658 | txn->req.sol = txn->req.eol = NULL; |
| 6659 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6660 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6661 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6662 | txn->req.hdr_content_len = 0LL; |
| 6663 | txn->rsp.hdr_content_len = 0LL; |
| 6664 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6665 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6666 | |
| 6667 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6668 | |
| 6669 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6670 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6671 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6672 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6673 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6674 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6675 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6676 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6677 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6678 | |
| 6679 | if (txn->hdr_idx.v) |
| 6680 | hdr_idx_init(&txn->hdr_idx); |
| 6681 | } |
| 6682 | |
| 6683 | /* to be used at the end of a transaction */ |
| 6684 | void http_end_txn(struct session *s) |
| 6685 | { |
| 6686 | struct http_txn *txn = &s->txn; |
| 6687 | |
| 6688 | /* these ones will have been dynamically allocated */ |
| 6689 | pool_free2(pool2_requri, txn->uri); |
| 6690 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6691 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6692 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6693 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6694 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6695 | txn->uri = NULL; |
| 6696 | txn->srv_cookie = NULL; |
| 6697 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6698 | |
| 6699 | if (txn->req.cap) { |
| 6700 | struct cap_hdr *h; |
| 6701 | for (h = s->fe->req_cap; h; h = h->next) |
| 6702 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6703 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6704 | } |
| 6705 | |
| 6706 | if (txn->rsp.cap) { |
| 6707 | struct cap_hdr *h; |
| 6708 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6709 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6710 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6711 | } |
| 6712 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6713 | } |
| 6714 | |
| 6715 | /* to be used at the end of a transaction to prepare a new one */ |
| 6716 | void http_reset_txn(struct session *s) |
| 6717 | { |
| 6718 | http_end_txn(s); |
| 6719 | http_init_txn(s); |
| 6720 | |
| 6721 | s->be = s->fe; |
| 6722 | s->req->analysers = s->listener->analysers; |
| 6723 | s->logs.logwait = s->fe->to_log; |
| 6724 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 6725 | /* re-init store persistence */ |
| 6726 | s->store_count = 0; |
| 6727 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6728 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6729 | |
| 6730 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6731 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 6732 | /* We must trim any excess data from the response buffer, because we |
| 6733 | * may have blocked an invalid response from a server that we don't |
| 6734 | * want to accidentely forward once we disable the analysers, nor do |
| 6735 | * we want those data to come along with next response. A typical |
| 6736 | * example of such data would be from a buggy server responding to |
| 6737 | * a HEAD with some data, or sending more than the advertised |
| 6738 | * content-length. |
| 6739 | */ |
| 6740 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 6741 | s->rep->l = s->rep->send_max; |
| 6742 | s->rep->r = s->rep->w + s->rep->l; |
| 6743 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 6744 | s->rep->r -= s->rep->size; |
| 6745 | } |
| 6746 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6747 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 6748 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6749 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 6750 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6751 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6752 | |
| 6753 | s->req->rex = TICK_ETERNITY; |
| 6754 | s->req->wex = TICK_ETERNITY; |
| 6755 | s->req->analyse_exp = TICK_ETERNITY; |
| 6756 | s->rep->rex = TICK_ETERNITY; |
| 6757 | s->rep->wex = TICK_ETERNITY; |
| 6758 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6759 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6760 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6761 | /************************************************************************/ |
| 6762 | /* The code below is dedicated to ACL parsing and matching */ |
| 6763 | /************************************************************************/ |
| 6764 | |
| 6765 | |
| 6766 | |
| 6767 | |
| 6768 | /* 1. Check on METHOD |
| 6769 | * We use the pre-parsed method if it is known, and store its number as an |
| 6770 | * integer. If it is unknown, we use the pointer and the length. |
| 6771 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6772 | 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] | 6773 | { |
| 6774 | int len, meth; |
| 6775 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6776 | len = strlen(*text); |
| 6777 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6778 | |
| 6779 | pattern->val.i = meth; |
| 6780 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6781 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6782 | if (!pattern->ptr.str) |
| 6783 | return 0; |
| 6784 | pattern->len = len; |
| 6785 | } |
| 6786 | return 1; |
| 6787 | } |
| 6788 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6789 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6790 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6791 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6792 | { |
| 6793 | int meth; |
| 6794 | struct http_txn *txn = l7; |
| 6795 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6796 | if (!txn) |
| 6797 | return 0; |
| 6798 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6799 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6800 | return 0; |
| 6801 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6802 | meth = txn->meth; |
| 6803 | test->i = meth; |
| 6804 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6805 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6806 | /* ensure the indexes are not affected */ |
| 6807 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6808 | test->len = txn->req.sl.rq.m_l; |
| 6809 | test->ptr = txn->req.sol; |
| 6810 | } |
| 6811 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6812 | return 1; |
| 6813 | } |
| 6814 | |
| 6815 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6816 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6817 | int icase; |
| 6818 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6819 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6820 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6821 | |
| 6822 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6823 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6824 | |
| 6825 | /* Other method, we must compare the strings */ |
| 6826 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6827 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6828 | |
| 6829 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6830 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6831 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6832 | return ACL_PAT_FAIL; |
| 6833 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | /* 2. Check on Request/Status Version |
| 6837 | * We simply compare strings here. |
| 6838 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6839 | 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] | 6840 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6841 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6842 | if (!pattern->ptr.str) |
| 6843 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6844 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6845 | return 1; |
| 6846 | } |
| 6847 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6848 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6849 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6850 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6851 | { |
| 6852 | struct http_txn *txn = l7; |
| 6853 | char *ptr; |
| 6854 | int len; |
| 6855 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6856 | if (!txn) |
| 6857 | return 0; |
| 6858 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6859 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6860 | return 0; |
| 6861 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6862 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6863 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6864 | |
| 6865 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6866 | if (len <= 0) |
| 6867 | return 0; |
| 6868 | |
| 6869 | test->ptr = ptr; |
| 6870 | test->len = len; |
| 6871 | |
| 6872 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6873 | return 1; |
| 6874 | } |
| 6875 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6876 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6877 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6878 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6879 | { |
| 6880 | struct http_txn *txn = l7; |
| 6881 | char *ptr; |
| 6882 | int len; |
| 6883 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6884 | if (!txn) |
| 6885 | return 0; |
| 6886 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6887 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6888 | return 0; |
| 6889 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6890 | len = txn->rsp.sl.st.v_l; |
| 6891 | ptr = txn->rsp.sol; |
| 6892 | |
| 6893 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6894 | if (len <= 0) |
| 6895 | return 0; |
| 6896 | |
| 6897 | test->ptr = ptr; |
| 6898 | test->len = len; |
| 6899 | |
| 6900 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6901 | return 1; |
| 6902 | } |
| 6903 | |
| 6904 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6905 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6906 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6907 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6908 | { |
| 6909 | struct http_txn *txn = l7; |
| 6910 | char *ptr; |
| 6911 | int len; |
| 6912 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6913 | if (!txn) |
| 6914 | return 0; |
| 6915 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6916 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6917 | return 0; |
| 6918 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6919 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6920 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6921 | |
| 6922 | test->i = __strl2ui(ptr, len); |
| 6923 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6924 | return 1; |
| 6925 | } |
| 6926 | |
| 6927 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6928 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6929 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6930 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6931 | { |
| 6932 | struct http_txn *txn = l7; |
| 6933 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6934 | if (!txn) |
| 6935 | return 0; |
| 6936 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6937 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6938 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6939 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6940 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6941 | /* ensure the indexes are not affected */ |
| 6942 | return 0; |
| 6943 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6944 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6945 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6946 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6947 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6948 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6949 | return 1; |
| 6950 | } |
| 6951 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6952 | static int |
| 6953 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6954 | struct acl_expr *expr, struct acl_test *test) |
| 6955 | { |
| 6956 | struct http_txn *txn = l7; |
| 6957 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6958 | if (!txn) |
| 6959 | return 0; |
| 6960 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6961 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6962 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6963 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6964 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6965 | /* ensure the indexes are not affected */ |
| 6966 | return 0; |
| 6967 | |
| 6968 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6969 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6970 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6971 | test->i = AF_INET; |
| 6972 | |
| 6973 | /* |
| 6974 | * If we are parsing url in frontend space, we prepare backend stage |
| 6975 | * to not parse again the same url ! optimization lazyness... |
| 6976 | */ |
| 6977 | if (px->options & PR_O_HTTP_PROXY) |
| 6978 | l4->flags |= SN_ADDR_SET; |
| 6979 | |
| 6980 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6981 | return 1; |
| 6982 | } |
| 6983 | |
| 6984 | static int |
| 6985 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6986 | struct acl_expr *expr, struct acl_test *test) |
| 6987 | { |
| 6988 | struct http_txn *txn = l7; |
| 6989 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6990 | if (!txn) |
| 6991 | return 0; |
| 6992 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6993 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6994 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6995 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6996 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6997 | /* ensure the indexes are not affected */ |
| 6998 | return 0; |
| 6999 | |
| 7000 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7001 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7002 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 7003 | |
| 7004 | if (px->options & PR_O_HTTP_PROXY) |
| 7005 | l4->flags |= SN_ADDR_SET; |
| 7006 | |
| 7007 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7008 | return 1; |
| 7009 | } |
| 7010 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7011 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7012 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7013 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7014 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7015 | 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] | 7016 | struct acl_expr *expr, struct acl_test *test) |
| 7017 | { |
| 7018 | struct http_txn *txn = l7; |
| 7019 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7020 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7021 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7022 | if (!txn) |
| 7023 | return 0; |
| 7024 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7025 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7026 | /* search for header from the beginning */ |
| 7027 | ctx->idx = 0; |
| 7028 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7029 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7030 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7031 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7032 | test->len = ctx->vlen; |
| 7033 | test->ptr = (char *)ctx->line + ctx->val; |
| 7034 | return 1; |
| 7035 | } |
| 7036 | |
| 7037 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7038 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7039 | return 0; |
| 7040 | } |
| 7041 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7042 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7043 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7044 | struct acl_expr *expr, struct acl_test *test) |
| 7045 | { |
| 7046 | struct http_txn *txn = l7; |
| 7047 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7048 | if (!txn) |
| 7049 | return 0; |
| 7050 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7051 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7052 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7053 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7054 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7055 | /* ensure the indexes are not affected */ |
| 7056 | return 0; |
| 7057 | |
| 7058 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7059 | } |
| 7060 | |
| 7061 | static int |
| 7062 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7063 | struct acl_expr *expr, struct acl_test *test) |
| 7064 | { |
| 7065 | struct http_txn *txn = l7; |
| 7066 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7067 | if (!txn) |
| 7068 | return 0; |
| 7069 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7070 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7071 | return 0; |
| 7072 | |
| 7073 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 7074 | } |
| 7075 | |
| 7076 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 7077 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7078 | */ |
| 7079 | static int |
| 7080 | 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] | 7081 | struct acl_expr *expr, struct acl_test *test) |
| 7082 | { |
| 7083 | struct http_txn *txn = l7; |
| 7084 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7085 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7086 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7087 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7088 | if (!txn) |
| 7089 | return 0; |
| 7090 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7091 | ctx.idx = 0; |
| 7092 | cnt = 0; |
| 7093 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 7094 | cnt++; |
| 7095 | |
| 7096 | test->i = cnt; |
| 7097 | test->flags = ACL_TEST_F_VOL_HDR; |
| 7098 | return 1; |
| 7099 | } |
| 7100 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7101 | static int |
| 7102 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7103 | struct acl_expr *expr, struct acl_test *test) |
| 7104 | { |
| 7105 | struct http_txn *txn = l7; |
| 7106 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7107 | if (!txn) |
| 7108 | return 0; |
| 7109 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7110 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7111 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7112 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7113 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7114 | /* ensure the indexes are not affected */ |
| 7115 | return 0; |
| 7116 | |
| 7117 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 7118 | } |
| 7119 | |
| 7120 | static int |
| 7121 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7122 | struct acl_expr *expr, struct acl_test *test) |
| 7123 | { |
| 7124 | struct http_txn *txn = l7; |
| 7125 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7126 | if (!txn) |
| 7127 | return 0; |
| 7128 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7129 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7130 | return 0; |
| 7131 | |
| 7132 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7133 | } |
| 7134 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7135 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7136 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7137 | * 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] | 7138 | */ |
| 7139 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7140 | 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] | 7141 | struct acl_expr *expr, struct acl_test *test) |
| 7142 | { |
| 7143 | struct http_txn *txn = l7; |
| 7144 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7145 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7146 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7147 | if (!txn) |
| 7148 | return 0; |
| 7149 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7150 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7151 | /* search for header from the beginning */ |
| 7152 | ctx->idx = 0; |
| 7153 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7154 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7155 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7156 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7157 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 7158 | return 1; |
| 7159 | } |
| 7160 | |
| 7161 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7162 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7163 | return 0; |
| 7164 | } |
| 7165 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7166 | static int |
| 7167 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7168 | struct acl_expr *expr, struct acl_test *test) |
| 7169 | { |
| 7170 | struct http_txn *txn = l7; |
| 7171 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7172 | if (!txn) |
| 7173 | return 0; |
| 7174 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7175 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7176 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7177 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7178 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7179 | /* ensure the indexes are not affected */ |
| 7180 | return 0; |
| 7181 | |
| 7182 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7183 | } |
| 7184 | |
| 7185 | static int |
| 7186 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7187 | struct acl_expr *expr, struct acl_test *test) |
| 7188 | { |
| 7189 | struct http_txn *txn = l7; |
| 7190 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7191 | if (!txn) |
| 7192 | return 0; |
| 7193 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7194 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7195 | return 0; |
| 7196 | |
| 7197 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7198 | } |
| 7199 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7200 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7201 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7202 | */ |
| 7203 | static int |
| 7204 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7205 | struct acl_expr *expr, struct acl_test *test) |
| 7206 | { |
| 7207 | struct http_txn *txn = l7; |
| 7208 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7209 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7210 | |
| 7211 | if (!txn) |
| 7212 | return 0; |
| 7213 | |
| 7214 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7215 | /* search for header from the beginning */ |
| 7216 | ctx->idx = 0; |
| 7217 | |
| 7218 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7219 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7220 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7221 | /* Same optimization as url_ip */ |
| 7222 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 7223 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 7224 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 7225 | test->i = AF_INET; |
| 7226 | return 1; |
| 7227 | } |
| 7228 | |
| 7229 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7230 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7231 | return 0; |
| 7232 | } |
| 7233 | |
| 7234 | static int |
| 7235 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7236 | struct acl_expr *expr, struct acl_test *test) |
| 7237 | { |
| 7238 | struct http_txn *txn = l7; |
| 7239 | |
| 7240 | if (!txn) |
| 7241 | return 0; |
| 7242 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7243 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7244 | return 0; |
| 7245 | |
| 7246 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7247 | /* ensure the indexes are not affected */ |
| 7248 | return 0; |
| 7249 | |
| 7250 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 7251 | } |
| 7252 | |
| 7253 | static int |
| 7254 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7255 | struct acl_expr *expr, struct acl_test *test) |
| 7256 | { |
| 7257 | struct http_txn *txn = l7; |
| 7258 | |
| 7259 | if (!txn) |
| 7260 | return 0; |
| 7261 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7262 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7263 | return 0; |
| 7264 | |
| 7265 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 7266 | } |
| 7267 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7268 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7269 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7270 | */ |
| 7271 | static int |
| 7272 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7273 | struct acl_expr *expr, struct acl_test *test) |
| 7274 | { |
| 7275 | struct http_txn *txn = l7; |
| 7276 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7277 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7278 | if (!txn) |
| 7279 | return 0; |
| 7280 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7281 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7282 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7283 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7284 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7285 | /* ensure the indexes are not affected */ |
| 7286 | return 0; |
| 7287 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7288 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 7289 | ptr = http_get_path(txn); |
| 7290 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7291 | return 0; |
| 7292 | |
| 7293 | /* OK, we got the '/' ! */ |
| 7294 | test->ptr = ptr; |
| 7295 | |
| 7296 | while (ptr < end && *ptr != '?') |
| 7297 | ptr++; |
| 7298 | |
| 7299 | test->len = ptr - test->ptr; |
| 7300 | |
| 7301 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7302 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7303 | return 1; |
| 7304 | } |
| 7305 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7306 | static int |
| 7307 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7308 | struct acl_expr *expr, struct acl_test *test) |
| 7309 | { |
| 7310 | struct buffer *req = s->req; |
| 7311 | struct http_txn *txn = &s->txn; |
| 7312 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7313 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7314 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7315 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7316 | */ |
| 7317 | |
| 7318 | if (!s || !req) |
| 7319 | return 0; |
| 7320 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7321 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7322 | /* Already decoded as OK */ |
| 7323 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7324 | return 1; |
| 7325 | } |
| 7326 | |
| 7327 | /* Try to decode HTTP request */ |
| 7328 | if (likely(req->lr < req->r)) |
| 7329 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 7330 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7331 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7332 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7333 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7334 | return 1; |
| 7335 | } |
| 7336 | /* wait for final state */ |
| 7337 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7338 | return 0; |
| 7339 | } |
| 7340 | |
| 7341 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7342 | * perform so that further checks can rely on HTTP tests. |
| 7343 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7344 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7345 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7346 | s->flags |= SN_REDIRECTABLE; |
| 7347 | |
| 7348 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7349 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7350 | return 1; |
| 7351 | } |
| 7352 | |
| 7353 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7354 | return 1; |
| 7355 | } |
| 7356 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7357 | static int |
| 7358 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 7359 | struct acl_expr *expr, struct acl_test *test) |
| 7360 | { |
| 7361 | |
| 7362 | if (!s) |
| 7363 | return 0; |
| 7364 | |
| 7365 | if (!get_http_auth(s)) |
| 7366 | return 0; |
| 7367 | |
| 7368 | test->ctx.a[0] = expr->arg.ul; |
| 7369 | test->ctx.a[1] = s->txn.auth.user; |
| 7370 | test->ctx.a[2] = s->txn.auth.pass; |
| 7371 | |
| 7372 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 7373 | |
| 7374 | return 1; |
| 7375 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7376 | |
| 7377 | /************************************************************************/ |
| 7378 | /* All supported keywords must be declared here. */ |
| 7379 | /************************************************************************/ |
| 7380 | |
| 7381 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7382 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7383 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7384 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7385 | { "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] | 7386 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 7387 | { "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] | 7388 | { "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] | 7389 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 7390 | { "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] | 7391 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7392 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7393 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7394 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7395 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7396 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 7397 | { "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] | 7398 | { "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] | 7399 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7400 | /* 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] | 7401 | { "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] | 7402 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7403 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7404 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7405 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7406 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7407 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7408 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7409 | { "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] | 7410 | { "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] | 7411 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 7412 | { "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] | 7413 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7414 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7415 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7416 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7417 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7418 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7419 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7420 | { "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] | 7421 | { "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] | 7422 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 7423 | { "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] | 7424 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7425 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7426 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7427 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7428 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7429 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7430 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7431 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7432 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7433 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7434 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7435 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7436 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7437 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7438 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7439 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7440 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7441 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7442 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7443 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7444 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7445 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7446 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7447 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7448 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7449 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7450 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth }, |
| 7451 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7452 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7453 | }}; |
| 7454 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 7455 | /************************************************************************/ |
| 7456 | /* The code below is dedicated to pattern fetching and matching */ |
| 7457 | /************************************************************************/ |
| 7458 | |
| 7459 | /* extract the IP address from the last occurrence of specified header. Note |
| 7460 | * that we should normally first extract the string then convert it to IP, |
| 7461 | * but right now we have all the functions to do this seemlessly, and we will |
| 7462 | * be able to change that later without touching the configuration. |
| 7463 | */ |
| 7464 | static int |
| 7465 | pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7466 | const char *arg, int arg_len, union pattern_data *data) |
| 7467 | { |
| 7468 | struct http_txn *txn = l7; |
| 7469 | |
| 7470 | data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg, arg_len, &txn->hdr_idx, -1)); |
| 7471 | return data->ip.s_addr != 0; |
| 7472 | } |
| 7473 | |
| 7474 | /************************************************************************/ |
| 7475 | /* All supported keywords must be declared here. */ |
| 7476 | /************************************************************************/ |
| 7477 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7478 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
| 7479 | { "hdr", pattern_fetch_hdr_ip, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
| 7480 | { NULL, NULL, 0, 0 }, |
| 7481 | }}; |
| 7482 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7483 | |
| 7484 | __attribute__((constructor)) |
| 7485 | static void __http_protocol_init(void) |
| 7486 | { |
| 7487 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 7488 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7489 | } |
| 7490 | |
| 7491 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7492 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7493 | * Local variables: |
| 7494 | * c-indent-level: 8 |
| 7495 | * c-basic-offset: 8 |
| 7496 | * End: |
| 7497 | */ |