From b5d7e43ddeff9c087d0f67949bea6ac795c5474a Mon Sep 17 00:00:00 2001 From: Federico Stazi <34340238+FedericoStazi@users.noreply.github.com> Date: Wed, 5 Aug 2020 10:54:31 +0000 Subject: [PATCH] Initial curl commit --- oss-internship-2020/curl/CMakeLists.txt | 80 +++++++++++++++++++ .../curl/custom_curl/CMakeLists.txt | 33 ++++++++ .../curl/custom_curl/custom_curl.cc | 33 ++++++++ .../curl/custom_curl/custom_curl.h | 32 ++++++++ .../curl/examples/CMakeLists.txt | 24 ++++++ oss-internship-2020/curl/examples/example1.cc | 78 ++++++++++++++++++ 6 files changed, 280 insertions(+) create mode 100644 oss-internship-2020/curl/CMakeLists.txt create mode 100644 oss-internship-2020/curl/custom_curl/CMakeLists.txt create mode 100644 oss-internship-2020/curl/custom_curl/custom_curl.cc create mode 100644 oss-internship-2020/curl/custom_curl/custom_curl.h create mode 100644 oss-internship-2020/curl/examples/CMakeLists.txt create mode 100644 oss-internship-2020/curl/examples/example1.cc diff --git a/oss-internship-2020/curl/CMakeLists.txt b/oss-internship-2020/curl/CMakeLists.txt new file mode 100644 index 0000000..5207271 --- /dev/null +++ b/oss-internship-2020/curl/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/oss-internship-2020/curl/custom_curl/CMakeLists.txt b/oss-internship-2020/curl/custom_curl/CMakeLists.txt new file mode 100644 index 0000000..4da372c --- /dev/null +++ b/oss-internship-2020/curl/custom_curl/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/oss-internship-2020/curl/custom_curl/custom_curl.cc b/oss-internship-2020/curl/custom_curl/custom_curl.cc new file mode 100644 index 0000000..3df5ed3 --- /dev/null +++ b/oss-internship-2020/curl/custom_curl/custom_curl.cc @@ -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 +#include "custom_curl.h" +#include + +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); +} \ No newline at end of file diff --git a/oss-internship-2020/curl/custom_curl/custom_curl.h b/oss-internship-2020/curl/custom_curl/custom_curl.h new file mode 100644 index 0000000..ad9cf02 --- /dev/null +++ b/oss-internship-2020/curl/custom_curl/custom_curl.h @@ -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 +#include +#include + +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 \ No newline at end of file diff --git a/oss-internship-2020/curl/examples/CMakeLists.txt b/oss-internship-2020/curl/examples/CMakeLists.txt new file mode 100644 index 0000000..a41ef47 --- /dev/null +++ b/oss-internship-2020/curl/examples/CMakeLists.txt @@ -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 +) \ No newline at end of file diff --git a/oss-internship-2020/curl/examples/example1.cc b/oss-internship-2020/curl/examples/example1.cc new file mode 100644 index 0000000..2365c26 --- /dev/null +++ b/oss-internship-2020/curl/examples/example1.cc @@ -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 +#include +#include +#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 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()); + +} +