Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 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> |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 29 | #include <base/string_split.h> |
| 30 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame] | 31 | #include <base/stringprintf.h> |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 32 | |
| 33 | #include "update_engine/http_common.h" |
| 34 | #include "update_engine/http_fetcher_unittest.h" |
| 35 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 36 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 37 | // HTTP end-of-line delimiter; sorry, this needs to be a macro. |
| 38 | #define EOL "\r\n" |
| 39 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 40 | using std::min; |
| 41 | using std::string; |
| 42 | using std::vector; |
| 43 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 44 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 45 | namespace chromeos_update_engine { |
| 46 | |
| 47 | struct HttpRequest { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 48 | HttpRequest() |
| 49 | : start_offset(0), end_offset(0), return_code(kHttpResponseOk) {} |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 50 | string host; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | string url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 52 | off_t start_offset; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 53 | off_t end_offset; // non-inclusive, zero indicates unspecified. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 54 | HttpResponseCode return_code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 57 | bool ParseRequest(int fd, HttpRequest* request) { |
| 58 | string headers; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 59 | do { |
| 60 | char buf[1024]; |
| 61 | ssize_t r = read(fd, buf, sizeof(buf)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 62 | if (r < 0) { |
| 63 | perror("read"); |
| 64 | exit(1); |
| 65 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 66 | headers.append(buf, r); |
| 67 | } while (!EndsWith(headers, EOL EOL, true)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 68 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 69 | LOG(INFO) << "got headers:\n--8<------8<------8<------8<----\n" |
| 70 | << headers |
| 71 | << "\n--8<------8<------8<------8<----"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 72 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 73 | // Break header into lines. |
| 74 | std::vector<string> lines; |
| 75 | base::SplitStringUsingSubstr( |
| 76 | headers.substr(0, headers.length() - strlen(EOL EOL)), EOL, &lines); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 77 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 78 | // Decode URL line. |
| 79 | std::vector<string> terms; |
| 80 | base::SplitStringAlongWhitespace(lines[0], &terms); |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 81 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(3)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 82 | CHECK_EQ(terms[0], "GET"); |
| 83 | request->url = terms[1]; |
| 84 | LOG(INFO) << "URL: " << request->url; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 85 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 86 | // Decode remaining lines. |
| 87 | size_t i; |
| 88 | for (i = 1; i < lines.size(); i++) { |
| 89 | std::vector<string> terms; |
| 90 | base::SplitStringAlongWhitespace(lines[i], &terms); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 91 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 92 | if (terms[0] == "Range:") { |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 93 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 94 | string &range = terms[1]; |
| 95 | LOG(INFO) << "range attribute: " << range; |
| 96 | CHECK(StartsWithASCII(range, "bytes=", true) && |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 97 | range.find('-') != string::npos); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 98 | request->start_offset = atoll(range.c_str() + strlen("bytes=")); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 99 | // Decode end offset and increment it by one (so it is non-inclusive). |
| 100 | if (range.find('-') < range.length() - 1) |
| 101 | request->end_offset = atoll(range.c_str() + range.find('-') + 1) + 1; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 102 | request->return_code = kHttpResponsePartialContent; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 103 | std::string tmp_str = StringPrintf("decoded range offsets: start=%jd " |
| 104 | "end=", request->start_offset); |
| 105 | if (request->end_offset > 0) |
| 106 | base::StringAppendF(&tmp_str, "%jd (non-inclusive)", |
| 107 | request->end_offset); |
| 108 | else |
| 109 | base::StringAppendF(&tmp_str, "unspecified"); |
| 110 | LOG(INFO) << tmp_str; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 111 | } else if (terms[0] == "Host:") { |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 112 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 113 | request->host = terms[1]; |
| 114 | LOG(INFO) << "host attribute: " << request->host; |
| 115 | } else { |
| 116 | LOG(WARNING) << "ignoring HTTP attribute: `" << lines[i] << "'"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 117 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 118 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 119 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 120 | return true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | string Itoa(off_t num) { |
| 124 | char buf[100] = {0}; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 125 | snprintf(buf, sizeof(buf), "%" PRIi64, num); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 126 | return buf; |
| 127 | } |
| 128 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 129 | // Writes a string into a file. Returns total number of bytes written or -1 if a |
| 130 | // write error occurred. |
| 131 | ssize_t WriteString(int fd, const string& str) { |
| 132 | const size_t total_size = str.size(); |
| 133 | size_t remaining_size = total_size; |
| 134 | char const *data = str.data(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 135 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 136 | while (remaining_size) { |
| 137 | ssize_t written = write(fd, data, remaining_size); |
| 138 | if (written < 0) { |
| 139 | perror("write"); |
| 140 | LOG(INFO) << "write failed"; |
| 141 | return -1; |
| 142 | } |
| 143 | data += written; |
| 144 | remaining_size -= written; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 145 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 146 | |
| 147 | return total_size; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 150 | // Writes the headers of an HTTP response into a file. |
| 151 | ssize_t WriteHeaders(int fd, const off_t start_offset, const off_t end_offset, |
| 152 | HttpResponseCode return_code) { |
| 153 | ssize_t written = 0, ret; |
| 154 | |
| 155 | ret = WriteString(fd, |
| 156 | string("HTTP/1.1 ") + Itoa(return_code) + " " + |
| 157 | GetHttpResponseDescription(return_code) + |
| 158 | EOL |
| 159 | "Content-Type: application/octet-stream" EOL); |
| 160 | if (ret < 0) |
| 161 | return -1; |
| 162 | written += ret; |
| 163 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 164 | // Compute content legnth. |
| 165 | const off_t content_length = end_offset - start_offset;; |
| 166 | |
| 167 | // A start offset that equals the end offset indicates that the response |
| 168 | // should contain the full range of bytes in the requested resource. |
| 169 | if (start_offset || start_offset == end_offset) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 170 | ret = WriteString(fd, |
| 171 | string("Accept-Ranges: bytes" EOL |
| 172 | "Content-Range: bytes ") + |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 173 | Itoa(start_offset == end_offset ? 0 : start_offset) + |
| 174 | "-" + Itoa(end_offset - 1) + "/" + Itoa(end_offset) + |
| 175 | EOL); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 176 | if (ret < 0) |
| 177 | return -1; |
| 178 | written += ret; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | ret = WriteString(fd, string("Content-Length: ") + Itoa(content_length) + |
| 182 | EOL EOL); |
| 183 | if (ret < 0) |
| 184 | return -1; |
| 185 | written += ret; |
| 186 | |
| 187 | return written; |
| 188 | } |
| 189 | |
| 190 | // Writes a predetermined payload of lines of ascending bytes to a file. The |
| 191 | // first byte of output is appropriately offset with respect to the request line |
| 192 | // length. Returns the number of successfully written bytes. |
| 193 | size_t WritePayload(int fd, const off_t start_offset, const off_t end_offset, |
| 194 | const char first_byte, const size_t line_len) { |
| 195 | CHECK_LE(start_offset, end_offset); |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 196 | CHECK_GT(line_len, static_cast<size_t>(0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 197 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 198 | LOG(INFO) << "writing payload: " << line_len << "-byte lines starting with `" |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 199 | << first_byte << "', offset range " << start_offset << " -> " |
| 200 | << end_offset; |
| 201 | |
| 202 | // Populate line of ascending characters. |
| 203 | string line; |
| 204 | line.reserve(line_len); |
| 205 | char byte = first_byte; |
| 206 | size_t i; |
| 207 | for (i = 0; i < line_len; i++) |
| 208 | line += byte++; |
| 209 | |
| 210 | const size_t total_len = end_offset - start_offset; |
| 211 | size_t remaining_len = total_len; |
| 212 | bool success = true; |
| 213 | |
| 214 | // If start offset is not aligned with line boundary, output partial line up |
| 215 | // to the first line boundary. |
| 216 | size_t start_modulo = start_offset % line_len; |
| 217 | if (start_modulo) { |
| 218 | string partial = line.substr(start_modulo, remaining_len); |
| 219 | ssize_t ret = WriteString(fd, partial); |
| 220 | if ((success = (ret >= 0 && (size_t) ret == partial.length()))) |
| 221 | remaining_len -= partial.length(); |
| 222 | } |
| 223 | |
| 224 | // Output full lines up to the maximal line boundary below the end offset. |
| 225 | while (success && remaining_len >= line_len) { |
| 226 | ssize_t ret = WriteString(fd, line); |
| 227 | if ((success = (ret >= 0 && (size_t) ret == line_len))) |
| 228 | remaining_len -= line_len; |
| 229 | } |
| 230 | |
| 231 | // Output a partial line up to the end offset. |
| 232 | if (success && remaining_len) { |
| 233 | string partial = line.substr(0, remaining_len); |
| 234 | ssize_t ret = WriteString(fd, partial); |
| 235 | if ((success = (ret >= 0 && (size_t) ret == partial.length()))) |
| 236 | remaining_len -= partial.length(); |
| 237 | } |
| 238 | |
| 239 | return (total_len - remaining_len); |
| 240 | } |
| 241 | |
| 242 | // Write default payload lines of the form 'abcdefghij'. |
| 243 | inline size_t WritePayload(int fd, const off_t start_offset, |
| 244 | const off_t end_offset) { |
| 245 | return WritePayload(fd, start_offset, end_offset, 'a', 10); |
| 246 | } |
| 247 | |
| 248 | // Send an empty response, then kill the server. |
| 249 | void HandleQuit(int fd) { |
| 250 | WriteHeaders(fd, 0, 0, kHttpResponseOk); |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 251 | LOG(INFO) << "pid(" << getpid() << "): HTTP server exiting ..."; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 252 | exit(0); |
| 253 | } |
| 254 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 255 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 256 | // Generates an HTTP response with payload corresponding to requested offsets |
| 257 | // and length. Optionally, truncate the payload at a given length and add a |
| 258 | // pause midway through the transfer. Returns the total number of bytes |
| 259 | // delivered or -1 for error. |
| 260 | ssize_t HandleGet(int fd, const HttpRequest& request, const size_t total_length, |
| 261 | const size_t truncate_length, const int sleep_every, |
| 262 | const int sleep_secs) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 263 | ssize_t ret; |
| 264 | size_t written = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 265 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 266 | // Obtain start offset, make sure it is within total payload length. |
| 267 | const size_t start_offset = request.start_offset; |
| 268 | if (start_offset >= total_length) { |
| 269 | LOG(WARNING) << "start offset (" << start_offset |
| 270 | << ") exceeds total length (" << total_length |
| 271 | << "), generating error response (" |
| 272 | << kHttpResponseReqRangeNotSat << ")"; |
| 273 | return WriteHeaders(fd, total_length, total_length, |
| 274 | kHttpResponseReqRangeNotSat); |
| 275 | } |
| 276 | |
| 277 | // Obtain end offset, adjust to fit in total payload length and ensure it does |
| 278 | // not preceded the start offset. |
| 279 | size_t end_offset = (request.end_offset > 0 ? |
| 280 | request.end_offset : total_length); |
| 281 | if (end_offset < start_offset) { |
| 282 | LOG(WARNING) << "end offset (" << end_offset << ") precedes start offset (" |
| 283 | << start_offset << "), generating error response"; |
| 284 | return WriteHeaders(fd, 0, 0, kHttpResponseBadRequest); |
| 285 | } |
| 286 | if (end_offset > total_length) { |
| 287 | LOG(INFO) << "requested end offset (" << end_offset |
| 288 | << ") exceeds total length (" << total_length << "), adjusting"; |
| 289 | end_offset = total_length; |
| 290 | } |
| 291 | |
| 292 | // Generate headers |
| 293 | LOG(INFO) << "generating response header: range=" << start_offset << "-" |
| 294 | << (end_offset - 1) << "/" << (end_offset - start_offset) |
| 295 | << ", return code=" << request.return_code; |
| 296 | if ((ret = WriteHeaders(fd, start_offset, end_offset, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 297 | request.return_code)) < 0) |
| 298 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 299 | LOG(INFO) << ret << " header bytes written"; |
| 300 | written += ret; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 301 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 302 | // Compute payload length, truncate as necessary. |
| 303 | size_t payload_length = end_offset - start_offset; |
| 304 | if (truncate_length > 0 && truncate_length < payload_length) { |
| 305 | LOG(INFO) << "truncating request payload length (" << payload_length |
| 306 | << ") at " << truncate_length; |
| 307 | payload_length = truncate_length; |
| 308 | end_offset = start_offset + payload_length; |
| 309 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 310 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 311 | LOG(INFO) << "generating response payload: range=" << start_offset << "-" |
| 312 | << (end_offset - 1) << "/" << (end_offset - start_offset); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 313 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 314 | // Decide about optional midway delay. |
| 315 | if (truncate_length > 0 && sleep_every > 0 && sleep_secs >= 0 && |
| 316 | start_offset % (truncate_length * sleep_every) == 0) { |
| 317 | const off_t midway_offset = start_offset + payload_length / 2; |
| 318 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 319 | if ((ret = WritePayload(fd, start_offset, midway_offset)) < 0) |
| 320 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 321 | LOG(INFO) << ret << " payload bytes written (first chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 322 | written += ret; |
| 323 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 324 | LOG(INFO) << "sleeping for " << sleep_secs << " seconds..."; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 325 | sleep(sleep_secs); |
| 326 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 327 | if ((ret = WritePayload(fd, midway_offset, end_offset)) < 0) |
| 328 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 329 | LOG(INFO) << ret << " payload bytes written (second chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 330 | written += ret; |
| 331 | } else { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 332 | if ((ret = WritePayload(fd, start_offset, end_offset)) < 0) |
| 333 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 334 | LOG(INFO) << ret << " payload bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 335 | written += ret; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 336 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 337 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 338 | LOG(INFO) << "response generation complete, " << written |
| 339 | << " total bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 340 | return written; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 343 | ssize_t HandleGet(int fd, const HttpRequest& request, |
| 344 | const size_t total_length) { |
| 345 | return HandleGet(fd, request, total_length, 0, 0, 0); |
| 346 | } |
| 347 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 348 | // Handles /redirect/<code>/<url> requests by returning the specified |
| 349 | // redirect <code> with a location pointing to /<url>. |
| 350 | void HandleRedirect(int fd, const HttpRequest& request) { |
| 351 | LOG(INFO) << "Redirecting..."; |
| 352 | string url = request.url; |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 353 | CHECK_EQ(static_cast<size_t>(0), url.find("/redirect/")); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 354 | url.erase(0, strlen("/redirect/")); |
| 355 | string::size_type url_start = url.find('/'); |
| 356 | CHECK_NE(url_start, string::npos); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 357 | HttpResponseCode code = StringToHttpResponseCode(url.c_str()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 358 | url.erase(0, url_start); |
| 359 | url = "http://" + request.host + url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 360 | const char *status = GetHttpResponseDescription(code); |
| 361 | if (!status) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 362 | CHECK(false) << "Unrecognized redirection code: " << code; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 363 | LOG(INFO) << "Code: " << code << " " << status; |
| 364 | LOG(INFO) << "New URL: " << url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 365 | |
| 366 | ssize_t ret; |
| 367 | if ((ret = WriteString(fd, "HTTP/1.1 " + Itoa(code) + " " + |
| 368 | status + EOL)) < 0) |
| 369 | return; |
| 370 | WriteString(fd, "Location: " + url + EOL); |
| 371 | } |
| 372 | |
| 373 | // Generate a page not found error response with actual text payload. Return |
| 374 | // number of bytes written or -1 for error. |
| 375 | ssize_t HandleError(int fd, const HttpRequest& request) { |
| 376 | LOG(INFO) << "Generating error HTTP response"; |
| 377 | |
| 378 | ssize_t ret; |
| 379 | size_t written = 0; |
| 380 | |
| 381 | const string data("This is an error page."); |
| 382 | |
| 383 | if ((ret = WriteHeaders(fd, 0, data.size(), kHttpResponseNotFound)) < 0) |
| 384 | return -1; |
| 385 | written += ret; |
| 386 | |
| 387 | if ((ret = WriteString(fd, data)) < 0) |
| 388 | return -1; |
| 389 | written += ret; |
| 390 | |
| 391 | return written; |
| 392 | } |
| 393 | |
| 394 | // Generate an error response if the requested offset is nonzero, up to a given |
| 395 | // maximal number of successive failures. The error generated is an "Internal |
| 396 | // Server Error" (500). |
| 397 | ssize_t HandleErrorIfOffset(int fd, const HttpRequest& request, |
| 398 | size_t end_offset, int max_fails) { |
| 399 | static int num_fails = 0; |
| 400 | |
| 401 | if (request.start_offset > 0 && num_fails < max_fails) { |
| 402 | LOG(INFO) << "Generating error HTTP response"; |
| 403 | |
| 404 | ssize_t ret; |
| 405 | size_t written = 0; |
| 406 | |
| 407 | const string data("This is an error page."); |
| 408 | |
| 409 | if ((ret = WriteHeaders(fd, 0, data.size(), |
| 410 | kHttpResponseInternalServerError)) < 0) |
| 411 | return -1; |
| 412 | written += ret; |
| 413 | |
| 414 | if ((ret = WriteString(fd, data)) < 0) |
| 415 | return -1; |
| 416 | written += ret; |
| 417 | |
| 418 | num_fails++; |
| 419 | return written; |
| 420 | } else { |
| 421 | num_fails = 0; |
| 422 | return HandleGet(fd, request, end_offset); |
| 423 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 424 | } |
| 425 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 426 | void HandleDefault(int fd, const HttpRequest& request) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 427 | const off_t start_offset = request.start_offset; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 428 | const string data("unhandled path"); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 429 | const size_t size = data.size(); |
| 430 | ssize_t ret; |
| 431 | |
| 432 | if ((ret = WriteHeaders(fd, start_offset, size, request.return_code)) < 0) |
| 433 | return; |
| 434 | WriteString(fd, (start_offset < static_cast<off_t>(size) ? |
| 435 | data.substr(start_offset) : "")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 438 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 439 | // Break a URL into terms delimited by slashes. |
| 440 | class UrlTerms { |
| 441 | public: |
| 442 | UrlTerms(string &url, size_t num_terms) { |
| 443 | // URL must be non-empty and start with a slash. |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 444 | CHECK_GT(url.size(), static_cast<size_t>(0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 445 | CHECK_EQ(url[0], '/'); |
| 446 | |
| 447 | // Split it into terms delimited by slashes, omitting the preceeding slash. |
| 448 | base::SplitStringDontTrim(url.substr(1), '/', &terms); |
| 449 | |
| 450 | // Ensure expected length. |
| 451 | CHECK_EQ(terms.size(), num_terms); |
| 452 | } |
| 453 | |
| 454 | inline string Get(const off_t index) const { |
| 455 | return terms[index]; |
| 456 | } |
| 457 | inline const char *GetCStr(const off_t index) const { |
| 458 | return Get(index).c_str(); |
| 459 | } |
| 460 | inline int GetInt(const off_t index) const { |
| 461 | return atoi(GetCStr(index)); |
| 462 | } |
| 463 | inline long GetLong(const off_t index) const { |
| 464 | return atol(GetCStr(index)); |
| 465 | } |
| 466 | |
| 467 | private: |
| 468 | std::vector<string> terms; |
| 469 | }; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 470 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 471 | void HandleConnection(int fd) { |
| 472 | HttpRequest request; |
| 473 | ParseRequest(fd, &request); |
| 474 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 475 | string &url = request.url; |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 476 | LOG(INFO) << "pid(" << getpid() << "): handling url " << url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 477 | if (url == "/quitquitquit") { |
| 478 | HandleQuit(fd); |
| 479 | } else if (StartsWithASCII(url, "/download/", true)) { |
| 480 | const UrlTerms terms(url, 2); |
| 481 | HandleGet(fd, request, terms.GetLong(1)); |
| 482 | } else if (StartsWithASCII(url, "/flaky/", true)) { |
| 483 | const UrlTerms terms(url, 5); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 484 | HandleGet(fd, request, terms.GetLong(1), terms.GetLong(2), terms.GetLong(3), |
| 485 | terms.GetLong(4)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 486 | } else if (url.find("/redirect/") == 0) { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 487 | HandleRedirect(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 488 | } else if (url == "/error") { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 489 | HandleError(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 490 | } else if (StartsWithASCII(url, "/error-if-offset/", true)) { |
| 491 | const UrlTerms terms(url, 3); |
| 492 | HandleErrorIfOffset(fd, request, terms.GetLong(1), terms.GetInt(2)); |
| 493 | } else { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 494 | HandleDefault(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 495 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 496 | |
| 497 | close(fd); |
| 498 | } |
| 499 | |
| 500 | } // namespace chromeos_update_engine |
| 501 | |
| 502 | using namespace chromeos_update_engine; |
| 503 | |
| 504 | int main(int argc, char** argv) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 505 | // Ignore SIGPIPE on write() to sockets. |
| 506 | signal(SIGPIPE, SIG_IGN); |
Darin Petkov | f67bb1f | 2010-11-08 16:10:26 -0800 | [diff] [blame] | 507 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 508 | socklen_t clilen; |
| 509 | struct sockaddr_in server_addr; |
| 510 | struct sockaddr_in client_addr; |
| 511 | memset(&server_addr, 0, sizeof(server_addr)); |
| 512 | memset(&client_addr, 0, sizeof(client_addr)); |
| 513 | |
| 514 | int listen_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 515 | if (listen_fd < 0) |
| 516 | LOG(FATAL) << "socket() failed"; |
| 517 | |
| 518 | server_addr.sin_family = AF_INET; |
| 519 | server_addr.sin_addr.s_addr = INADDR_ANY; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 520 | server_addr.sin_port = htons(kServerPort); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 521 | |
| 522 | { |
| 523 | // Get rid of "Address in use" error |
| 524 | int tr = 1; |
| 525 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &tr, |
| 526 | sizeof(int)) == -1) { |
| 527 | perror("setsockopt"); |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 528 | exit(2); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | if (bind(listen_fd, reinterpret_cast<struct sockaddr *>(&server_addr), |
| 533 | sizeof(server_addr)) < 0) { |
| 534 | perror("bind"); |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 535 | exit(3); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 536 | } |
| 537 | CHECK_EQ(listen(listen_fd,5), 0); |
| 538 | while (1) { |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 539 | LOG(INFO) << "pid(" << getpid() << "): waiting to accept new connection"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 540 | clilen = sizeof(client_addr); |
| 541 | int client_fd = accept(listen_fd, |
| 542 | (struct sockaddr *) &client_addr, |
| 543 | &clilen); |
| 544 | LOG(INFO) << "got past accept"; |
| 545 | if (client_fd < 0) |
| 546 | LOG(FATAL) << "ERROR on accept"; |
| 547 | HandleConnection(client_fd); |
| 548 | } |
| 549 | return 0; |
| 550 | } |