Initial curl commit

pull/48/head
Federico Stazi 2020-08-05 10:54:31 +00:00 committed by Federico Stazi
parent 08a956a415
commit b5d7e43dde
6 changed files with 280 additions and 0 deletions

View 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)

View 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
)

View 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);
}

View 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

View 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
)

View 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());
}