xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/curl/examples/example1.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 simple.c
16*ec63e07aSXin Li // Simple HTTP GET request
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <cstdlib>
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include "../curl_util.h"    // NOLINT(build/include)
21*ec63e07aSXin Li #include "../sandbox.h"      // NOLINT(build/include)
22*ec63e07aSXin Li #include "curl_sapi.sapi.h"  // NOLINT(build/include)
23*ec63e07aSXin Li #include "absl/strings/str_cat.h"
24*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h"
25*ec63e07aSXin Li 
26*ec63e07aSXin Li namespace {
27*ec63e07aSXin Li 
Example1()28*ec63e07aSXin Li absl::Status Example1() {
29*ec63e07aSXin Li   // Initialize sandbox2 and sapi
30*ec63e07aSXin Li   curl::CurlSapiSandbox sandbox;
31*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(sandbox.Init());
32*ec63e07aSXin Li   curl::CurlApi api(&sandbox);
33*ec63e07aSXin Li 
34*ec63e07aSXin Li   // Initialize the curl session
35*ec63e07aSXin Li   curl::CURL* curl_handle;
36*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(curl_handle, api.curl_easy_init());
37*ec63e07aSXin Li   sapi::v::RemotePtr curl(curl_handle);
38*ec63e07aSXin Li   if (!curl_handle) {
39*ec63e07aSXin Li     return absl::UnknownError("curl_easy_init failed: Invalid curl handle");
40*ec63e07aSXin Li   }
41*ec63e07aSXin Li 
42*ec63e07aSXin Li   int curl_code;
43*ec63e07aSXin Li 
44*ec63e07aSXin Li   // Specify URL to get
45*ec63e07aSXin Li   sapi::v::ConstCStr url("http://example.com");
46*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
47*ec63e07aSXin Li       curl_code,
48*ec63e07aSXin Li       api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_URL, url.PtrBefore()));
49*ec63e07aSXin Li   if (curl_code != 0) {
50*ec63e07aSXin Li     return absl::UnknownError(absl::StrCat("curl_easy_setopt_ptr failed: ",
51*ec63e07aSXin Li                                            curl::StrError(&api, curl_code)));
52*ec63e07aSXin Li   }
53*ec63e07aSXin Li 
54*ec63e07aSXin Li   // Set the library to follow a redirection
55*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
56*ec63e07aSXin Li       curl_code,
57*ec63e07aSXin Li       api.curl_easy_setopt_long(&curl, curl::CURLOPT_FOLLOWLOCATION, 1l));
58*ec63e07aSXin Li   if (curl_code != 0) {
59*ec63e07aSXin Li     return absl::UnknownError(absl::StrCat("curl_easy_setopt_long failed: ",
60*ec63e07aSXin Li                                            curl::StrError(&api, curl_code)));
61*ec63e07aSXin Li   }
62*ec63e07aSXin Li 
63*ec63e07aSXin Li   // Disable authentication of peer certificate
64*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
65*ec63e07aSXin Li       curl_code,
66*ec63e07aSXin Li       api.curl_easy_setopt_long(&curl, curl::CURLOPT_SSL_VERIFYPEER, 0l));
67*ec63e07aSXin Li   if (curl_code != 0) {
68*ec63e07aSXin Li     return absl::UnknownError(absl::StrCat("curl_easy_setopt_long failed: ",
69*ec63e07aSXin Li                                            curl::StrError(&api, curl_code)));
70*ec63e07aSXin Li   }
71*ec63e07aSXin Li 
72*ec63e07aSXin Li   // Perform the request
73*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(curl_code, api.curl_easy_perform(&curl));
74*ec63e07aSXin Li   if (curl_code != 0) {
75*ec63e07aSXin Li     return absl::UnknownError(absl::StrCat("curl_easy_perform failed: ",
76*ec63e07aSXin Li                                            curl::StrError(&api, curl_code)));
77*ec63e07aSXin Li   }
78*ec63e07aSXin Li 
79*ec63e07aSXin Li   // Cleanup curl
80*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.curl_easy_cleanup(&curl));
81*ec63e07aSXin Li 
82*ec63e07aSXin Li   return absl::OkStatus();
83*ec63e07aSXin Li }
84*ec63e07aSXin Li 
85*ec63e07aSXin Li }  // namespace
86*ec63e07aSXin Li 
main(int argc,char * argv[])87*ec63e07aSXin Li int main(int argc, char* argv[]) {
88*ec63e07aSXin Li   gflags::ParseCommandLineFlags(&argc, &argv, true);
89*ec63e07aSXin Li   sapi::InitLogging(argv[0]);
90*ec63e07aSXin Li 
91*ec63e07aSXin Li   if (absl::Status status = Example1(); !status.ok()) {
92*ec63e07aSXin Li     LOG(ERROR) << "Example1 failed: " << status.ToString();
93*ec63e07aSXin Li     return EXIT_FAILURE;
94*ec63e07aSXin Li   }
95*ec63e07aSXin Li 
96*ec63e07aSXin Li   return EXIT_SUCCESS;
97*ec63e07aSXin Li }
98