From ec991df012c662698e673d2f607d025f0b72afab Mon Sep 17 00:00:00 2001
From: Alex Vakulenko <avakulenko@chromium.org>
Date: Thu, 4 Sep 2014 16:16:28 -0700
Subject: [PATCH] metrics: Switch uploader to use http_utils from libchromeos

Removed explicit dependency on libcurl from metrics and using
chromeos::http::PostText() function instead.

BUG=chromium:411076
TEST=FEATURES=test emerge-link metrics

Change-Id: Ida883fa18d266e9bb87c3a4271e57d44c9308c79
Reviewed-on: https://chromium-review.googlesource.com/216526
Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
---
 metrics/metrics.gyp                |  3 +-
 metrics/uploader/curl_sender.cc    | 70 ------------------------------
 metrics/uploader/curl_sender.h     | 33 --------------
 metrics/uploader/sender_http.cc    | 36 +++++++++++++++
 metrics/uploader/sender_http.h     | 29 +++++++++++++
 metrics/uploader/upload_service.cc | 34 +++++++--------
 metrics/uploader/upload_service.h  |  6 +--
 7 files changed, 86 insertions(+), 125 deletions(-)
 delete mode 100644 metrics/uploader/curl_sender.cc
 delete mode 100644 metrics/uploader/curl_sender.h
 create mode 100644 metrics/uploader/sender_http.cc
 create mode 100644 metrics/uploader/sender_http.h

diff --git a/metrics/metrics.gyp b/metrics/metrics.gyp
index 7a4338b05..d8f697ce8 100644
--- a/metrics/metrics.gyp
+++ b/metrics/metrics.gyp
@@ -12,7 +12,6 @@
         'gthread-2.0',
         'libchrome-<(libbase_ver)',
         'libchromeos-<(libbase_ver)',
