Address reviews and improve code style

This commit is contained in:
Federico Stazi 2020-08-18 10:44:53 +00:00
parent f47e1cc6ac
commit 9291231275
16 changed files with 187 additions and 207 deletions

5
.gitmodules vendored
View File

@ -4,7 +4,6 @@
[submodule "oss-internship-2020/pffft/master"]
path = oss-internship-2020/pffft/master
url = https://bitbucket.org/jpommier/pffft/src/master/
[submodule "oss-internship-2020/curl/custom_curl/curl"]
path = oss-internship-2020/curl/custom_curl/curl
[submodule "oss-internship-2020/curl/curl_wrapper/curl"]
path = oss-internship-2020/curl/curl_wrapper/curl
url = https://github.com/curl/curl

View File

@ -48,7 +48,7 @@ The pointers can then be obtained using an `RPCChannel` object, as shown in `exa
## Examples
The `examples` directory contains the sandboxed versions of example source codes taken from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website.
The `examples` directory contains the sandboxed versions of example source codes taken from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website. More information about each example can be found in the examples' [README](examples/README.md).
To build these examples when building the library, the cmake variable `CURL_SAPI_ENABLE_EXAMPLES` must be set to `ON`.

View File

@ -12,16 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# Wrapper library including curl,
# adds the explicit versions of the variadic methods and the callbacks
# Wrapper library including curl, wrappers for some methods and callbacks
# The CURL_SAPI_CALLBACKS variable should contain the absolute paths of
# all the files implementing the callbacks
add_library(custom_curl_and_callbacks OBJECT
custom_curl.h
custom_curl.cc
add_library(curl_wrapper_and_callbacks OBJECT
curl_wrapper.h
curl_wrapper.cc
"${CURL_SAPI_CALLBACKS}"
)
set_target_properties(custom_curl_and_callbacks
set_target_properties(curl_wrapper_and_callbacks
PROPERTIES LINKER_LANGUAGE C
)
@ -31,6 +30,6 @@ set(BUILD_SHARED_LIBS OFF)
# Link the wrapper to the original curl library
add_subdirectory(curl)
target_link_libraries(custom_curl_and_callbacks
target_link_libraries(curl_wrapper_and_callbacks
CURL::libcurl
)

@ -0,0 +1 @@
Subproject commit 6332f65714d14b3f19662fd0607c7b59af400b9b

View File

@ -12,83 +12,78 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "custom_curl.h"
#include "curl_wrapper.h"
#include <cstdlib>
#include <iostream>
CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
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,
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,
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) {
CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option, void* parameter) {
return curl_easy_getinfo(handle, option, parameter);
}
time_t_sapi curl_getdate_sapi(char *datestring, time_t_sapi *now ) {
time_t_sapi curl_getdate_sapi(char* datestring, time_t_sapi* now) {
return curl_getdate(datestring, now);
}
CURLMcode curl_multi_fdset_sapi(CURLM *multi_handle,
fd_set_sapi *read_fd_set,
fd_set_sapi *write_fd_set,
fd_set_sapi *exc_fd_set,
int *max_fd) {
return curl_multi_fdset(multi_handle, read_fd_set, write_fd_set, exc_fd_set,
CURLMcode curl_multi_fdset_sapi(CURLM* multi_handle, fd_set_sapi* read_fd_set,
fd_set_sapi* write_fd_set,
fd_set_sapi* exc_fd_set, int* max_fd) {
return curl_multi_fdset(multi_handle, read_fd_set, write_fd_set, exc_fd_set,
max_fd);
}
CURLMcode curl_mutti_setopt_ptr(CURLM* handle, CURLMoption option,
CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option,
void* parameter) {
return curl_multi_setopt(handle, option, parameter);
}
CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
long parameter) {
CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
long parameter) {
return curl_multi_setopt(handle, option, parameter);
}
CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, CURLMoption option,
curl_off_t parameter) {
CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, CURLMoption option,
curl_off_t parameter) {
return curl_multi_setopt(handle, option, parameter);
}
CURLMcode curl_multi_poll_sapi(CURLM *multi_handle,
CURLMcode curl_multi_poll_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds,
unsigned int extra_nfds,
int timeout_ms,
int *numfds) {
return curl_multi_poll(multi_handle, extra_fds, extra_nfds, timeout_ms,
unsigned int extra_nfds, int timeout_ms,
int* numfds) {
return curl_multi_poll(multi_handle, extra_fds, extra_nfds, timeout_ms,
numfds);
}
CURLMcode curl_multi_wait_sapi(CURLM *multi_handle,
CURLMcode curl_multi_wait_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds,
unsigned int extra_nfds,
int timeout_ms,
int *numfds) {
return curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms,
unsigned int extra_nfds, int timeout_ms,
int* numfds) {
return curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms,
numfds);
}
CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
void* parameter) {
CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
void* parameter) {
return curl_share_setopt(handle, option, parameter);
}
CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option,
long parameter) {
CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option,
long parameter) {
return curl_share_setopt(handle, option, parameter);
}

View File

@ -14,17 +14,17 @@
// Wrapper for curl library
#ifndef CUSTOM_CURL_H
#define CUSTOM_CURL_H
#ifndef CURL_WRAPPER_H
#define CURL_WRAPPER_H
#include <curl/curl.h>
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
extern "C" CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
void* parameter);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option,
extern "C" CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option,
long parameter);
// The wrapper method is needed to make the variadic argument explicit
@ -32,56 +32,55 @@ extern "C" CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option,
curl_off_t parameter);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option,
extern "C" CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option,
void* parameter);
// The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h
extern "C" typedef time_t time_t_sapi;
extern "C" time_t_sapi curl_getdate_sapi(char *datestring, time_t_sapi *now );
extern "C" time_t_sapi curl_getdate_sapi(char* datestring, time_t_sapi* now);
// The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h
extern "C" typedef fd_set fd_set_sapi;
extern "C" CURLMcode curl_multi_fdset_sapi(CURLM *multi_handle,
fd_set_sapi *read_fd_set,
fd_set_sapi *write_fd_set,
fd_set_sapi *exc_fd_set,
int *max_fd);
extern "C" CURLMcode curl_multi_fdset_sapi(CURLM* multi_handle,
fd_set_sapi* read_fd_set,
fd_set_sapi* write_fd_set,
fd_set_sapi* exc_fd_set,
int* max_fd);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option,
extern "C" CURLMcode curl_multi_setopt_ptr(CURLM* handle, CURLMoption option,
void* parameter);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
extern "C" CURLMcode curl_multi_setopt_long(CURLM* handle, CURLMoption option,
long parameter);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle,
extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle,
CURLMoption option,
curl_off_t parameter);
// The wrapper method is needed because incomplete array type is not supported
extern "C" CURLMcode curl_multi_poll_sapi(CURLM *multi_handle,
extern "C" CURLMcode curl_multi_poll_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds,
unsigned int extra_nfds,
int timeout_ms,
int *numfds);
int timeout_ms, int* numfds);
// The wrapper method is needed because incomplete array type is not supported
extern "C" CURLMcode curl_multi_wait_sapi(CURLM *multi_handle,
extern "C" CURLMcode curl_multi_wait_sapi(CURLM* multi_handle,
struct curl_waitfd* extra_fds,
unsigned int extra_nfds,
int timeout_ms,
int *numfds);
int timeout_ms, int* numfds);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
extern "C" CURLSHcode curl_share_setopt_ptr(CURLSH* handle, CURLSHoption option,
void* parameter);
// The wrapper method is needed to make the variadic argument explicit
extern "C" CURLSHcode curl_share_setopt_long(CURLSH* handle, CURLSHoption option,
extern "C" CURLSHcode curl_share_setopt_long(CURLSH* handle,
CURLSHoption option,
long parameter);
#endif // CUSTOM_CURL_H
#endif // CURL_WRAPPER_H

@ -1 +0,0 @@
Subproject commit a0c461434c747ad6daff0c85c0a8bbe9cfc44d75

View File

@ -16,11 +16,11 @@
# (https://curl.haxx.se/libcurl/c/example.html)
# Example 1: simple.c
add_executable(example1
add_executable(example1
example1.cc
)
target_link_libraries(example1 PRIVATE
curl_sapi
curl_sapi
sapi::sapi
)
@ -58,4 +58,4 @@ add_executable(example5
target_link_libraries(example5 PRIVATE
curl_sapi
sapi::sapi
)
)

View File

@ -0,0 +1,11 @@
# Curl Sandboxed Examples
Each example in this folder is the sandboxed version of a code snippet from [this page](https://curl.haxx.se/libcurl/c/example.html) on curl's website. These examples perform some basic tasks using libcurl, and can be useful both to understand how to use Curl Sandboxed, but also to get an idea of how regular and sandboxed code compare to each other.
This is the list of the examples:
- **example1**: sandboxed version of [simple.c](https://curl.haxx.se/libcurl/c/simple.html). Really simple HTTP request, downloads and prints out the page at [example.com](http://example.com).
- **example2**: sandboxed version of [getinmemory.c](https://curl.haxx.se/libcurl/c/getinmemory.html). Same HTTP request as example1. The difference is that this example uses a callback to save the page directly in memory. Only the page size is printed out.
- **example3**: sandboxed version of [simplessl.c](https://curl.haxx.se/libcurl/c/simplessl.html). HTTPS request of the [example.com](https://example.com) page, using SSL authentication. This script takes 4 arguments (SSL certificates file, SSL keys file, SSL keys password and CA certificates files), and prints out the page.
- **example4**: sandboxed version of [multi-poll.c](https://curl.haxx.se/libcurl/c/multi-poll.html). Same HTTP request as example1, with the addition of a polling method that can be used to track the status of the request. The page is printed out after it is downloaded.
- **example5**: sandboxed version of [multithread.c](https://curl.haxx.se/libcurl/c/multithread.html). Four HTTP request of the pages [example.com](http://example.com), [example.edu](http://example.edu), [example.net](http://example.net), [example.org](http://example.org) pages, performed at the same time using libcurl's multithreading methods. The threads' status and the pages are printed out.

View File

@ -17,16 +17,15 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
// Function taken from curl's getinmemory.c
size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) {
// Function taken from curl's getinmemory.c
size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) {
size_t realsize = size * nmemb;
struct MemoryStruct* mem = (struct MemoryStruct*)userp;
char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1);
if(ptr == NULL) { // Out of memory
if (ptr == NULL) { // Out of memory
std::cout << "not enough memory (realloc returned NULL)\n";
return 0;
}
@ -35,7 +34,6 @@ size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
return realsize;
}

View File

@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CALLBACKS_H
#define CALLBACKS_H
#ifndef EXAMPLES_CALLBACKS_H
#define EXAMPLES_CALLBACKS_H
#include <curl/curl.h>
@ -22,7 +22,7 @@ extern "C" struct MemoryStruct {
size_t size;
};
extern "C" size_t WriteMemoryCallback(void* contents, size_t size,
size_t nmemb, void* userp);
extern "C" size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp);
#endif // CALLBACKS_H
#endif // EXAMPLES_CALLBACKS_H

View File

@ -22,26 +22,25 @@
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx1 : public CurlSandbox {
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
};
int main(int argc, char* argv[]) {
absl::Status status;
sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int;
// Initialize sandbox2 and sapi
CurlApiSandboxEx1 sandbox;
CurlApiSandboxEx1 sandbox;
status = sandbox.Init();
assert(status.ok());
CurlApi api(&sandbox);
@ -53,7 +52,7 @@ int main(int argc, char* argv[]) {
assert(curl.GetValue()); // Checking curl != nullptr
// Specify URL to get
sapi::v::ConstCStr url("http://example.com");
sapi::v::ConstCStr url("http://example.com");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -63,7 +62,7 @@ int main(int argc, char* argv[]) {
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
//curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
status_or_int = api.curl_easy_setopt_long(&curl, CURLOPT_SSL_VERIFYPEER, 0l);
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -78,5 +77,4 @@ int main(int argc, char* argv[]) {
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -27,20 +27,19 @@ struct MemoryStruct {
};
class CurlApiSandboxEx2 : public CurlSandbox {
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
};
int main() {
absl::Status status;
sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int;
@ -71,21 +70,21 @@ int main() {
assert(status_or_int.value() == CURLE_OK);
// Set WriteMemoryCallback as the write function
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEFUNCTION,
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEFUNCTION,
&remote_function_ptr);
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Pass 'chunk' struct to the callback function
sapi::v::Struct<MemoryStruct> chunk;
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA,
chunk.PtrBoth());
status_or_int =
api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA, chunk.PtrBoth());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Set a user agent
sapi::v::ConstCStr user_agent("libcurl-agent/1.0");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_USERAGENT,
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_USERAGENT,
user_agent.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -107,5 +106,4 @@ int main() {
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -22,38 +22,33 @@
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx3 : public CurlSandbox {
public:
CurlApiSandboxEx3(std::string ssl_certificate, std::string ssl_key,
std::string ca_certificates)
: ssl_certificate(ssl_certificate),
ssl_key(ssl_key),
ca_certificates(ca_certificates) {}
public:
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.AddFile(ssl_certificate)
.AddFile(ssl_key)
.AddFile(ca_certificates)
.BuildOrDie();
}
CurlApiSandboxEx3(std::string ssl_certificate, std::string ssl_key,
std::string ca_certificates)
: ssl_certificate(ssl_certificate),
ssl_key(ssl_key),
ca_certificates(ca_certificates) {}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.AddFile(ssl_certificate)
.AddFile(ssl_key)
.AddFile(ca_certificates)
.BuildOrDie();
}
std::string ssl_certificate;
std::string ssl_key;
std::string ca_certificates;
std::string ssl_certificate;
std::string ssl_key;
std::string ca_certificates;
};
int main(int argc, char* argv[]) {
absl::Status status;
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl;
@ -70,7 +65,7 @@ int main(int argc, char* argv[]) {
status = sandbox.Init();
assert(status.ok());
CurlApi api(&sandbox);
// Initialize curl (CURL_GLOBAL_DEFAULT = 3)
status_or_int = api.curl_global_init(3l);
assert(status_or_int.ok());
@ -84,29 +79,28 @@ int main(int argc, char* argv[]) {
// Specify URL to get (using HTTPS)
sapi::v::ConstCStr url("https://example.com");
status_or_int =
api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Set the SSL certificate type to "PEM"
sapi::v::ConstCStr ssl_cert_type("PEM");
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERTTYPE,
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERTTYPE,
ssl_cert_type.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Set the certificate for client authentication
sapi::v::ConstCStr sapi_ssl_certificate(ssl_certificate.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERT,
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLCERT,
sapi_ssl_certificate.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Set the private key for client authentication
sapi::v::ConstCStr sapi_ssl_key(ssl_key.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY,
sapi_ssl_key.PtrBefore());
status_or_int =
api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY, sapi_ssl_key.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -119,7 +113,7 @@ int main(int argc, char* argv[]) {
// Set the file with the certificates vaildating the server
sapi::v::ConstCStr sapi_ca_certificates(ca_certificates.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_CAINFO,
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_CAINFO,
sapi_ca_certificates.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -141,7 +135,6 @@ int main(int argc, char* argv[]) {
// Cleanup curl
status = api.curl_global_cleanup();
assert(status.ok());
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -22,20 +22,19 @@
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx4 : public CurlSandbox {
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
};
int main() {
absl::Status status;
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl;
@ -60,11 +59,11 @@ int main() {
assert(status_or_curl.ok());
sapi::v::RemotePtr http_handle(status_or_curl.value());
assert(http_handle.GetValue()); // Checking http_handle != nullptr
// Specify URL to get
sapi::v::ConstCStr url("http://example.com");
status_or_int =
api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore());
status_or_int =
api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -79,20 +78,19 @@ int main() {
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
while (still_running.GetValue()) {
while (still_running.GetValue()) {
sapi::v::Int numfds(0);
// Perform the request
status_or_int = api.curl_multi_perform(&multi_handle,
still_running.PtrBoth());
status_or_int =
api.curl_multi_perform(&multi_handle, still_running.PtrBoth());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
if (still_running.GetValue()) {
// Wait for an event or timeout
sapi::v::NullPtr null_ptr;
status_or_int = api.curl_multi_poll_sapi(&multi_handle, &null_ptr, 0,
status_or_int = api.curl_multi_poll_sapi(&multi_handle, &null_ptr, 0,
1000, numfds.PtrBoth());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLM_OK);
@ -116,7 +114,6 @@ int main() {
// Cleanup curl
status = api.curl_global_cleanup();
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -24,27 +24,26 @@
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx5 : public CurlSandbox {
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll()
.AllowUnrestrictedNetworking()
.AddDirectory("/lib")
.BuildOrDie();
}
};
struct thread_args {
const char* url;
CurlApi* api;
};
constexpr int kThreadsnumber = 4;
void *pull_one_url(void* args) {
void* pull_one_url(void* args) {
absl::Status status;
sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int;
@ -58,9 +57,9 @@ void *pull_one_url(void* args) {
assert(curl.GetValue()); // Checking curl != nullptr
// Specify URL to get
sapi::v::ConstCStr sapi_url(((thread_args*)args)->url);
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL,
sapi_url.PtrBefore());
sapi::v::ConstCStr sapi_url(((thread_args*)args)->url);
status_or_int =
api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, sapi_url.PtrBefore());
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
@ -74,18 +73,13 @@ void *pull_one_url(void* args) {
assert(status.ok());
return NULL;
}
const char * const urls[kThreadsnumber] = {
"http://example.com",
"http://example.edu",
"http://example.net",
"http://example.org"
};
int main(int argc, char **argv) {
const char* const urls[kThreadsnumber] = {
"http://example.com", "http://example.edu", "http://example.net",
"http://example.org"};
int main(int argc, char** argv) {
pthread_t tid[kThreadsnumber];
absl::Status status;
@ -96,22 +90,22 @@ int main(int argc, char **argv) {
status = sandbox.Init();
assert(status.ok());
CurlApi api(&sandbox);
// Initialize curl (CURL_GLOBAL_DEFAULT = 3)
status_or_int = api.curl_global_init(3l);
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
// Create the threads
for(int i = 0; i < kThreadsnumber; ++i) {
for (int i = 0; i < kThreadsnumber; ++i) {
thread_args args = {urls[i], &api};
int error = pthread_create(&tid[i], NULL, pull_one_url, (void*)&args);
assert(!error);
std::cout << "Thread "<< i << " gets " << urls[i] << std::endl;
std::cout << "Thread " << i << " gets " << urls[i] << std::endl;
}
// Join the threads
for(int i = 0; i< kThreadsnumber; i++) {
for (int i = 0; i < kThreadsnumber; i++) {
pthread_join(tid[i], NULL);
std::cout << "Thread " << i << " terminated" << std::endl;
}
@ -119,7 +113,6 @@ int main(int argc, char **argv) {
// Cleanup curl
status = api.curl_global_cleanup();
assert(status.ok());
return EXIT_SUCCESS;
return EXIT_SUCCESS;
}