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,
# adds the explicit versions of the variadic methods
add_library(custom_curl STATIC
custom_curl.h
custom_curl.cc
custom_curl.h
custom_curl.cc
)
set_target_properties(custom_curl
PROPERTIES LINKER_LANGUAGE C
PROPERTIES LINKER_LANGUAGE C
)
target_link_libraries(custom_curl
CURL::libcurl
CURL::libcurl
)

View File

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

View File

@ -19,12 +19,16 @@
#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

View File

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

View File

@ -15,53 +15,51 @@
#include <iostream>
#include "curl_sapi.sapi.h"
#include "sandboxed_api/util/flag.h"
class CurlApiSandboxEx1 : public CurlSandbox {
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
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();
}
};
// GET http://example.com
int main(int argc, char* argv[]) {
// GET http://example.com
CurlApiSandboxEx1 sandbox;
auto status_sandbox_init = sandbox.Init();
assert(status_sandbox_init.ok());
absl::Status status = sandbox.Init();
assert(status.ok());
CurlApi api(&sandbox);
auto status_init = api.curl_easy_init();
assert(status_init.ok());
sapi::StatusOr<CURL*> status_or_culrptr = api.curl_easy_init();
assert(status.ok());
::sapi::v::RemotePtr curl(status_init.value());
assert(curl.GetValue());
sapi::v::RemotePtr curl(status_or_culrptr.value());
assert(curl.GetValue()); // checking curl != NULL
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::StatusOr<int> status_or_int =
api.curl_easy_setopt_long(&curl, CURLOPT_VERBOSE, 1l);
assert(status_or_int.ok());
assert(status_or_int.value() == CURLE_OK);
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);
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);
status_or_int = api.curl_easy_perform(&curl);
assert(status_or_int.ok());
assert(status_or_int.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());
status = api.curl_easy_cleanup(&curl);
assert(status.ok());
}