Goby3  3.1.4
2024.02.22
httplib.h File Reference
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <csignal>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cctype>
#include <climits>
#include <condition_variable>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>

Go to the source code of this file.

Classes

struct  httplib::detail::ci
 
struct  httplib::detail::scope_exit
 
struct  httplib::MultipartFormData
 
class  httplib::DataSink
 
struct  httplib::MultipartFormDataProvider
 
class  httplib::ContentReader
 
struct  httplib::Request
 
struct  httplib::Response
 
class  httplib::Stream
 
class  httplib::TaskQueue
 
class  httplib::ThreadPool
 
class  httplib::detail::MatcherBase
 
class  httplib::detail::PathParamsMatcher
 
class  httplib::detail::RegexMatcher
 
class  httplib::Server
 
class  httplib::Result
 
class  httplib::ClientImpl
 
struct  httplib::ClientImpl::Socket
 
class  httplib::Client
 
class  httplib::detail::BufferStream
 
class  httplib::detail::compressor
 
class  httplib::detail::decompressor
 
class  httplib::detail::nocompressor
 
class  httplib::detail::stream_line_reader
 
class  httplib::detail::mmap
 
class  httplib::detail::SocketStream
 
class  httplib::detail::MultipartFormDataParser
 
class  httplib::detail::ContentProviderAdapter
 

Namespaces

 httplib
 
 httplib::detail
 
 httplib::detail::udl
 

Macros

#define CPPHTTPLIB_VERSION   "0.14.0"
 
#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND   5
 
#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT   5
 
#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND   300
 
#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND   0
 
#define CPPHTTPLIB_READ_TIMEOUT_SECOND   5
 
#define CPPHTTPLIB_READ_TIMEOUT_USECOND   0
 
#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND   5
 
#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND   0
 
#define CPPHTTPLIB_IDLE_INTERVAL_SECOND   0
 
#define CPPHTTPLIB_IDLE_INTERVAL_USECOND   0
 
#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH   8192
 
#define CPPHTTPLIB_HEADER_MAX_LENGTH   8192
 
#define CPPHTTPLIB_REDIRECT_MAX_COUNT   20
 
#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT   1024
 
#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH   ((std::numeric_limits<size_t>::max)())
 
#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH   8192
 
#define CPPHTTPLIB_TCP_NODELAY   false
 
#define CPPHTTPLIB_RECV_BUFSIZ   size_t(4096u)
 
#define CPPHTTPLIB_COMPRESSION_BUFSIZ   size_t(16384u)
 
#define CPPHTTPLIB_THREAD_POOL_COUNT
 
#define CPPHTTPLIB_RECV_FLAGS   0
 
#define CPPHTTPLIB_SEND_FLAGS   0
 
#define CPPHTTPLIB_LISTEN_BACKLOG   5
 
#define INVALID_SOCKET   (-1)
 
#define USE_IF2IP
 

Typedefs

using socket_t = int
 
using httplib::Headers = std::multimap< std::string, std::string, detail::ci >
 
using httplib::Params = std::multimap< std::string, std::string >
 
using httplib::Match = std::smatch
 
using httplib::Progress = std::function< bool(uint64_t current, uint64_t total)>
 
using httplib::ResponseHandler = std::function< bool(const Response &response)>
 
using httplib::MultipartFormDataItems = std::vector< MultipartFormData >
 
using httplib::MultipartFormDataMap = std::multimap< std::string, MultipartFormData >
 
using httplib::ContentProvider = std::function< bool(size_t offset, size_t length, DataSink &sink)>
 
using httplib::ContentProviderWithoutLength = std::function< bool(size_t offset, DataSink &sink)>
 
using httplib::ContentProviderResourceReleaser = std::function< void(bool success)>
 
using httplib::MultipartFormDataProviderItems = std::vector< MultipartFormDataProvider >
 
using httplib::ContentReceiverWithProgress = std::function< bool(const char *data, size_t data_length, uint64_t offset, uint64_t total_length)>
 
using httplib::ContentReceiver = std::function< bool(const char *data, size_t data_length)>
 
using httplib::MultipartContentHeader = std::function< bool(const MultipartFormData &file)>
 