-        'libcurl',
       ]
     },
     'cflags_cc': [
@@ -83,7 +82,7 @@
         'uploader/upload_service.cc',
         'uploader/metrics_log.cc',
         'uploader/system_profile_cache.cc',
-        'uploader/curl_sender.cc',
+        'uploader/sender_http.cc',
         'components/metrics/metrics_log_base.cc',
         'components/metrics/metrics_log_manager.cc',
         'components/metrics/metrics_hashes.cc',
diff --git a/metrics/uploader/curl_sender.cc b/metrics/uploader/curl_sender.cc
deleted file mode 100644
index 5049ef4b9..000000000
--- a/metrics/uploader/curl_sender.cc
+++ /dev/null
@@ -1,70 +0,0 @@
-// Copyright 2014 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "uploader/curl_sender.h"
-
-#include <curl/curl.h>
-#include <string>
-
-#include "base/logging.h"
-#include "base/strings/string_number_conversions.h"
-
-CurlSender::CurlSender(const std::string server_url)
-    : server_url_(server_url) {}
-
-bool CurlSender::Send(const std::string& content,
-                      const std::string& content_hash) {
-  CURL* postrequest = curl_easy_init();
-
-  if (!postrequest) {
-    DLOG(ERROR) << "Error creating the post request";
-    return false;
-  }
-
-  curl_easy_setopt(postrequest, CURLOPT_URL, server_url_.c_str());
-  curl_easy_setopt(postrequest, CURLOPT_POST, 1);
-
-  const std::string hash =
-      base::HexEncode(content_hash.data(), content_hash.size());
-
-  curl_slist* headers =
-      curl_slist_append(nullptr, ("X-Chrome-UMA-Log-SHA1: " + hash).c_str());
-  if (!headers) {
-    DLOG(ERROR) << "failed setting the headers";
-    curl_easy_cleanup(postrequest);
-    return false;
-  }
-
-  std::string output;
-
-  curl_easy_setopt(postrequest, CURLOPT_HTTPHEADER, headers);
-  curl_easy_setopt(postrequest, CURLOPT_POSTFIELDSIZE, content.size());
-  curl_easy_setopt(postrequest, CURLOPT_POSTFIELDS, content.c_str());
-
-  // Set the callback function used to read the response and the destination.
-  curl_easy_setopt(postrequest, CURLOPT_WRITEFUNCTION, ReadData);
-  curl_easy_setopt(postrequest, CURLOPT_WRITEDATA, &output);
-
-  CURLcode result = curl_easy_perform(postrequest);
-
-  if (result == CURLE_OK && output == "OK") {
-    curl_easy_cleanup(postrequest);
-    return true;
-  }
-
-  curl_easy_cleanup(postrequest);
-
-  return false;
-}
-
-// static
-size_t CurlSender::ReadData(void* buffer, size_t size, size_t nmember,
-                            std::string* out) {
-  CHECK(out);
-
-  // This function might be called several time so we want to append the data at
-  // the end of the string.
-  *out += std::string(static_cast<char*>(buffer), size * nmember);
-  return size * nmember;
-}
diff --git a/metrics/uploader/curl_sender.h b/metrics/uploader/curl_sender.h
deleted file mode 100644
index b7e958524..000000000
--- a/metrics/uploader/curl_sender.h
+++ /dev/null
@@ -1,33 +0,0 @@
-// Copyright 2014 The Chromium OS Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef METRICS_UPLOADER_CURL_SENDER_H_
-#define METRICS_UPLOADER_CURL_SENDER_H_
-
-#include <string>
-
-#include "base/compiler_specific.h"
-#include "uploader/sender.h"
-
-// Sender implemented using libcurl
-class CurlSender : public Sender {
- public:
-  explicit CurlSender(std::string server_url);
-  virtual ~CurlSender() {}
-  // Sends |content| whose SHA1 hash is |hash| to server_url with a synchronous
-  // POST request to server_url.
-  bool Send(const std::string& content, const std::string& hash) override;
-
-  // Static callback required by curl to retrieve the response data.
-  //
-  // Copies |size| * |nmember| bytes of data from |buffer| to |out|.
-  // Returns the number of bytes copied.
-  static size_t ReadData(void* buffer, size_t size, size_t nmember,
-                         std::string* out);
-
- private:
-  const std::string server_url_;
-};
-
-#endif  // METRICS_UPLOADER_CURL_SENDER_H_
diff --git a/metrics/uploader/sender_http.cc b/metrics/uploader/sender_http.cc
new file mode 100644
index 000000000..585287f22
--- /dev/null
+++ b/metrics/uploader/sender_http.cc
@@ -0,0 +1,36 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "metrics/uploader/sender_http.h"
+
+#include <string>
+
+#include <base/logging.h>
+#include <base/strings/string_number_conversions.h>
+#include <chromeos/http/http_utils.h>
+#include <chromeos/mime_utils.h>
+
+HttpSender::HttpSender(const std::string server_url)
+    : server_url_(server_url) {}
+
+bool HttpSender::Send(const std::string& content,
+                      const std::string& content_hash) {
+  const std::string hash =
+      base::HexEncode(content_hash.data(), content_hash.size());
+
+  chromeos::http::HeaderList headers = {{"X-Chrome-UMA-Log-SHA1", hash}};
+  chromeos::ErrorPtr error;
+  auto response = chromeos::http::PostText(
+      server_url_,
+      content.c_str(),
+      chromeos::mime::application::kWwwFormUrlEncoded,
+      headers,
+      chromeos::http::Transport::CreateDefault(),
+      &error);
+  if (!response || response->GetDataAsString() != "OK") {
+    DLOG(ERROR) << "Failed to send data: " << error->GetMessage();
+    return false;
+  }
+  return true;
+}
diff --git a/metrics/uploader/sender_http.h b/metrics/uploader/sender_http.h
new file mode 100644
index 000000000..4880b2880
--- /dev/null
+++ b/metrics/uploader/sender_http.h
@@ -0,0 +1,29 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef METRICS_UPLOADER_SENDER_HTTP_H_
+#define METRICS_UPLOADER_SENDER_HTTP_H_
+
+#include <string>
+
+#include <base/macros.h>
+
+#include "metrics/uploader/sender.h"
+
+// Sender implemented using http_utils from libchromeos
+class HttpSender : public Sender {
+ public:
+  explicit HttpSender(std::string server_url);
+  ~HttpSender() override = default;
+  // Sends |content| whose SHA1 hash is |hash| to server_url with a synchronous
+  // POST request to server_url.
+  bool Send(const std::string& content, const std::string& hash) override;
+
+ private:
+  const std::string server_url_;
+
+  DISALLOW_COPY_AND_ASSIGN(HttpSender);
+};
+
+#endif  // METRICS_UPLOADER_SENDER_HTTP_H_
diff --git a/metrics/uploader/upload_service.cc b/metrics/uploader/upload_service.cc
index 8b8e351bb..8094fecdf 100644
--- a/metrics/uploader/upload_service.cc
+++ b/metrics/uploader/upload_service.cc
@@ -2,26 +2,26 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-#include "uploader/upload_service.h"
+#include "metrics/uploader/upload_service.h"
 
-#include <curl/curl.h>
 #include <glib.h>
 #include <string>
 
-#include "base/logging.h"
-#include "base/memory/scoped_vector.h"
-#include "base/metrics/histogram.h"
-#include "base/metrics/histogram_base.h"
-#include "base/metrics/histogram_snapshot_manager.h"
-#include "base/metrics/sparse_histogram.h"
-#include "base/metrics/statistics_recorder.h"
-#include "base/sha1.h"
-#include "components/metrics/chromeos/metric_sample.h"
-#include "components/metrics/chromeos/serialization_utils.h"
-#include "gflags/gflags.h"
-#include "uploader/curl_sender.h"
-#include "uploader/metrics_log.h"
-#include "uploader/system_profile_cache.h"
+#include <base/logging.h>
+#include <base/memory/scoped_vector.h>
+#include <base/metrics/histogram.h>
+#include <base/metrics/histogram_base.h>
+#include <base/metrics/histogram_snapshot_manager.h>
+#include <base/metrics/sparse_histogram.h>
+#include <base/metrics/statistics_recorder.h>
+#include <base/sha1.h>
+#include <components/metrics/chromeos/metric_sample.h>
+#include <components/metrics/chromeos/serialization_utils.h>
+#include <gflags/gflags.h>
+
+#include "metrics/uploader/metrics_log.h"
+#include "metrics/uploader/sender_http.h"
+#include "metrics/uploader/system_profile_cache.h"
 
 DEFINE_int32(
     upload_interval_secs,
@@ -41,7 +41,7 @@ const int UploadService::kMaxFailedUpload = 10;
 UploadService::UploadService()
     : system_profile_setter_(new SystemProfileCache()),
       histogram_snapshot_manager_(this),
-      sender_(new CurlSender(FLAGS_server)) {
+      sender_(new HttpSender(FLAGS_server)) {
 }
 
 void UploadService::Init() {
diff --git a/metrics/uploader/upload_service.h b/metrics/uploader/upload_service.h
index d677424e1..0d17e0e39 100644
--- a/metrics/uploader/upload_service.h
+++ b/metrics/uploader/upload_service.h
@@ -10,9 +10,9 @@
 #include "base/metrics/histogram_base.h"
 #include "base/metrics/histogram_flattener.h"
 #include "base/metrics/histogram_snapshot_manager.h"
-#include "uploader/metrics_log.h"
-#include "uploader/sender.h"
-#include "uploader/system_profile_cache.h"
+#include "metrics/uploader/metrics_log.h"
+#include "metrics/uploader/sender.h"
+#include "metrics/uploader/system_profile_cache.h"
 
 namespace metrics {
 class ChromeUserMetricsExtension;