xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/curl/examples/example5.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Sandboxed version of multithread.c
16 // Multithreaded HTTP GET requests
17 
18 #include <cstdlib>
19 #include <future>  // NOLINT(build/c++11)
20 #include <thread>  // NOLINT(build/c++11)
21 
22 #include "../curl_util.h"    // NOLINT(build/include)
23 #include "../sandbox.h"      // NOLINT(build/include)
24 #include "curl_sapi.sapi.h"  // NOLINT(build/include)
25 #include "absl/strings/str_cat.h"
26 #include "sandboxed_api/util/status_macros.h"
27 
28 namespace {
29 
pull_one_url(const std::string & url,curl::CurlApi & api)30 absl::Status pull_one_url(const std::string& url, curl::CurlApi& api) {
31   // Initialize the curl session
32   curl::CURL* curl_handle;
33   SAPI_ASSIGN_OR_RETURN(curl_handle, api.curl_easy_init());
34   sapi::v::RemotePtr curl(curl_handle);
35   if (!curl_handle) {
36     return absl::UnavailableError("curl_easy_init failed: Invalid curl handle");
37   }
38 
39   int curl_code;
40 
41   // Specify URL to get
42   sapi::v::ConstCStr sapi_url(url.c_str());
43   SAPI_ASSIGN_OR_RETURN(
44       curl_code,
45       api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_URL, sapi_url.PtrBefore()));
46   if (curl_code != 0) {
47     return absl::UnavailableError(absl::StrCat(
48         "curl_easy_setopt_ptr failed: ", curl::StrError(&api, curl_code)));
49   }
50 
51   // Perform the request
52   SAPI_ASSIGN_OR_RETURN(curl_code, api.curl_easy_perform(&curl));
53   if (curl_code != 0) {
54     return absl::UnavailableError(absl::StrCat(
55         "curl_easy_perform failed: ", curl::StrError(&api, curl_code)));
56   }
57 
58   // Cleanup curl easy handle
59   SAPI_RETURN_IF_ERROR(api.curl_easy_cleanup(&curl));
60 
61   return absl::OkStatus();
62 }
63 
Example5()64 absl::Status Example5() {
65   // Initialize sandbox2 and sapi
66   curl::CurlSapiSandbox sandbox;
67   SAPI_RETURN_IF_ERROR(sandbox.Init());
68   curl::CurlApi api(&sandbox);
69 
70   int curl_code;
71 
72   // Initialize curl (CURL_GLOBAL_DEFAULT = 3)
73   SAPI_ASSIGN_OR_RETURN(curl_code, api.curl_global_init(3l));
74   if (curl_code != 0) {
75     return absl::UnavailableError(absl::StrCat(
76         "curl_global_init failed: ", curl::StrError(&api, curl_code)));
77   }
78 
79   // Create the threads (by using futures)
80   const std::vector<std::string> urls = {
81       "http://example.com", "http://example.edu", "http://example.net",
82       "http://example.org"};
83   std::vector<std::future<absl::Status>> futures;
84   for (auto& url : urls) {
85     futures.emplace_back(
86         std::async(pull_one_url, std::ref(url), std::ref(api)));
87   }
88 
89   // Join the threads and check for errors
90   for (auto& future : futures) {
91     SAPI_RETURN_IF_ERROR(future.get());
92   }
93 
94   // Cleanup curl
95   SAPI_RETURN_IF_ERROR(api.curl_global_cleanup());
96 
97   return absl::OkStatus();
98 }
99 
100 }  // namespace
101 
main(int argc,char * argv[])102 int main(int argc, char* argv[]) {
103   gflags::ParseCommandLineFlags(&argc, &argv, true);
104   sapi::InitLogging(argv[0]);
105 
106   if (absl::Status status = Example5(); !status.ok()) {
107     LOG(ERROR) << "Example5 failed: " << status.ToString();
108     return EXIT_FAILURE;
109   }
110 
111   return EXIT_SUCCESS;
112 }
113