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