Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki-cpp committed Jun 7, 2018
1 parent aa9e5b6 commit 1fdedaa
Show file tree
Hide file tree
Showing 16 changed files with 896 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

# Ignore stuff controlled by cmake
CMake*
*.cmake
!CMakeLists.txt
/Makefile
!/cmake/*.cmake

# Project files
.project
.cproject
.kdev4/
*.kdev4
*.kdev_include_paths
.settings/

# Build and binary files
build/
release/
bin/
*.pyc
*.so
*.o
84 changes: 84 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
cmake_minimum_required(VERSION 3.9.0)
set(CMAKE_VERBOSE_MAKEFILE OFF)

project(yukihttp VERSION 1.0.0 LANGUAGES CXX)


add_library(yukihttp SHARED src/request.cpp src/mime.cpp src/mime_part.cpp src/slist_wrapper.cpp src/utils.cpp)

target_compile_features(yukihttp PRIVATE cxx_std_17)
target_compile_options(yukihttp PRIVATE -Wall -Wextra -Wpedantic -Wunused-parameter -Wunreachable-code)

target_include_directories(yukihttp
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)



find_package(CURL 7.56 REQUIRED)
target_link_libraries(yukihttp
PRIVATE
${CURL_LIBRARIES}
)


# Installation instructions
include(GNUInstallDirs)
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/yukiHTTP)

install(TARGETS yukihttp
EXPORT yukihttp-targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)



#This is required so that the exported target has the name yukiHTTP and not yukihttp
set_target_properties(yukihttp PROPERTIES EXPORT_NAME yukiHTTP)

install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})


#Export the targets to a script
install(EXPORT yukihttp-targets
FILE
yukiHTTPTargets.cmake
NAMESPACE
yukiHTTP::
DESTINATION
${INSTALL_CONFIGDIR}
)

#Create a ConfigVersion.cmake file
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/yukiHTTPConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

configure_package_config_file(${CMAKE_CURRENT_LIST_DIR}/cmake/yukiHTTPConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/yukiHTTPConfig.cmake
INSTALL_DESTINATION ${INSTALL_CONFIGDIR}
)


#Install the config, configversion and custom find modules
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/yukiHTTPConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/yukiHTTPConfigVersion.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)


##############################################
## Exporting from the build tree
export(EXPORT yukihttp-targets FILE ${CMAKE_CURRENT_BINARY_DIR}/yukiHTTPTargets.cmake NAMESPACE yukiHTTP::)

#Register package in user's package registry
export(PACKAGE yukiHTTP)
14 changes: 14 additions & 0 deletions cmake/yukiHTTPConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

get_filename_component(yukiHTTP_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
include(CMakeFindDependencyMacro)

list(APPEND CMAKE_MODULE_PATH ${yukiHTTP_CMAKE_DIR})

find_package(CURL 7.56 REQUIRED)
list(REMOVE_AT CMAKE_MODULE_PATH -1)

if(NOT TARGET yukiHTTP::yukiHTTP)
include("${yukiHTTP_CMAKE_DIR}/yukiHTTPTargets.cmake")
endif()

set(yukiHTTP_LIBRARIES yukiHTTP::yukiHTTP)
13 changes: 13 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.9.0)
set(CMAKE_VERBOSE_MAKEFILE OFF)

project(yukihttp_example VERSION 1.0.0 LANGUAGES CXX)

add_executable(example main.cpp)
target_compile_features(example PRIVATE cxx_std_17)
target_compile_options(example PRIVATE -Wall -Wextra -Wpedantic -Wunused-parameter -Wunreachable-code)


find_package(yukiHTTP 1.0 REQUIRED)
target_link_libraries(example yukiHTTP::yukiHTTP)

16 changes: 16 additions & 0 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <yukihttp/yukihttp.hpp>

int main()
{
yuki::http::request req("https://example.com");
req.set_user_agent("yukiHTTP-Client");
req.set_header("Accept", "text/html");

auto result = req.GET();
std::cout << result.code << "\n" << result.body << std::endl;

return 0;
}


35 changes: 35 additions & 0 deletions include/yukihttp/mime.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <string>
#include <vector>
#include <memory>

namespace yuki::http
{


class part;

class mime
{
friend class request;
public:
mime();
mime(const mime&) = delete;
mime& operator=(const mime&) = delete;
mime(mime&&) = default;
mime& operator=(mime && ) = default;
~mime();

void add_data_part(const std::string & name, const std::string & data);
void add_file_part(const std::string & name, const std::string & content, const std::string & file_name);
void add_file_part(const std::string & name, const std::string & path);

private:
std::vector<std::unique_ptr<yuki::http::part>> m_parts;
};




}
67 changes: 67 additions & 0 deletions include/yukihttp/request.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <iosfwd>
#include <functional>
#include <map>
#include <string>


namespace yuki::http
{

using progress_callback = std::function<void(long, long, long, long)>;

using headers_t = std::map<std::string, std::string>;

class mime;

struct response
{
int code;
std::string body;
yuki::http::headers_t headers;
};


class request
{
public:
explicit request(const std::string & url);
~request();

request(const request&) = delete;
request& operator=(const request&) = delete;
request(request&&) = default;
request& operator=(request && ) = default;

void set_verbose(bool is_verbose) const;
void set_verbose_output(std::ostream & out) const;

void enable_progress_meter(bool is_enabled) const;
void set_progress_callback(yuki::http::progress_callback & callback) const;

void set_timeout(unsigned int ms) const;

void set_user_agent(const std::string & ua) const;

void set_header(const std::string & name, const std::string & value);

yuki::http::response GET() const;
yuki::http::response HEAD() const;
yuki::http::response DELETE() const;
yuki::http::response POST(const std::string & data) const;
yuki::http::response POST(const yuki::http::mime & mime_data) const;

private:
yuki::http::response execute_request() const;

void* m_handle = nullptr;
yuki::http::headers_t m_headers_map;
};






}
6 changes: 6 additions & 0 deletions include/yukihttp/yukihttp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
//Conveniencd include

#include "request.hpp"
#include "mime.hpp"

25 changes: 25 additions & 0 deletions src/mime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "yukihttp/mime.hpp"

#include <string>
#include <vector>
#include <memory>

#include "mime_part.hpp"

yuki::http::mime::mime() = default;
yuki::http::mime::~mime() = default;

void yuki::http::mime::add_data_part(const std::string & name, const std::string& data)
{
m_parts.emplace_back(std::make_unique<yuki::http::raw_part>(name, data));
}

void yuki::http::mime::add_file_part(const std::string& name, const std::string& content, const std::string& file_name)
{
m_parts.emplace_back(std::make_unique<yuki::http::file_data_part>(name, file_name, content));
}

void yuki::http::mime::add_file_part(const std::string& name, const std::string& path)
{
m_parts.emplace_back(std::make_unique<yuki::http::file_part>(name, path));
}
53 changes: 53 additions & 0 deletions src/mime_part.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "mime_part.hpp"

#include <string>

#include <curl/curl.h>

yuki::http::part::part(const std::string& name):
m_name(name)
{}

yuki::http::raw_part::raw_part(const std::string& name, const std::string& data):
part(name),
m_data(data)
{}


yuki::http::file_part::file_part(const std::string& name, const std::string& file_name):
part(name),
m_file_name(file_name)
{}

yuki::http::file_data_part::file_data_part(const std::string& name, const std::string& file_name, const std::string& file_content):
part(name),
m_file_name(file_name),
m_file_content(file_content)
{}



void yuki::http::part::set_up_handle(curl_mime* mime_handle) const
{
curl_mimepart* handle = curl_mime_addpart(mime_handle);
curl_mime_name(handle, m_name.c_str());
set_up_handle_impl(handle);
}


void yuki::http::file_part::set_up_handle_impl(curl_mimepart* handle) const
{
curl_mime_filedata(handle, m_file_name.c_str());
}


void yuki::http::file_data_part::set_up_handle_impl(curl_mimepart* handle) const
{
curl_mime_data(handle, m_file_content.data(), m_file_content.size());
curl_mime_filename(handle, m_file_name.c_str());
}

void yuki::http::raw_part::set_up_handle_impl(curl_mimepart* handle) const
{
curl_mime_data(handle, m_data.data(), m_data.size());
}
Loading

0 comments on commit 1fdedaa

Please sign in to comment.