summaryrefslogtreecommitdiff
path: root/test_http_server.cc
diff options
context:
space:
mode:
authorAlex Deymo <deymo@google.com>2016-03-03 22:35:43 -0800
committerAlex Deymo <deymo@google.com>2016-03-07 13:34:56 -0800
commit6f10c5f7c550b1bd6df1d9a04b5e75e03f943639 (patch)
treefbadd6266c6ae84e28016a7c65d96c035b1ef2a7 /test_http_server.cc
parent1cfb124437d8b8d430c46ec076e21959ab6a6614 (diff)
Parse and use extra HTTP headers when downloading the payload.
Android OTA backend requires to pass an Authorization HTTP header in order to download some payload. This patch allows to specify such header when initiating a payload download from Android. Bug: 27047110 TEST=Added unittests to check the headers sent. (cherry picked from commit fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9) Change-Id: I59d38d79a7b7a8975d105c611c692522b6c33707
Diffstat (limited to 'test_http_server.cc')
-rw-r--r--test_http_server.cc23
1 files changed, 16 insertions, 7 deletions
diff --git a/test_http_server.cc b/test_http_server.cc
index 98e7a6da..2955e79f 100644
--- a/test_http_server.cc
+++ b/test_http_server.cc
@@ -72,13 +72,12 @@ enum {
};
struct HttpRequest {
- HttpRequest()
- : start_offset(0), end_offset(0), return_code(kHttpResponseOk) {}
+ string raw_headers;
string host;
string url;
- off_t start_offset;
- off_t end_offset; // non-inclusive, zero indicates unspecified.
- HttpResponseCode return_code;
+ off_t start_offset{0};
+ off_t end_offset{0}; // non-inclusive, zero indicates unspecified.
+ HttpResponseCode return_code{kHttpResponseOk};
};
bool ParseRequest(int fd, HttpRequest* request) {
@@ -96,6 +95,7 @@ bool ParseRequest(int fd, HttpRequest* request) {
LOG(INFO) << "got headers:\n--8<------8<------8<------8<----\n"
<< headers
<< "\n--8<------8<------8<------8<----";
+ request->raw_headers = headers;
// Break header into lines.
vector<string> lines;
@@ -452,6 +452,13 @@ ssize_t HandleErrorIfOffset(int fd, const HttpRequest& request,
}
}
+// Returns a valid response echoing in the body of the response all the headers
+// sent by the client.
+void HandleEchoHeaders(int fd, const HttpRequest& request) {
+ WriteHeaders(fd, 0, request.raw_headers.size(), kHttpResponseOk);
+ WriteString(fd, request.raw_headers);
+}
+
void HandleHang(int fd) {
LOG(INFO) << "Hanging until the other side of the connection is closed.";
char c;
@@ -512,8 +519,8 @@ void HandleConnection(int fd) {
LOG(INFO) << "pid(" << getpid() << "): handling url " << url;
if (url == "/quitquitquit") {
HandleQuit(fd);
- } else if (base::StartsWith(url, "/download/",
- base::CompareCase::SENSITIVE)) {
+ } else if (base::StartsWith(
+ url, "/download/", base::CompareCase::SENSITIVE)) {
const UrlTerms terms(url, 2);
HandleGet(fd, request, terms.GetSizeT(1));
} else if (base::StartsWith(url, "/flaky/", base::CompareCase::SENSITIVE)) {
@@ -528,6 +535,8 @@ void HandleConnection(int fd) {
base::CompareCase::SENSITIVE)) {
const UrlTerms terms(url, 3);
HandleErrorIfOffset(fd, request, terms.GetSizeT(1), terms.GetInt(2));
+ } else if (url == "/echo-headers") {
+ HandleEchoHeaders(fd, request);
} else if (url == "/hang") {
HandleHang(fd);
} else {