-
-
Save caiorss/d890edcb1cf3814ffcd6915940e4f2ca to your computer and use it in GitHub Desktop.
Cpp-Httplib usage with CMake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cassert> | |
#include <functional> | |
#include <fstream> | |
#define CPPHTTPLIB_ZLIB_SUPPORT | |
#define CPPHTTPLIB_OPENSSL_SUPPORT | |
#include <httplib.h> | |
namespace h = httplib; | |
template<typename T> using shp = std::shared_ptr<T>; | |
int main(int argc, char** argv) | |
{ | |
std::cout << "\n [TRACE] Started Ok. " << "\n\n"; | |
// -------- E X P E R I M E N T / 1 --------------------------------------// | |
//------------------------------------------------------------------------// | |
std::cout << " [EXPERIMENT 1] HttpS (HTTP + SSL) Request " << '\n'; | |
std::puts(" ----------------------------------------------------"); | |
{ | |
h::SSLClient cli("www.httpbin.org"); | |
// cli.enable_server_certificate_verification(true); | |
shp<h::Response> res = cli.Get("/get"); | |
assert( res != nullptr ); | |
if(res) | |
{ | |
std::cout << " => Http Version = " << res->version << '\n'; | |
std::cout << " => Status = " << res->status << '\n'; | |
std::cout << " => Content-Type = " << res->get_header_value("Content-Type") << '\n'; | |
std::puts(" --- All HTTP headers -----------"); | |
for( auto const& [key, value] : res->headers ) | |
std::cout << std::setw(40) << std::right << key | |
<< " " | |
<< std::setw(35) << std::left << value | |
<< "\n"; | |
if(res->status == 200) | |
std::cout << "\n [INFO] HTTP Response = " << res->body << '\n'; | |
} | |
} | |
// -------- E X P E R I M E N T / 1 --------------------------------------// | |
//------------------------------------------------------------------------// | |
std::cout << " [EXPERIMENT 2] Https (HTTP + SSL) ==>> Download binary file " << '\n'; | |
std::puts(" ----------------------------------------------------"); | |
auto ifs = std::ofstream("image.jpeg"); | |
auto cli = h::Client2("https://www.httpbin.org"); | |
auto res = cli.Get("/image/jpeg" | |
,[&](const char* bytes, size_t len) -> bool | |
{ | |
ifs.write(bytes, len); | |
// Note: return false for stopping processing the request | |
return true; | |
}); | |
// Status 200 means successful HTTP response from server. | |
std::cout << " Res->status = " << res->status << '\n'; | |
std::cout << "\n [TRACE] Finish Ok. " << "\n"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.9) | |
project(httplib-client) | |
#========== Global Configurations =============# | |
#----------------------------------------------# | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_VERBOSE_MAKEFILE ON) | |
set(CMAKE_CXX_EXTENSIONS OFF) | |
# ------------ Download CPM CMake Script ----------------# | |
## Automatically donwload and use module CPM.cmake | |
file(DOWNLOAD https://raw.githubusercontent.com/TheLartians/CPM.cmake/v0.26.2/cmake/CPM.cmake | |
"${CMAKE_BINARY_DIR}/CPM.cmake") | |
include("${CMAKE_BINARY_DIR}/CPM.cmake") | |
#----------- Add dependencies --------------------------# | |
CPMAddPackage( | |
NAME httplib | |
URL "https://github.com/yhirose/cpp-httplib/archive/v0.6.6.zip" | |
) | |
include_directories( ${httplib_SOURCE_DIR} ) | |
find_package(PkgConfig REQUIRED) | |
pkg_search_module(OPENSSL REQUIRED openssl) | |
message([TRACE] " httplib source = ${httplib_SOURCE_DIR} ") | |
message([TRACE] " OPENSSL_LIBRARIES = ${OPENSSL_LIBRARIES} ") | |
#----------- Set targets -------------------------------# | |
add_executable(client client.cpp) | |
# Requires implementation of this for Windows. | |
IF(UNIX) | |
target_link_libraries(client pthread z ${OPENSSL_LIBRARIES} ) | |
ENDIF() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment