adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // This file implements a simple HTTP server. It can exhibit odd behavior |
| 6 | // that's useful for testing. For example, it's useful to test that |
| 7 | // the updater can continue a connection if it's dropped, or that it |
| 8 | // handles very slow data transfers. |
| 9 | |
| 10 | // To use this, simply make an HTTP connection to localhost:port and |
| 11 | // GET a url. |
| 12 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include <errno.h> |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 14 | #include <inttypes.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 15 | #include <netinet/in.h> |
| 16 | #include <signal.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 20 | #include <sys/socket.h> |
| 21 | #include <sys/types.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 23 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <string> |
| 26 | #include <vector> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 27 | |
| 28 | #include <base/logging.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 29 | |
| 30 | using std::min; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | namespace chromeos_update_engine { |
| 35 | |
| 36 | struct HttpRequest { |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 37 | HttpRequest() : offset(0), return_code(200) {} |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 38 | string host; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | string url; |
| 40 | off_t offset; |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 41 | int return_code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | namespace { |
Darin Petkov | f67bb1f | 2010-11-08 16:10:26 -0800 | [diff] [blame] | 45 | const int kPort = 8088; // hardcoded for now |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 46 | const int kBigLength = 100000; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 47 | const int kMediumLength = 1000; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | bool ParseRequest(int fd, HttpRequest* request) { |
| 51 | string headers; |
| 52 | while(headers.find("\r\n\r\n") == string::npos) { |
| 53 | vector<char> buf(1024); |
| 54 | memset(&buf[0], 0, buf.size()); |
| 55 | ssize_t r = read(fd, &buf[0], buf.size() - 1); |
| 56 | if (r < 0) { |
| 57 | perror("read"); |
| 58 | exit(1); |
| 59 | } |
| 60 | buf.resize(r); |
| 61 | |
| 62 | headers.insert(headers.end(), buf.begin(), buf.end()); |
| 63 | } |
| 64 | LOG(INFO) << "got headers: " << headers; |
| 65 | |
| 66 | string::size_type url_start, url_end; |
| 67 | CHECK_NE(headers.find("GET "), string::npos); |
| 68 | url_start = headers.find("GET ") + strlen("GET "); |
| 69 | url_end = headers.find(' ', url_start); |
| 70 | CHECK_NE(string::npos, url_end); |
| 71 | string url = headers.substr(url_start, url_end - url_start); |
| 72 | LOG(INFO) << "URL: " << url; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 73 | request->url = url; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | |
| 75 | string::size_type range_start, range_end; |
| 76 | if (headers.find("\r\nRange: ") == string::npos) { |
| 77 | request->offset = 0; |
| 78 | } else { |
| 79 | range_start = headers.find("\r\nRange: ") + strlen("\r\nRange: "); |
| 80 | range_end = headers.find('\r', range_start); |
| 81 | CHECK_NE(string::npos, range_end); |
| 82 | string range_header = headers.substr(range_start, range_end - range_start); |
| 83 | |
| 84 | LOG(INFO) << "Range: " << range_header; |
| 85 | CHECK(*range_header.rbegin() == '-'); |
| 86 | request->offset = atoll(range_header.c_str() + strlen("bytes=")); |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 87 | request->return_code = 206; // Success for Range: request |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 88 | LOG(INFO) << "Offset: " << request->offset; |
| 89 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 90 | |
| 91 | if (headers.find("\r\nHost: ") == string::npos) { |
| 92 | request->host = ""; |
| 93 | } else { |
| 94 | string::size_type host_start = |
| 95 | headers.find("\r\nHost: ") + strlen("\r\nHost: "); |
| 96 | string::size_type host_end = headers.find('\r', host_start); |
| 97 | CHECK_NE(string::npos, host_end); |
| 98 | string host = headers.substr(host_start, host_end - host_start); |
| 99 | |
| 100 | LOG(INFO) << "Host: " << host; |
| 101 | request->host = host; |
| 102 | } |
| 103 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 107 | bool WriteString(int fd, const string& str) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 108 | unsigned int bytes_written = 0; |
| 109 | while (bytes_written < str.size()) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 110 | ssize_t r = write(fd, str.data() + bytes_written, |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 111 | str.size() - bytes_written); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 112 | if (r < 0) { |
| 113 | perror("write"); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 114 | LOG(INFO) << "write failed"; |
| 115 | return false; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 116 | } |
| 117 | bytes_written += r; |
| 118 | } |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 119 | return true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | string Itoa(off_t num) { |
| 123 | char buf[100] = {0}; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 124 | snprintf(buf, sizeof(buf), "%" PRIi64, num); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 125 | return buf; |
| 126 | } |
| 127 | |
| 128 | void WriteHeaders(int fd, bool support_range, off_t full_size, |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 129 | off_t start_offset, int return_code) { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 130 | LOG(INFO) << "writing headers"; |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 131 | WriteString(fd, string("HTTP/1.1 ") + Itoa(return_code) + " OK\r\n"); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | WriteString(fd, "Content-Type: application/octet-stream\r\n"); |
| 133 | if (support_range) { |
| 134 | WriteString(fd, "Accept-Ranges: bytes\r\n"); |
| 135 | WriteString(fd, string("Content-Range: bytes ") + Itoa(start_offset) + |
| 136 | "-" + Itoa(full_size - 1) + "/" + Itoa(full_size) + "\r\n"); |
| 137 | } |
| 138 | off_t content_length = full_size; |
| 139 | if (support_range) |
| 140 | content_length -= start_offset; |
| 141 | WriteString(fd, string("Content-Length: ") + Itoa(content_length) + "\r\n"); |
| 142 | WriteString(fd, "\r\n"); |
| 143 | } |
| 144 | |
| 145 | void HandleQuitQuitQuit(int fd) { |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 146 | WriteHeaders(fd, true, 0, 0, 200); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 | exit(0); |
| 148 | } |
| 149 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 150 | void HandleBig(int fd, const HttpRequest& request, int big_length) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 151 | LOG(INFO) << "starting big"; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 152 | const off_t full_length = big_length; |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 153 | WriteHeaders(fd, true, full_length, request.offset, request.return_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 154 | int i = request.offset; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 155 | bool success = true; |
| 156 | for (; (i % 10) && success; i++) |
| 157 | success = WriteString(fd, string(1, 'a' + (i % 10))); |
| 158 | if (success) |
| 159 | CHECK_EQ(i % 10, 0); |
| 160 | for (; (i < full_length) && success; i += 10) { |
| 161 | success = WriteString(fd, "abcdefghij"); |
| 162 | } |
| 163 | if (success) |
| 164 | CHECK_EQ(i, full_length); |
| 165 | LOG(INFO) << "Done w/ big"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // This is like /big, but it writes at most 9000 bytes. Also, |
| 169 | // half way through it sleeps for 70 seconds |
| 170 | // (technically, when (offset % (9000 * 7)) == 0). |
| 171 | void HandleFlaky(int fd, const HttpRequest& request) { |
| 172 | const off_t full_length = kBigLength; |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 173 | WriteHeaders(fd, true, full_length, request.offset, request.return_code); |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 174 | const off_t content_length = |
| 175 | min(static_cast<off_t>(9000), full_length - request.offset); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 176 | const bool should_sleep = (request.offset % (9000 * 7)) == 0; |
| 177 | |
| 178 | string buf; |
| 179 | |
| 180 | for (int i = request.offset; i % 10; i++) |
| 181 | buf.append(1, 'a' + (i % 10)); |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 182 | while (static_cast<off_t>(buf.size()) < content_length) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 183 | buf.append("abcdefghij"); |
| 184 | buf.resize(content_length); |
| 185 | |
| 186 | if (!should_sleep) { |
| 187 | LOG(INFO) << "writing data blob of size " << buf.size(); |
| 188 | WriteString(fd, buf); |
| 189 | } else { |
| 190 | string::size_type half_way_point = buf.size() / 2; |
| 191 | LOG(INFO) << "writing small data blob of size " << half_way_point; |
| 192 | WriteString(fd, buf.substr(0, half_way_point)); |
| 193 | sleep(10); |
| 194 | LOG(INFO) << "writing small data blob of size " |
| 195 | << (buf.size() - half_way_point); |
| 196 | WriteString(fd, buf.substr(half_way_point, buf.size() - half_way_point)); |
| 197 | } |
| 198 | } |
| 199 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 200 | // Handles /redirect/<code>/<url> requests by returning the specified |
| 201 | // redirect <code> with a location pointing to /<url>. |
| 202 | void HandleRedirect(int fd, const HttpRequest& request) { |
| 203 | LOG(INFO) << "Redirecting..."; |
| 204 | string url = request.url; |
| 205 | CHECK_EQ(0, url.find("/redirect/")); |
| 206 | url.erase(0, strlen("/redirect/")); |
| 207 | string::size_type url_start = url.find('/'); |
| 208 | CHECK_NE(url_start, string::npos); |
| 209 | string code = url.substr(0, url_start); |
| 210 | url.erase(0, url_start); |
| 211 | url = "http://" + request.host + url; |
| 212 | string status; |
| 213 | if (code == "301") { |
| 214 | status = "Moved Permanently"; |
| 215 | } else if (code == "302") { |
| 216 | status = "Found"; |
| 217 | } else if (code == "303") { |
| 218 | status = "See Other"; |
| 219 | } else if (code == "307") { |
| 220 | status = "Temporary Redirect"; |
| 221 | } else { |
| 222 | CHECK(false) << "Unrecognized redirection code: " << code; |
| 223 | } |
| 224 | LOG(INFO) << "Code: " << code << " " << status; |
| 225 | LOG(INFO) << "New URL: " << url; |
| 226 | WriteString(fd, "HTTP/1.1 " + code + " " + status + "\r\n"); |
| 227 | WriteString(fd, "Location: " + url + "\r\n"); |
| 228 | } |
| 229 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 230 | void HandleDefault(int fd, const HttpRequest& request) { |
| 231 | const string data("unhandled path"); |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 232 | WriteHeaders(fd, true, data.size(), request.offset, request.return_code); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 233 | const string data_to_write(data.substr(request.offset, |
| 234 | data.size() - request.offset)); |
| 235 | WriteString(fd, data_to_write); |
| 236 | } |
| 237 | |
| 238 | void HandleConnection(int fd) { |
| 239 | HttpRequest request; |
| 240 | ParseRequest(fd, &request); |
| 241 | |
| 242 | if (request.url == "/quitquitquit") |
| 243 | HandleQuitQuitQuit(fd); |
| 244 | else if (request.url == "/big") |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 245 | HandleBig(fd, request, kBigLength); |
| 246 | else if (request.url == "/medium") |
| 247 | HandleBig(fd, request, kMediumLength); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 248 | else if (request.url == "/flaky") |
| 249 | HandleFlaky(fd, request); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 250 | else if (request.url.find("/redirect/") == 0) |
| 251 | HandleRedirect(fd, request); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 252 | else |
| 253 | HandleDefault(fd, request); |
| 254 | |
| 255 | close(fd); |
| 256 | } |
| 257 | |
| 258 | } // namespace chromeos_update_engine |
| 259 | |
| 260 | using namespace chromeos_update_engine; |
| 261 | |
| 262 | int main(int argc, char** argv) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 263 | // Ignore SIGPIPE on write() to sockets. |
| 264 | signal(SIGPIPE, SIG_IGN); |
Darin Petkov | f67bb1f | 2010-11-08 16:10:26 -0800 | [diff] [blame] | 265 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 266 | socklen_t clilen; |
| 267 | struct sockaddr_in server_addr; |
| 268 | struct sockaddr_in client_addr; |
| 269 | memset(&server_addr, 0, sizeof(server_addr)); |
| 270 | memset(&client_addr, 0, sizeof(client_addr)); |
| 271 | |
| 272 | int listen_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 273 | if (listen_fd < 0) |
| 274 | LOG(FATAL) << "socket() failed"; |
| 275 | |
| 276 | server_addr.sin_family = AF_INET; |
| 277 | server_addr.sin_addr.s_addr = INADDR_ANY; |
| 278 | server_addr.sin_port = htons(kPort); |
| 279 | |
| 280 | { |
| 281 | // Get rid of "Address in use" error |
| 282 | int tr = 1; |
| 283 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &tr, |
| 284 | sizeof(int)) == -1) { |
| 285 | perror("setsockopt"); |
| 286 | exit(1); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (bind(listen_fd, reinterpret_cast<struct sockaddr *>(&server_addr), |
| 291 | sizeof(server_addr)) < 0) { |
| 292 | perror("bind"); |
| 293 | exit(1); |
| 294 | } |
| 295 | CHECK_EQ(listen(listen_fd,5), 0); |
| 296 | while (1) { |
| 297 | clilen = sizeof(client_addr); |
| 298 | int client_fd = accept(listen_fd, |
| 299 | (struct sockaddr *) &client_addr, |
| 300 | &clilen); |
| 301 | LOG(INFO) << "got past accept"; |
| 302 | if (client_fd < 0) |
| 303 | LOG(FATAL) << "ERROR on accept"; |
| 304 | HandleConnection(client_fd); |
| 305 | } |
| 306 | return 0; |
| 307 | } |