using httplib::Range = std::pair< ssize_t, ssize_t >
 
using httplib::Ranges = std::vector< Range >
 
using httplib::Logger = std::function< void(const Request &, const Response &)>
 
using httplib::SocketOptions = std::function< void(socket_t sock)>
 

Enumerations

enum  httplib::Error {
  httplib::Error::Success = 0, httplib::Error::Unknown, httplib::Error::Connection, httplib::Error::BindIPAddress,
  httplib::Error::Read, httplib::Error::Write, httplib::Error::ExceedRedirectCount, httplib::Error::Canceled,
  httplib::Error::SSLConnection, httplib::Error::SSLLoadingCerts, httplib::Error::SSLServerVerification, httplib::Error::UnsupportedMultipartBoundaryChars,
  httplib::Error::Compression, httplib::Error::ConnectionTimeout, httplib::Error::ProxyConnection, httplib::Error::SSLPeerCouldBeClosed_
}
 
enum  httplib::detail::EncodingType { httplib::detail::EncodingType::None = 0, httplib::detail::EncodingType::Gzip, httplib::detail::EncodingType::Brotli }
 

Functions

template<class T , class... Args>
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type httplib::detail::make_unique (Args &&... args)
 
template<class T >
std::enable_if< std::is_array< T >::value, std::unique_ptr< T > >::type httplib::detail::make_unique (std::size_t n)
 
void httplib::default_socket_options (socket_t sock)
 
const charhttplib::status_message (int status)
 
std::string httplib::to_string (const Error error)
 
std::ostream & httplib::operator<< (std::ostream &os, const Error &obj)
 
template<typename T , typename U >
void httplib::detail::duration_to_sec_and_usec (const T &duration, U callback)
 
uint64_t httplib::detail::get_header_value_u64 (const Headers &headers, const std::string &key, size_t id, uint64_t def)
 
std::string httplib::hosted_at (const std::string &hostname)
 
void httplib::hosted_at (const std::string &hostname, std::vector< std::string > &addrs)
 
std::string httplib::append_query_params (const std::string &path, const Params &params)
 
std::pair< std::string, std::string > httplib::make_range_header (Ranges ranges)
 
std::pair< std::string, std::string > httplib::make_basic_authentication_header (const std::string &username, const std::string &password, bool is_proxy=false)
 
std::string httplib::detail::encode_query_param (const std::string &value)
 
std::string httplib::detail::decode_url (const std::string &s, bool convert_plus_to_space)
 
void httplib::detail::read_file (const std::string &path, std::string &out)
 
std::string httplib::detail::trim_copy (const std::string &s)
 
void httplib::detail::split (const char *b, const char *e, char d, std::function< void(const char *, const char *)> fn)
 
bool httplib::detail::process_client_socket (socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, std::function< bool(Stream &)> callback)
 
socket_t httplib::detail::create_client_socket (const std::string &host, const std::string &ip, int port, int address_family, bool tcp_nodelay, SocketOptions socket_options, time_t connection_timeout_sec, time_t connection_timeout_usec, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, const std::string &intf, Error &error)
 
const charhttplib::detail::get_header_value (const Headers &headers, const std::string &key, size_t id=0, const char *def=nullptr)
 
std::string httplib::detail::params_to_query_str (const Params &params)
 
void httplib::detail::parse_query_text (const std::string &s, Params &params)
 
bool httplib::detail::parse_multipart_boundary (const std::string &content_type, std::string &boundary)
 
bool httplib::detail::parse_range_header (const std::string &s, Ranges &ranges)
 
int httplib::detail::close_socket (socket_t sock)
 
ssize_t httplib::detail::send_socket (socket_t sock, const void *ptr, size_t size, int flags)
 
ssize_t httplib::detail::read_socket (socket_t sock, void *ptr, size_t size, int flags)
 
EncodingType httplib::detail::encoding_type (const Request &req, const Response &res)
 
bool httplib::detail::is_hex (char c, int &v)
 
bool httplib::detail::from_hex_to_i (const std::string &s, size_t i, size_t cnt, int &val)
 
std::string httplib::detail::from_i_to_hex (size_t n)
 
