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