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