size_t httplib::detail::to_utf8 (int code, char *buff)
 
std::string httplib::detail::base64_encode (const std::string &in)
 
bool httplib::detail::is_file (const std::string &path)
 
bool httplib::detail::is_dir (const std::string &path)
 
bool httplib::detail::is_valid_path (const std::string &path)
 
std::string httplib::detail::encode_url (const std::string &s)
 
std::string httplib::detail::file_extension (const std::string &path)
 
bool httplib::detail::is_space_or_tab (char c)
 
std::pair< size_t, size_t > httplib::detail::trim (const char *b, const char *e, size_t left, size_t right)
 
std::string httplib::detail::trim_double_quotes_copy (const std::string &s)
 
template<typename T >
ssize_t httplib::detail::handle_EINTR (T fn)
 
ssize_t httplib::detail::select_read (socket_t sock, time_t sec, time_t usec)
 
ssize_t httplib::detail::select_write (socket_t sock, time_t sec, time_t usec)
 
Error httplib::detail::wait_until_socket_is_ready (socket_t sock, time_t sec, time_t usec)
 
bool httplib::detail::is_socket_alive (socket_t sock)
 
bool httplib::detail::keep_alive (socket_t sock, time_t keep_alive_timeout_sec)
 
template<typename T >
bool httplib::detail::process_server_socket_core (const std::atomic< socket_t > &svr_sock, socket_t sock, size_t keep_alive_max_count, time_t keep_alive_timeout_sec, T callback)
 
template<typename T >
bool httplib::detail::process_server_socket (const std::atomic< socket_t > &svr_sock, socket_t sock, size_t keep_alive_max_count, time_t keep_alive_timeout_sec, time_t read_timeout_sec, time_t read_timeout_usec, time_t write_timeout_sec, time_t write_timeout_usec, T callback)
 
int httplib::detail::shutdown_socket (socket_t sock)
 
template<typename BindOrConnect >
socket_t httplib::detail::create_socket (const std::string &host, const std::string &ip, int port, int address_family, int socket_flags, bool tcp_nodelay, SocketOptions socket_options, BindOrConnect bind_or_connect)
 
void httplib::detail::set_nonblocking (socket_t sock, bool nonblocking)
 
bool httplib::detail::is_connection_error ()
 
bool httplib::detail::bind_ip_address (socket_t sock, const std::string &host)
 
std::string httplib::detail::if2ip (int address_family, const std::string &ifn)
 
bool httplib::detail::get_ip_and_port (const struct sockaddr_storage &addr, socklen_t addr_len, std::string &ip, int &port)
 
void httplib::detail::get_local_ip_and_port (socket_t sock, std::string &ip, int &port)
 
void httplib::detail::get_remote_ip_and_port (socket_t sock, std::string &ip, int &port)
 
constexpr unsigned int httplib::detail::str2tag_core (const char *s, size_t l, unsigned int h)
 
unsigned int httplib::detail::str2tag (const std::string &s)
 
constexpr unsigned int httplib::detail::udl::operator""_t (const char *s, size_t l)
 
std::string httplib::detail::find_content_type (const std::string &path, const std::map< std::string, std::string > &user_data, const std::string &default_content_type)
 
bool httplib::detail::can_compress_content_type (const std::string &content_type)
 
bool httplib::detail::has_header (const Headers &headers, const std::string &key)
 
bool httplib::detail::compare_case_ignore (const std::string &a, const std::string &b)
 
template<typename T >
bool httplib::detail::parse_header (const char *beg, const char *end, T fn)
 
bool httplib::detail::read_headers (Stream &strm, Headers &headers)
 
bool httplib::detail::read_content_with_length (Stream &strm, uint64_t len, Progress progress, ContentReceiverWithProgress out)
 
void httplib::detail::skip_content_with_length (Stream &strm, uint64_t len)
 
bool httplib::detail::read_content_without_length (Stream &strm, ContentReceiverWithProgress out)
 
template<typename T >
bool httplib::detail::read_content_chunked (Stream &strm, T &x, ContentReceiverWithProgress out)
 
bool httplib::detail::is_chunked_transfer_encoding (const Headers &headers)
 
