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,7 +12,7 @@
// 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>
@ -32,25 +32,22 @@ CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option,
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) {
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);
}
@ -65,20 +62,18 @@ CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle, CURLMoption option,
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) {
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) {
unsigned int extra_nfds, int timeout_ms,
int* numfds) {
return curl_multi_wait(multi_handle, extra_fds, extra_nfds, timeout_ms,
numfds);
}

View File

@ -14,8 +14,8 @@
// Wrapper for curl library
#ifndef CUSTOM_CURL_H
#define CUSTOM_CURL_H
#ifndef CURL_WRAPPER_H
#define CURL_WRAPPER_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
// 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,
@ -63,25 +63,24 @@ extern "C" CURLMcode curl_multi_setopt_curl_off_t(CURLM* handle,
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,
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

@ -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
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;
}
@ -37,5 +36,4 @@ size_t WriteMemoryCallback(void* contents, size_t size, size_t nmemb,
mem->memory[mem->size] = 0;
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

@ -35,7 +35,6 @@ class CurlApiSandboxEx1 : public CurlSandbox {
};
int main(int argc, char* argv[]) {
absl::Status status;
sapi::StatusOr<CURL*> status_or_curl;
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.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

@ -40,7 +40,6 @@ class CurlApiSandboxEx2 : public CurlSandbox {
};
int main() {
absl::Status status;
sapi::StatusOr<CURL*> status_or_curl;
sapi::StatusOr<int> status_or_int;
@ -78,8 +77,8 @@ int main() {
// 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);
@ -107,5 +106,4 @@ int main() {
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -22,9 +22,7 @@
#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),
@ -32,7 +30,6 @@ class CurlApiSandboxEx3 : public CurlSandbox {
ca_certificates(ca_certificates) {}
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
// Return a new policy
@ -51,9 +48,7 @@ class CurlApiSandboxEx3 : public CurlSandbox {
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;
@ -84,8 +79,7 @@ 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);
@ -105,8 +99,8 @@ int main(int argc, char* argv[]) {
// 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);
@ -143,5 +137,4 @@ int main(int argc, char* argv[]) {
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -35,7 +35,6 @@ class CurlApiSandboxEx4 : public CurlSandbox {
};
int main() {
absl::Status status;
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<CURL*> status_or_curl;
@ -80,12 +79,11 @@ int main() {
assert(status_or_int.value() == CURLE_OK);
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);
@ -118,5 +116,4 @@ int main() {
assert(status.ok());
return EXIT_SUCCESS;
}

View File

@ -43,8 +43,7 @@ struct thread_args {
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;
@ -59,8 +58,8 @@ void *pull_one_url(void* args) {
// 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());
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;
@ -103,15 +97,15 @@ int main(int argc, char **argv) {
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;
}
@ -121,5 +115,4 @@ int main(int argc, char **argv) {
assert(status.ok());
return EXIT_SUCCESS;
}