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