template<typename T , typename U >
bool httplib::detail::prepare_content_receiver (T &x, int &status, ContentReceiverWithProgress receiver, bool decompress, U callback)
 
template<typename T >
bool httplib::detail::read_content (Stream &strm, T &x, size_t payload_max_length, int &status, Progress progress, ContentReceiverWithProgress receiver, bool decompress)
 
ssize_t httplib::detail::write_headers (Stream &strm, const Headers &headers)
 
bool httplib::detail::write_data (Stream &strm, const char *d, size_t l)
 
template<typename T >
bool httplib::detail::write_content (Stream &strm, const ContentProvider &content_provider, size_t offset, size_t length, T is_shutting_down, Error &error)
 
template<typename T >
bool httplib::detail::write_content (Stream &strm, const ContentProvider &content_provider, size_t offset, size_t length, const T &is_shutting_down)
 
template<typename T >
bool httplib::detail::write_content_without_length (Stream &strm, const ContentProvider &content_provider, const T &is_shutting_down)
 
template<typename T , typename U >
bool httplib::detail::write_content_chunked (Stream &strm, const ContentProvider &content_provider, const T &is_shutting_down, U &compressor, Error &error)
 
template<typename T , typename U >
bool httplib::detail::write_content_chunked (Stream &strm, const ContentProvider &content_provider, const T &is_shutting_down, U &compressor)
 
template<typename T >
bool httplib::detail::redirect (T &cli, Request &req, Response &res, const std::string &path, const std::string &location, Error &error)
 
void httplib::detail::parse_disposition_params (const std::string &s, Params &params)
 
std::string httplib::detail::to_lower (const char *beg, const char *end)
 
std::string httplib::detail::make_multipart_data_boundary ()
 
bool httplib::detail::is_multipart_boundary_chars_valid (const std::string &boundary)
 
template<typename T >
std::string httplib::detail::serialize_multipart_formdata_item_begin (const T &item, const std::string &boundary)
 
std::string httplib::detail::serialize_multipart_formdata_item_end ()
 
std::string httplib::detail::serialize_multipart_formdata_finish (const std::string &boundary)
 
std::string httplib::detail::serialize_multipart_formdata_get_content_type (const std::string &boundary)
 
std::string httplib::detail::serialize_multipart_formdata (const MultipartFormDataItems &items, const std::string &boundary, bool finish=true)
 
std::pair< size_t, size_t > httplib::detail::get_range_offset_and_length (const Request &req, size_t content_length, size_t index)
 
std::string httplib::detail::make_content_range_header_field (const std::pair< ssize_t, ssize_t > &range, size_t content_length)
 
template<typename SToken , typename CToken , typename Content >
bool httplib::detail::process_multipart_ranges_data (const Request &req, Response &res, const std::string &boundary, const std::string &content_type, SToken stoken, CToken ctoken, Content content)
 
bool httplib::detail::make_multipart_ranges_data (const Request &req, Response &res, const std::string &boundary, const std::string &content_type, std::string &data)
 
size_t httplib::detail::get_multipart_ranges_data_length (const Request &req, Response &res, const std::string &boundary, const std::string &content_type)
 
template<typename T >
bool httplib::detail::write_multipart_ranges_data (Stream &strm, const Request &req, Response &res, const std::string &boundary, const std::string &content_type, const T &is_shutting_down)
 
std::pair< size_t, size_t > httplib::detail::get_range_offset_and_length (const Request &req, const Response &res, size_t index)
 
bool httplib::detail::expect_content (const Request &req)
 
bool httplib::detail::has_crlf (const std::string &s)
 
bool httplib::detail::parse_www_authenticate (const Response &res, std::map< std::string, std::string > &auth, bool is_proxy)
 
std::string httplib::detail::random_string (size_t length)
 
std::pair< std::string, std::string > httplib::make_bearer_token_authentication_header (const std::string &token, bool is_proxy=false)
 

Macro Definition Documentation

◆ CPPHTTPLIB_COMPRESSION_BUFSIZ

#define CPPHTTPLIB_COMPRESSION_BUFSIZ   size_t(16384u)

