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 getinmemory.c
16*ec63e07aSXin Li // HTTP GET request using callbacks
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 "absl/strings/str_cat.h"
23*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h"
24*ec63e07aSXin Li
25*ec63e07aSXin Li namespace {
26*ec63e07aSXin Li
Example2()27*ec63e07aSXin Li absl::Status Example2() {
28*ec63e07aSXin Li // Initialize sandbox2 and sapi
29*ec63e07aSXin Li curl::CurlSapiSandbox sandbox;
30*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(sandbox.Init());
31*ec63e07aSXin Li curl::CurlApi api(&sandbox);
32*ec63e07aSXin Li
33*ec63e07aSXin Li // Generate pointer to WriteMemoryCallback function
34*ec63e07aSXin Li void* function_ptr;
35*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(
36*ec63e07aSXin Li sandbox.rpc_channel()->Symbol("WriteToMemory", &function_ptr));
37*ec63e07aSXin Li sapi::v::RemotePtr write_to_memory(function_ptr);
38*ec63e07aSXin Li
39*ec63e07aSXin Li // Initialize the curl session
40*ec63e07aSXin Li curl::CURL* curl_handle;
41*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(curl_handle, api.curl_easy_init());
42*ec63e07aSXin Li sapi::v::RemotePtr curl(curl_handle);
43*ec63e07aSXin Li if (!curl_handle) {
44*ec63e07aSXin Li return absl::UnavailableError("curl_easy_init failed: Invalid curl handle");
45*ec63e07aSXin Li }
46*ec63e07aSXin Li
47*ec63e07aSXin Li int curl_code;
48*ec63e07aSXin Li
49*ec63e07aSXin Li // Specify URL to get
50*ec63e07aSXin Li sapi::v::ConstCStr url("http://example.com");
51*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(
52*ec63e07aSXin Li curl_code,
53*ec63e07aSXin Li api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_URL, url.PtrBefore()));
54*ec63e07aSXin Li if (curl_code != 0) {
55*ec63e07aSXin Li return absl::UnavailableError(absl::StrCat(
56*ec63e07aSXin Li "curl_easy_setopt_ptr failed: ", curl::StrError(&api, curl_code)));
57*ec63e07aSXin Li }
58*ec63e07aSXin Li
59*ec63e07aSXin Li // Set WriteMemoryCallback as the write function
60*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(
61*ec63e07aSXin Li curl_code, api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_WRITEFUNCTION,
62*ec63e07aSXin Li &write_to_memory));
63*ec63e07aSXin Li if (curl_code != 0) {
64*ec63e07aSXin Li return absl::UnavailableError(absl::StrCat(
65*ec63e07aSXin Li "curl_easy_setopt_ptr failed: ", curl::StrError(&api, curl_code)));
66*ec63e07aSXin Li }
67*ec63e07aSXin Li
68*ec63e07aSXin Li // Pass 'chunk' struct to the callback function
69*ec63e07aSXin Li sapi::v::LenVal chunk(0);
70*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(curl_code,
71*ec63e07aSXin Li api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_WRITEDATA,
72*ec63e07aSXin Li chunk.PtrBoth()));
73*ec63e07aSXin Li if (curl_code != 0) {
74*ec63e07aSXin Li return absl::UnavailableError(absl::StrCat(
75*ec63e07aSXin Li "curl_easy_setopt_ptr failed: ", curl::StrError(&api, curl_code)));
76*ec63e07aSXin Li }
77*ec63e07aSXin Li
78*ec63e07aSXin Li // Set a user agent
79*ec63e07aSXin Li sapi::v::ConstCStr user_agent("libcurl-agent/1.0");
80*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(curl_code,
81*ec63e07aSXin Li api.curl_easy_setopt_ptr(&curl, curl::CURLOPT_USERAGENT,
82*ec63e07aSXin Li user_agent.PtrBefore()));
83*ec63e07aSXin Li if (curl_code != 0) {
84*ec63e07aSXin Li return absl::UnavailableError(absl::StrCat(
85*ec63e07aSXin Li "curl_easy_setopt_ptr failed: ", curl::StrError(&api, curl_code)));
86*ec63e07aSXin Li }
87*ec63e07aSXin Li
88*ec63e07aSXin Li // Perform the request
89*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(curl_code, api.curl_easy_perform(&curl));
90*ec63e07aSXin Li if (curl_code != 0) {
91*ec63e07aSXin Li return absl::UnavailableError(absl::StrCat(
92*ec63e07aSXin Li "curl_easy_perform failed: ", curl::StrError(&api, curl_code)));
93*ec63e07aSXin Li }
94*ec63e07aSXin Li
95*ec63e07aSXin Li // Retrieve memory size
96*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(sandbox.TransferFromSandboxee(&chunk));
97*ec63e07aSXin Li std::cout << "memory size: " << chunk.GetDataSize() << " bytes" << std::endl;
98*ec63e07aSXin Li
99*ec63e07aSXin Li // Cleanup curl
100*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(api.curl_easy_cleanup(&curl));
101*ec63e07aSXin Li
102*ec63e07aSXin Li return absl::OkStatus();
103*ec63e07aSXin Li }
104*ec63e07aSXin Li
105*ec63e07aSXin Li } // namespace
106*ec63e07aSXin Li
main(int argc,char * argv[])107*ec63e07aSXin Li int main(int argc, char* argv[]) {
108*ec63e07aSXin Li gflags::ParseCommandLineFlags(&argc, &argv, true);
109*ec63e07aSXin Li sapi::InitLogging(argv[0]);
110*ec63e07aSXin Li
111*ec63e07aSXin Li if (absl::Status status = Example2(); !status.ok()) {
112*ec63e07aSXin Li LOG(ERROR) << "Example2 failed: " << status.ToString();
113*ec63e07aSXin Li return EXIT_FAILURE;
114*ec63e07aSXin Li }
115*ec63e07aSXin Li
116*ec63e07aSXin Li return EXIT_SUCCESS;
117*ec63e07aSXin Li }
118