Compare commits

...

3 Commits

9 changed files with 61 additions and 51 deletions

View File

@ -1,10 +1,12 @@
project(librcw)
cmake_minimum_required(VERSION 3.0.2)
include(GNUInstallDirs)
set(librcw_VERSION_STRING "000001000L")
include(GNUInstallDirs)
find_package(PkgConfig)
set(librcw_VERSION_STRING "000003000L")
set(librcw_VERSION_MAJOR 0)
set(librcw_VERSION_MINOR 1)
set(librcw_VERSION_MINOR 3)
set(librcw_VERSION_REVISION 0)
set(INCLUDE_PATH ${CMAKE_SOURCE_DIR}/include)
include_directories(BEFORE SYSTEM "${INCLUDE_PATH}")
@ -15,16 +17,21 @@ option(ENABLE_PROFILING "Enable asan" OFF)
option(BUILD_TESTS "Enable testing" OFF)
mark_as_advanced(ENABLE_PROFILING)
pkg_check_modules(LIBREXY REQUIRED librexy)
set(LIBREXY_REAL_LIBRARIES ${LIBREXY_LIBRARIES})
list(TRANSFORM LIBREXY_REAL_LIBRARIES PREPEND "-l")
set(SOURCE_LIST "src/async.cpp" "src/curl_easy_handle.cpp" "src/curl_header_list.cpp" "src/httpint.cpp" "src/read_write_cback.cpp" "src/response.cpp" "src/sync.cpp" "src/types.cpp")
if(ENABLE_SHARED)
add_library(rcw SHARED ${SOURCE_LIST})
set_target_properties(rcw PROPERTIES SOVERSION "${librcw_VERSION_MAJOR}.${librcw_VERSION_MINOR}")
set(LIBRCW_LIBFLAGS "-lrcw")
target_link_libraries(rcw "-lcurl -lrexy -lpthread")
target_link_libraries(rcw "-lcurl -lpthread ${LIBREXY_LINK_LIBRARIES}")
else()
add_library(rcw STATIC ${SOURCE_LIST})
set(LIBRCW_LIBFLAGS "-lrcw -lcurl -lrexy -lpthread")
target_link_libraries(rcw "-lcurl -lrexy -lpthread")
set(LIBRCW_LIBFLAGS "-lrcw -lcurl -lpthread ${LIBREXY_REAL_LIBRARIES}")
target_link_libraries(rcw "-lcurl -lpthread ${LIBREXY_LINK_LIBRARIES}")
endif()
set_target_properties(rcw PROPERTIES VERSION "${librcw_VERSION_MAJOR}.${librcw_VERSION_MINOR}.${librcw_VERSION_REVISION}")
@ -38,7 +45,8 @@ if(BUILD_TESTS)
endif()
set(LIBRCW_PUBLIC_HEADERS "include/async.hpp" "include/curl_easy_handle.hpp" "include/curl_header_list.hpp" "include/httpint.hpp" "include/rcw.hpp" "include/read_write_cback.hpp" "include/response.hpp" "include/string.hpp" "include/sync.hpp" "include/types.hpp")
target_compile_options(rcw PRIVATE -Wall -Wextra -pedantic -std=c++17)
target_compile_options(rcw PRIVATE -Wall -Wextra -pedantic -std=c++17 ${LIBREXY_CFLAGS_OTHER})
target_include_directories(rcw PUBLIC ${LIBREXY_INCLUDE_DIRS})
install(TARGETS rcw
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}

View File

