Improve style

This commit is contained in:
Federico Stazi 2020-08-07 10:14:48 +00:00
parent 5be51fabbe
commit 34338411b8
5 changed files with 60 additions and 54 deletions

View File

@ -21,12 +21,12 @@ add_subdirectory(curl)
# Wrapper library including curl, # Wrapper library including curl,
# adds the explicit versions of the variadic methods # adds the explicit versions of the variadic methods
add_library(custom_curl STATIC add_library(custom_curl STATIC
custom_curl.h custom_curl.h
custom_curl.cc custom_curl.cc
) )
set_target_properties(custom_curl set_target_properties(custom_curl
PROPERTIES LINKER_LANGUAGE C PROPERTIES LINKER_LANGUAGE C
) )
target_link_libraries(custom_curl target_link_libraries(custom_curl
CURL::libcurl CURL::libcurl
) )

View File

@ -17,18 +17,22 @@
#include "custom_curl.h" #include "custom_curl.h"
CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option, void* parameter) { CURLcode curl_easy_setopt_ptr(CURL* handle, CURLoption option,
return curl_easy_setopt(handle, option, parameter); void* parameter) {
return curl_easy_setopt(handle, option, parameter);
} }
CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option, long parameter) { CURLcode curl_easy_setopt_long(CURL* handle, CURLoption option,
return curl_easy_setopt(handle, option, parameter); long parameter) {
return curl_easy_setopt(handle, option, parameter);
} }
CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option, curl_off_t parameter) { CURLcode curl_easy_setopt_curl_off_t(CURL* handle, CURLoption option,
return curl_easy_setopt(handle, option, parameter); 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,
return curl_easy_getinfo(handle, option, parameter); void* parameter) {
return curl_easy_getinfo(handle, option, parameter);
} }

View File

@ -19,12 +19,16 @@
#include <curl/curl.h> #include <curl/curl.h>
extern "C" CURLcode curl_easy_setopt_ptr(CURL *handle, CURLoption option, void* parameter); 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_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_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); extern "C" CURLcode curl_easy_getinfo_ptr(CURL *handle, CURLINFO option,
void* parameter);
#endif // CUSTOM_CURL_H #endif // CUSTOM_CURL_H

View File

@ -14,9 +14,9 @@
# Example 1: GET request, verbose # Example 1: GET request, verbose
add_executable(example1 add_executable(example1
example1.cc example1.cc
) )
target_link_libraries(example1 PRIVATE target_link_libraries(example1 PRIVATE
curl_sapi curl_sapi
sapi::sapi sapi::sapi
) )

View File

@ -15,53 +15,51 @@
#include <iostream> #include <iostream>
#include "curl_sapi.sapi.h" #include "curl_sapi.sapi.h"
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx1 : public CurlSandbox { class CurlApiSandboxEx1 : public CurlSandbox {
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy( std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override { sandbox2::PolicyBuilder*) override {
// Return a new policy
return sandbox2::PolicyBuilder() return sandbox2::PolicyBuilder()
.DangerDefaultAllowAll() .DangerDefaultAllowAll()
.AllowUnrestrictedNetworking() .AllowUnrestrictedNetworking()
.AddDirectory("/lib") .AddDirectory("/lib")
.BuildOrDie(); .BuildOrDie();
} }
}; };
// GET http://example.com
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// GET http://example.com CurlApiSandboxEx1 sandbox;
absl::Status status = sandbox.Init();
CurlApiSandboxEx1 sandbox; assert(status.ok());
auto status_sandbox_init = sandbox.Init();
assert(status_sandbox_init.ok());
CurlApi api(&sandbox); CurlApi api(&sandbox);
auto status_init = api.curl_easy_init(); sapi::StatusOr<CURL*> status_or_culrptr = api.curl_easy_init();
assert(status_init.ok()); assert(status.ok());
::sapi::v::RemotePtr curl(status_init.value()); sapi::v::RemotePtr curl(status_or_culrptr.value());
assert(curl.GetValue()); assert(curl.GetValue()); // checking curl != NULL
auto status_setopt_verbose = api.curl_easy_setopt_long(&curl, CURLOPT_VERBOSE, 1l); sapi::StatusOr<int> status_or_int =
assert(status_setopt_verbose.ok()); api.curl_easy_setopt_long(&curl, CURLOPT_VERBOSE, 1l);
assert(status_setopt_verbose.value() == CURLE_OK); assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
sapi::v::ConstCStr url("http://example.com"); sapi::v::ConstCStr url("http://example.com");
status_or_int =
auto status_setopt_url = api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore()); api.curl_easy_setopt_ptr(&curl, CURLOPT_URL, url.PtrBefore());
assert(status_setopt_url.ok()); assert(status_or_int.ok());
assert(status_setopt_url.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
auto status_perform = api.curl_easy_perform(&curl); status_or_int = api.curl_easy_perform(&curl);
assert(status_perform.ok()); assert(status_or_int.ok());
assert(status_perform.value() == CURLE_OK); assert(status_or_int.value() == CURLE_OK);
auto status_cleanup = api.curl_easy_cleanup(&curl); status = api.curl_easy_cleanup(&curl);
assert(status_cleanup.ok()); assert(status.ok());
} }