Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
| 17 | // This file implements a simple HTTP server. It can exhibit odd behavior |
| 18 | // that's useful for testing. For example, it's useful to test that |
| 19 | // the updater can continue a connection if it's dropped, or that it |
| 20 | // handles very slow data transfers. |
| 21 | |
| 22 | // To use this, simply make an HTTP connection to localhost:port and |
| 23 | // GET a url. |
| 24 | |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 25 | #include <err.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 27 | #include <fcntl.h> |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 28 | #include <inttypes.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 29 | #include <netinet/in.h> |
| 30 | #include <signal.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 34 | #include <sys/socket.h> |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 35 | #include <sys/stat.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 36 | #include <sys/types.h> |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 37 | #include <unistd.h> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 38 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 39 | #include <algorithm> |
| 40 | #include <string> |
| 41 | #include <vector> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 42 | |
| 43 | #include <base/logging.h> |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 44 | #include <base/posix/eintr_wrapper.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 45 | #include <base/strings/string_split.h> |
| 46 | #include <base/strings/string_util.h> |
| 47 | #include <base/strings/stringprintf.h> |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 48 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 49 | #include "update_engine/common/http_common.h" |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 50 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 51 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 52 | // HTTP end-of-line delimiter; sorry, this needs to be a macro. |
| 53 | #define EOL "\r\n" |
| 54 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | using std::string; |
| 56 | using std::vector; |
| 57 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 58 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 59 | namespace chromeos_update_engine { |
| 60 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 61 | static const char* kListeningMsgPrefix = "listening on port "; |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 62 | |
| 63 | enum { |
| 64 | RC_OK = 0, |
| 65 | RC_BAD_ARGS, |
| 66 | RC_ERR_READ, |
| 67 | RC_ERR_SETSOCKOPT, |
| 68 | RC_ERR_BIND, |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 69 | RC_ERR_LISTEN, |
| 70 | RC_ERR_GETSOCKNAME, |
| 71 | RC_ERR_REPORT, |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 74 | struct HttpRequest { |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 75 | HttpRequest() |
| 76 | : start_offset(0), end_offset(0), return_code(kHttpResponseOk) {} |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 77 | string host; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 78 | string url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 79 | off_t start_offset; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 80 | off_t end_offset; // non-inclusive, zero indicates unspecified. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 81 | HttpResponseCode return_code; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 84 | bool ParseRequest(int fd, HttpRequest* request) { |
| 85 | string headers; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 86 | do { |
| 87 | char buf[1024]; |
| 88 | ssize_t r = read(fd, buf, sizeof(buf)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 89 | if (r < 0) { |
| 90 | perror("read"); |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 91 | exit(RC_ERR_READ); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 92 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 93 | headers.append(buf, r); |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 94 | } while (!base::EndsWith(headers, EOL EOL, base::CompareCase::SENSITIVE)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 95 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 96 | LOG(INFO) << "got headers:\n--8<------8<------8<------8<----\n" |
| 97 | << headers |
| 98 | << "\n--8<------8<------8<------8<----"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 99 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 100 | // Break header into lines. |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 101 | vector<string> lines; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 102 | base::SplitStringUsingSubstr( |
| 103 | headers.substr(0, headers.length() - strlen(EOL EOL)), EOL, &lines); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 104 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 105 | // Decode URL line. |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 106 | vector<string> terms = base::SplitString(lines[0], base::kWhitespaceASCII, |
| 107 | base::KEEP_WHITESPACE, |
| 108 | base::SPLIT_WANT_NONEMPTY); |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 109 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(3)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 110 | CHECK_EQ(terms[0], "GET"); |
| 111 | request->url = terms[1]; |
| 112 | LOG(INFO) << "URL: " << request->url; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 114 | // Decode remaining lines. |
| 115 | size_t i; |
| 116 | for (i = 1; i < lines.size(); i++) { |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 117 | terms = base::SplitString(lines[i], base::kWhitespaceASCII, |
| 118 | base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 119 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 120 | if (terms[0] == "Range:") { |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 121 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 122 | string &range = terms[1]; |
| 123 | LOG(INFO) << "range attribute: " << range; |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 124 | CHECK(base::StartsWith(range, "bytes=", base::CompareCase::SENSITIVE) && |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 125 | range.find('-') != string::npos); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 126 | request->start_offset = atoll(range.c_str() + strlen("bytes=")); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 127 | // Decode end offset and increment it by one (so it is non-inclusive). |
| 128 | if (range.find('-') < range.length() - 1) |
| 129 | request->end_offset = atoll(range.c_str() + range.find('-') + 1) + 1; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 130 | request->return_code = kHttpResponsePartialContent; |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 131 | string tmp_str = base::StringPrintf("decoded range offsets: " |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 132 | "start=%jd end=", |
| 133 | (intmax_t)request->start_offset); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 134 | if (request->end_offset > 0) |
| 135 | base::StringAppendF(&tmp_str, "%jd (non-inclusive)", |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 136 | (intmax_t)request->end_offset); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 137 | else |
| 138 | base::StringAppendF(&tmp_str, "unspecified"); |
| 139 | LOG(INFO) << tmp_str; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 140 | } else if (terms[0] == "Host:") { |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 141 | CHECK_EQ(terms.size(), static_cast<vector<string>::size_type>(2)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 142 | request->host = terms[1]; |
| 143 | LOG(INFO) << "host attribute: " << request->host; |
| 144 | } else { |
| 145 | LOG(WARNING) << "ignoring HTTP attribute: `" << lines[i] << "'"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 146 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 147 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 148 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 149 | return true; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | string Itoa(off_t num) { |
| 153 | char buf[100] = {0}; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 154 | snprintf(buf, sizeof(buf), "%" PRIi64, num); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 155 | return buf; |
| 156 | } |
| 157 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 158 | // Writes a string into a file. Returns total number of bytes written or -1 if a |
| 159 | // write error occurred. |
| 160 | ssize_t WriteString(int fd, const string& str) { |
| 161 | const size_t total_size = str.size(); |
| 162 | size_t remaining_size = total_size; |
| 163 | char const *data = str.data(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 164 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 165 | while (remaining_size) { |
| 166 | ssize_t written = write(fd, data, remaining_size); |
| 167 | if (written < 0) { |
| 168 | perror("write"); |
| 169 | LOG(INFO) << "write failed"; |
| 170 | return -1; |
| 171 | } |
| 172 | data += written; |
| 173 | remaining_size -= written; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 174 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 175 | |
| 176 | return total_size; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 179 | // Writes the headers of an HTTP response into a file. |
| 180 | ssize_t WriteHeaders(int fd, const off_t start_offset, const off_t end_offset, |
| 181 | HttpResponseCode return_code) { |
| 182 | ssize_t written = 0, ret; |
| 183 | |
| 184 | ret = WriteString(fd, |
| 185 | string("HTTP/1.1 ") + Itoa(return_code) + " " + |
| 186 | GetHttpResponseDescription(return_code) + |
| 187 | EOL |
| 188 | "Content-Type: application/octet-stream" EOL); |
| 189 | if (ret < 0) |
| 190 | return -1; |
| 191 | written += ret; |
| 192 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 193 | // Compute content legnth. |
| 194 | const off_t content_length = end_offset - start_offset;; |
| 195 | |
| 196 | // A start offset that equals the end offset indicates that the response |
| 197 | // should contain the full range of bytes in the requested resource. |
| 198 | if (start_offset || start_offset == end_offset) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 199 | ret = WriteString(fd, |
| 200 | string("Accept-Ranges: bytes" EOL |
| 201 | "Content-Range: bytes ") + |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 202 | Itoa(start_offset == end_offset ? 0 : start_offset) + |
| 203 | "-" + Itoa(end_offset - 1) + "/" + Itoa(end_offset) + |
| 204 | EOL); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 205 | if (ret < 0) |
| 206 | return -1; |
| 207 | written += ret; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | ret = WriteString(fd, string("Content-Length: ") + Itoa(content_length) + |
| 211 | EOL EOL); |
| 212 | if (ret < 0) |
| 213 | return -1; |
| 214 | written += ret; |
| 215 | |
| 216 | return written; |
| 217 | } |
| 218 | |
| 219 | // Writes a predetermined payload of lines of ascending bytes to a file. The |
| 220 | // first byte of output is appropriately offset with respect to the request line |
| 221 | // length. Returns the number of successfully written bytes. |
| 222 | size_t WritePayload(int fd, const off_t start_offset, const off_t end_offset, |
| 223 | const char first_byte, const size_t line_len) { |
| 224 | CHECK_LE(start_offset, end_offset); |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 225 | CHECK_GT(line_len, static_cast<size_t>(0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 226 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 227 | LOG(INFO) << "writing payload: " << line_len << "-byte lines starting with `" |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 228 | << first_byte << "', offset range " << start_offset << " -> " |
| 229 | << end_offset; |
| 230 | |
| 231 | // Populate line of ascending characters. |
| 232 | string line; |
| 233 | line.reserve(line_len); |
| 234 | char byte = first_byte; |
| 235 | size_t i; |
| 236 | for (i = 0; i < line_len; i++) |
| 237 | line += byte++; |
| 238 | |
| 239 | const size_t total_len = end_offset - start_offset; |
| 240 | size_t remaining_len = total_len; |
| 241 | bool success = true; |
| 242 | |
| 243 | // If start offset is not aligned with line boundary, output partial line up |
| 244 | // to the first line boundary. |
| 245 | size_t start_modulo = start_offset % line_len; |
| 246 | if (start_modulo) { |
| 247 | string partial = line.substr(start_modulo, remaining_len); |
| 248 | ssize_t ret = WriteString(fd, partial); |
| 249 | if ((success = (ret >= 0 && (size_t) ret == partial.length()))) |
| 250 | remaining_len -= partial.length(); |
| 251 | } |
| 252 | |
| 253 | // Output full lines up to the maximal line boundary below the end offset. |
| 254 | while (success && remaining_len >= line_len) { |
| 255 | ssize_t ret = WriteString(fd, line); |
| 256 | if ((success = (ret >= 0 && (size_t) ret == line_len))) |
| 257 | remaining_len -= line_len; |
| 258 | } |
| 259 | |
| 260 | // Output a partial line up to the end offset. |
| 261 | if (success && remaining_len) { |
| 262 | string partial = line.substr(0, remaining_len); |
| 263 | ssize_t ret = WriteString(fd, partial); |
| 264 | if ((success = (ret >= 0 && (size_t) ret == partial.length()))) |
| 265 | remaining_len -= partial.length(); |
| 266 | } |
| 267 | |
| 268 | return (total_len - remaining_len); |
| 269 | } |
| 270 | |
| 271 | // Write default payload lines of the form 'abcdefghij'. |
| 272 | inline size_t WritePayload(int fd, const off_t start_offset, |
| 273 | const off_t end_offset) { |
| 274 | return WritePayload(fd, start_offset, end_offset, 'a', 10); |
| 275 | } |
| 276 | |
| 277 | // Send an empty response, then kill the server. |
| 278 | void HandleQuit(int fd) { |
| 279 | WriteHeaders(fd, 0, 0, kHttpResponseOk); |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 280 | LOG(INFO) << "pid(" << getpid() << "): HTTP server exiting ..."; |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 281 | exit(RC_OK); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 284 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 285 | // Generates an HTTP response with payload corresponding to requested offsets |
| 286 | // and length. Optionally, truncate the payload at a given length and add a |
| 287 | // pause midway through the transfer. Returns the total number of bytes |
| 288 | // delivered or -1 for error. |
| 289 | ssize_t HandleGet(int fd, const HttpRequest& request, const size_t total_length, |
| 290 | const size_t truncate_length, const int sleep_every, |
| 291 | const int sleep_secs) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 292 | ssize_t ret; |
| 293 | size_t written = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 294 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 295 | // Obtain start offset, make sure it is within total payload length. |
| 296 | const size_t start_offset = request.start_offset; |
| 297 | if (start_offset >= total_length) { |
| 298 | LOG(WARNING) << "start offset (" << start_offset |
| 299 | << ") exceeds total length (" << total_length |
| 300 | << "), generating error response (" |
| 301 | << kHttpResponseReqRangeNotSat << ")"; |
| 302 | return WriteHeaders(fd, total_length, total_length, |
| 303 | kHttpResponseReqRangeNotSat); |
| 304 | } |
| 305 | |
| 306 | // Obtain end offset, adjust to fit in total payload length and ensure it does |
| 307 | // not preceded the start offset. |
| 308 | size_t end_offset = (request.end_offset > 0 ? |
| 309 | request.end_offset : total_length); |
| 310 | if (end_offset < start_offset) { |
| 311 | LOG(WARNING) << "end offset (" << end_offset << ") precedes start offset (" |
| 312 | << start_offset << "), generating error response"; |
| 313 | return WriteHeaders(fd, 0, 0, kHttpResponseBadRequest); |
| 314 | } |
| 315 | if (end_offset > total_length) { |
| 316 | LOG(INFO) << "requested end offset (" << end_offset |
| 317 | << ") exceeds total length (" << total_length << "), adjusting"; |
| 318 | end_offset = total_length; |
| 319 | } |
| 320 | |
| 321 | // Generate headers |
| 322 | LOG(INFO) << "generating response header: range=" << start_offset << "-" |
| 323 | << (end_offset - 1) << "/" << (end_offset - start_offset) |
| 324 | << ", return code=" << request.return_code; |
| 325 | if ((ret = WriteHeaders(fd, start_offset, end_offset, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 326 | request.return_code)) < 0) |
| 327 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 328 | LOG(INFO) << ret << " header bytes written"; |
| 329 | written += ret; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 330 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 331 | // Compute payload length, truncate as necessary. |
| 332 | size_t payload_length = end_offset - start_offset; |
| 333 | if (truncate_length > 0 && truncate_length < payload_length) { |
| 334 | LOG(INFO) << "truncating request payload length (" << payload_length |
| 335 | << ") at " << truncate_length; |
| 336 | payload_length = truncate_length; |
| 337 | end_offset = start_offset + payload_length; |
| 338 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 339 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 340 | LOG(INFO) << "generating response payload: range=" << start_offset << "-" |
| 341 | << (end_offset - 1) << "/" << (end_offset - start_offset); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 342 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 343 | // Decide about optional midway delay. |
| 344 | if (truncate_length > 0 && sleep_every > 0 && sleep_secs >= 0 && |
| 345 | start_offset % (truncate_length * sleep_every) == 0) { |
| 346 | const off_t midway_offset = start_offset + payload_length / 2; |
| 347 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 348 | if ((ret = WritePayload(fd, start_offset, midway_offset)) < 0) |
| 349 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 350 | LOG(INFO) << ret << " payload bytes written (first chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 351 | written += ret; |
| 352 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 353 | LOG(INFO) << "sleeping for " << sleep_secs << " seconds..."; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 354 | sleep(sleep_secs); |
| 355 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 356 | if ((ret = WritePayload(fd, midway_offset, end_offset)) < 0) |
| 357 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 358 | LOG(INFO) << ret << " payload bytes written (second chunk)"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 359 | written += ret; |
| 360 | } else { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 361 | if ((ret = WritePayload(fd, start_offset, end_offset)) < 0) |
| 362 | return -1; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 363 | LOG(INFO) << ret << " payload bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 364 | written += ret; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 365 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 366 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 367 | LOG(INFO) << "response generation complete, " << written |
| 368 | << " total bytes written"; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 369 | return written; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 372 | ssize_t HandleGet(int fd, const HttpRequest& request, |
| 373 | const size_t total_length) { |
| 374 | return HandleGet(fd, request, total_length, 0, 0, 0); |
| 375 | } |
| 376 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 377 | // Handles /redirect/<code>/<url> requests by returning the specified |
| 378 | // redirect <code> with a location pointing to /<url>. |
| 379 | void HandleRedirect(int fd, const HttpRequest& request) { |
| 380 | LOG(INFO) << "Redirecting..."; |
| 381 | string url = request.url; |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 382 | CHECK_EQ(static_cast<size_t>(0), url.find("/redirect/")); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 383 | url.erase(0, strlen("/redirect/")); |
| 384 | string::size_type url_start = url.find('/'); |
| 385 | CHECK_NE(url_start, string::npos); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 386 | HttpResponseCode code = StringToHttpResponseCode(url.c_str()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 387 | url.erase(0, url_start); |
| 388 | url = "http://" + request.host + url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 389 | const char *status = GetHttpResponseDescription(code); |
| 390 | if (!status) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 391 | CHECK(false) << "Unrecognized redirection code: " << code; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 392 | LOG(INFO) << "Code: " << code << " " << status; |
| 393 | LOG(INFO) << "New URL: " << url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 394 | |
| 395 | ssize_t ret; |
| 396 | if ((ret = WriteString(fd, "HTTP/1.1 " + Itoa(code) + " " + |
| 397 | status + EOL)) < 0) |
| 398 | return; |
| 399 | WriteString(fd, "Location: " + url + EOL); |
| 400 | } |
| 401 | |
| 402 | // Generate a page not found error response with actual text payload. Return |
| 403 | // number of bytes written or -1 for error. |
| 404 | ssize_t HandleError(int fd, const HttpRequest& request) { |
| 405 | LOG(INFO) << "Generating error HTTP response"; |
| 406 | |
| 407 | ssize_t ret; |
| 408 | size_t written = 0; |
| 409 | |
| 410 | const string data("This is an error page."); |
| 411 | |
| 412 | if ((ret = WriteHeaders(fd, 0, data.size(), kHttpResponseNotFound)) < 0) |
| 413 | return -1; |
| 414 | written += ret; |
| 415 | |
| 416 | if ((ret = WriteString(fd, data)) < 0) |
| 417 | return -1; |
| 418 | written += ret; |
| 419 | |
| 420 | return written; |
| 421 | } |
| 422 | |
| 423 | // Generate an error response if the requested offset is nonzero, up to a given |
| 424 | // maximal number of successive failures. The error generated is an "Internal |
| 425 | // Server Error" (500). |
| 426 | ssize_t HandleErrorIfOffset(int fd, const HttpRequest& request, |
| 427 | size_t end_offset, int max_fails) { |
| 428 | static int num_fails = 0; |
| 429 | |
| 430 | if (request.start_offset > 0 && num_fails < max_fails) { |
| 431 | LOG(INFO) << "Generating error HTTP response"; |
| 432 | |
| 433 | ssize_t ret; |
| 434 | size_t written = 0; |
| 435 | |
| 436 | const string data("This is an error page."); |
| 437 | |
| 438 | if ((ret = WriteHeaders(fd, 0, data.size(), |
| 439 | kHttpResponseInternalServerError)) < 0) |
| 440 | return -1; |
| 441 | written += ret; |
| 442 | |
| 443 | if ((ret = WriteString(fd, data)) < 0) |
| 444 | return -1; |
| 445 | written += ret; |
| 446 | |
| 447 | num_fails++; |
| 448 | return written; |
| 449 | } else { |
| 450 | num_fails = 0; |
| 451 | return HandleGet(fd, request, end_offset); |
| 452 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 455 | void HandleHang(int fd) { |
| 456 | LOG(INFO) << "Hanging until the other side of the connection is closed."; |
| 457 | char c; |
| 458 | while (HANDLE_EINTR(read(fd, &c, 1)) > 0) {} |
| 459 | } |
| 460 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 461 | void HandleDefault(int fd, const HttpRequest& request) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 462 | const off_t start_offset = request.start_offset; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 463 | const string data("unhandled path"); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 464 | const size_t size = data.size(); |
| 465 | ssize_t ret; |
| 466 | |
| 467 | if ((ret = WriteHeaders(fd, start_offset, size, request.return_code)) < 0) |
| 468 | return; |
| 469 | WriteString(fd, (start_offset < static_cast<off_t>(size) ? |
| 470 | data.substr(start_offset) : "")); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 473 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 474 | // Break a URL into terms delimited by slashes. |
| 475 | class UrlTerms { |
| 476 | public: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 477 | UrlTerms(const string &url, size_t num_terms) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 478 | // URL must be non-empty and start with a slash. |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 479 | CHECK_GT(url.size(), static_cast<size_t>(0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 480 | CHECK_EQ(url[0], '/'); |
| 481 | |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 482 | // Split it into terms delimited by slashes, omitting the preceding slash. |
| 483 | terms = base::SplitString(url.substr(1), "/", base::KEEP_WHITESPACE, |
| 484 | base::SPLIT_WANT_ALL); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 485 | |
| 486 | // Ensure expected length. |
| 487 | CHECK_EQ(terms.size(), num_terms); |
| 488 | } |
| 489 | |
| 490 | inline string Get(const off_t index) const { |
| 491 | return terms[index]; |
| 492 | } |
| 493 | inline const char *GetCStr(const off_t index) const { |
| 494 | return Get(index).c_str(); |
| 495 | } |
| 496 | inline int GetInt(const off_t index) const { |
| 497 | return atoi(GetCStr(index)); |
| 498 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 499 | inline size_t GetSizeT(const off_t index) const { |
| 500 | return static_cast<size_t>(atol(GetCStr(index))); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | private: |
Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 504 | vector<string> terms; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 505 | }; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 506 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 507 | void HandleConnection(int fd) { |
| 508 | HttpRequest request; |
| 509 | ParseRequest(fd, &request); |
| 510 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 511 | string &url = request.url; |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 512 | LOG(INFO) << "pid(" << getpid() << "): handling url " << url; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 513 | if (url == "/quitquitquit") { |
| 514 | HandleQuit(fd); |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 515 | } else if (base::StartsWith(url, "/download/", |
| 516 | base::CompareCase::SENSITIVE)) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 517 | const UrlTerms terms(url, 2); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 518 | HandleGet(fd, request, terms.GetSizeT(1)); |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 519 | } else if (base::StartsWith(url, "/flaky/", base::CompareCase::SENSITIVE)) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 520 | const UrlTerms terms(url, 5); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 521 | HandleGet(fd, request, terms.GetSizeT(1), terms.GetSizeT(2), |
| 522 | terms.GetInt(3), terms.GetInt(4)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 523 | } else if (url.find("/redirect/") == 0) { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 524 | HandleRedirect(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 525 | } else if (url == "/error") { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 526 | HandleError(fd, request); |
Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 527 | } else if (base::StartsWith(url, "/error-if-offset/", |
| 528 | base::CompareCase::SENSITIVE)) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 529 | const UrlTerms terms(url, 3); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 530 | HandleErrorIfOffset(fd, request, terms.GetSizeT(1), terms.GetInt(2)); |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 531 | } else if (url == "/hang") { |
| 532 | HandleHang(fd); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 533 | } else { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 534 | HandleDefault(fd, request); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 535 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 536 | |
| 537 | close(fd); |
| 538 | } |
| 539 | |
| 540 | } // namespace chromeos_update_engine |
| 541 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 542 | using namespace chromeos_update_engine; // NOLINT(build/namespaces) |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 543 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 544 | |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 545 | void usage(const char *prog_arg) { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 546 | fprintf( |
| 547 | stderr, |
| 548 | "Usage: %s [ FILE ]\n" |
| 549 | "Once accepting connections, the following is written to FILE (or " |
| 550 | "stdout):\n" |
| 551 | "\"%sN\" (where N is an integer port number)\n", |
| 552 | basename(prog_arg), kListeningMsgPrefix); |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 553 | } |
| 554 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 555 | int main(int argc, char** argv) { |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 556 | // Check invocation. |
| 557 | if (argc > 2) |
| 558 | errx(RC_BAD_ARGS, "unexpected number of arguments (use -h for usage)"); |
| 559 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 560 | // Parse (optional) argument. |
| 561 | int report_fd = STDOUT_FILENO; |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 562 | if (argc == 2) { |
| 563 | if (!strcmp(argv[1], "-h")) { |
| 564 | usage(argv[0]); |
| 565 | exit(RC_OK); |
| 566 | } |
| 567 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 568 | report_fd = open(argv[1], O_WRONLY | O_CREAT, 00644); |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 571 | // Ignore SIGPIPE on write() to sockets. |
| 572 | signal(SIGPIPE, SIG_IGN); |
Darin Petkov | f67bb1f | 2010-11-08 16:10:26 -0800 | [diff] [blame] | 573 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 574 | int listen_fd = socket(AF_INET, SOCK_STREAM, 0); |
| 575 | if (listen_fd < 0) |
| 576 | LOG(FATAL) << "socket() failed"; |
| 577 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 578 | struct sockaddr_in server_addr = sockaddr_in(); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 579 | server_addr.sin_family = AF_INET; |
| 580 | server_addr.sin_addr.s_addr = INADDR_ANY; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 581 | server_addr.sin_port = 0; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 582 | |
| 583 | { |
| 584 | // Get rid of "Address in use" error |
| 585 | int tr = 1; |
| 586 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &tr, |
| 587 | sizeof(int)) == -1) { |
| 588 | perror("setsockopt"); |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 589 | exit(RC_ERR_SETSOCKOPT); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 593 | // Bind the socket and set for listening. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 594 | if (bind(listen_fd, reinterpret_cast<struct sockaddr *>(&server_addr), |
| 595 | sizeof(server_addr)) < 0) { |
| 596 | perror("bind"); |
Gilad Arnold | 97bdb3f | 2013-06-30 23:05:50 -0700 | [diff] [blame] | 597 | exit(RC_ERR_BIND); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 598 | } |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 599 | if (listen(listen_fd, 5) < 0) { |
| 600 | perror("listen"); |
| 601 | exit(RC_ERR_LISTEN); |
| 602 | } |
| 603 | |
| 604 | // Check the actual port. |
| 605 | struct sockaddr_in bound_addr = sockaddr_in(); |
| 606 | socklen_t bound_addr_len = sizeof(bound_addr); |
| 607 | if (getsockname(listen_fd, reinterpret_cast<struct sockaddr*>(&bound_addr), |
| 608 | &bound_addr_len) < 0) { |
| 609 | perror("getsockname"); |
| 610 | exit(RC_ERR_GETSOCKNAME); |
| 611 | } |
| 612 | in_port_t port = ntohs(bound_addr.sin_port); |
| 613 | |
| 614 | // Output the listening port, indicating that the server is processing |
| 615 | // requests. IMPORTANT! (a) the format of this message is as expected by some |
| 616 | // unit tests, avoid unilateral changes; (b) it is necessary to flush/sync the |
| 617 | // file to prevent the spawning process from waiting indefinitely for this |
| 618 | // message. |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 619 | string listening_msg = base::StringPrintf("%s%hu", kListeningMsgPrefix, port); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 620 | LOG(INFO) << listening_msg; |
| 621 | CHECK_EQ(write(report_fd, listening_msg.c_str(), listening_msg.length()), |
| 622 | static_cast<int>(listening_msg.length())); |
| 623 | CHECK_EQ(write(report_fd, "\n", 1), 1); |
| 624 | if (report_fd == STDOUT_FILENO) |
| 625 | fsync(report_fd); |
| 626 | else |
| 627 | close(report_fd); |
| 628 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 629 | while (1) { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 630 | LOG(INFO) << "pid(" << getpid() << "): waiting to accept new connection"; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 631 | int client_fd = accept(listen_fd, nullptr, nullptr); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 632 | LOG(INFO) << "got past accept"; |
| 633 | if (client_fd < 0) |
| 634 | LOG(FATAL) << "ERROR on accept"; |
| 635 | HandleConnection(client_fd); |
| 636 | } |
| 637 | return 0; |
| 638 | } |