@ -79,18 +79,18 @@ namespace rcw{
response perform(void);
string escape(const char* data, int len = 0);
string escape(const rexy::string_base<char>& data);
string escape(rexy::string_view data);
std::string escape_to_std(const char* data, int len = 0);
std::string escape_to_std(const rexy::string_base<char>& data);
std::string escape_to_std(rexy::string_view data);
char* escape_to_cstr(const char* data, int* outlen, int len = 0);
char* escape_to_cstr(const rexy::string_base<char>& data, int* outlen);
char* escape_to_cstr(rexy::string_view data, int* outlen);
std::string unescape_to_std(const char* data, int len = 0);
std::string unescape_to_std(const rexy::string_base<char>& data);
std::string unescape_to_std(rexy::string_view data);
string unescape(const char* data, int len = 0);
string unescape(const rexy::string_base<char>& data);
string unescape(rexy::string_view data);
char* unescape_to_cstr(const char* data, int* outlen, int len = 0);
char* unescape_to_cstr(const rexy::string_base<char>& data, int* outlen);
char* unescape_to_cstr(rexy::string_view data, int* outlen);
long get_last_status(void)const;
CURL* get(void);

View File

@ -22,6 +22,8 @@
#include "httpint.hpp"
#include "string.hpp"
#include <rexy/string_view.hpp>
namespace rcw{
class response
@ -31,7 +33,7 @@ namespace rcw{
size_t elapsed_time_us; //CURLINFO_TOTAL_TIME_T
size_t uploaded_bytes; //CURLINFO_SIZE_UPLOAD_T
size_t downloaded_bytes; //CURLINFO_SIZE_DOWNLOAD_T
rexy::static_string<const char> content_type; //CURLINFO_CONTENT_TYPE
rexy::string_view content_type; //CURLINFO_CONTENT_TYPE
int liberror = 0;
string text;

View File

@ -20,7 +20,7 @@
#define RCW_TYPES_HPP
#include <cstdlib> //size_t
#include <rexy/string.hpp>
#include <rexy/string_view.hpp>
namespace rcw{
@ -34,14 +34,14 @@ namespace rcw{
class url
{
public:
rexy::static_string<char> data;
rexy::string_view data;
explicit url(const char*, size_t len = 0);
};
class body
{
public:
rexy::static_string<char> data;
rexy::string_view data;
explicit body(const char*, size_t len = 0);
};

View File

@ -35,7 +35,7 @@ namespace rcw{
std::shared_ptr<curl_header_list> hlist(new curl_header_list(headers));
return std::async([=]() -> response
{
return get(rcw::url(*url_copy), *hlist);
return get(rcw::url(url_copy->data()), *hlist);
});
}
@ -55,7 +55,7 @@ namespace rcw{
std::shared_ptr<curl_header_list> hlist(new curl_header_list(headers));
return std::async([=]() -> response
{
return post(rcw::url(*url_copy), rcw::body(*body_copy), *hlist);
return post(rcw::url(url_copy->data()), rcw::body(body_copy->data()), *hlist);
});
}
@ -74,7 +74,7 @@ namespace rcw{
std::shared_ptr<curl_header_list> hlist(new curl_header_list(headers));
return std::async([=]() -> response
{
return put(rcw::url(*url_copy), rcw::body(*body_copy), *hlist);
return put(rcw::url(url_copy->data()), rcw::body(body_copy->data()), *hlist);
});
}
@ -86,7 +86,7 @@ namespace rcw{
std::shared_ptr<curl_header_list> hlist(new curl_header_list(headers));
return std::async([=]() -> response
{
return del(rcw::url(*url_copy), *hlist);
return del(rcw::url(url_copy->data()), *hlist);
});
}
std::future<response> async_del(const url& u, const body& b, std::initializer_list<header> headers){
@ -98,7 +98,7 @@ namespace rcw{
std::shared_ptr<curl_header_list> hlist(new curl_header_list(headers));
return std::async([=]() -> response
{
return del(rcw::url(*url_copy), rcw::body(*body_copy), *hlist);
return del(rcw::url(url_copy->data()), rcw::body(body_copy->data()), *hlist);
});
}

View File

@ -144,7 +144,7 @@ namespace rcw{
curl_easy_getinfo(m_curl, CURLINFO_SIZE_DOWNLOAD_T, &x);
retval.downloaded_bytes = x;
curl_easy_getinfo(m_curl, CURLINFO_CONTENT_TYPE, &y);
retval.content_type = rexy::static_string<char>(y);
retval.content_type = rexy::string_view(y);
return retval;
}
@ -179,28 +179,28 @@ namespace rcw{
char* tmp = escape_to_cstr(data, &len, len);
return string(rexy::steal(tmp), len, len);
}
string curl_easy_handle::escape(const rexy::string_base<char>& data){
return escape(data.get(), data.length());
string curl_easy_handle::escape(rexy::string_view data){
return escape(data.data(), data.length());
}
std::string curl_easy_handle::escape_to_std(const rexy::string_base<char>& data){
return escape_to_std(data.get(), data.length());
std::string curl_easy_handle::escape_to_std(rexy::string_view data){
return escape_to_std(data.data(), data.length());
}
char* curl_easy_handle::escape_to_cstr(const rexy::string_base<char>& data, int* outlen){
return escape_to_cstr(data.get(), outlen, data.length());
char* curl_easy_handle::escape_to_cstr(rexy::string_view data, int* outlen){
return escape_to_cstr(data.data(), outlen, data.length());
}
string curl_easy_handle::unescape(const char* data, int len){
char* tmp = unescape_to_cstr(data, &len, len);
return string(rexy::steal(tmp), len, len);
}
string curl_easy_handle::unescape(const rexy::string_base<char>& data){
return unescape(data.get(), data.length());
string curl_easy_handle::unescape(rexy::string_view data){
return unescape(data.data(), data.length());
}
std::string curl_easy_handle::unescape_to_std(const rexy::string_base<char>& data){
return unescape_to_std(data.get(), data.length());
std::string curl_easy_handle::unescape_to_std(rexy::string_view data){
return unescape_to_std(data.data(), data.length());
}
char* curl_easy_handle::unescape_to_cstr(const rexy::string_base<char>& data, int* outlen){
return escape_to_cstr(data.get(), outlen, data.length());
char* curl_easy_handle::unescape_to_cstr(rexy::string_view data, int* outlen){
return escape_to_cstr(data.data(), outlen, data.length());
}

View File

@ -35,7 +35,7 @@ namespace rcw{
c.set_header(headers);
c.set_read_data(nullptr);
c.set_write_data(cback);
c.set_url(u.data);
c.set_url(u.data.data());
return c.perform();
}
response post_impl(curl_easy_handle& c, curl_cback_invoker* read_cback, curl_cback_invoker* write_cback, const url& u, const curl_header_list& headers){
@ -44,7 +44,7 @@ namespace rcw{
c.set_read_fun(curl_read_callback);
c.set_read_data(read_cback);
c.set_write_data(write_cback);
c.set_url(u.data);
c.set_url(u.data.data());
c.set_header(headers);
return c.perform();
}
@ -54,7 +54,7 @@ namespace rcw{
c.set_read_fun(curl_read_callback);
c.set_read_data(read_cback);
c.set_write_data(write_cback);
c.set_url(u.data);
c.set_url(u.data.data());
c.set_header(headers);
return c.perform();
}
@ -67,7 +67,7 @@ namespace rcw{
c.set_read_fun(curl_read_callback);
c.set_write_data(write_cback);
c.set_read_data(read_cback);
c.set_url(u.data);
c.set_url(u.data.data());
c.set_header(headers);
return c.perform();
}
@ -79,7 +79,7 @@ namespace rcw{
c.set_write_fun(curl_write_callback);
c.set_write_data(write_cback);
c.set_read_data(nullptr);
c.set_url(u.data);
c.set_url(u.data.data());
c.set_header(headers);
return c.perform();
}
@ -130,7 +130,7 @@ namespace rcw{
return post(c, u, b, curl_header_list(headers));
}
response post(curl_easy_handle& c, const url& u, const body& b, const curl_header_list& headers){
default_curl_read_callback rcb{b.data.get(), b.data.length()};
default_curl_read_callback rcb{b.data.data(), b.data.length()};
default_curl_write_callback wcb{};
response retval = post(c, rcb, wcb, u, headers);
retval.text = std::move(wcb.data);
@ -164,7 +164,7 @@ namespace rcw{
return put(c, u, b, curl_header_list(headers));
}
response put(curl_easy_handle& c, const url& u, const body& b, const curl_header_list& headers){
default_curl_read_callback rcb{b.data.get(), b.data.length()};
default_curl_read_callback rcb{b.data.data(), b.data.length()};
default_curl_write_callback wcb{};
response retval = put(c, rcb, wcb, u, headers);
retval.text = std::move(wcb.data);
@ -200,7 +200,7 @@ namespace rcw{
return del(c, u, b, curl_header_list(headers));
}
response del(curl_easy_handle& c, const url& u, const body& b, const curl_header_list& headers){
default_curl_read_callback rcb{b.data.get(), b.data.length()};
default_curl_read_callback rcb{b.data.data(), b.data.length()};
default_curl_write_callback wcb{};
response retval = del(c, rcb, wcb, u, headers);
retval.text = std::move(wcb.data);

View File

@ -9,7 +9,7 @@ void do_async_get(){
rcw::response reply = f_reply.get();
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_async_post(){
PRINT_RUNNING_FUNC();
@ -17,7 +17,7 @@ void do_async_post(){
rcw::response reply = f_reply.get();
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_async_put(){
PRINT_RUNNING_FUNC();
@ -25,7 +25,7 @@ void do_async_put(){
rcw::response reply = f_reply.get();
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_async_del(){
PRINT_RUNNING_FUNC();
@ -33,7 +33,7 @@ void do_async_del(){
rcw::response reply = f_reply.get();
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
int main(){

View File

@ -8,7 +8,7 @@ void do_get(){
rcw::response reply = rcw::get(rcw::url("https://httpbin.org/get"));
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_post(){
@ -16,21 +16,21 @@ void do_post(){
rcw::response reply = rcw::post(rcw::url("https://httpbin.org/post"));
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_put(){
PRINT_RUNNING_FUNC();
rcw::response reply = rcw::put(rcw::url("https://httpbin.org/put"));
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
void do_del(){
PRINT_RUNNING_FUNC();
rcw::response reply = rcw::del(rcw::url("https://httpbin.org/delete"));
printf("%ld\n", reply.status.get());
if(reply.ok())
printf("%s\n", reply.text.get());
printf("%s\n", reply.text.data());
}
int main(){