2929
3030#include < android-base/logging.h>
3131#include < android-base/strings.h>
32- #include < curl/curl.h>
3332#include < gflags/gflags.h>
3433
35- #include " cuttlefish/common/libs/utils/tee_logging.h"
3634#include " cuttlefish/host/libs/metrics/metrics_defs.h"
35+ #include " cuttlefish/host/libs/web/http_client/curl_global_init.h"
36+ #include " cuttlefish/host/libs/web/http_client/curl_http_client.h"
37+ #include " cuttlefish/host/libs/web/http_client/http_client.h"
38+ #include " cuttlefish/host/libs/web/http_client/http_string.h"
3739
3840namespace cuttlefish ::metrics {
3941
@@ -129,20 +131,6 @@ uint64_t GetEpochTimeMs() {
129131 return milliseconds_since_epoch;
130132}
131133
132- size_t curl_out_writer ([[maybe_unused]] char * response, size_t size,
133- size_t nmemb, [[maybe_unused]] void * userdata) {
134- return size * nmemb;
135- }
136-
137- CURLUcode SetCurlUrlPart (CURLU* url, CURLUPart part, const char * value) {
138- CURLUcode urc = curl_url_set (url, part, value, 0 );
139- if (urc != 0 ) {
140- LOG (ERROR) << " Failed to set url part '" << part << " ' to '" << value
141- << " ': Error '" << curl_url_strerror (urc) << " '" ;
142- }
143- return urc;
144- }
145-
146134std::string ClearcutServerUrl (metrics::ClearcutServer server) {
147135 switch (server) {
148136 case metrics::kLocal :
@@ -160,51 +148,33 @@ std::string ClearcutServerUrl(metrics::ClearcutServer server) {
160148 }
161149}
162150
163- MetricsExitCodes PostRequest (const std::string& output,
164- metrics::ClearcutServer server) {
165- std::string clearcut_url = ClearcutServerUrl (server);
166-
167- std::unique_ptr<CURLU, void (*)(CURLU*)> url (curl_url (), curl_url_cleanup);
168- if (!url) {
169- LOG (ERROR) << " Failed to initialize CURLU." ;
170- return kMetricsError ;
171- }
172-
173- CURLUcode urc =
174- curl_url_set (url.get (), CURLUPART_URL, clearcut_url.c_str (), 0 );
175- if (urc != 0 ) {
176- LOG (ERROR) << " Failed to set url to " << url.get () << clearcut_url
177- << " ': " << curl_url_strerror (urc) << " '" ;
178- return kMetricsError ;
151+ MetricsExitCodes PostRequest (const std::string& output, ClearcutServer server) {
152+ CurlGlobalInit curl_global_init;
153+ std::unique_ptr<HttpClient> http_client = CurlHttpClient ();
154+ if (!http_client) {
155+ return MetricsExitCodes::kMetricsError ;
179156 }
180- curl_global_init (CURL_GLOBAL_ALL);
157+ return PostRequest (*http_client, output, server);
158+ }
181159
182- std::unique_ptr<CURL, void (*)(CURL*)> curl (curl_easy_init (),
183- curl_easy_cleanup);
160+ MetricsExitCodes PostRequest (HttpClient& http_client, const std::string& output,
161+ ClearcutServer server) {
162+ std::string clearcut_url = ClearcutServerUrl (server);
184163
185- if (!curl) {
186- LOG (ERROR) << " Failed to initialize CURL." ;
187- return kMetricsError ;
164+ Result<HttpResponse<std::string>> http_res =
165+ HttpPostToString (http_client, clearcut_url, output);
166+ if (!http_res.ok ()) {
167+ LOG (ERROR) << " HTTP command failed: " << http_res.error ().FormatForEnv ();
168+ return MetricsExitCodes::kMetricsError ;
188169 }
189170
190- curl_easy_setopt (curl.get (), CURLOPT_WRITEFUNCTION, &curl_out_writer);
191- curl_easy_setopt (curl.get (), CURLOPT_SSL_VERIFYPEER, 0L );
192- curl_easy_setopt (curl.get (), CURLOPT_CURLU, url.get ());
193- curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDS, output.data ());
194- curl_easy_setopt (curl.get (), CURLOPT_POSTFIELDSIZE, output.size ());
195- CURLcode rc = curl_easy_perform (curl.get ());
196- long http_code = 0 ;
197- curl_easy_getinfo (curl.get (), CURLINFO_RESPONSE_CODE, &http_code);
198-
199- if (rc == CURLE_ABORTED_BY_CALLBACK || http_code != 200 ) {
200- LOG (ERROR) << " Metrics message failed: [" << output << " ]" ;
201- LOG (ERROR) << " http error code: " << http_code;
202- LOG (ERROR) << " curl error code: " << rc << " | " << curl_easy_strerror (rc);
203- return kMetricsError ;
171+ if (!http_res->HttpSuccess ()) {
172+ LOG (ERROR) << " Metrics message failed: [" << http_res->data << " ]" ;
173+ LOG (ERROR) << " http error code: " << http_res->http_code ;
174+ return MetricsExitCodes::kMetricsError ;
204175 }
205176 LOG (INFO) << " Metrics posted to ClearCut" ;
206- curl_global_cleanup ();
207- return kSuccess ;
177+ return MetricsExitCodes::kSuccess ;
208178}
209179
210180} // namespace cuttlefish::metrics
0 commit comments