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); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 251 | exit(0); |
| 252 | } |
| 253 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 254 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 255 | // Generates an HTTP response with payload corresponding to requested offsets |
| 256 | // and length. Optionally, truncate the payload at a given length and add a |
| 257 | // pause midway through the transfer. Returns the total number of bytes |
| 258 | // delivered or -1 for error. |
| 259 | ssize_t HandleGet(int fd, const HttpRequest& request, const size_t total_length, |
| 260 | const size_t truncate_length, const int sleep_every, |
| 261 | const int sleep_secs) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 262 | ssize_t ret; |
| 263 | size_t written = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 264 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 265 | // Obtain start offset, make sure it is within total payload length. |
| 266 | const size_t start_offset = request.start_offset; |
| 267 | if (start_offset >= total_length) { |
| 268 | LOG(WARNING) << "start offset (" << start_offset |
| 269 | << ") exceeds total length (" << total_length |
| 270 | << "), generating error response (" |
| 271 | << kHttpResponseReqRangeNotSat << ")"; |
| 272 | return WriteHeaders(fd, total_length, total_length, |
| 273 | kHttpResponseReqRangeNotSat); |
| 274 | } |
| 275 | |
| 276 | // Obtain end offset, adjust to fit in total payload length and ensure it does |
| 277 | // not preceded the start offset. |
| 278 | size_t end_offset = (request.end_offset > 0 ? |
| 279 | request.end_offset : total_length); |
| 280 | if (end_offset < start_offset) { |
| 281 | LOG(WARNING) << "end offset (" << end_offset << ") precedes start offset (" |
| 282 | << start_offset << "), generating error response"; |
| 283 | return WriteHeaders(fd, 0, 0, kHttpResponseBadRequest); |
| 284 | } |
| 285 | if (end_offset > total_length) { |
| 286 | LOG(INFO) << "requested end offset (" << end_offset |
| 287 | << ") exceeds total length (" << total_length << "), adjusting"; |
| 288 | end_offset = total_length; |
| 289 | } |
| 290 | |
| 291 | // Generate headers |
| 292 | LOG(INFO) << "generating response header: range=" << start_offset << "-" |
| 293 | << (end_offset - 1) << "/" << (end_offset - start_offset) |
| 294 | << ", return code=" << request.return_code; |
| 295 | if ((ret = WriteHeaders(fd, start_offset, end_offset, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 296 | request.return_code)) < 0) |
| 297 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 298 | LOG(INFO) << ret << " header bytes written"; |
| 299 | written += ret; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 300 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 301 | // Compute payload length, truncate as necessary. |
| 302 | size_t payload_length = end_offset - start_offset; |
| 303 | if (truncate_length > 0 && truncate_length < payload_length) { |
| 304 | LOG(INFO) << "truncating request payload length (" << payload_length |
| 305 | << ") at " << truncate_length; |
| 306 | payload_length = truncate_length; |
| 307 | end_offset = start_offset + payload_length; |
| 308 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 309 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 310 | LOG(INFO) << "generating response payload: range=" << start_offset << "-" |
| 311 | << (end_offset - 1) << "/" << (end_offset - start_offset); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 312 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 313 | // Decide about optional midway delay. |
| 314 | if (truncate_length > 0 && sleep_every > 0 && sleep_secs >= 0 && |
| 315 | start_offset % (truncate_length * sleep_every) == 0) { |
| 316 | const off_t midway_offset = start_offset + payload_length / 2; |
| 317 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 318 | if ((ret = WritePayload(fd, start_offset, midway_offset)) < 0) |
| 319 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 320 | LOG(INFO) << ret << " payload bytes written (first chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 321 | written += ret; |
| 322 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 323 | LOG(INFO) << "sleeping for " << sleep_secs << " seconds..."; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 324 | sleep(sleep_secs); |
| 325 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 326 | if ((ret = WritePayload(fd, midway_offset, end_offset)) < 0) |
| 327 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 328 | LOG(INFO) << ret << " payload bytes written (second chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 329 | written += ret; |
| 330 | } else { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 331 | if ((ret = WritePayload(fd, start_offset, end_offset)) < 0) |
| 332 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 333 | LOG(INFO) << ret << " payload bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 334 | written += ret; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 335 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 336 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 337 | LOG(INFO) << "response generation complete, " << written |
| 338 | << " total bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 339 | return written; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 342 | ssize_t HandleGet(int fd, const HttpRequest& request, |
| 343 | const size_t total_length) { |
| 344 | return HandleGet(fd, request, total_length, 0, 0, 0); |
| 345 | } |
| 346 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 347 | // Handles /redirect/<code>/<url> requests by returning the specified |
| 348 | // redirect <code> with a location pointing to /<url>. |
| 349 | void HandleRedirect(int fd, const HttpRequest& request) { |
| 350 | LOG(INFO) << "Redirecting..."; |
| 351 | string url = request.url; |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 352 | CHECK_EQ(static_cast<size_t>(0), url.find("/redirect/")); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 353 | url.erase(0, strlen("/redirect/")); |
| 354 | string::size_type url_start = url.find('/'); |
| 355 | CHECK_NE(url_start, string::npos); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 356 | HttpResponseCode code = StringToHttpResponseCode(url.c_str()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 357 | url.erase(0, url_start); |
| 358 | url = "http://" + request.host + url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 359 | const char *status = GetHttpResponseDescription(code); |
| 360 | if (!status) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 361 | CHECK(false) << "Unrecognized redirection code: " << code; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 362 | LOG(INFO) << "Code: " << code << " " << status; |
| 363 | LOG(INFO) << "New URL: " << url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 364 | |
| 365 | ssize_t ret; |
| 366 | if ((ret = WriteString(fd, "HTTP/1.1 " + Itoa(code) + " " + |
| 367 | status + EOL)) < 0) |
| 368 | return; |
| 369 | WriteString(fd, "Location: " + url + EOL); |
| 370 | } |
| 371 | |
| 372 | // Generate a page not found error response with actual text payload. Return |
| 373 | // number of bytes written or -1 for error. |
| 374 | ssize_t HandleError(int fd, const HttpRequest& request) { |
| 375 | LOG(INFO) << "Generating error HTTP response"; |
| 376 | |
| 377 | ssize_t ret; |
| 378 | size_t written = 0; |
| 379 | |
| 380 | const string data("This is an error page."); |
| 381 | |
| 382 | if ((ret = WriteHeaders(fd, 0, data.size(), kHttpResponseNotFound)) < 0) |
| 383 | return -1; |
| 384 | written += ret; |
| 385 | |
| 386 | if ((ret = WriteString(fd, data)) < 0) |
| 387 | return -1; |
| 388 | written += ret; |
| 389 | |
| 390 | return written; |
| 391 | } |
| 392 | |
| 393 | // Generate an error response if the requested offset is nonzero, up to a given |
| 394 | // maximal number of successive failures. The error generated is an "Internal |
| 395 | // Server Error" (500). |
| 396 | ssize_t HandleErrorIfOffset(int fd, const HttpRequest& request, |
| 397 | size_t end_offset, int max_fails) { |
| 398 | static int num_fails = 0; |
| 399 | |
| 400 | if (request.start_offset > 0 && num_fails < max_fails) { |
| 401 | LOG(INFO) << "Generating error HTTP response"; |
| 402 | |
| 403 | ssize_t ret; |
| 404 | size_t written = 0; |
| 405 | |
| 406 | const string data("This is an error page."); |
| 407 | |
| 408 | if ((ret = WriteHeaders(fd, 0, data.size(), |
| 409 | kHttpResponseInternalServerError)) < 0) |
| 410 | return -1; |
| 411 | written += ret; |
| 412 | |
| 413 | if ((ret = WriteString(fd, data)) < 0) |
| 414 | return -1; |
| 415 | written += ret; |
| 416 | |
| 417 | num_fails++; |
| 418 | return written; |
| 419 | } else { |
| 420 | num_fails = 0; |
| 421 | return HandleGet(fd, request, end_offset); |
| 422 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 423 | } |
| 424 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 425 | void HandleDefault(int fd, const HttpRequest& request) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 426 | const off_t start_offset = request.start_offset; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 427 | const string data("unhandled path"); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 428 | const size_t size = data.size(); |
| 429 | ssize_t ret; |
| 430 | |
| 431 | if ((ret = WriteHeaders(fd, start_offset, size, request.return_code)) < 0) |
| 432 | return; |
| 433 | WriteString(fd, (start_offset < static_cast<off_t>(size) ? |
| 434 | data.substr(start_offset) : "")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 437 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 438 | // Break a URL into terms delimited by slashes. |
| 439 | class UrlTerms { |
| 440 | public: |
| 441 | UrlTerms(string &url, size_t num_terms) { |
| 442 | // URL must be non-empty and start with a slash. |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 443 | CHECK_GT(url.size(), static_cast<size_t>(0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 444 | CHECK_EQ(url[0], '/'); |
| 445 | |
| 446 | // Split it into terms delimited by slashes, omitting the preceeding slash. |
| 447 | base::SplitStringDontTrim(url.substr(1), '/', &terms); |
| 448 | |
| 449 | // Ensure expected length. |
| 450 | CHECK_EQ(terms.size(), num_terms); |
| 451 | } |
| 452 | |
| 453 | inline string Get(const off_t index) const { |
| 454 | return terms[index]; |
| 455 | } |
| 456 | inline const char *GetCStr(const off_t index) const { |
| 457 | return Get(index).c_str(); |
| 458 | } |
| 459 | inline int GetInt(const off_t index) const { |
| 460 | return atoi(GetCStr(index)); |
| 461 | } |
| 462 | inline long GetLong(const off_t index) const { |
| 463 | return atol(GetCStr(index)); |
| 464 | } |
| 465 | |
| 466 | private: |
| 467 | std::vector<string> terms; |
| 468 | }; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 469 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 470 | void HandleConnection(int fd) { |
| 471 | HttpRequest request; |
| 472 | ParseRequest(fd, &request); |
| 473 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 474 | string &url = request.url; |
| 475 | if (url == "/quitquitquit") { |
| 476 | HandleQuit(fd); |
| 477 | } else if (StartsWithASCII(url, "/download/", true)) { |
| 478 | const UrlTerms terms(url, 2); |
| 479 | HandleGet(fd, request, terms.GetLong(1)); |
| 480 | } else if (StartsWithASCII(url, "/flaky/", true)) { |
| 481 | const UrlTerms terms(url, 5); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 482 | HandleGet(fd, request, terms.GetLong(1), terms.GetLong(2), terms.GetLong(3), |
| 483 | terms.GetLong(4)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 484 | } else if (url.find("/redirect/") == 0) { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 485 | HandleRedirect(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 486 | } else if (url == "/error") { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 487 | HandleError(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 488 | } else if (StartsWithASCII(url, "/error-if-offset/", true)) { |
| 489 | const UrlTerms terms(url, 3); |
| 490 | HandleErrorIfOffset(fd, request, terms.GetLong(1), terms.GetInt(2)); |
| 491 | } else { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 492 | HandleDefault(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 493 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 494 | |
| 495 | close(fd); |
| 496 | } |
| 497 | |
| 498 | } // namespace chromeos_update_engine |
| 499 | |
| 500 | using namespace chromeos_update_engine; |
| 501 | |
| 502 | int main(int argc, char** argv) { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 503 | // Ignore SIGPIPE on write() to sockets. |
| 504 | signal(SIGPIPE, SIG_IGN); |
Darin Petkov | f67bb1f | 2010-11-08 16:10:26 -0800 | [diff] [blame] | 505 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 506 | socklen_t clilen; |
| 507 | struct sockaddr_in server_addr; |
| 508 | struct sockaddr_in client_addr; |
| 509 | memset(&server_addr, 0, sizeof(server_addr)); |
| 510 | memset(&client_addr, 0, sizeof(client_addr)); |
| 511 | |
| 512 | int listen_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 513 | if (listen_fd < 0) |
| 514 | LOG(FATAL) << "socket() failed"; |
| 515 | |
| 516 | server_addr.sin_family = AF_INET; |
| 517 | server_addr.sin_addr.s_addr = INADDR_ANY; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 518 | server_addr.sin_port = htons(kServerPort); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 519 | |
| 520 | { |
| 521 | // Get rid of "Address in use" error |
| 522 | int tr = 1; |
| 523 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &tr, |
| 524 | sizeof(int)) == -1) { |
| 525 | perror("setsockopt"); |
| 526 | exit(1); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if (bind(listen_fd, reinterpret_cast<struct sockaddr *>(&server_addr), |
| 531 | sizeof(server_addr)) < 0) { |
| 532 | perror("bind"); |
| 533 | exit(1); |
| 534 | } |
| 535 | CHECK_EQ(listen(listen_fd,5), 0); |
| 536 | while (1) { |
| 537 | clilen = sizeof(client_addr); |
| 538 | int client_fd = accept(listen_fd, |
| 539 | (struct sockaddr *) &client_addr, |
| 540 | &clilen); |
| 541 | LOG(INFO) << "got past accept"; |
| 542 | if (client_fd < 0) |
| 543 | LOG(FATAL) << "ERROR on accept"; |
| 544 | HandleConnection(client_fd); |
| 545 | } |
| 546 | return 0; |
| 547 | } |