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"] [submodule "oss-internship-2020/pffft/master"]
path = oss-internship-2020/pffft/master path = oss-internship-2020/pffft/master
url = https://bitbucket.org/jpommier/pffft/src/master/ url = https://bitbucket.org/jpommier/pffft/src/master/
[submodule "oss-internship-2020/curl/custom_curl/curl"] [submodule "oss-internship-2020/curl/curl_wrapper/curl"]
path = oss-internship-2020/curl/custom_curl/curl path = oss-internship-2020/curl/curl_wrapper/curl
url = https://github.com/curl/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 ## 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`. 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 # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# Wrapper library including curl, # Wrapper library including curl, wrappers for some methods and callbacks
# adds the explicit versions of the variadic methods and the callbacks
# The CURL_SAPI_CALLBACKS variable should contain the absolute paths of # The CURL_SAPI_CALLBACKS variable should contain the absolute paths of
# all the files implementing the callbacks # all the files implementing the callbacks
add_library(custom_curl_and_callbacks OBJECT add_library(curl_wrapper_and_callbacks OBJECT
custom_curl.h curl_wrapper.h
custom_curl.cc curl_wrapper.cc
"${CURL_SAPI_CALLBACKS}" "${CURL_SAPI_CALLBACKS}"
) )
set_target_properties(custom_curl_and_callbacks set_target_properties(curl_wrapper_and_callbacks
PROPERTIES LINKER_LANGUAGE C PROPERTIES LINKER_LANGUAGE C
) )
@ -31,6 +30,6 @@ set(BUILD_SHARED_LIBS OFF)
# Link the wrapper to the original curl library # Link the wrapper to the original curl library
add_subdirectory(curl) add_subdirectory(curl)
target_link_libraries(custom_curl_and_callbacks target_link_libraries(curl_wrapper_and_callbacks
CURL::libcurl CURL::libcurl
) )

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

View File

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

View File

@ -14,8 +14,8 @@
// Wrapper for curl library // Wrapper for curl library
#ifndef CUSTOM_CURL_H #ifndef CURL_WRAPPER_H
#define CUSTOM_CURL_H #define CURL_WRAPPER_H
#include <curl/curl.h> #include <curl/curl.h>
@ -38,16 +38,16 @@ extern "C" CURLcode curl_easy_getinfo_ptr(CURL* handle, CURLINFO option,
// The typedef and wrapper method are needed because the original method has // The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h // some conflicts in curl_sapi.sapi.h
extern "C" typedef time_t time_t_sapi; 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 // The typedef and wrapper method are needed because the original method has
// some conflicts in curl_sapi.sapi.h // some conflicts in curl_sapi.sapi.h
extern "C" typedef fd_set fd_set_sapi; extern "C" typedef fd_set fd_set_sapi;
extern "C" CURLMcode curl_multi_fdset_sapi(CURLM *multi_handle, extern "C" CURLMcode curl_multi_fdset_sapi(CURLM* multi_handle,
fd_set_sapi *read_fd_set, fd_set_sapi* read_fd_set,
fd_set_sapi *write_fd_set, fd_set_sapi* write_fd_set,
fd_set_sapi *exc_fd_set, fd_set_sapi* exc_fd_set,
int *max_fd); int* max_fd);
// The wrapper method is needed to make the variadic argument explicit // 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,
@ -63,25 +63,24 @@ extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle,
curl_off_t parameter); curl_off_t parameter);
// The wrapper method is needed because incomplete array type is not supported // 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, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds,
int timeout_ms, int timeout_ms, int* numfds);
int *numfds);
// The wrapper method is needed because incomplete array type is not supported // 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, struct curl_waitfd* extra_fds,
unsigned int extra_nfds, unsigned int extra_nfds,
int timeout_ms, int timeout_ms, int* numfds);
int *numfds);
// The wrapper method is needed to make the variadic argument explicit // 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); void* parameter);
// The wrapper method is needed to make the variadic argument explicit // 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); long parameter);
#endif // CUSTOM_CURL_H #endif // CURL_WRAPPER_H

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

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

@ -21,12 +21,11 @@
// Function taken from curl's getinmemory.c // Function taken from curl's getinmemory.c
size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb, size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
void* userp) { void* userp) {
size_t realsize = size * nmemb; size_t realsize = size * nmemb;
struct MemoryStruct* mem = (struct MemoryStruct*)userp; struct MemoryStruct* mem = (struct MemoryStruct*)userp;
char* ptr = (char*)realloc(mem->memory, mem->size + realsize + 1); 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"; std::cout << "not enough memory (realloc returned NULL)\n";
return 0; return 0;
} }
@ -37,5 +36,4 @@ size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
mem->memory[mem->size] = 0; 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 // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#ifndef CALLBACKS_H #ifndef EXAMPLES_CALLBACKS_H
#define CALLBACKS_H #define EXAMPLES_CALLBACKS_H
#include <curl/curl.h> #include <curl/curl.h>
@ -22,7 +22,7 @@ extern "C" struct MemoryStruct {
size_t size; size_t size;
}; };
extern "C" size_t WriteMemoryCallback(void* contents, size_t size, extern "C" size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
size_t nmemb, void* userp); void* userp);
#endif // CALLBACKS_H #endif // EXAMPLES_CALLBACKS_H

View File

