update_engine: add new headers am: 31d9e55283 am: 551c288bb5

Original change: https://android-review.googlesource.com/c/platform/system/update_engine/+/3214118

Change-Id: Ife48e74a1379b000b574664f2de9b2cdc70af40e
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/aosp/update_attempter_android.cc b/aosp/update_attempter_android.cc
index b5ec1e0..909ce59 100644
--- a/aosp/update_attempter_android.cc
+++ b/aosp/update_attempter_android.cc
@@ -381,11 +381,26 @@
 #endif  // _UE_SIDELOAD
   }
   // Setup extra headers.
-  if (!headers[kPayloadPropertyAuthorization].empty())
+  if (!headers[kPayloadPropertyAuthorization].empty()) {
     fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
-  if (!headers[kPayloadPropertyUserAgent].empty())
+  }
+  if (!headers[kPayloadPropertyUserAgent].empty()) {
     fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
-
+  }
+  if (!headers[kPayloadPropertyHTTPExtras].empty()) {
+    auto entries =
+        android::base::Split(headers[kPayloadPropertyHTTPExtras], " ");
+    for (auto& entry : entries) {
+      auto parts = android::base::Split(entry, ":");
+      if (parts.size() != 2) {
+        LOG(ERROR)
+            << "HTTP headers are not in expected format. "
+               "headers[kPayloadPropertyHTTPExtras] = key1:val1 key2:val2";
+        continue;
+      }
+      fetcher->SetHeader(parts[0], parts[1]);
+    }
+  }
   if (!headers[kPayloadPropertyNetworkProxy].empty()) {
     LOG(INFO) << "Using proxy url from payload headers: "
               << headers[kPayloadPropertyNetworkProxy];
diff --git a/common/constants.h b/common/constants.h
index 3fcf1f1..6b1bf6b 100644
--- a/common/constants.h
+++ b/common/constants.h
@@ -163,6 +163,8 @@
 static constexpr const auto& kPayloadPropertyMetadataHash = "METADATA_HASH";
 // The Authorization: HTTP header to be sent when downloading the payload.
 static constexpr const auto& kPayloadPropertyAuthorization = "AUTHORIZATION";
+// HTTP headers extra entries in the format of key1:val1 key2:val2 key3:val3
+static constexpr const auto& kPayloadPropertyHTTPExtras = "HTTP_EXTRAS";
 // The User-Agent HTTP header to be sent when downloading the payload.
 static constexpr const auto& kPayloadPropertyUserAgent = "USER_AGENT";
 // Set "POWERWASH=1" to powerwash (factory data reset) the device after
diff --git a/common/http_fetcher_unittest.cc b/common/http_fetcher_unittest.cc
index 06f3e15..b229660 100644
--- a/common/http_fetcher_unittest.cc
+++ b/common/http_fetcher_unittest.cc
@@ -548,6 +548,8 @@
   fetcher->SetHeader("User-Agent", "MyTest");
   fetcher->SetHeader("user-agent", "Override that header");
   fetcher->SetHeader("Authorization", "Basic user:passwd");
+  fetcher->SetHeader("Cache-Control", "testControl");
+  fetcher->SetHeader("Connection", "testConnection");
 
   // Invalid headers.
   fetcher->SetHeader("X-Foo", "Invalid\nHeader\nIgnored");
@@ -571,6 +573,8 @@
             delegate.data.find("user-agent: Override that header\r\n"));
   EXPECT_NE(string::npos,
             delegate.data.find("Authorization: Basic user:passwd\r\n"));
+  EXPECT_NE(string::npos, delegate.data.find("Cache-Control: testControl\r\n"));
+  EXPECT_NE(string::npos, delegate.data.find("Connection: testConnection\r\n"));
 
   EXPECT_EQ(string::npos, delegate.data.find("\nAccept:"));
   EXPECT_EQ(string::npos, delegate.data.find("X-Foo: Invalid"));