Definition at line 94 of file httplib.h.

◆ CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND

#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND   300

Definition at line 26 of file httplib.h.

◆ CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND

#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND   0

Definition at line 30 of file httplib.h.

◆ CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH

#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH   8192

Definition at line 82 of file httplib.h.

◆ CPPHTTPLIB_HEADER_MAX_LENGTH

#define CPPHTTPLIB_HEADER_MAX_LENGTH   8192

Definition at line 66 of file httplib.h.

◆ CPPHTTPLIB_IDLE_INTERVAL_SECOND

#define CPPHTTPLIB_IDLE_INTERVAL_SECOND   0

Definition at line 50 of file httplib.h.

◆ CPPHTTPLIB_IDLE_INTERVAL_USECOND

#define CPPHTTPLIB_IDLE_INTERVAL_USECOND   0

Definition at line 57 of file httplib.h.

◆ CPPHTTPLIB_KEEPALIVE_MAX_COUNT

#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT   5

Definition at line 22 of file httplib.h.

◆ CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND

#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND   5

Definition at line 18 of file httplib.h.

◆ CPPHTTPLIB_LISTEN_BACKLOG

#define CPPHTTPLIB_LISTEN_BACKLOG   5

Definition at line 113 of file httplib.h.

◆ CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT

#define CPPHTTPLIB_MULTIPART_FORM_DATA_FILE_MAX_COUNT   1024

Definition at line 74 of file httplib.h.

◆ CPPHTTPLIB_PAYLOAD_MAX_LENGTH

#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH   ((std::numeric_limits<size_t>::max)())

Definition at line 78 of file httplib.h.

◆ CPPHTTPLIB_READ_TIMEOUT_SECOND

#define CPPHTTPLIB_READ_TIMEOUT_SECOND   5

Definition at line 34 of file httplib.h.

◆ CPPHTTPLIB_READ_TIMEOUT_USECOND

#define CPPHTTPLIB_READ_TIMEOUT_USECOND   0

Definition at line 38 of file httplib.h.

◆ CPPHTTPLIB_RECV_BUFSIZ

#define CPPHTTPLIB_RECV_BUFSIZ   size_t(4096u)

Definition at line 90 of file httplib.h.

◆ CPPHTTPLIB_RECV_FLAGS

#define CPPHTTPLIB_RECV_FLAGS   0

Definition at line 105 of file httplib.h.

◆ CPPHTTPLIB_REDIRECT_MAX_COUNT

#define CPPHTTPLIB_REDIRECT_MAX_COUNT   20

Definition at line 70 of file httplib.h.

◆ CPPHTTPLIB_REQUEST_URI_MAX_LENGTH

#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH   8192

Definition at line 62 of file httplib.h.

◆ CPPHTTPLIB_SEND_FLAGS

#define CPPHTTPLIB_SEND_FLAGS   0

Definition at line 109 of file httplib.h.

◆ CPPHTTPLIB_TCP_NODELAY

#define CPPHTTPLIB_TCP_NODELAY   false

Definition at line 86 of file httplib.h.

◆ CPPHTTPLIB_THREAD_POOL_COUNT

#define CPPHTTPLIB_THREAD_POOL_COUNT
Value:
((std::max)(8u, std::thread::hardware_concurrency() > 0 \
? std::thread::hardware_concurrency() - 1 \
: 0))

Definition at line 98 of file httplib.h.

◆ CPPHTTPLIB_VERSION

#define CPPHTTPLIB_VERSION   "0.14.0"

Definition at line 11 of file httplib.h.

◆ CPPHTTPLIB_WRITE_TIMEOUT_SECOND

#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND   5

Definition at line 42 of file httplib.h.

◆ CPPHTTPLIB_WRITE_TIMEOUT_USECOND

#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND   0

Definition at line 46 of file httplib.h.

◆ INVALID_SOCKET

#define INVALID_SOCKET   (-1)

Definition at line 204 of file httplib.h.

◆ USE_IF2IP

#define USE_IF2IP

Definition at line 3271 of file httplib.h.

Typedef Documentation

◆ socket_t

using socket_t = int

Definition at line 202 of file httplib.h.