@ -22,20 +22,19 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx1 : public CurlSandbox { class CurlApiSandboxEx1 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -63,7 +62,7 @@ int main(int argc, char* argv[]) {
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_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); status_or_int = api.curl_easy_setopt_long(&curl, CURLOPT_SSL_VERIFYPEER, 0l);
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -78,5 +77,4 @@ int main(int argc, char* argv[]) {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -27,20 +27,19 @@ struct MemoryStruct {
}; };
class CurlApiSandboxEx2 : public CurlSandbox { class CurlApiSandboxEx2 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main() { int main() {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -78,8 +77,8 @@ int main() {
// Pass 'chunk' struct to the callback function // Pass 'chunk' struct to the callback function
sapi::v::Struct<MemoryStruct> chunk; sapi::v::Struct<MemoryStruct> chunk;
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA, status_or_int =
chunk.PtrBoth()); api.curl_easy_setopt_ptr(&curl, CURLOPT_WRITEDATA, chunk.PtrBoth());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -107,5 +106,4 @@ int main() {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -22,38 +22,33 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx3 : public CurlSandbox { 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 ssl_certificate;
std::string ca_certificates) std::string ssl_key;
: ssl_certificate(ssl_certificate), std::string ca_certificates;
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;
}; };
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
absl::Status status; absl::Status status;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
@ -84,8 +79,7 @@ int main(int argc, char* argv[]) {
// Specify URL to get (using HTTPS) // Specify URL to get (using HTTPS)
sapi::v::ConstCStr url("https://example.com"); sapi::v::ConstCStr url("https://example.com");
status_or_int = status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -105,8 +99,8 @@ int main(int argc, char* argv[]) {
// Set the private key for client authentication // Set the private key for client authentication
sapi::v::ConstCStr sapi_ssl_key(ssl_key.c_str()); sapi::v::ConstCStr sapi_ssl_key(ssl_key.c_str());
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY, status_or_int =
sapi_ssl_key.PtrBefore()); api.curl_easy_setopt_ptr(&curl, CURLOPT_SSLKEY, sapi_ssl_key.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -143,5 +137,4 @@ int main(int argc, char* argv[]) {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -22,20 +22,19 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx4 : public CurlSandbox { class CurlApiSandboxEx4 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
int main() { int main() {
absl::Status status; absl::Status status;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
@ -64,7 +63,7 @@ int main() {
// Specify URL to get // Specify URL to get
sapi::v::ConstCStr url("http://example.com"); sapi::v::ConstCStr url("http://example.com");
status_or_int = status_or_int =
api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore()); api.curl_easy_setopt_ptr(&http_handle, CURLOPT_URL, url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -80,12 +79,11 @@ int main() {
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
while (still_running.GetValue()) { while (still_running.GetValue()) {
sapi::v::Int numfds(0); sapi::v::Int numfds(0);
// Perform the request // Perform the request
status_or_int = api.curl_multi_perform(&multi_handle, status_or_int =
still_running.PtrBoth()); api.curl_multi_perform(&multi_handle, still_running.PtrBoth());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -118,5 +116,4 @@ int main() {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -24,16 +24,16 @@
#include "sandboxed_api/util/flag.h" #include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx5 : public CurlSandbox { class CurlApiSandboxEx5 : public CurlSandbox {
private: private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy // Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
struct thread_args { struct thread_args {
@ -43,8 +43,7 @@ struct thread_args {
constexpr int kThreadsnumber = 4; constexpr int kThreadsnumber = 4;
void *pull_one_url(void* args) { void* pull_one_url(void* args) {
absl::Status status; absl::Status status;
sapi::StatusOr<CURL*> status_or_curl; sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int; sapi::StatusOr<int> status_or_int;
@ -59,8 +58,8 @@ void *pull_one_url(void* args) {
// Specify URL to get // Specify URL to get
sapi::v::ConstCStr sapi_url(((thread_args*)args)->url); sapi::v::ConstCStr sapi_url(((thread_args*)args)->url);
status_or_int = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, status_or_int =
sapi_url.PtrBefore()); api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, sapi_url.PtrBefore());
assert(status_or_int.ok()); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
@ -74,18 +73,13 @@ void *pull_one_url(void* args) {
assert(status.ok()); assert(status.ok());
return NULL; return NULL;
} }
const char * const urls[kThreadsnumber] = { const char* const urls[kThreadsnumber] = {
"http://example.com", "http://example.com", "http://example.edu", "http://example.net",
"http://example.edu", "http://example.org"};
"http://example.net",
"http://example.org"
};
int main(int argc, char **argv) {
int main(int argc, char** argv) {
pthread_t tid[kThreadsnumber]; pthread_t tid[kThreadsnumber];
absl::Status status; absl::Status status;
@ -103,15 +97,15 @@ int main(int argc, char **argv) {
assert(status_or_int.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
// Create the threads // Create the threads
for(int i = 0; i < kThreadsnumber; ++i) { for (int i = 0; i < kThreadsnumber; ++i) {
thread_args args = {urls[i], &api}; thread_args args = {urls[i], &api};
int error = pthread_create(&tid[i], NULL, pull_one_url, (void*)&args); int error = pthread_create(&tid[i], NULL, pull_one_url, (void*)&args);
assert(!error); assert(!error);
std::cout << "Thread "<< i << " gets " << urls[i] << std::endl; std::cout << "Thread " << i << " gets " << urls[i] << std::endl;
} }
// Join the threads // Join the threads
for(int i = 0; i< kThreadsnumber; i++) { for (int i = 0; i < kThreadsnumber; i++) {
pthread_join(tid[i], NULL); pthread_join(tid[i], NULL);
std::cout << "Thread " << i << " terminated" << std::endl; std::cout << "Thread " << i << " terminated" << std::endl;
} }
@ -121,5 +115,4 @@ int main(int argc, char **argv) {
assert(status.ok()); assert(status.ok());
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }