xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/curl/examples/example6.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 using transactions
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/transaction.h"
25 #include "sandboxed_api/util/status_macros.h"
26 
27 namespace {
28 
29 class CurlTransaction : public sapi::Transaction {
30  public:
CurlTransaction(std::unique_ptr<sapi::Sandbox> sandbox)31   explicit CurlTransaction(std::unique_ptr<sapi::Sandbox> sandbox)
32       : sapi::Transaction(std::move(sandbox)) {
33     sapi::Transaction::SetTimeLimit(kTimeOutVal);
34   }
35 
36  private:
37   // Default timeout value for each transaction run.
38   static constexpr time_t kTimeOutVal = 2;
39 
40   // The main processing function.
41   absl::Status Main() override;
42 };
43 
Main()44 absl::Status CurlTransaction::Main() {
45   curl::CurlApi api(sandbox());
46 
47   // Initialize the curl session
48   SAPI_ASSIGN_OR_RETURN(void* curl_remote, api.curl_easy_init());
49   sapi::v::RemotePtr curl(curl_remote);
50   TRANSACTION_FAIL_IF_NOT(curl.GetValue(), "curl_easy_init failed");
51 
52   // Specify URL to get
53   sapi::v::ConstCStr url("http://example.com");
54   SAPI_ASSIGN_OR_RETURN(
55       int setopt_url_code,
56       api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_URL, url.PtrBefore()));
57   TRANSACTION_FAIL_IF_NOT(setopt_url_code == curl::CURLE_OK,
58                           "curl_easy_setopt_ptr failed");
59 
60   // Perform the request
61   SAPI_ASSIGN_OR_RETURN(int perform_code, api.curl_easy_perform(&curl));
62   TRANSACTION_FAIL_IF_NOT(setopt_url_code == curl::CURLE_OK,
63                           "curl_easy_perform failed");
64 
65   // Cleanup curl
66   TRANSACTION_FAIL_IF_NOT(api.curl_easy_cleanup(&curl).ok(),
67                           "curl_easy_cleanup failed");
68 
69   return absl::OkStatus();
70 }
71 
72 }  // namespace
73 
main(int argc,char * argv[])74 int main(int argc, char* argv[]) {
75   CurlTransaction curl{std::make_unique<curl::CurlSapiSandbox>()};
76   absl::Status status = curl.Run();
77   CHECK(status.ok()) << "CurlTransaction failed";
78 
79   return EXIT_SUCCESS;
80 }
81