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>
This commit is contained in:
Alex Vakulenko 2014-09-04 16:16:28 -07:00 committed by chrome-internal-fetch
parent 652d6971b1
commit ec991df012
7 changed files with 86 additions and 125 deletions

View File

@ -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',

View File

@ -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;
}

View File

@ -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_

View File

@ -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;
}

View File

@ -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_

View File

@ -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() {

View File

@ -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;