1 /*
2  * Copyright (C) 2015 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 #include "uploader/sender_http.h"
18 
19 #include <string>
20 
21 #include <base/logging.h>
22 #include <base/strings/string_number_conversions.h>
23 #include <brillo/http/http_utils.h>
24 #include <brillo/mime_utils.h>
25 
HttpSender(const std::string server_url)26 HttpSender::HttpSender(const std::string server_url)
27     : server_url_(server_url) {}
28 
Send(const std::string & content,const std::string & content_hash)29 bool HttpSender::Send(const std::string& content,
30                       const std::string& content_hash) {
31   const std::string hash =
32       base::HexEncode(content_hash.data(), content_hash.size());
33 
34   brillo::http::HeaderList headers = {{"X-Chrome-UMA-Log-SHA1", hash}};
35   brillo::ErrorPtr error;
36   auto response = brillo::http::PostTextAndBlock(
37       server_url_,
38       content,
39       brillo::mime::application::kWwwFormUrlEncoded,
40       headers,
41       brillo::http::Transport::CreateDefault(),
42       &error);
43   if (!response || response->ExtractDataAsString() != "OK") {
44     if (error) {
45       DLOG(ERROR) << "Failed to send data: " << error->GetMessage();
46     }
47     return false;
48   }
49   return true;
50 }
51