mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Initial curl commit
This commit is contained in:
parent
08a956a415
commit
b5d7e43dde
80
oss-internship-2020/curl/CMakeLists.txt
Normal file
80
oss-internship-2020/curl/CMakeLists.txt
Normal file
|
@ -0,0 +1,80 @@
|
|||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(curl_sandboxed)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# add folder containing the non-sandboxed custom curl library
|
||||
|
||||
add_subdirectory(custom_curl)
|
||||
|
||||
# setup sandboxed api
|
||||
|
||||
set(SAPI_ROOT "" CACHE PATH "Path to the Sandboxed API source tree")
|
||||
set(SAPI_ENABLE_EXAMPLES OFF CACHE BOOL "")
|
||||
set(SAPI_ENABLE_TESTS OFF CACHE BOOL "")
|
||||
|
||||
add_subdirectory(
|
||||
"${SAPI_ROOT}"
|
||||
"${CMAKE_BINARY_DIR}/sandboxed-api-build"
|
||||
EXCLUDE_FROM_ALL
|
||||
)
|
||||
|
||||
# generate and include generated sapi header
|
||||
|
||||
add_sapi_library(curl_sapi SHARED
|
||||
|
||||
FUNCTIONS curl_easy_cleanup
|
||||
curl_easy_duphandle
|
||||
curl_easy_escape
|
||||
curl_easy_getinfo
|
||||
curl_easy_getinfo_ptr
|
||||
curl_easy_init
|
||||
curl_easy_pause
|
||||
curl_easy_perform
|
||||
curl_easy_recv
|
||||
curl_easy_reset
|
||||
curl_easy_send
|
||||
curl_easy_setopt
|
||||
curl_easy_setopt_ptr
|
||||
curl_easy_setopt_long
|
||||
curl_easy_setopt_curl_off_t
|
||||
curl_easy_strerror
|
||||
curl_easy_unescape
|
||||
curl_easy_upkeep
|
||||
|
||||
INPUTS "custom_curl/curl/include/curl/curlver.h"
|
||||
"custom_curl/curl/include/curl/system.h"
|
||||
"custom_curl/curl/include/curl/curl.h"
|
||||
"custom_curl/curl/include/curl/easy.h"
|
||||
"custom_curl/custom_curl.h"
|
||||
|
||||
LIBRARY custom_curl
|
||||
|
||||
LIBRARY_NAME Curl
|
||||
|
||||
NAMESPACE ""
|
||||
)
|
||||
|
||||
target_include_directories(curl_sapi INTERFACE
|
||||
"${PROJECT_BINARY_DIR}"
|
||||
)
|
||||
|
||||
# add examples
|
||||
|
||||
add_subdirectory(examples)
|
33
oss-internship-2020/curl/custom_curl/CMakeLists.txt
Normal file
33
oss-internship-2020/curl/custom_curl/CMakeLists.txt
Normal file
|
@ -0,0 +1,33 @@
|
|||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# flags needed to build curl statically
|
||||
|
||||
set(CURL_HIDDEN_SYMBOLS OFF)
|
||||
set(BUILD_SHARED_LIBS OFF)
|
||||
|
||||
add_subdirectory(curl)
|
||||
|
||||
add_library(custom_curl STATIC
|
||||
custom_curl.h
|
||||
custom_curl.cc
|
||||
)
|
||||
|
||||
set_target_properties(custom_curl
|
||||
PROPERTIES LINKER_LANGUAGE C
|
||||
)
|
||||
|
||||
target_link_libraries(custom_curl
|
||||
CURL::libcurl
|
||||
)
|
33
oss-internship-2020/curl/custom_curl/custom_curl.cc
Normal file
33
oss-internship-2020/curl/custom_curl/custom_curl.cc
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <iostream>
|
||||
#include "custom_curl.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option, void* parameter) {
|
||||
return curl_easy_setopt(handle, option, parameter);
|
||||
}
|
||||
|
||||
CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option, long parameter) {
|
||||
return curl_easy_setopt(handle, option, parameter);
|
||||
}
|
||||
|
||||
CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option, curl_off_t parameter) {
|
||||
return curl_easy_setopt(handle, option, parameter);
|
||||
}
|
||||
|
||||
CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option, void* parameter) {
|
||||
return curl_easy_getinfo(handle, option, parameter);
|
||||
}
|
32
oss-internship-2020/curl/custom_curl/custom_curl.h
Normal file
32
oss-internship-2020/curl/custom_curl/custom_curl.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// wrapper for curl library used to implement variadic functions explicitly
|
||||
|
||||
#ifndef CUSTOM_CURL_H
|
||||
#define CUSTOM_CURL_H
|
||||
|
||||
#include <curl/curlver.h>
|
||||
#include <curl/system.h>
|
||||
#include <curl/curl.h>
|
||||
|
||||
extern "C" CURLcode curl_easy_setopt_ptr(CURL *handle, CURLoption option, void* parameter);
|
||||
|
||||
extern "C" CURLcode curl_easy_setopt_long(CURL *handle, CURLoption option, long parameter);
|
||||
|
||||
extern "C" CURLcode curl_easy_setopt_curl_off_t(CURL *handle, CURLoption option, curl_off_t parameter);
|
||||
|
||||
extern "C" CURLcode curl_easy_getinfo_ptr(CURL *handle, CURLINFO option, void* parameter);
|
||||
|
||||
#endif // CUSTOM_CURL_H
|
24
oss-internship-2020/curl/examples/CMakeLists.txt
Normal file
24
oss-internship-2020/curl/examples/CMakeLists.txt
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Copyright 2020 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
# example 1: GET request, verbose
|
||||
|
||||
add_executable(example1
|
||||
example1.cc
|
||||
)
|
||||
|
||||
target_link_libraries(example1 PRIVATE
|
||||
curl_sapi
|
||||
sapi::sapi
|
||||
)
|
78
oss-internship-2020/curl/examples/example1.cc
Normal file
78
oss-internship-2020/curl/examples/example1.cc
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2020 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <libgen.h>
|
||||
#include <syscall.h>
|
||||
#include <iostream>
|
||||
#include "curl_sapi.sapi.h"
|
||||
#include "sandboxed_api/util/flag.h"
|
||||
|
||||
/*
|
||||
int main() {
|
||||
CURL *curl = curl_easy_init();
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1l);
|
||||
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
|
||||
curl_easy_perform(curl);
|
||||
curl_easy_cleanup(curl);
|
||||
}
|
||||
*/
|
||||
|
||||
class CurlApiSandboxEx1 : public CurlSandbox {
|
||||
|
||||
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
|
||||
sandbox2::PolicyBuilder*) override {
|
||||
|
||||
return sandbox2::PolicyBuilder()
|
||||
.DangerDefaultAllowAll()
|
||||
.AllowUnrestrictedNetworking()
|
||||
.AddDirectory("/lib")
|
||||
.BuildOrDie();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
CurlApiSandboxEx1 sandbox;
|
||||
|
||||
auto status_sandbox_init = sandbox.Init();
|
||||
assert(status_sandbox_init.ok());
|
||||
|
||||
CurlApi api(&sandbox);
|
||||
|
||||
auto status_init = api.curl_easy_init();
|
||||
assert(status_init.ok());
|
||||
|
||||
::sapi::v::RemotePtr curl(status_init.value());
|
||||
assert(curl.GetValue());
|
||||
|
||||
auto status_setopt_verbose = api.curl_easy_setopt_long(&curl, CURLOPT_VERBOSE, 1l);
|
||||
assert(status_setopt_verbose.ok());
|
||||
assert(status_setopt_verbose.value() == CURLE_OK);
|
||||
|
||||
sapi::v::ConstCStr url("http://example.com");
|
||||
|
||||
auto status_setopt_url = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
|
||||
assert(status_setopt_url.ok());
|
||||
assert(status_setopt_url.value() == CURLE_OK);
|
||||
|
||||
auto status_perform = api.curl_easy_perform(&curl);
|
||||
assert(status_perform.ok());
|
||||
assert(status_perform.value() == CURLE_OK);
|
||||
|
||||
auto status_cleanup = api.curl_easy_cleanup(&curl);
|
||||
assert(status_cleanup.ok());
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user