summaryrefslogtreecommitdiff
path: root/externals
diff options
context:
space:
mode:
authorGravatar Levi2021-01-10 22:09:56 -0700
committerGravatar Levi2021-01-10 22:09:56 -0700
commit7a3c884e39fccfbb498b855080bffabc9ce2e7f1 (patch)
tree5056f9406dec188439cb0deb87603498243a9412 /externals
parentMore forgetting... duh (diff)
parentMerge pull request #5229 from Morph1984/fullscreen-opt (diff)
downloadyuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.tar.gz
yuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.tar.xz
yuzu-7a3c884e39fccfbb498b855080bffabc9ce2e7f1.zip
Merge remote-tracking branch 'upstream/master' into int-flags
Diffstat (limited to 'externals')
-rw-r--r--externals/CMakeLists.txt32
m---------externals/cubeb0
m---------externals/dynarmic0
-rw-r--r--externals/find-modules/FindFFmpeg.cmake100
-rw-r--r--externals/httplib/README.md2
-rw-r--r--externals/httplib/httplib.h4762
m---------externals/inih/inih0
m---------externals/libressl0
-rw-r--r--externals/lurlparser/CMakeLists.txt8
-rw-r--r--externals/lurlparser/LUrlParser.cpp265
-rw-r--r--externals/lurlparser/LUrlParser.h78
-rw-r--r--externals/lurlparser/README.md19
-rw-r--r--externals/microprofile/microprofile.h18
m---------externals/unicorn0
14 files changed, 3478 insertions, 1806 deletions
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index d1dcc403b..c629bbc5c 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -61,9 +61,7 @@ if (USE_DISCORD_PRESENCE)
61endif() 61endif()
62 62
63# Sirit 63# Sirit
64if (ENABLE_VULKAN) 64add_subdirectory(sirit)
65 add_subdirectory(sirit)
66endif()
67 65
68# libzip 66# libzip
69find_package(Libzip 1.5) 67find_package(Libzip 1.5)
@@ -73,23 +71,29 @@ if (NOT LIBZIP_FOUND)
73endif() 71endif()
74 72
75if (ENABLE_WEB_SERVICE) 73if (ENABLE_WEB_SERVICE)
76 # LibreSSL 74 find_package(OpenSSL 1.1)
77 set(LIBRESSL_SKIP_INSTALL ON CACHE BOOL "") 75 if (OPENSSL_FOUND)
78 add_subdirectory(libressl EXCLUDE_FROM_ALL) 76 set(OPENSSL_LIBRARIES OpenSSL::SSL OpenSSL::Crypto)
79 target_include_directories(ssl INTERFACE ./libressl/include) 77 else()
80 target_compile_definitions(ssl PRIVATE -DHAVE_INET_NTOP) 78 # LibreSSL
81 get_directory_property(OPENSSL_LIBRARIES 79 set(LIBRESSL_SKIP_INSTALL ON CACHE BOOL "")
82 DIRECTORY libressl 80 set(OPENSSLDIR "/etc/ssl/")
83 DEFINITION OPENSSL_LIBS) 81 add_subdirectory(libressl EXCLUDE_FROM_ALL)
84 82 target_include_directories(ssl INTERFACE ./libressl/include)
85 # lurlparser 83 target_compile_definitions(ssl PRIVATE -DHAVE_INET_NTOP)
86 add_subdirectory(lurlparser EXCLUDE_FROM_ALL) 84 get_directory_property(OPENSSL_LIBRARIES
85 DIRECTORY libressl
86 DEFINITION OPENSSL_LIBS)
87 endif()
87 88
88 # httplib 89 # httplib
89 add_library(httplib INTERFACE) 90 add_library(httplib INTERFACE)
90 target_include_directories(httplib INTERFACE ./httplib) 91 target_include_directories(httplib INTERFACE ./httplib)
91 target_compile_definitions(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT) 92 target_compile_definitions(httplib INTERFACE -DCPPHTTPLIB_OPENSSL_SUPPORT)
92 target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES}) 93 target_link_libraries(httplib INTERFACE ${OPENSSL_LIBRARIES})
94 if (WIN32)
95 target_link_libraries(httplib INTERFACE crypt32 cryptui ws2_32)
96 endif()
93endif() 97endif()
94 98
95# Opus 99# Opus
diff --git a/externals/cubeb b/externals/cubeb
Subproject 616d773441b5355800ce64197a699e6cd6b3617 Subproject 1d66483ad2b93f0e00e175f9480c771af90003a
diff --git a/externals/dynarmic b/externals/dynarmic
Subproject 0e1112b7df77ae55a62a51622940d5c8f9e8c84 Subproject 3806284cbefc4115436dcdc687776a45ec31309
diff --git a/externals/find-modules/FindFFmpeg.cmake b/externals/find-modules/FindFFmpeg.cmake
new file mode 100644
index 000000000..77b331e00
--- /dev/null
+++ b/externals/find-modules/FindFFmpeg.cmake
@@ -0,0 +1,100 @@
1# - Try to find ffmpeg libraries (libavcodec, libavformat and libavutil)
2# Once done this will define
3#
4# FFMPEG_FOUND - system has ffmpeg or libav
5# FFMPEG_INCLUDE_DIR - the ffmpeg include directory
6# FFMPEG_LIBRARIES - Link these to use ffmpeg
7# FFMPEG_LIBAVCODEC
8# FFMPEG_LIBAVFORMAT
9# FFMPEG_LIBAVUTIL
10#
11# Copyright (c) 2008 Andreas Schneider <mail@cynapses.org>
12# Modified for other libraries by Lasse Kärkkäinen <tronic>
13# Modified for Hedgewars by Stepik777
14# Modified for FFmpeg-example Tuukka Pasanen 2018
15# Modified for yuzu toastUnlimted 2020
16#
17# Redistribution and use is allowed according to the terms of the New
18# BSD license.
19#
20
21include(FindPackageHandleStandardArgs)
22
23find_package_handle_standard_args(FFMPEG
24 FOUND_VAR FFMPEG_FOUND
25 REQUIRED_VARS
26 FFMPEG_LIBRARY
27 FFMPEG_INCLUDE_DIR
28 VERSION_VAR FFMPEG_VERSION
29)
30
31if(FFMPEG_LIBRARIES AND FFMPEG_INCLUDE_DIR)
32 # in cache already
33 set(FFMPEG_FOUND TRUE)
34else()
35 # use pkg-config to get the directories and then use these values
36 # in the FIND_PATH() and FIND_LIBRARY() calls
37 find_package(PkgConfig)
38 if(PKG_CONFIG_FOUND)
39 pkg_check_modules(_FFMPEG_AVCODEC libavcodec)
40 pkg_check_modules(_FFMPEG_AVUTIL libavutil)
41 pkg_check_modules(_FFMPEG_SWSCALE libswscale)
42 endif()
43
44 find_path(FFMPEG_AVCODEC_INCLUDE_DIR
45 NAMES libavcodec/avcodec.h
46 PATHS ${_FFMPEG_AVCODEC_INCLUDE_DIRS}
47 /usr/include
48 /usr/local/include
49 /opt/local/include
50 /sw/include
51 PATH_SUFFIXES ffmpeg libav)
52
53 find_library(FFMPEG_LIBAVCODEC
54 NAMES avcodec
55 PATHS ${_FFMPEG_AVCODEC_LIBRARY_DIRS}
56 /usr/lib
57 /usr/local/lib
58 /opt/local/lib
59 /sw/lib)
60
61 find_library(FFMPEG_LIBAVUTIL
62 NAMES avutil
63 PATHS ${_FFMPEG_AVUTIL_LIBRARY_DIRS}
64 /usr/lib
65 /usr/local/lib
66 /opt/local/lib
67 /sw/lib)
68
69 find_library(FFMPEG_LIBSWSCALE
70 NAMES swscale
71 PATHS ${_FFMPEG_SWSCALE_LIBRARY_DIRS}
72 /usr/lib
73 /usr/local/lib
74 /opt/local/lib
75 /sw/lib)
76
77 if(FFMPEG_LIBAVCODEC AND FFMPEG_LIBAVUTIL AND FFMPEG_LIBSWSCALE)
78 set(FFMPEG_FOUND TRUE)
79 endif()
80
81 if(FFMPEG_FOUND)
82 set(FFMPEG_INCLUDE_DIR ${FFMPEG_AVCODEC_INCLUDE_DIR})
83 set(FFMPEG_LIBRARIES
84 ${FFMPEG_LIBAVCODEC}
85 ${FFMPEG_LIBAVUTIL}
86 ${FFMPEG_LIBSWSCALE})
87 endif()
88
89 if(FFMPEG_FOUND)
90 if(NOT FFMPEG_FIND_QUIETLY)
91 message(STATUS
92 "Found FFMPEG or Libav: ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIR}")
93 endif()
94 else()
95 if(FFMPEG_FIND_REQUIRED)
96 message(FATAL_ERROR
97 "Could not find libavcodec or libavutil or libswscale")
98 endif()
99 endif()
100endif()
diff --git a/externals/httplib/README.md b/externals/httplib/README.md
index 73037d297..1940e446c 100644
--- a/externals/httplib/README.md
+++ b/externals/httplib/README.md
@@ -1,4 +1,4 @@
1From https://github.com/yhirose/cpp-httplib/tree/fce8e6fefdab4ad48bc5b25c98e5ebfda4f3cf53 1From https://github.com/yhirose/cpp-httplib/tree/ff5677ad197947177c158fe857caff4f0e242045 with https://github.com/yhirose/cpp-httplib/pull/701
2 2
3MIT License 3MIT License
4 4
diff --git a/externals/httplib/httplib.h b/externals/httplib/httplib.h
index e03842e6d..8982054e2 100644
--- a/externals/httplib/httplib.h
+++ b/externals/httplib/httplib.h
@@ -16,14 +16,18 @@
16#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5 16#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND 5
17#endif 17#endif
18 18
19#ifndef CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND
20#define CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND 0
21#endif
22
23#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT 19#ifndef CPPHTTPLIB_KEEPALIVE_MAX_COUNT
24#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5 20#define CPPHTTPLIB_KEEPALIVE_MAX_COUNT 5
25#endif 21#endif
26 22
23#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND
24#define CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND 300
25#endif
26
27#ifndef CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND
28#define CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND 0
29#endif
30
27#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND 31#ifndef CPPHTTPLIB_READ_TIMEOUT_SECOND
28#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5 32#define CPPHTTPLIB_READ_TIMEOUT_SECOND 5
29#endif 33#endif
@@ -32,6 +36,26 @@
32#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0 36#define CPPHTTPLIB_READ_TIMEOUT_USECOND 0
33#endif 37#endif
34 38
39#ifndef CPPHTTPLIB_WRITE_TIMEOUT_SECOND
40#define CPPHTTPLIB_WRITE_TIMEOUT_SECOND 5
41#endif
42
43#ifndef CPPHTTPLIB_WRITE_TIMEOUT_USECOND
44#define CPPHTTPLIB_WRITE_TIMEOUT_USECOND 0
45#endif
46
47#ifndef CPPHTTPLIB_IDLE_INTERVAL_SECOND
48#define CPPHTTPLIB_IDLE_INTERVAL_SECOND 0
49#endif
50
51#ifndef CPPHTTPLIB_IDLE_INTERVAL_USECOND
52#ifdef _WIN32
53#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 10000
54#else
55#define CPPHTTPLIB_IDLE_INTERVAL_USECOND 0
56#endif
57#endif
58
35#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 59#ifndef CPPHTTPLIB_REQUEST_URI_MAX_LENGTH
36#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192 60#define CPPHTTPLIB_REQUEST_URI_MAX_LENGTH 8192
37#endif 61#endif
@@ -41,16 +65,26 @@
41#endif 65#endif
42 66
43#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH 67#ifndef CPPHTTPLIB_PAYLOAD_MAX_LENGTH
44#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH (std::numeric_limits<size_t>::max()) 68#define CPPHTTPLIB_PAYLOAD_MAX_LENGTH ((std::numeric_limits<size_t>::max)())
69#endif
70
71#ifndef CPPHTTPLIB_TCP_NODELAY
72#define CPPHTTPLIB_TCP_NODELAY false
45#endif 73#endif
46 74
47#ifndef CPPHTTPLIB_RECV_BUFSIZ 75#ifndef CPPHTTPLIB_RECV_BUFSIZ
48#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u) 76#define CPPHTTPLIB_RECV_BUFSIZ size_t(4096u)
49#endif 77#endif
50 78
79#ifndef CPPHTTPLIB_COMPRESSION_BUFSIZ
80#define CPPHTTPLIB_COMPRESSION_BUFSIZ size_t(16384u)
81#endif
82
51#ifndef CPPHTTPLIB_THREAD_POOL_COUNT 83#ifndef CPPHTTPLIB_THREAD_POOL_COUNT
52#define CPPHTTPLIB_THREAD_POOL_COUNT \ 84#define CPPHTTPLIB_THREAD_POOL_COUNT \
53 (std::max(1u, std::thread::hardware_concurrency() - 1)) 85 ((std::max)(8u, std::thread::hardware_concurrency() > 0 \
86 ? std::thread::hardware_concurrency() - 1 \
87 : 0))
54#endif 88#endif
55 89
56/* 90/*
@@ -92,6 +126,8 @@ using ssize_t = int;
92 126
93#include <io.h> 127#include <io.h>
94#include <winsock2.h> 128#include <winsock2.h>
129
130#include <wincrypt.h>
95#include <ws2tcpip.h> 131#include <ws2tcpip.h>
96 132
97#ifndef WSA_FLAG_NO_HANDLE_INHERIT 133#ifndef WSA_FLAG_NO_HANDLE_INHERIT
@@ -100,6 +136,8 @@ using ssize_t = int;
100 136
101#ifdef _MSC_VER 137#ifdef _MSC_VER
102#pragma comment(lib, "ws2_32.lib") 138#pragma comment(lib, "ws2_32.lib")
139#pragma comment(lib, "crypt32.lib")
140#pragma comment(lib, "cryptui.lib")
103#endif 141#endif
104 142
105#ifndef strcasecmp 143#ifndef strcasecmp
@@ -118,6 +156,10 @@ using socket_t = SOCKET;
118#include <ifaddrs.h> 156#include <ifaddrs.h>
119#include <netdb.h> 157#include <netdb.h>
120#include <netinet/in.h> 158#include <netinet/in.h>
159#ifdef __linux__
160#include <resolv.h>
161#endif
162#include <netinet/tcp.h>
121#ifdef CPPHTTPLIB_USE_POLL 163#ifdef CPPHTTPLIB_USE_POLL
122#include <poll.h> 164#include <poll.h>
123#endif 165#endif
@@ -131,20 +173,25 @@ using socket_t = int;
131#define INVALID_SOCKET (-1) 173#define INVALID_SOCKET (-1)
132#endif //_WIN32 174#endif //_WIN32
133 175
176#include <algorithm>
134#include <array> 177#include <array>
135#include <atomic> 178#include <atomic>
136#include <cassert> 179#include <cassert>
180#include <cctype>
181#include <climits>
137#include <condition_variable> 182#include <condition_variable>
138#include <errno.h> 183#include <errno.h>
139#include <fcntl.h> 184#include <fcntl.h>
140#include <fstream> 185#include <fstream>
141#include <functional> 186#include <functional>
187#include <iostream>
142#include <list> 188#include <list>
143#include <map> 189#include <map>
144#include <memory> 190#include <memory>
145#include <mutex> 191#include <mutex>
146#include <random> 192#include <random>
147#include <regex> 193#include <regex>
194#include <sstream>
148#include <string> 195#include <string>
149#include <sys/stat.h> 196#include <sys/stat.h>
150#include <thread> 197#include <thread>
@@ -155,12 +202,17 @@ using socket_t = int;
155#include <openssl/ssl.h> 202#include <openssl/ssl.h>
156#include <openssl/x509v3.h> 203#include <openssl/x509v3.h>
157 204
205#if defined(_WIN32) && defined(OPENSSL_USE_APPLINK)
206#include <openssl/applink.c>
207#endif
208
158#include <iomanip> 209#include <iomanip>
210#include <iostream>
159#include <sstream> 211#include <sstream>
160 212
161// #if OPENSSL_VERSION_NUMBER < 0x1010100fL 213#if OPENSSL_VERSION_NUMBER < 0x1010100fL
162// #error Sorry, OpenSSL versions prior to 1.1.1 are not supported 214#error Sorry, OpenSSL versions prior to 1.1.1 are not supported
163// #endif 215#endif
164 216
165#if OPENSSL_VERSION_NUMBER < 0x10100000L 217#if OPENSSL_VERSION_NUMBER < 0x10100000L
166#include <openssl/crypto.h> 218#include <openssl/crypto.h>
@@ -174,6 +226,11 @@ inline const unsigned char *ASN1_STRING_get0_data(const ASN1_STRING *asn1) {
174#include <zlib.h> 226#include <zlib.h>
175#endif 227#endif
176 228
229#ifdef CPPHTTPLIB_BROTLI_SUPPORT
230#include <brotli/decode.h>
231#include <brotli/encode.h>
232#endif
233
177/* 234/*
178 * Declaration 235 * Declaration
179 */ 236 */
@@ -181,6 +238,27 @@ namespace httplib {
181 238
182namespace detail { 239namespace detail {
183 240
241/*
242 * Backport std::make_unique from C++14.
243 *
244 * NOTE: This code came up with the following stackoverflow post:
245 * https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique
246 *
247 */
248
249template <class T, class... Args>
250typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
251make_unique(Args &&... args) {
252 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
253}
254
255template <class T>
256typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type
257make_unique(std::size_t n) {
258 typedef typename std::remove_extent<T>::type RT;
259 return std::unique_ptr<T>(new RT[n]);
260}
261
184struct ci { 262struct ci {
185 bool operator()(const std::string &s1, const std::string &s2) const { 263 bool operator()(const std::string &s1, const std::string &s2) const {
186 return std::lexicographical_compare( 264 return std::lexicographical_compare(
@@ -212,7 +290,8 @@ using MultipartFormDataMap = std::multimap<std::string, MultipartFormData>;
212 290
213class DataSink { 291class DataSink {
214public: 292public:
215 DataSink() = default; 293 DataSink() : os(&sb_), sb_(*this) {}
294
216 DataSink(const DataSink &) = delete; 295 DataSink(const DataSink &) = delete;
217 DataSink &operator=(const DataSink &) = delete; 296 DataSink &operator=(const DataSink &) = delete;
218 DataSink(DataSink &&) = delete; 297 DataSink(DataSink &&) = delete;
@@ -221,10 +300,35 @@ public:
221 std::function<void(const char *data, size_t data_len)> write; 300 std::function<void(const char *data, size_t data_len)> write;
222 std::function<void()> done; 301 std::function<void()> done;
223 std::function<bool()> is_writable; 302 std::function<bool()> is_writable;
303 std::ostream os;
304
305private:
306 class data_sink_streambuf : public std::streambuf {
307 public:
308 explicit data_sink_streambuf(DataSink &sink) : sink_(sink) {}
309
310 protected:
311 std::streamsize xsputn(const char *s, std::streamsize n) {
312 sink_.write(s, static_cast<size_t>(n));
313 return n;
314 }
315
316 private:
317 DataSink &sink_;
318 };
319
320 data_sink_streambuf sb_;
224}; 321};
225 322
226using ContentProvider = 323using ContentProvider =
227 std::function<void(size_t offset, size_t length, DataSink &sink)>; 324 std::function<bool(size_t offset, size_t length, DataSink &sink)>;
325
326using ContentProviderWithoutLength =
327 std::function<bool(size_t offset, DataSink &sink)>;
328
329using ContentReceiverWithProgress =
330 std::function<bool(const char *data, size_t data_length, uint64_t offset,
331 uint64_t total_length)>;
228 332
229using ContentReceiver = 333using ContentReceiver =
230 std::function<bool(const char *data, size_t data_length)>; 334 std::function<bool(const char *data, size_t data_length)>;
@@ -238,18 +342,21 @@ public:
238 using MultipartReader = std::function<bool(MultipartContentHeader header, 342 using MultipartReader = std::function<bool(MultipartContentHeader header,
239 ContentReceiver receiver)>; 343 ContentReceiver receiver)>;
240 344
241 ContentReader(Reader reader, MultipartReader muitlpart_reader) 345 ContentReader(Reader reader, MultipartReader multipart_reader)
242 : reader_(reader), muitlpart_reader_(muitlpart_reader) {} 346 : reader_(std::move(reader)),
347 multipart_reader_(std::move(multipart_reader)) {}
243 348
244 bool operator()(MultipartContentHeader header, 349 bool operator()(MultipartContentHeader header,
245 ContentReceiver receiver) const { 350 ContentReceiver receiver) const {
246 return muitlpart_reader_(header, receiver); 351 return multipart_reader_(std::move(header), std::move(receiver));
247 } 352 }
248 353
249 bool operator()(ContentReceiver receiver) const { return reader_(receiver); } 354 bool operator()(ContentReceiver receiver) const {
355 return reader_(std::move(receiver));
356 }
250 357
251 Reader reader_; 358 Reader reader_;
252 MultipartReader muitlpart_reader_; 359 MultipartReader multipart_reader_;
253}; 360};
254 361
255using Range = std::pair<ssize_t, ssize_t>; 362using Range = std::pair<ssize_t, ssize_t>;
@@ -261,6 +368,9 @@ struct Request {
261 Headers headers; 368 Headers headers;
262 std::string body; 369 std::string body;
263 370
371 std::string remote_addr;
372 int remote_port = -1;
373
264 // for server 374 // for server
265 std::string version; 375 std::string version;
266 std::string target; 376 std::string target;
@@ -272,7 +382,9 @@ struct Request {
272 // for client 382 // for client
273 size_t redirect_count = CPPHTTPLIB_REDIRECT_MAX_COUNT; 383 size_t redirect_count = CPPHTTPLIB_REDIRECT_MAX_COUNT;
274 ResponseHandler response_handler; 384 ResponseHandler response_handler;
275 ContentReceiver content_receiver; 385 ContentReceiverWithProgress content_receiver;
386 size_t content_length = 0;
387 ContentProvider content_provider;
276 Progress progress; 388 Progress progress;
277 389
278#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 390#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -281,6 +393,8 @@ struct Request {
281 393
282 bool has_header(const char *key) const; 394 bool has_header(const char *key) const;
283 std::string get_header_value(const char *key, size_t id = 0) const; 395 std::string get_header_value(const char *key, size_t id = 0) const;
396 template <typename T>
397 T get_header_value(const char *key, size_t id = 0) const;
284 size_t get_header_value_count(const char *key) const; 398 size_t get_header_value_count(const char *key) const;
285 void set_header(const char *key, const char *val); 399 void set_header(const char *key, const char *val);
286 void set_header(const char *key, const std::string &val); 400 void set_header(const char *key, const std::string &val);
@@ -295,35 +409,40 @@ struct Request {
295 MultipartFormData get_file_value(const char *key) const; 409 MultipartFormData get_file_value(const char *key) const;
296 410
297 // private members... 411 // private members...
298 size_t content_length; 412 size_t authorization_count_ = 0;
299 ContentProvider content_provider;
300}; 413};
301 414
302struct Response { 415struct Response {
303 std::string version; 416 std::string version;
304 int status = -1; 417 int status = -1;
418 std::string reason;
305 Headers headers; 419 Headers headers;
306 std::string body; 420 std::string body;
307 421
308 bool has_header(const char *key) const; 422 bool has_header(const char *key) const;
309 std::string get_header_value(const char *key, size_t id = 0) const; 423 std::string get_header_value(const char *key, size_t id = 0) const;
424 template <typename T>
425 T get_header_value(const char *key, size_t id = 0) const;
310 size_t get_header_value_count(const char *key) const; 426 size_t get_header_value_count(const char *key) const;
311 void set_header(const char *key, const char *val); 427 void set_header(const char *key, const char *val);
312 void set_header(const char *key, const std::string &val); 428 void set_header(const char *key, const std::string &val);
313 429
314 void set_redirect(const char *url); 430 void set_redirect(const char *url, int status = 302);
431 void set_redirect(const std::string &url, int status = 302);
315 void set_content(const char *s, size_t n, const char *content_type); 432 void set_content(const char *s, size_t n, const char *content_type);
316 void set_content(const std::string &s, const char *content_type); 433 void set_content(std::string s, const char *content_type);
317 434
318 void set_content_provider( 435 void set_content_provider(
319 size_t length, 436 size_t length, const char *content_type, ContentProvider provider,
320 std::function<void(size_t offset, size_t length, DataSink &sink)> 437 const std::function<void()> &resource_releaser = nullptr);
321 provider, 438
322 std::function<void()> resource_releaser = [] {}); 439 void set_content_provider(
440 const char *content_type, ContentProviderWithoutLength provider,
441 const std::function<void()> &resource_releaser = nullptr);
323 442
324 void set_chunked_content_provider( 443 void set_chunked_content_provider(
325 std::function<void(size_t offset, DataSink &sink)> provider, 444 const char *content_type, ContentProviderWithoutLength provider,
326 std::function<void()> resource_releaser = [] {}); 445 const std::function<void()> &resource_releaser = nullptr);
327 446
328 Response() = default; 447 Response() = default;
329 Response(const Response &) = default; 448 Response(const Response &) = default;
@@ -331,15 +450,16 @@ struct Response {
331 Response(Response &&) = default; 450 Response(Response &&) = default;
332 Response &operator=(Response &&) = default; 451 Response &operator=(Response &&) = default;
333 ~Response() { 452 ~Response() {
334 if (content_provider_resource_releaser) { 453 if (content_provider_resource_releaser_) {
335 content_provider_resource_releaser(); 454 content_provider_resource_releaser_();
336 } 455 }
337 } 456 }
338 457
339 // private members... 458 // private members...
340 size_t content_length = 0; 459 size_t content_length_ = 0;
341 ContentProvider content_provider; 460 ContentProvider content_provider_;
342 std::function<void()> content_provider_resource_releaser; 461 std::function<void()> content_provider_resource_releaser_;
462 bool is_chunked_content_provider = false;
343}; 463};
344 464
345class Stream { 465class Stream {
@@ -349,22 +469,25 @@ public:
349 virtual bool is_readable() const = 0; 469 virtual bool is_readable() const = 0;
350 virtual bool is_writable() const = 0; 470 virtual bool is_writable() const = 0;
351 471
352 virtual int read(char *ptr, size_t size) = 0; 472 virtual ssize_t read(char *ptr, size_t size) = 0;
353 virtual int write(const char *ptr, size_t size) = 0; 473 virtual ssize_t write(const char *ptr, size_t size) = 0;
354 virtual std::string get_remote_addr() const = 0; 474 virtual void get_remote_ip_and_port(std::string &ip, int &port) const = 0;
355 475
356 template <typename... Args> 476 template <typename... Args>
357 int write_format(const char *fmt, const Args &... args); 477 ssize_t write_format(const char *fmt, const Args &... args);
358 int write(const char *ptr); 478 ssize_t write(const char *ptr);
359 int write(const std::string &s); 479 ssize_t write(const std::string &s);
360}; 480};
361 481
362class TaskQueue { 482class TaskQueue {
363public: 483public:
364 TaskQueue() = default; 484 TaskQueue() = default;
365 virtual ~TaskQueue() = default; 485 virtual ~TaskQueue() = default;
486
366 virtual void enqueue(std::function<void()> fn) = 0; 487 virtual void enqueue(std::function<void()> fn) = 0;
367 virtual void shutdown() = 0; 488 virtual void shutdown() = 0;
489
490 virtual void on_idle(){};
368}; 491};
369 492
370class ThreadPool : public TaskQueue { 493class ThreadPool : public TaskQueue {
@@ -381,7 +504,7 @@ public:
381 504
382 void enqueue(std::function<void()> fn) override { 505 void enqueue(std::function<void()> fn) override {
383 std::unique_lock<std::mutex> lock(mutex_); 506 std::unique_lock<std::mutex> lock(mutex_);
384 jobs_.push_back(fn); 507 jobs_.push_back(std::move(fn));
385 cond_.notify_one(); 508 cond_.notify_one();
386 } 509 }
387 510
@@ -439,6 +562,26 @@ private:
439 562
440using Logger = std::function<void(const Request &, const Response &)>; 563using Logger = std::function<void(const Request &, const Response &)>;
441 564
565using SocketOptions = std::function<void(socket_t sock)>;
566
567inline void default_socket_options(socket_t sock) {
568 int yes = 1;
569#ifdef _WIN32
570 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&yes),
571 sizeof(yes));
572 setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
573 reinterpret_cast<char *>(&yes), sizeof(yes));
574#else
575#ifdef SO_REUSEPORT
576 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<void *>(&yes),
577 sizeof(yes));
578#else
579 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<void *>(&yes),
580 sizeof(yes));
581#endif
582#endif
583}
584
442class Server { 585class Server {
443public: 586public:
444 using Handler = std::function<void(const Request &, Response &)>; 587 using Handler = std::function<void(const Request &, Response &)>;
@@ -461,23 +604,30 @@ public:
461 Server &Patch(const char *pattern, Handler handler); 604 Server &Patch(const char *pattern, Handler handler);
462 Server &Patch(const char *pattern, HandlerWithContentReader handler); 605 Server &Patch(const char *pattern, HandlerWithContentReader handler);
463 Server &Delete(const char *pattern, Handler handler); 606 Server &Delete(const char *pattern, Handler handler);
607 Server &Delete(const char *pattern, HandlerWithContentReader handler);
464 Server &Options(const char *pattern, Handler handler); 608 Server &Options(const char *pattern, Handler handler);
465 609
466 [[deprecated]] bool set_base_dir(const char *dir, 610 bool set_base_dir(const char *dir, const char *mount_point = nullptr);
467 const char *mount_point = nullptr); 611 bool set_mount_point(const char *mount_point, const char *dir,
468 bool set_mount_point(const char *mount_point, const char *dir); 612 Headers headers = Headers());
469 bool remove_mount_point(const char *mount_point); 613 bool remove_mount_point(const char *mount_point);
470 void set_file_extension_and_mimetype_mapping(const char *ext, 614 void set_file_extension_and_mimetype_mapping(const char *ext,
471 const char *mime); 615 const char *mime);
472 void set_file_request_handler(Handler handler); 616 void set_file_request_handler(Handler handler);
473 617
474 void set_error_handler(Handler handler); 618 void set_error_handler(Handler handler);
619 void set_expect_100_continue_handler(Expect100ContinueHandler handler);
475 void set_logger(Logger logger); 620 void set_logger(Logger logger);
476 621
477 void set_expect_100_continue_handler(Expect100ContinueHandler handler); 622 void set_tcp_nodelay(bool on);
623 void set_socket_options(SocketOptions socket_options);
478 624
479 void set_keep_alive_max_count(size_t count); 625 void set_keep_alive_max_count(size_t count);
480 void set_read_timeout(time_t sec, time_t usec); 626 void set_keep_alive_timeout(time_t sec);
627 void set_read_timeout(time_t sec, time_t usec = 0);
628 void set_write_timeout(time_t sec, time_t usec = 0);
629 void set_idle_interval(time_t sec, time_t usec = 0);
630
481 void set_payload_max_length(size_t length); 631 void set_payload_max_length(size_t length);
482 632
483 bool bind_to_port(const char *host, int port, int socket_flags = 0); 633 bool bind_to_port(const char *host, int port, int socket_flags = 0);
@@ -492,54 +642,66 @@ public:
492 std::function<TaskQueue *(void)> new_task_queue; 642 std::function<TaskQueue *(void)> new_task_queue;
493 643
494protected: 644protected:
495 bool process_request(Stream &strm, bool last_connection, 645 bool process_request(Stream &strm, bool close_connection,
496 bool &connection_close, 646 bool &connection_closed,
497 const std::function<void(Request &)> &setup_request); 647 const std::function<void(Request &)> &setup_request);
498 648
499 size_t keep_alive_max_count_; 649 std::atomic<socket_t> svr_sock_;
500 time_t read_timeout_sec_; 650 size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT;
501 time_t read_timeout_usec_; 651 time_t keep_alive_timeout_sec_ = CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND;
502 size_t payload_max_length_; 652 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
653 time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
654 time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND;
655 time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND;
656 time_t idle_interval_sec_ = CPPHTTPLIB_IDLE_INTERVAL_SECOND;
657 time_t idle_interval_usec_ = CPPHTTPLIB_IDLE_INTERVAL_USECOND;
658 size_t payload_max_length_ = CPPHTTPLIB_PAYLOAD_MAX_LENGTH;
503 659
504private: 660private:
505 using Handlers = std::vector<std::pair<std::regex, Handler>>; 661 using Handlers = std::vector<std::pair<std::regex, Handler>>;
506 using HandlersForContentReader = 662 using HandlersForContentReader =
507 std::vector<std::pair<std::regex, HandlerWithContentReader>>; 663 std::vector<std::pair<std::regex, HandlerWithContentReader>>;
508 664
509 socket_t create_server_socket(const char *host, int port, 665 socket_t create_server_socket(const char *host, int port, int socket_flags,
510 int socket_flags) const; 666 SocketOptions socket_options) const;
511 int bind_internal(const char *host, int port, int socket_flags); 667 int bind_internal(const char *host, int port, int socket_flags);
512 bool listen_internal(); 668 bool listen_internal();
513 669
514 bool routing(Request &req, Response &res, Stream &strm, bool last_connection); 670 bool routing(Request &req, Response &res, Stream &strm);
515 bool handle_file_request(Request &req, Response &res, bool head = false); 671 bool handle_file_request(Request &req, Response &res, bool head = false);
516 bool dispatch_request(Request &req, Response &res, Handlers &handlers); 672 bool dispatch_request(Request &req, Response &res, const Handlers &handlers);
517 bool dispatch_request_for_content_reader(Request &req, Response &res, 673 bool
518 ContentReader content_reader, 674 dispatch_request_for_content_reader(Request &req, Response &res,
519 HandlersForContentReader &handlers); 675 ContentReader content_reader,
676 const HandlersForContentReader &handlers);
520 677
521 bool parse_request_line(const char *s, Request &req); 678 bool parse_request_line(const char *s, Request &req);
522 bool write_response(Stream &strm, bool last_connection, const Request &req, 679 bool write_response(Stream &strm, bool close_connection, const Request &req,
523 Response &res); 680 Response &res);
524 bool write_content_with_provider(Stream &strm, const Request &req, 681 bool write_content_with_provider(Stream &strm, const Request &req,
525 Response &res, const std::string &boundary, 682 Response &res, const std::string &boundary,
526 const std::string &content_type); 683 const std::string &content_type);
527 bool read_content(Stream &strm, bool last_connection, Request &req, 684 bool read_content(Stream &strm, Request &req, Response &res);
528 Response &res); 685 bool
529 bool read_content_with_content_receiver( 686 read_content_with_content_receiver(Stream &strm, Request &req, Response &res,
530 Stream &strm, bool last_connection, Request &req, Response &res, 687 ContentReceiver receiver,
531 ContentReceiver receiver, MultipartContentHeader multipart_header, 688 MultipartContentHeader multipart_header,
532 ContentReceiver multipart_receiver); 689 ContentReceiver multipart_receiver);
533 bool read_content_core(Stream &strm, bool last_connection, Request &req, 690 bool read_content_core(Stream &strm, Request &req, Response &res,
534 Response &res, ContentReceiver receiver, 691 ContentReceiver receiver,
535 MultipartContentHeader mulitpart_header, 692 MultipartContentHeader mulitpart_header,
536 ContentReceiver multipart_receiver); 693 ContentReceiver multipart_receiver);
537 694
538 virtual bool process_and_close_socket(socket_t sock); 695 virtual bool process_and_close_socket(socket_t sock);
539 696
697 struct MountPointEntry {
698 std::string mount_point;
699 std::string base_dir;
700 Headers headers;
701 };
702 std::vector<MountPointEntry> base_dirs_;
703
540 std::atomic<bool> is_running_; 704 std::atomic<bool> is_running_;
541 std::atomic<socket_t> svr_sock_;
542 std::vector<std::pair<std::string, std::string>> base_dirs_;
543 std::map<std::string, std::string> file_extension_and_mimetype_map_; 705 std::map<std::string, std::string> file_extension_and_mimetype_map_;
544 Handler file_request_handler_; 706 Handler file_request_handler_;
545 Handlers get_handlers_; 707 Handlers get_handlers_;
@@ -550,293 +712,454 @@ private:
550 Handlers patch_handlers_; 712 Handlers patch_handlers_;
551 HandlersForContentReader patch_handlers_for_content_reader_; 713 HandlersForContentReader patch_handlers_for_content_reader_;
552 Handlers delete_handlers_; 714 Handlers delete_handlers_;
715 HandlersForContentReader delete_handlers_for_content_reader_;
553 Handlers options_handlers_; 716 Handlers options_handlers_;
554 Handler error_handler_; 717 Handler error_handler_;
555 Logger logger_; 718 Logger logger_;
556 Expect100ContinueHandler expect_100_continue_handler_; 719 Expect100ContinueHandler expect_100_continue_handler_;
557};
558
559class Client {
560public:
561 explicit Client(const std::string &host, int port = 80,
562 const std::string &client_cert_path = std::string(),
563 const std::string &client_key_path = std::string());
564 720
565 virtual ~Client(); 721 bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
566 722 SocketOptions socket_options_ = default_socket_options;
567 virtual bool is_valid() const; 723};
568
569 std::shared_ptr<Response> Get(const char *path);
570
571 std::shared_ptr<Response> Get(const char *path, const Headers &headers);
572
573 std::shared_ptr<Response> Get(const char *path, Progress progress);
574
575 std::shared_ptr<Response> Get(const char *path, const Headers &headers,
576 Progress progress);
577
578 std::shared_ptr<Response> Get(const char *path,
579 ContentReceiver content_receiver);
580
581 std::shared_ptr<Response> Get(const char *path, const Headers &headers,
582 ContentReceiver content_receiver);
583
584 std::shared_ptr<Response>
585 Get(const char *path, ContentReceiver content_receiver, Progress progress);
586
587 std::shared_ptr<Response> Get(const char *path, const Headers &headers,
588 ContentReceiver content_receiver,
589 Progress progress);
590
591 std::shared_ptr<Response> Get(const char *path, const Headers &headers,
592 ResponseHandler response_handler,
593 ContentReceiver content_receiver);
594
595 std::shared_ptr<Response> Get(const char *path, const Headers &headers,
596 ResponseHandler response_handler,
597 ContentReceiver content_receiver,
598 Progress progress);
599
600 std::shared_ptr<Response> Head(const char *path);
601
602 std::shared_ptr<Response> Head(const char *path, const Headers &headers);
603
604 std::shared_ptr<Response> Post(const char *path, const std::string &body,
605 const char *content_type);
606
607 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
608 const std::string &body,
609 const char *content_type);
610
611 std::shared_ptr<Response> Post(const char *path, size_t content_length,
612 ContentProvider content_provider,
613 const char *content_type);
614
615 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
616 size_t content_length,
617 ContentProvider content_provider,
618 const char *content_type);
619
620 std::shared_ptr<Response> Post(const char *path, const Params &params);
621
622 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
623 const Params &params);
624
625 std::shared_ptr<Response> Post(const char *path,
626 const MultipartFormDataItems &items);
627
628 std::shared_ptr<Response> Post(const char *path, const Headers &headers,
629 const MultipartFormDataItems &items);
630
631 std::shared_ptr<Response> Put(const char *path, const std::string &body,
632 const char *content_type);
633
634 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
635 const std::string &body,
636 const char *content_type);
637
638 std::shared_ptr<Response> Put(const char *path, size_t content_length,
639 ContentProvider content_provider,
640 const char *content_type);
641
642 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
643 size_t content_length,
644 ContentProvider content_provider,
645 const char *content_type);
646
647 std::shared_ptr<Response> Put(const char *path, const Params &params);
648
649 std::shared_ptr<Response> Put(const char *path, const Headers &headers,
650 const Params &params);
651
652 std::shared_ptr<Response> Patch(const char *path, const std::string &body,
653 const char *content_type);
654 724
655 std::shared_ptr<Response> Patch(const char *path, const Headers &headers, 725enum Error {
656 const std::string &body, 726 Success = 0,
657 const char *content_type); 727 Unknown,
728 Connection,
729 BindIPAddress,
730 Read,
731 Write,
732 ExceedRedirectCount,
733 Canceled,
734 SSLConnection,
735 SSLLoadingCerts,
736 SSLServerVerification,
737 UnsupportedMultipartBoundaryChars
738};
658 739
659 std::shared_ptr<Response> Patch(const char *path, size_t content_length, 740class Result {
660 ContentProvider content_provider, 741public:
661 const char *content_type); 742 Result(std::unique_ptr<Response> res, Error err)
743 : res_(std::move(res)), err_(err) {}
744 operator bool() const { return res_ != nullptr; }
745 bool operator==(std::nullptr_t) const { return res_ == nullptr; }
746 bool operator!=(std::nullptr_t) const { return res_ != nullptr; }
747 const Response &value() const { return *res_; }
748 Response &value() { return *res_; }
749 const Response &operator*() const { return *res_; }
750 Response &operator*() { return *res_; }
751 const Response *operator->() const { return res_.get(); }
752 Response *operator->() { return res_.get(); }
753 Error error() const { return err_; }
662 754
663 std::shared_ptr<Response> Patch(const char *path, const Headers &headers, 755private:
664 size_t content_length, 756 std::unique_ptr<Response> res_;
665 ContentProvider content_provider, 757 Error err_;
666 const char *content_type); 758};
667 759
668 std::shared_ptr<Response> Delete(const char *path); 760class ClientImpl {
761public:
762 explicit ClientImpl(const std::string &host);
669 763
670 std::shared_ptr<Response> Delete(const char *path, const std::string &body, 764 explicit ClientImpl(const std::string &host, int port);
671 const char *content_type);
672 765
673 std::shared_ptr<Response> Delete(const char *path, const Headers &headers); 766 explicit ClientImpl(const std::string &host, int port,
767 const std::string &client_cert_path,
768 const std::string &client_key_path);
674 769
675 std::shared_ptr<Response> Delete(const char *path, const Headers &headers, 770 virtual ~ClientImpl();
676 const std::string &body,
677 const char *content_type);
678 771
679 std::shared_ptr<Response> Options(const char *path); 772 virtual bool is_valid() const;
680 773
681 std::shared_ptr<Response> Options(const char *path, const Headers &headers); 774 Result Get(const char *path);
775 Result Get(const char *path, const Headers &headers);
776 Result Get(const char *path, Progress progress);
777 Result Get(const char *path, const Headers &headers, Progress progress);
778 Result Get(const char *path, ContentReceiver content_receiver);
779 Result Get(const char *path, const Headers &headers,
780 ContentReceiver content_receiver);
781 Result Get(const char *path, ContentReceiver content_receiver,
782 Progress progress);
783 Result Get(const char *path, const Headers &headers,
784 ContentReceiver content_receiver, Progress progress);
785 Result Get(const char *path, ResponseHandler response_handler,
786 ContentReceiver content_receiver);
787 Result Get(const char *path, const Headers &headers,
788 ResponseHandler response_handler,
789 ContentReceiver content_receiver);
790 Result Get(const char *path, ResponseHandler response_handler,
791 ContentReceiver content_receiver, Progress progress);
792 Result Get(const char *path, const Headers &headers,
793 ResponseHandler response_handler, ContentReceiver content_receiver,
794 Progress progress);
795
796 Result Head(const char *path);
797 Result Head(const char *path, const Headers &headers);
798
799 Result Post(const char *path);
800 Result Post(const char *path, const std::string &body,
801 const char *content_type);
802 Result Post(const char *path, const Headers &headers, const std::string &body,
803 const char *content_type);
804 Result Post(const char *path, size_t content_length,
805 ContentProvider content_provider, const char *content_type);
806 Result Post(const char *path, const Headers &headers, size_t content_length,
807 ContentProvider content_provider, const char *content_type);
808 Result Post(const char *path, const Params &params);
809 Result Post(const char *path, const Headers &headers, const Params &params);
810 Result Post(const char *path, const MultipartFormDataItems &items);
811 Result Post(const char *path, const Headers &headers,
812 const MultipartFormDataItems &items);
813 Result Post(const char *path, const Headers &headers,
814 const MultipartFormDataItems &items, const std::string &boundary);
815
816 Result Put(const char *path);
817 Result Put(const char *path, const std::string &body,
818 const char *content_type);
819 Result Put(const char *path, const Headers &headers, const std::string &body,
820 const char *content_type);
821 Result Put(const char *path, size_t content_length,
822 ContentProvider content_provider, const char *content_type);
823 Result Put(const char *path, const Headers &headers, size_t content_length,
824 ContentProvider content_provider, const char *content_type);
825 Result Put(const char *path, const Params &params);
826 Result Put(const char *path, const Headers &headers, const Params &params);
827
828 Result Patch(const char *path, const std::string &body,
829 const char *content_type);
830 Result Patch(const char *path, const Headers &headers,
831 const std::string &body, const char *content_type);
832 Result Patch(const char *path, size_t content_length,
833 ContentProvider content_provider, const char *content_type);
834 Result Patch(const char *path, const Headers &headers, size_t content_length,
835 ContentProvider content_provider, const char *content_type);
836
837 Result Delete(const char *path);
838 Result Delete(const char *path, const std::string &body,
839 const char *content_type);
840 Result Delete(const char *path, const Headers &headers);
841 Result Delete(const char *path, const Headers &headers,
842 const std::string &body, const char *content_type);
843
844 Result Options(const char *path);
845 Result Options(const char *path, const Headers &headers);
682 846
683 bool send(const Request &req, Response &res); 847 bool send(const Request &req, Response &res);
684 848
685 bool send(const std::vector<Request> &requests, 849 size_t is_socket_open() const;
686 std::vector<Response> &responses);
687 850
688 void set_timeout_sec(time_t timeout_sec); 851 void stop();
689 852
690 void set_read_timeout(time_t sec, time_t usec); 853 void set_default_headers(Headers headers);
691 854
692 void set_keep_alive_max_count(size_t count); 855 void set_tcp_nodelay(bool on);
856 void set_socket_options(SocketOptions socket_options);
693 857
694 void set_basic_auth(const char *username, const char *password); 858 void set_connection_timeout(time_t sec, time_t usec = 0);
859 void set_read_timeout(time_t sec, time_t usec = 0);
860 void set_write_timeout(time_t sec, time_t usec = 0);
695 861
862 void set_basic_auth(const char *username, const char *password);
863 void set_bearer_token_auth(const char *token);
696#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 864#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
697 void set_digest_auth(const char *username, const char *password); 865 void set_digest_auth(const char *username, const char *password);
698#endif 866#endif
699 867
868 void set_keep_alive(bool on);
700 void set_follow_location(bool on); 869 void set_follow_location(bool on);
701 870
702 void set_compress(bool on); 871 void set_compress(bool on);
703 872
873 void set_decompress(bool on);
874
704 void set_interface(const char *intf); 875 void set_interface(const char *intf);
705 876
706 void set_proxy(const char *host, int port); 877 void set_proxy(const char *host, int port);
707
708 void set_proxy_basic_auth(const char *username, const char *password); 878 void set_proxy_basic_auth(const char *username, const char *password);
709 879 void set_proxy_bearer_token_auth(const char *token);
710#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 880#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
711 void set_proxy_digest_auth(const char *username, const char *password); 881 void set_proxy_digest_auth(const char *username, const char *password);
712#endif 882#endif
713 883
884#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
885 void enable_server_certificate_verification(bool enabled);
886#endif
887
714 void set_logger(Logger logger); 888 void set_logger(Logger logger);
715 889
716protected: 890protected:
891 struct Socket {
892 socket_t sock = INVALID_SOCKET;
893#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
894 SSL *ssl = nullptr;
895#endif
896
897 bool is_open() const { return sock != INVALID_SOCKET; }
898 };
899
900 virtual bool create_and_connect_socket(Socket &socket);
901
902 // All of:
903 // shutdown_ssl
904 // shutdown_socket
905 // close_socket
906 // should ONLY be called when socket_mutex_ is locked.
907 // Also, shutdown_ssl and close_socket should also NOT be called concurrently
908 // with a DIFFERENT thread sending requests using that socket.
909 virtual void shutdown_ssl(Socket &socket, bool shutdown_gracefully);
910 void shutdown_socket(Socket &socket);
911 void close_socket(Socket &socket);
912
913 // Similar to shutdown_ssl and close_socket, this should NOT be called
914 // concurrently with a DIFFERENT thread sending requests from the socket
915 void lock_socket_and_shutdown_and_close();
916
717 bool process_request(Stream &strm, const Request &req, Response &res, 917 bool process_request(Stream &strm, const Request &req, Response &res,
718 bool last_connection, bool &connection_close); 918 bool close_connection);
919
920 Error get_last_error() const;
921
922 void copy_settings(const ClientImpl &rhs);
719 923
924 // Error state
925 mutable std::atomic<Error> error_;
926
927 // Socket endoint information
720 const std::string host_; 928 const std::string host_;
721 const int port_; 929 const int port_;
722 const std::string host_and_port_; 930 const std::string host_and_port_;
723 931
932 // Current open socket
933 Socket socket_;
934 mutable std::mutex socket_mutex_;
935 std::recursive_mutex request_mutex_;
936
937 // These are all protected under socket_mutex
938 int socket_requests_in_flight_ = 0;
939 std::thread::id socket_requests_are_from_thread_ = std::thread::id();
940 bool socket_should_be_closed_when_request_is_done_ = false;
941
942 // Default headers
943 Headers default_headers_;
944
724 // Settings 945 // Settings
725 std::string client_cert_path_; 946 std::string client_cert_path_;
726 std::string client_key_path_; 947 std::string client_key_path_;
727 948
728 time_t timeout_sec_ = 300; 949 time_t connection_timeout_sec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_SECOND;
950 time_t connection_timeout_usec_ = CPPHTTPLIB_CONNECTION_TIMEOUT_USECOND;
729 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND; 951 time_t read_timeout_sec_ = CPPHTTPLIB_READ_TIMEOUT_SECOND;
730 time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND; 952 time_t read_timeout_usec_ = CPPHTTPLIB_READ_TIMEOUT_USECOND;
731 953 time_t write_timeout_sec_ = CPPHTTPLIB_WRITE_TIMEOUT_SECOND;
732 size_t keep_alive_max_count_ = CPPHTTPLIB_KEEPALIVE_MAX_COUNT; 954 time_t write_timeout_usec_ = CPPHTTPLIB_WRITE_TIMEOUT_USECOND;
733 955
734 std::string basic_auth_username_; 956 std::string basic_auth_username_;
735 std::string basic_auth_password_; 957 std::string basic_auth_password_;
958 std::string bearer_token_auth_token_;
736#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 959#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
737 std::string digest_auth_username_; 960 std::string digest_auth_username_;
738 std::string digest_auth_password_; 961 std::string digest_auth_password_;
739#endif 962#endif
740 963
964 bool keep_alive_ = false;
741 bool follow_location_ = false; 965 bool follow_location_ = false;
742 966
967 bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
968 SocketOptions socket_options_ = nullptr;
969
743 bool compress_ = false; 970 bool compress_ = false;
971 bool decompress_ = true;
744 972
745 std::string interface_; 973 std::string interface_;
746 974
747 std::string proxy_host_; 975 std::string proxy_host_;
748 int proxy_port_; 976 int proxy_port_ = -1;
749 977
750 std::string proxy_basic_auth_username_; 978 std::string proxy_basic_auth_username_;
751 std::string proxy_basic_auth_password_; 979 std::string proxy_basic_auth_password_;
980 std::string proxy_bearer_token_auth_token_;
752#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 981#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
753 std::string proxy_digest_auth_username_; 982 std::string proxy_digest_auth_username_;
754 std::string proxy_digest_auth_password_; 983 std::string proxy_digest_auth_password_;
755#endif 984#endif
756 985
757 Logger logger_;
758
759 void copy_settings(const Client &rhs) {
760 client_cert_path_ = rhs.client_cert_path_;
761 client_key_path_ = rhs.client_key_path_;
762 timeout_sec_ = rhs.timeout_sec_;
763 read_timeout_sec_ = rhs.read_timeout_sec_;
764 read_timeout_usec_ = rhs.read_timeout_usec_;
765 keep_alive_max_count_ = rhs.keep_alive_max_count_;
766 basic_auth_username_ = rhs.basic_auth_username_;
767 basic_auth_password_ = rhs.basic_auth_password_;
768#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
769 digest_auth_username_ = rhs.digest_auth_username_;
770 digest_auth_password_ = rhs.digest_auth_password_;
771#endif
772 follow_location_ = rhs.follow_location_;
773 compress_ = rhs.compress_;
774 interface_ = rhs.interface_;
775 proxy_host_ = rhs.proxy_host_;
776 proxy_port_ = rhs.proxy_port_;
777 proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
778 proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
779#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 986#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
780 proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_; 987 bool server_certificate_verification_ = true;
781 proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
782#endif 988#endif
783 logger_ = rhs.logger_; 989
784 } 990 Logger logger_;
785 991
786private: 992private:
787 socket_t create_client_socket() const; 993 socket_t create_client_socket() const;
788 bool read_response_line(Stream &strm, Response &res); 994 bool read_response_line(Stream &strm, Response &res);
789 bool write_request(Stream &strm, const Request &req, bool last_connection); 995 bool write_request(Stream &strm, const Request &req, bool close_connection);
790 bool redirect(const Request &req, Response &res); 996 bool redirect(const Request &req, Response &res);
791 bool handle_request(Stream &strm, const Request &req, Response &res, 997 bool handle_request(Stream &strm, const Request &req, Response &res,
792 bool last_connection, bool &connection_close); 998 bool close_connection);
793#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 999 std::unique_ptr<Response> send_with_content_provider(
794 bool connect(socket_t sock, Response &res, bool &error);
795#endif
796
797 std::shared_ptr<Response> send_with_content_provider(
798 const char *method, const char *path, const Headers &headers, 1000 const char *method, const char *path, const Headers &headers,
799 const std::string &body, size_t content_length, 1001 const std::string &body, size_t content_length,
800 ContentProvider content_provider, const char *content_type); 1002 ContentProvider content_provider, const char *content_type);
801 1003
802 virtual bool process_and_close_socket( 1004 // socket is const because this function is called when socket_mutex_ is not locked
803 socket_t sock, size_t request_count, 1005 virtual bool process_socket(const Socket &socket,
804 std::function<bool(Stream &strm, bool last_connection, 1006 std::function<bool(Stream &strm)> callback);
805 bool &connection_close)>
806 callback);
807
808 virtual bool is_ssl() const; 1007 virtual bool is_ssl() const;
809}; 1008};
810 1009
811inline void Get(std::vector<Request> &requests, const char *path, 1010class Client {
812 const Headers &headers) { 1011public:
813 Request req; 1012 // Universal interface
814 req.method = "GET"; 1013 explicit Client(const char *scheme_host_port);
815 req.path = path; 1014
816 req.headers = headers; 1015 explicit Client(const char *scheme_host_port,
817 requests.emplace_back(std::move(req)); 1016 const std::string &client_cert_path,
818} 1017 const std::string &client_key_path);
1018
1019 // HTTP only interface
1020 explicit Client(const std::string &host, int port);
1021
1022 explicit Client(const std::string &host, int port,
1023 const std::string &client_cert_path,
1024 const std::string &client_key_path);
1025
1026 ~Client();
1027
1028 bool is_valid() const;
1029
1030 Result Get(const char *path);
1031 Result Get(const char *path, const Headers &headers);
1032 Result Get(const char *path, Progress progress);
1033 Result Get(const char *path, const Headers &headers, Progress progress);
1034 Result Get(const char *path, ContentReceiver content_receiver);
1035 Result Get(const char *path, const Headers &headers,
1036 ContentReceiver content_receiver);
1037 Result Get(const char *path, ContentReceiver content_receiver,
1038 Progress progress);
1039 Result Get(const char *path, const Headers &headers,
1040 ContentReceiver content_receiver, Progress progress);
1041 Result Get(const char *path, ResponseHandler response_handler,
1042 ContentReceiver content_receiver);
1043 Result Get(const char *path, const Headers &headers,
1044 ResponseHandler response_handler,
1045 ContentReceiver content_receiver);
1046 Result Get(const char *path, const Headers &headers,
1047 ResponseHandler response_handler, ContentReceiver content_receiver,
1048 Progress progress);
1049 Result Get(const char *path, ResponseHandler response_handler,
1050 ContentReceiver content_receiver, Progress progress);
1051
1052 Result Head(const char *path);
1053 Result Head(const char *path, const Headers &headers);
1054
1055 Result Post(const char *path);
1056 Result Post(const char *path, const std::string &body,
1057 const char *content_type);
1058 Result Post(const char *path, const Headers &headers, const std::string &body,
1059 const char *content_type);
1060 Result Post(const char *path, size_t content_length,
1061 ContentProvider content_provider, const char *content_type);
1062 Result Post(const char *path, const Headers &headers, size_t content_length,
1063 ContentProvider content_provider, const char *content_type);
1064 Result Post(const char *path, const Params &params);
1065 Result Post(const char *path, const Headers &headers, const Params &params);
1066 Result Post(const char *path, const MultipartFormDataItems &items);
1067 Result Post(const char *path, const Headers &headers,
1068 const MultipartFormDataItems &items);
1069 Result Post(const char *path, const Headers &headers,
1070 const MultipartFormDataItems &items, const std::string &boundary);
1071 Result Put(const char *path);
1072 Result Put(const char *path, const std::string &body,
1073 const char *content_type);
1074 Result Put(const char *path, const Headers &headers, const std::string &body,
1075 const char *content_type);
1076 Result Put(const char *path, size_t content_length,
1077 ContentProvider content_provider, const char *content_type);
1078 Result Put(const char *path, const Headers &headers, size_t content_length,
1079 ContentProvider content_provider, const char *content_type);
1080 Result Put(const char *path, const Params &params);
1081 Result Put(const char *path, const Headers &headers, const Params &params);
1082 Result Patch(const char *path, const std::string &body,
1083 const char *content_type);
1084 Result Patch(const char *path, const Headers &headers,
1085 const std::string &body, const char *content_type);
1086 Result Patch(const char *path, size_t content_length,
1087 ContentProvider content_provider, const char *content_type);
1088 Result Patch(const char *path, const Headers &headers, size_t content_length,
1089 ContentProvider content_provider, const char *content_type);
1090
1091 Result Delete(const char *path);
1092 Result Delete(const char *path, const std::string &body,
1093 const char *content_type);
1094 Result Delete(const char *path, const Headers &headers);
1095 Result Delete(const char *path, const Headers &headers,
1096 const std::string &body, const char *content_type);
1097
1098 Result Options(const char *path);
1099 Result Options(const char *path, const Headers &headers);
819 1100
820inline void Get(std::vector<Request> &requests, const char *path) { 1101 bool send(const Request &req, Response &res);
821 Get(requests, path, Headers());
822}
823 1102
824inline void Post(std::vector<Request> &requests, const char *path, 1103 size_t is_socket_open() const;
825 const Headers &headers, const std::string &body,
826 const char *content_type) {
827 Request req;
828 req.method = "POST";
829 req.path = path;
830 req.headers = headers;
831 req.headers.emplace("Content-Type", content_type);
832 req.body = body;
833 requests.emplace_back(std::move(req));
834}
835 1104
836inline void Post(std::vector<Request> &requests, const char *path, 1105 void stop();
837 const std::string &body, const char *content_type) { 1106
838 Post(requests, path, Headers(), body, content_type); 1107 void set_default_headers(Headers headers);
839} 1108
1109 void set_tcp_nodelay(bool on);
1110 void set_socket_options(SocketOptions socket_options);
1111
1112 void set_connection_timeout(time_t sec, time_t usec = 0);
1113 void set_read_timeout(time_t sec, time_t usec = 0);
1114 void set_write_timeout(time_t sec, time_t usec = 0);
1115
1116 void set_basic_auth(const char *username, const char *password);
1117 void set_bearer_token_auth(const char *token);
1118#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1119 void set_digest_auth(const char *username, const char *password);
1120#endif
1121
1122 void set_keep_alive(bool on);
1123 void set_follow_location(bool on);
1124
1125 void set_compress(bool on);
1126
1127 void set_decompress(bool on);
1128
1129 void set_interface(const char *intf);
1130
1131 void set_proxy(const char *host, int port);
1132 void set_proxy_basic_auth(const char *username, const char *password);
1133 void set_proxy_bearer_token_auth(const char *token);
1134#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1135 void set_proxy_digest_auth(const char *username, const char *password);
1136#endif
1137
1138#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1139 void enable_server_certificate_verification(bool enabled);
1140#endif
1141
1142 void set_logger(Logger logger);
1143
1144 // SSL
1145#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1146 void set_ca_cert_path(const char *ca_cert_file_path,
1147 const char *ca_cert_dir_path = nullptr);
1148
1149 void set_ca_cert_store(X509_STORE *ca_cert_store);
1150
1151 long get_openssl_verify_result() const;
1152
1153 SSL_CTX *ssl_context() const;
1154#endif
1155
1156private:
1157 std::unique_ptr<ClientImpl> cli_;
1158
1159#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1160 bool is_ssl_ = false;
1161#endif
1162}; // namespace httplib
840 1163
841#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 1164#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
842class SSLServer : public Server { 1165class SSLServer : public Server {
@@ -845,43 +1168,58 @@ public:
845 const char *client_ca_cert_file_path = nullptr, 1168 const char *client_ca_cert_file_path = nullptr,
846 const char *client_ca_cert_dir_path = nullptr); 1169 const char *client_ca_cert_dir_path = nullptr);
847 1170
848 virtual ~SSLServer(); 1171 SSLServer(X509 *cert, EVP_PKEY *private_key,
1172 X509_STORE *client_ca_cert_store = nullptr);
849 1173
850 virtual bool is_valid() const; 1174 ~SSLServer() override;
1175
1176 bool is_valid() const override;
851 1177
852private: 1178private:
853 virtual bool process_and_close_socket(socket_t sock); 1179 bool process_and_close_socket(socket_t sock) override;
854 1180
855 SSL_CTX *ctx_; 1181 SSL_CTX *ctx_;
856 std::mutex ctx_mutex_; 1182 std::mutex ctx_mutex_;
857}; 1183};
858 1184
859class SSLClient : public Client { 1185class SSLClient : public ClientImpl {
860public: 1186public:
861 SSLClient(const std::string &host, int port = 443, 1187 explicit SSLClient(const std::string &host);
862 const std::string &client_cert_path = std::string(),
863 const std::string &client_key_path = std::string());
864 1188
865 virtual ~SSLClient(); 1189 explicit SSLClient(const std::string &host, int port);
866 1190
867 virtual bool is_valid() const; 1191 explicit SSLClient(const std::string &host, int port,
1192 const std::string &client_cert_path,
1193 const std::string &client_key_path);
1194
1195 explicit SSLClient(const std::string &host, int port, X509 *client_cert,
1196 EVP_PKEY *client_key);
1197
1198 ~SSLClient() override;
868 1199
869 void set_ca_cert_path(const char *ca_ceert_file_path, 1200 bool is_valid() const override;
1201
1202 void set_ca_cert_path(const char *ca_cert_file_path,
870 const char *ca_cert_dir_path = nullptr); 1203 const char *ca_cert_dir_path = nullptr);
871 1204
872 void enable_server_certificate_verification(bool enabled); 1205 void set_ca_cert_store(X509_STORE *ca_cert_store);
873 1206
874 long get_openssl_verify_result() const; 1207 long get_openssl_verify_result() const;
875 1208
876 SSL_CTX *ssl_context() const noexcept; 1209 SSL_CTX *ssl_context() const;
877 1210
878private: 1211private:
879 virtual bool process_and_close_socket( 1212 bool create_and_connect_socket(Socket &socket) override;
880 socket_t sock, size_t request_count, 1213 void shutdown_ssl(Socket &socket, bool shutdown_gracefully) override;
881 std::function<bool(Stream &strm, bool last_connection, 1214
882 bool &connection_close)> 1215 bool process_socket(const Socket &socket,
883 callback); 1216 std::function<bool(Stream &strm)> callback) override;
884 virtual bool is_ssl() const; 1217 bool is_ssl() const override;
1218
1219 bool connect_with_proxy(Socket &sock, Response &res, bool &success);
1220 bool initialize_ssl(Socket &socket);
1221
1222 bool load_certs();
885 1223
886 bool verify_host(X509 *server_cert) const; 1224 bool verify_host(X509 *server_cert) const;
887 bool verify_host_with_subject_alt_name(X509 *server_cert) const; 1225 bool verify_host_with_subject_alt_name(X509 *server_cert) const;
@@ -890,12 +1228,15 @@ private:
890 1228
891 SSL_CTX *ctx_; 1229 SSL_CTX *ctx_;
892 std::mutex ctx_mutex_; 1230 std::mutex ctx_mutex_;
1231 std::once_flag initialize_cert_;
1232
893 std::vector<std::string> host_components_; 1233 std::vector<std::string> host_components_;
894 1234
895 std::string ca_cert_file_path_; 1235 std::string ca_cert_file_path_;
896 std::string ca_cert_dir_path_; 1236 std::string ca_cert_dir_path_;
897 bool server_certificate_verification_ = false;
898 long verify_result_ = 0; 1237 long verify_result_ = 0;
1238
1239 friend class ClientImpl;
899}; 1240};
900#endif 1241#endif
901 1242
@@ -948,31 +1289,39 @@ inline std::string from_i_to_hex(size_t n) {
948 return ret; 1289 return ret;
949} 1290}
950 1291
1292inline bool start_with(const std::string &a, const std::string &b) {
1293 if (a.size() < b.size()) { return false; }
1294 for (size_t i = 0; i < b.size(); i++) {
1295 if (::tolower(a[i]) != ::tolower(b[i])) { return false; }
1296 }
1297 return true;
1298}
1299
951inline size_t to_utf8(int code, char *buff) { 1300inline size_t to_utf8(int code, char *buff) {
952 if (code < 0x0080) { 1301 if (code < 0x0080) {
953 buff[0] = (code & 0x7F); 1302 buff[0] = (code & 0x7F);
954 return 1; 1303 return 1;
955 } else if (code < 0x0800) { 1304 } else if (code < 0x0800) {
956 buff[0] = (0xC0 | ((code >> 6) & 0x1F)); 1305 buff[0] = static_cast<char>(0xC0 | ((code >> 6) & 0x1F));
957 buff[1] = (0x80 | (code & 0x3F)); 1306 buff[1] = static_cast<char>(0x80 | (code & 0x3F));
958 return 2; 1307 return 2;
959 } else if (code < 0xD800) { 1308 } else if (code < 0xD800) {
960 buff[0] = (0xE0 | ((code >> 12) & 0xF)); 1309 buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF));
961 buff[1] = (0x80 | ((code >> 6) & 0x3F)); 1310 buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
962 buff[2] = (0x80 | (code & 0x3F)); 1311 buff[2] = static_cast<char>(0x80 | (code & 0x3F));
963 return 3; 1312 return 3;
964 } else if (code < 0xE000) { // D800 - DFFF is invalid... 1313 } else if (code < 0xE000) { // D800 - DFFF is invalid...
965 return 0; 1314 return 0;
966 } else if (code < 0x10000) { 1315 } else if (code < 0x10000) {
967 buff[0] = (0xE0 | ((code >> 12) & 0xF)); 1316 buff[0] = static_cast<char>(0xE0 | ((code >> 12) & 0xF));
968 buff[1] = (0x80 | ((code >> 6) & 0x3F)); 1317 buff[1] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
969 buff[2] = (0x80 | (code & 0x3F)); 1318 buff[2] = static_cast<char>(0x80 | (code & 0x3F));
970 return 3; 1319 return 3;
971 } else if (code < 0x110000) { 1320 } else if (code < 0x110000) {
972 buff[0] = (0xF0 | ((code >> 18) & 0x7)); 1321 buff[0] = static_cast<char>(0xF0 | ((code >> 18) & 0x7));
973 buff[1] = (0x80 | ((code >> 12) & 0x3F)); 1322 buff[1] = static_cast<char>(0x80 | ((code >> 12) & 0x3F));
974 buff[2] = (0x80 | ((code >> 6) & 0x3F)); 1323 buff[2] = static_cast<char>(0x80 | ((code >> 6) & 0x3F));
975 buff[3] = (0x80 | (code & 0x3F)); 1324 buff[3] = static_cast<char>(0x80 | (code & 0x3F));
976 return 4; 1325 return 4;
977 } 1326 }
978 1327
@@ -992,8 +1341,8 @@ inline std::string base64_encode(const std::string &in) {
992 int val = 0; 1341 int val = 0;
993 int valb = -6; 1342 int valb = -6;
994 1343
995 for (uint8_t c : in) { 1344 for (auto c : in) {
996 val = (val << 8) + c; 1345 val = (val << 8) + static_cast<uint8_t>(c);
997 valb += 8; 1346 valb += 8;
998 while (valb >= 0) { 1347 while (valb >= 0) {
999 out.push_back(lookup[(val >> valb) & 0x3F]); 1348 out.push_back(lookup[(val >> valb) & 0x3F]);
@@ -1057,13 +1406,81 @@ inline bool is_valid_path(const std::string &path) {
1057 return true; 1406 return true;
1058} 1407}
1059 1408
1409inline std::string encode_url(const std::string &s) {
1410 std::string result;
1411
1412 for (size_t i = 0; s[i]; i++) {
1413 switch (s[i]) {
1414 case ' ': result += "%20"; break;
1415 case '+': result += "%2B"; break;
1416 case '\r': result += "%0D"; break;
1417 case '\n': result += "%0A"; break;
1418 case '\'': result += "%27"; break;
1419 case ',': result += "%2C"; break;
1420 // case ':': result += "%3A"; break; // ok? probably...
1421 case ';': result += "%3B"; break;
1422 default:
1423 auto c = static_cast<uint8_t>(s[i]);
1424 if (c >= 0x80) {
1425 result += '%';
1426 char hex[4];
1427 auto len = snprintf(hex, sizeof(hex) - 1, "%02X", c);
1428 assert(len == 2);
1429 result.append(hex, static_cast<size_t>(len));
1430 } else {
1431 result += s[i];
1432 }
1433 break;
1434 }
1435 }
1436
1437 return result;
1438}
1439
1440inline std::string decode_url(const std::string &s,
1441 bool convert_plus_to_space) {
1442 std::string result;
1443
1444 for (size_t i = 0; i < s.size(); i++) {
1445 if (s[i] == '%' && i + 1 < s.size()) {
1446 if (s[i + 1] == 'u') {
1447 int val = 0;
1448 if (from_hex_to_i(s, i + 2, 4, val)) {
1449 // 4 digits Unicode codes
1450 char buff[4];
1451 size_t len = to_utf8(val, buff);
1452 if (len > 0) { result.append(buff, len); }
1453 i += 5; // 'u0000'
1454 } else {
1455 result += s[i];
1456 }
1457 } else {
1458 int val = 0;
1459 if (from_hex_to_i(s, i + 1, 2, val)) {
1460 // 2 digits hex codes
1461 result += static_cast<char>(val);
1462 i += 2; // '00'
1463 } else {
1464 result += s[i];
1465 }
1466 }
1467 } else if (convert_plus_to_space && s[i] == '+') {
1468 result += ' ';
1469 } else {
1470 result += s[i];
1471 }
1472 }
1473
1474 return result;
1475}
1476
1060inline void read_file(const std::string &path, std::string &out) { 1477inline void read_file(const std::string &path, std::string &out) {
1061 std::ifstream fs(path, std::ios_base::binary); 1478 std::ifstream fs(path, std::ios_base::binary);
1062 fs.seekg(0, std::ios_base::end); 1479 fs.seekg(0, std::ios_base::end);
1063 auto size = fs.tellg(); 1480 auto size = fs.tellg();
1064 fs.seekg(0); 1481 fs.seekg(0);
1065 out.resize(static_cast<size_t>(size)); 1482 out.resize(static_cast<size_t>(size));
1066 fs.read(&out[0], size); 1483 fs.read(&out[0], static_cast<std::streamsize>(size));
1067} 1484}
1068 1485
1069inline std::string file_extension(const std::string &path) { 1486inline std::string file_extension(const std::string &path) {
@@ -1073,19 +1490,41 @@ inline std::string file_extension(const std::string &path) {
1073 return std::string(); 1490 return std::string();
1074} 1491}
1075 1492
1493inline bool is_space_or_tab(char c) { return c == ' ' || c == '\t'; }
1494
1495inline std::pair<size_t, size_t> trim(const char *b, const char *e, size_t left,
1496 size_t right) {
1497 while (b + left < e && is_space_or_tab(b[left])) {
1498 left++;
1499 }
1500 while (right > 0 && is_space_or_tab(b[right - 1])) {
1501 right--;
1502 }
1503 return std::make_pair(left, right);
1504}
1505
1506inline std::string trim_copy(const std::string &s) {
1507 auto r = trim(s.data(), s.data() + s.size(), 0, s.size());
1508 return s.substr(r.first, r.second - r.first);
1509}
1510
1076template <class Fn> void split(const char *b, const char *e, char d, Fn fn) { 1511template <class Fn> void split(const char *b, const char *e, char d, Fn fn) {
1077 int i = 0; 1512 size_t i = 0;
1078 int beg = 0; 1513 size_t beg = 0;
1079 1514
1080 while (e ? (b + i != e) : (b[i] != '\0')) { 1515 while (e ? (b + i < e) : (b[i] != '\0')) {
1081 if (b[i] == d) { 1516 if (b[i] == d) {
1082 fn(&b[beg], &b[i]); 1517 auto r = trim(b, e, beg, i);
1518 if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
1083 beg = i + 1; 1519 beg = i + 1;
1084 } 1520 }
1085 i++; 1521 i++;
1086 } 1522 }
1087 1523
1088 if (i) { fn(&b[beg], &b[i]); } 1524 if (i) {
1525 auto r = trim(b, e, beg, i);
1526 if (r.first < r.second) { fn(&b[r.first], &b[r.second]); }
1527 }
1089} 1528}
1090 1529
1091// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer` 1530// NOTE: until the read size reaches `fixed_buffer_size`, use `fixed_buffer`
@@ -1172,7 +1611,17 @@ inline int close_socket(socket_t sock) {
1172#endif 1611#endif
1173} 1612}
1174 1613
1175inline int select_read(socket_t sock, time_t sec, time_t usec) { 1614template <typename T> inline ssize_t handle_EINTR(T fn) {
1615 ssize_t res = false;
1616 while (true) {
1617 res = fn();
1618 if (res < 0 && errno == EINTR) { continue; }
1619 break;
1620 }
1621 return res;
1622}
1623
1624inline ssize_t select_read(socket_t sock, time_t sec, time_t usec) {
1176#ifdef CPPHTTPLIB_USE_POLL 1625#ifdef CPPHTTPLIB_USE_POLL
1177 struct pollfd pfd_read; 1626 struct pollfd pfd_read;
1178 pfd_read.fd = sock; 1627 pfd_read.fd = sock;
@@ -1180,7 +1629,7 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
1180 1629
1181 auto timeout = static_cast<int>(sec * 1000 + usec / 1000); 1630 auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
1182 1631
1183 return poll(&pfd_read, 1, timeout); 1632 return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); });
1184#else 1633#else
1185 fd_set fds; 1634 fd_set fds;
1186 FD_ZERO(&fds); 1635 FD_ZERO(&fds);
@@ -1188,13 +1637,15 @@ inline int select_read(socket_t sock, time_t sec, time_t usec) {
1188 1637
1189 timeval tv; 1638 timeval tv;
1190 tv.tv_sec = static_cast<long>(sec); 1639 tv.tv_sec = static_cast<long>(sec);
1191 tv.tv_usec = static_cast<long>(usec); 1640 tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
1192 1641
1193 return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv); 1642 return handle_EINTR([&]() {
1643 return select(static_cast<int>(sock + 1), &fds, nullptr, nullptr, &tv);
1644 });
1194#endif 1645#endif
1195} 1646}
1196 1647
1197inline int select_write(socket_t sock, time_t sec, time_t usec) { 1648inline ssize_t select_write(socket_t sock, time_t sec, time_t usec) {
1198#ifdef CPPHTTPLIB_USE_POLL 1649#ifdef CPPHTTPLIB_USE_POLL
1199 struct pollfd pfd_read; 1650 struct pollfd pfd_read;
1200 pfd_read.fd = sock; 1651 pfd_read.fd = sock;
@@ -1202,7 +1653,7 @@ inline int select_write(socket_t sock, time_t sec, time_t usec) {
1202 1653
1203 auto timeout = static_cast<int>(sec * 1000 + usec / 1000); 1654 auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
1204 1655
1205 return poll(&pfd_read, 1, timeout); 1656 return handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); });
1206#else 1657#else
1207 fd_set fds; 1658 fd_set fds;
1208 FD_ZERO(&fds); 1659 FD_ZERO(&fds);
@@ -1210,9 +1661,11 @@ inline int select_write(socket_t sock, time_t sec, time_t usec) {
1210 1661
1211 timeval tv; 1662 timeval tv;
1212 tv.tv_sec = static_cast<long>(sec); 1663 tv.tv_sec = static_cast<long>(sec);
1213 tv.tv_usec = static_cast<long>(usec); 1664 tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
1214 1665
1215 return select(static_cast<int>(sock + 1), nullptr, &fds, nullptr, &tv); 1666 return handle_EINTR([&]() {
1667 return select(static_cast<int>(sock + 1), nullptr, &fds, nullptr, &tv);
1668 });
1216#endif 1669#endif
1217} 1670}
1218 1671
@@ -1224,13 +1677,14 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1224 1677
1225 auto timeout = static_cast<int>(sec * 1000 + usec / 1000); 1678 auto timeout = static_cast<int>(sec * 1000 + usec / 1000);
1226 1679
1227 if (poll(&pfd_read, 1, timeout) > 0 && 1680 auto poll_res = handle_EINTR([&]() { return poll(&pfd_read, 1, timeout); });
1228 pfd_read.revents & (POLLIN | POLLOUT)) { 1681
1682 if (poll_res > 0 && pfd_read.revents & (POLLIN | POLLOUT)) {
1229 int error = 0; 1683 int error = 0;
1230 socklen_t len = sizeof(error); 1684 socklen_t len = sizeof(error);
1231 return getsockopt(sock, SOL_SOCKET, SO_ERROR, 1685 auto res = getsockopt(sock, SOL_SOCKET, SO_ERROR,
1232 reinterpret_cast<char *>(&error), &len) >= 0 && 1686 reinterpret_cast<char *>(&error), &len);
1233 !error; 1687 return res >= 0 && !error;
1234 } 1688 }
1235 return false; 1689 return false;
1236#else 1690#else
@@ -1243,10 +1697,13 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1243 1697
1244 timeval tv; 1698 timeval tv;
1245 tv.tv_sec = static_cast<long>(sec); 1699 tv.tv_sec = static_cast<long>(sec);
1246 tv.tv_usec = static_cast<long>(usec); 1700 tv.tv_usec = static_cast<decltype(tv.tv_usec)>(usec);
1701
1702 auto ret = handle_EINTR([&]() {
1703 return select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv);
1704 });
1247 1705
1248 if (select(static_cast<int>(sock + 1), &fdsr, &fdsw, &fdse, &tv) > 0 && 1706 if (ret > 0 && (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
1249 (FD_ISSET(sock, &fdsr) || FD_ISSET(sock, &fdsw))) {
1250 int error = 0; 1707 int error = 0;
1251 socklen_t len = sizeof(error); 1708 socklen_t len = sizeof(error);
1252 return getsockopt(sock, SOL_SOCKET, SO_ERROR, 1709 return getsockopt(sock, SOL_SOCKET, SO_ERROR,
@@ -1259,40 +1716,45 @@ inline bool wait_until_socket_is_ready(socket_t sock, time_t sec, time_t usec) {
1259 1716
1260class SocketStream : public Stream { 1717class SocketStream : public Stream {
1261public: 1718public:
1262 SocketStream(socket_t sock, time_t read_timeout_sec, 1719 SocketStream(socket_t sock, time_t read_timeout_sec, time_t read_timeout_usec,
1263 time_t read_timeout_usec); 1720 time_t write_timeout_sec, time_t write_timeout_usec);
1264 ~SocketStream() override; 1721 ~SocketStream() override;
1265 1722
1266 bool is_readable() const override; 1723 bool is_readable() const override;
1267 bool is_writable() const override; 1724 bool is_writable() const override;
1268 int read(char *ptr, size_t size) override; 1725 ssize_t read(char *ptr, size_t size) override;
1269 int write(const char *ptr, size_t size) override; 1726 ssize_t write(const char *ptr, size_t size) override;
1270 std::string get_remote_addr() const override; 1727 void get_remote_ip_and_port(std::string &ip, int &port) const override;
1271 1728
1272private: 1729private:
1273 socket_t sock_; 1730 socket_t sock_;
1274 time_t read_timeout_sec_; 1731 time_t read_timeout_sec_;
1275 time_t read_timeout_usec_; 1732 time_t read_timeout_usec_;
1733 time_t write_timeout_sec_;
1734 time_t write_timeout_usec_;
1276}; 1735};
1277 1736
1278#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 1737#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1279class SSLSocketStream : public Stream { 1738class SSLSocketStream : public Stream {
1280public: 1739public:
1281 SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec, 1740 SSLSocketStream(socket_t sock, SSL *ssl, time_t read_timeout_sec,
1282 time_t read_timeout_usec); 1741 time_t read_timeout_usec, time_t write_timeout_sec,
1283 virtual ~SSLSocketStream(); 1742 time_t write_timeout_usec);
1743 ~SSLSocketStream() override;
1284 1744
1285 bool is_readable() const override; 1745 bool is_readable() const override;
1286 bool is_writable() const override; 1746 bool is_writable() const override;
1287 int read(char *ptr, size_t size) override; 1747 ssize_t read(char *ptr, size_t size) override;
1288 int write(const char *ptr, size_t size) override; 1748 ssize_t write(const char *ptr, size_t size) override;
1289 std::string get_remote_addr() const override; 1749 void get_remote_ip_and_port(std::string &ip, int &port) const override;
1290 1750
1291private: 1751private:
1292 socket_t sock_; 1752 socket_t sock_;
1293 SSL *ssl_; 1753 SSL *ssl_;
1294 time_t read_timeout_sec_; 1754 time_t read_timeout_sec_;
1295 time_t read_timeout_usec_; 1755 time_t read_timeout_usec_;
1756 time_t write_timeout_sec_;
1757 time_t write_timeout_usec_;
1296}; 1758};
1297#endif 1759#endif
1298 1760
@@ -1303,58 +1765,76 @@ public:
1303 1765
1304 bool is_readable() const override; 1766 bool is_readable() const override;
1305 bool is_writable() const override; 1767 bool is_writable() const override;
1306 int read(char *ptr, size_t size) override; 1768 ssize_t read(char *ptr, size_t size) override;
1307 int write(const char *ptr, size_t size) override; 1769 ssize_t write(const char *ptr, size_t size) override;
1308 std::string get_remote_addr() const override; 1770 void get_remote_ip_and_port(std::string &ip, int &port) const override;
1309 1771
1310 const std::string &get_buffer() const; 1772 const std::string &get_buffer() const;
1311 1773
1312private: 1774private:
1313 std::string buffer; 1775 std::string buffer;
1314 int position = 0; 1776 size_t position = 0;
1315}; 1777};
1316 1778
1779inline bool keep_alive(socket_t sock, time_t keep_alive_timeout_sec) {
1780 using namespace std::chrono;
1781 auto start = steady_clock::now();
1782 while (true) {
1783 auto val = select_read(sock, 0, 10000);
1784 if (val < 0) {
1785 return false;
1786 } else if (val == 0) {
1787 auto current = steady_clock::now();
1788 auto duration = duration_cast<milliseconds>(current - start);
1789 auto timeout = keep_alive_timeout_sec * 1000;
1790 if (duration.count() > timeout) { return false; }
1791 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1792 } else {
1793 return true;
1794 }
1795 }
1796}
1797
1317template <typename T> 1798template <typename T>
1318inline bool process_socket(bool is_client_request, socket_t sock, 1799inline bool
1319 size_t keep_alive_max_count, time_t read_timeout_sec, 1800process_server_socket_core(socket_t sock, size_t keep_alive_max_count,
1320 time_t read_timeout_usec, T callback) { 1801 time_t keep_alive_timeout_sec, T callback) {
1321 assert(keep_alive_max_count > 0); 1802 assert(keep_alive_max_count > 0);
1322
1323 auto ret = false; 1803 auto ret = false;
1324 1804 auto count = keep_alive_max_count;
1325 if (keep_alive_max_count > 1) { 1805 while (count > 0 && keep_alive(sock, keep_alive_timeout_sec)) {
1326 auto count = keep_alive_max_count; 1806 auto close_connection = count == 1;
1327 while (count > 0 && 1807 auto connection_closed = false;
1328 (is_client_request || 1808 ret = callback(close_connection, connection_closed);
1329 select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 1809 if (!ret || connection_closed) { break; }
1330 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 1810 count--;
1331 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1332 auto last_connection = count == 1;
1333 auto connection_close = false;
1334
1335 ret = callback(strm, last_connection, connection_close);
1336 if (!ret || connection_close) { break; }
1337
1338 count--;
1339 }
1340 } else { // keep_alive_max_count is 0 or 1
1341 SocketStream strm(sock, read_timeout_sec, read_timeout_usec);
1342 auto dummy_connection_close = false;
1343 ret = callback(strm, true, dummy_connection_close);
1344 } 1811 }
1345
1346 return ret; 1812 return ret;
1347} 1813}
1348 1814
1349template <typename T> 1815template <typename T>
1350inline bool process_and_close_socket(bool is_client_request, socket_t sock, 1816inline bool
1351 size_t keep_alive_max_count, 1817process_server_socket(socket_t sock, size_t keep_alive_max_count,
1352 time_t read_timeout_sec, 1818 time_t keep_alive_timeout_sec, time_t read_timeout_sec,
1353 time_t read_timeout_usec, T callback) { 1819 time_t read_timeout_usec, time_t write_timeout_sec,
1354 auto ret = process_socket(is_client_request, sock, keep_alive_max_count, 1820 time_t write_timeout_usec, T callback) {
1355 read_timeout_sec, read_timeout_usec, callback); 1821 return process_server_socket_core(
1356 close_socket(sock); 1822 sock, keep_alive_max_count, keep_alive_timeout_sec,
1357 return ret; 1823 [&](bool close_connection, bool &connection_closed) {
1824 SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
1825 write_timeout_sec, write_timeout_usec);
1826 return callback(strm, close_connection, connection_closed);
1827 });
1828}
1829
1830template <typename T>
1831inline bool process_client_socket(socket_t sock, time_t read_timeout_sec,
1832 time_t read_timeout_usec,
1833 time_t write_timeout_sec,
1834 time_t write_timeout_usec, T callback) {
1835 SocketStream strm(sock, read_timeout_sec, read_timeout_usec,
1836 write_timeout_sec, write_timeout_usec);
1837 return callback(strm);
1358} 1838}
1359 1839
1360inline int shutdown_socket(socket_t sock) { 1840inline int shutdown_socket(socket_t sock) {
@@ -1365,18 +1845,10 @@ inline int shutdown_socket(socket_t sock) {
1365#endif 1845#endif
1366} 1846}
1367 1847
1368template <typename Fn> 1848template <typename BindOrConnect>
1369socket_t create_socket(const char *host, int port, Fn fn, 1849socket_t create_socket(const char *host, int port, int socket_flags,
1370 int socket_flags = 0) { 1850 bool tcp_nodelay, SocketOptions socket_options,
1371#ifdef _WIN32 1851 BindOrConnect bind_or_connect) {
1372#define SO_SYNCHRONOUS_NONALERT 0x20
1373#define SO_OPENTYPE 0x7008
1374
1375 int opt = SO_SYNCHRONOUS_NONALERT;
1376 setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE, (char *)&opt,
1377 sizeof(opt));
1378#endif
1379
1380 // Get address info 1852 // Get address info
1381 struct addrinfo hints; 1853 struct addrinfo hints;
1382 struct addrinfo *result; 1854 struct addrinfo *result;
@@ -1390,6 +1862,9 @@ socket_t create_socket(const char *host, int port, Fn fn,
1390 auto service = std::to_string(port); 1862 auto service = std::to_string(port);
1391 1863
1392 if (getaddrinfo(host, service.c_str(), &hints, &result)) { 1864 if (getaddrinfo(host, service.c_str(), &hints, &result)) {
1865#ifdef __linux__
1866 res_init();
1867#endif
1393 return INVALID_SOCKET; 1868 return INVALID_SOCKET;
1394 } 1869 }
1395 1870
@@ -1424,17 +1899,22 @@ socket_t create_socket(const char *host, int port, Fn fn,
1424 if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; } 1899 if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) { continue; }
1425#endif 1900#endif
1426 1901
1427 // Make 'reuse address' option available 1902 if (tcp_nodelay) {
1428 int yes = 1; 1903 int yes = 1;
1429 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&yes), 1904 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<char *>(&yes),
1430 sizeof(yes)); 1905 sizeof(yes));
1431#ifdef SO_REUSEPORT 1906 }
1432 setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, reinterpret_cast<char *>(&yes), 1907
1433 sizeof(yes)); 1908 if (socket_options) { socket_options(sock); }
1434#endif 1909
1910 if (rp->ai_family == AF_INET6) {
1911 int no = 0;
1912 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char *>(&no),
1913 sizeof(no));
1914 }
1435 1915
1436 // bind or connect 1916 // bind or connect
1437 if (fn(sock, *rp)) { 1917 if (bind_or_connect(sock, *rp)) {
1438 freeaddrinfo(result); 1918 freeaddrinfo(result);
1439 return sock; 1919 return sock;
1440 } 1920 }
@@ -1479,7 +1959,7 @@ inline bool bind_ip_address(socket_t sock, const char *host) {
1479 auto ret = false; 1959 auto ret = false;
1480 for (auto rp = result; rp; rp = rp->ai_next) { 1960 for (auto rp = result; rp; rp = rp->ai_next) {
1481 const auto &ai = *rp; 1961 const auto &ai = *rp;
1482 if (!::bind(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen))) { 1962 if (!::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
1483 ret = true; 1963 ret = true;
1484 break; 1964 break;
1485 } 1965 }
@@ -1489,8 +1969,12 @@ inline bool bind_ip_address(socket_t sock, const char *host) {
1489 return ret; 1969 return ret;
1490} 1970}
1491 1971
1972#if !defined _WIN32 && !defined ANDROID
1973#define USE_IF2IP
1974#endif
1975
1976#ifdef USE_IF2IP
1492inline std::string if2ip(const std::string &ifn) { 1977inline std::string if2ip(const std::string &ifn) {
1493#ifndef _WIN32
1494 struct ifaddrs *ifap; 1978 struct ifaddrs *ifap;
1495 getifaddrs(&ifap); 1979 getifaddrs(&ifap);
1496 for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) { 1980 for (auto ifa = ifap; ifa; ifa = ifa->ifa_next) {
@@ -1506,51 +1990,83 @@ inline std::string if2ip(const std::string &ifn) {
1506 } 1990 }
1507 } 1991 }
1508 freeifaddrs(ifap); 1992 freeifaddrs(ifap);
1509#endif
1510 return std::string(); 1993 return std::string();
1511} 1994}
1995#endif
1512 1996
1513inline socket_t create_client_socket(const char *host, int port, 1997inline socket_t create_client_socket(const char *host, int port,
1514 time_t timeout_sec, 1998 bool tcp_nodelay,
1515 const std::string &intf) { 1999 SocketOptions socket_options,
1516 return create_socket( 2000 time_t timeout_sec, time_t timeout_usec,
1517 host, port, [&](socket_t sock, struct addrinfo &ai) -> bool { 2001 const std::string &intf, std::atomic<Error> &error) {
2002 auto sock = create_socket(
2003 host, port, 0, tcp_nodelay, std::move(socket_options),
2004 [&](socket_t sock, struct addrinfo &ai) -> bool {
1518 if (!intf.empty()) { 2005 if (!intf.empty()) {
2006#ifdef USE_IF2IP
1519 auto ip = if2ip(intf); 2007 auto ip = if2ip(intf);
1520 if (ip.empty()) { ip = intf; } 2008 if (ip.empty()) { ip = intf; }
1521 if (!bind_ip_address(sock, ip.c_str())) { return false; } 2009 if (!bind_ip_address(sock, ip.c_str())) {
2010 error = Error::BindIPAddress;
2011 return false;
2012 }
2013#endif
1522 } 2014 }
1523 2015
1524 set_nonblocking(sock, true); 2016 set_nonblocking(sock, true);
1525 2017
1526 auto ret = ::connect(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen)); 2018 auto ret =
2019 ::connect(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen));
2020
1527 if (ret < 0) { 2021 if (ret < 0) {
1528 if (is_connection_error() || 2022 if (is_connection_error() ||
1529 !wait_until_socket_is_ready(sock, timeout_sec, 0)) { 2023 !wait_until_socket_is_ready(sock, timeout_sec, timeout_usec)) {
1530 close_socket(sock); 2024 close_socket(sock);
2025 error = Error::Connection;
1531 return false; 2026 return false;
1532 } 2027 }
1533 } 2028 }
1534 2029
1535 set_nonblocking(sock, false); 2030 set_nonblocking(sock, false);
2031 error = Error::Success;
1536 return true; 2032 return true;
1537 }); 2033 });
1538}
1539 2034
1540inline std::string get_remote_addr(socket_t sock) { 2035 if (sock != INVALID_SOCKET) {
1541 struct sockaddr_storage addr; 2036 error = Error::Success;
1542 socklen_t len = sizeof(addr); 2037 } else {
2038 if (error == Error::Success) { error = Error::Connection; }
2039 }
1543 2040
1544 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr), &len)) { 2041 return sock;
1545 std::array<char, NI_MAXHOST> ipstr{}; 2042}
1546 2043
1547 if (!getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), len, 2044inline void get_remote_ip_and_port(const struct sockaddr_storage &addr,
1548 ipstr.data(), static_cast<unsigned int>(ipstr.size()), nullptr, 0, NI_NUMERICHOST)) { 2045 socklen_t addr_len, std::string &ip,
1549 return ipstr.data(); 2046 int &port) {
1550 } 2047 if (addr.ss_family == AF_INET) {
2048 port = ntohs(reinterpret_cast<const struct sockaddr_in *>(&addr)->sin_port);
2049 } else if (addr.ss_family == AF_INET6) {
2050 port =
2051 ntohs(reinterpret_cast<const struct sockaddr_in6 *>(&addr)->sin6_port);
1551 } 2052 }
1552 2053
1553 return std::string(); 2054 std::array<char, NI_MAXHOST> ipstr{};
2055 if (!getnameinfo(reinterpret_cast<const struct sockaddr *>(&addr), addr_len,
2056 ipstr.data(), static_cast<socklen_t>(ipstr.size()), nullptr,
2057 0, NI_NUMERICHOST)) {
2058 ip = ipstr.data();
2059 }
2060}
2061
2062inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
2063 struct sockaddr_storage addr;
2064 socklen_t addr_len = sizeof(addr);
2065
2066 if (!getpeername(sock, reinterpret_cast<struct sockaddr *>(&addr),
2067 &addr_len)) {
2068 get_remote_ip_and_port(addr, addr_len, ip, port);
2069 }
1554} 2070}
1555 2071
1556inline const char * 2072inline const char *
@@ -1596,121 +2112,336 @@ find_content_type(const std::string &path,
1596inline const char *status_message(int status) { 2112inline const char *status_message(int status) {
1597 switch (status) { 2113 switch (status) {
1598 case 100: return "Continue"; 2114 case 100: return "Continue";
2115 case 101: return "Switching Protocol";
2116 case 102: return "Processing";
2117 case 103: return "Early Hints";
1599 case 200: return "OK"; 2118 case 200: return "OK";
2119 case 201: return "Created";
1600 case 202: return "Accepted"; 2120 case 202: return "Accepted";
2121 case 203: return "Non-Authoritative Information";
1601 case 204: return "No Content"; 2122 case 204: return "No Content";
2123 case 205: return "Reset Content";
1602 case 206: return "Partial Content"; 2124 case 206: return "Partial Content";
2125 case 207: return "Multi-Status";
2126 case 208: return "Already Reported";
2127 case 226: return "IM Used";
2128 case 300: return "Multiple Choice";
1603 case 301: return "Moved Permanently"; 2129 case 301: return "Moved Permanently";
1604 case 302: return "Found"; 2130 case 302: return "Found";
1605 case 303: return "See Other"; 2131 case 303: return "See Other";
1606 case 304: return "Not Modified"; 2132 case 304: return "Not Modified";
2133 case 305: return "Use Proxy";
2134 case 306: return "unused";
2135 case 307: return "Temporary Redirect";
2136 case 308: return "Permanent Redirect";
1607 case 400: return "Bad Request"; 2137 case 400: return "Bad Request";
1608 case 401: return "Unauthorized"; 2138 case 401: return "Unauthorized";
2139 case 402: return "Payment Required";
1609 case 403: return "Forbidden"; 2140 case 403: return "Forbidden";
1610 case 404: return "Not Found"; 2141 case 404: return "Not Found";
2142 case 405: return "Method Not Allowed";
2143 case 406: return "Not Acceptable";
2144 case 407: return "Proxy Authentication Required";
2145 case 408: return "Request Timeout";
2146 case 409: return "Conflict";
2147 case 410: return "Gone";
2148 case 411: return "Length Required";
2149 case 412: return "Precondition Failed";
1611 case 413: return "Payload Too Large"; 2150 case 413: return "Payload Too Large";
1612 case 414: return "Request-URI Too Long"; 2151 case 414: return "URI Too Long";
1613 case 415: return "Unsupported Media Type"; 2152 case 415: return "Unsupported Media Type";
1614 case 416: return "Range Not Satisfiable"; 2153 case 416: return "Range Not Satisfiable";
1615 case 417: return "Expectation Failed"; 2154 case 417: return "Expectation Failed";
2155 case 418: return "I'm a teapot";
2156 case 421: return "Misdirected Request";
2157 case 422: return "Unprocessable Entity";
2158 case 423: return "Locked";
2159 case 424: return "Failed Dependency";
2160 case 425: return "Too Early";
2161 case 426: return "Upgrade Required";
2162 case 428: return "Precondition Required";
2163 case 429: return "Too Many Requests";
2164 case 431: return "Request Header Fields Too Large";
2165 case 451: return "Unavailable For Legal Reasons";
2166 case 501: return "Not Implemented";
2167 case 502: return "Bad Gateway";
1616 case 503: return "Service Unavailable"; 2168 case 503: return "Service Unavailable";
2169 case 504: return "Gateway Timeout";
2170 case 505: return "HTTP Version Not Supported";
2171 case 506: return "Variant Also Negotiates";
2172 case 507: return "Insufficient Storage";
2173 case 508: return "Loop Detected";
2174 case 510: return "Not Extended";
2175 case 511: return "Network Authentication Required";
1617 2176
1618 default: 2177 default:
1619 case 500: return "Internal Server Error"; 2178 case 500: return "Internal Server Error";
1620 } 2179 }
1621} 2180}
1622 2181
1623#ifdef CPPHTTPLIB_ZLIB_SUPPORT 2182inline bool can_compress_content_type(const std::string &content_type) {
1624inline bool can_compress(const std::string &content_type) { 2183 return (!content_type.find("text/") && content_type != "text/event-stream") ||
1625 return !content_type.find("text/") || content_type == "image/svg+xml" || 2184 content_type == "image/svg+xml" ||
1626 content_type == "application/javascript" || 2185 content_type == "application/javascript" ||
1627 content_type == "application/json" || 2186 content_type == "application/json" ||
1628 content_type == "application/xml" || 2187 content_type == "application/xml" ||
1629 content_type == "application/xhtml+xml"; 2188 content_type == "application/xhtml+xml";
1630} 2189}
1631 2190
1632inline bool compress(std::string &content) { 2191enum class EncodingType { None = 0, Gzip, Brotli };
1633 z_stream strm;
1634 strm.zalloc = Z_NULL;
1635 strm.zfree = Z_NULL;
1636 strm.opaque = Z_NULL;
1637 2192
1638 auto ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8, 2193inline EncodingType encoding_type(const Request &req, const Response &res) {
1639 Z_DEFAULT_STRATEGY); 2194 auto ret =
1640 if (ret != Z_OK) { return false; } 2195 detail::can_compress_content_type(res.get_header_value("Content-Type"));
2196 if (!ret) { return EncodingType::None; }
1641 2197
1642 strm.avail_in = content.size(); 2198 const auto &s = req.get_header_value("Accept-Encoding");
1643 strm.next_in = 2199 (void)(s);
1644 const_cast<Bytef *>(reinterpret_cast<const Bytef *>(content.data()));
1645 2200
1646 std::string compressed; 2201#ifdef CPPHTTPLIB_BROTLI_SUPPORT
2202 // TODO: 'Accept-Encoding' has br, not br;q=0
2203 ret = s.find("br") != std::string::npos;
2204 if (ret) { return EncodingType::Brotli; }
2205#endif
1647 2206
1648 std::array<char, 16384> buff{}; 2207#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1649 do { 2208 // TODO: 'Accept-Encoding' has gzip, not gzip;q=0
1650 strm.avail_out = buff.size(); 2209 ret = s.find("gzip") != std::string::npos;
1651 strm.next_out = reinterpret_cast<Bytef *>(buff.data()); 2210 if (ret) { return EncodingType::Gzip; }
1652 ret = deflate(&strm, Z_FINISH); 2211#endif
1653 assert(ret != Z_STREAM_ERROR);
1654 compressed.append(buff.data(), buff.size() - strm.avail_out);
1655 } while (strm.avail_out == 0);
1656 2212
1657 assert(ret == Z_STREAM_END); 2213 return EncodingType::None;
1658 assert(strm.avail_in == 0); 2214}
1659 2215
1660 content.swap(compressed); 2216class compressor {
2217public:
2218 virtual ~compressor(){};
1661 2219
1662 deflateEnd(&strm); 2220 typedef std::function<bool(const char *data, size_t data_len)> Callback;
1663 return true; 2221 virtual bool compress(const char *data, size_t data_length, bool last,
1664} 2222 Callback callback) = 0;
2223};
1665 2224
1666class decompressor { 2225class decompressor {
1667public: 2226public:
1668 decompressor() { 2227 virtual ~decompressor() {}
1669 strm.zalloc = Z_NULL; 2228
1670 strm.zfree = Z_NULL; 2229 virtual bool is_valid() const = 0;
1671 strm.opaque = Z_NULL; 2230
2231 typedef std::function<bool(const char *data, size_t data_len)> Callback;
2232 virtual bool decompress(const char *data, size_t data_length,
2233 Callback callback) = 0;
2234};
2235
2236class nocompressor : public compressor {
2237public:
2238 ~nocompressor(){};
2239
2240 bool compress(const char *data, size_t data_length, bool /*last*/,
2241 Callback callback) override {
2242 if (!data_length) { return true; }
2243 return callback(data, data_length);
2244 }
2245};
2246
2247#ifdef CPPHTTPLIB_ZLIB_SUPPORT
2248class gzip_compressor : public compressor {
2249public:
2250 gzip_compressor() {
2251 std::memset(&strm_, 0, sizeof(strm_));
2252 strm_.zalloc = Z_NULL;
2253 strm_.zfree = Z_NULL;
2254 strm_.opaque = Z_NULL;
2255
2256 is_valid_ = deflateInit2(&strm_, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 31, 8,
2257 Z_DEFAULT_STRATEGY) == Z_OK;
2258 }
2259
2260 ~gzip_compressor() { deflateEnd(&strm_); }
2261
2262 bool compress(const char *data, size_t data_length, bool last,
2263 Callback callback) override {
2264 assert(is_valid_);
2265
2266 auto flush = last ? Z_FINISH : Z_NO_FLUSH;
2267
2268 strm_.avail_in = static_cast<decltype(strm_.avail_in)>(data_length);
2269 strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
2270
2271 int ret = Z_OK;
2272
2273 std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2274 do {
2275 strm_.avail_out = buff.size();
2276 strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
2277
2278 ret = deflate(&strm_, flush);
2279 assert(ret != Z_STREAM_ERROR);
2280
2281 if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
2282 return false;
2283 }
2284 } while (strm_.avail_out == 0);
2285
2286 assert((last && ret == Z_STREAM_END) || (!last && ret == Z_OK));
2287 assert(strm_.avail_in == 0);
2288 return true;
2289 }
2290
2291private:
2292 bool is_valid_ = false;
2293 z_stream strm_;
2294};
2295
2296class gzip_decompressor : public decompressor {
2297public:
2298 gzip_decompressor() {
2299 std::memset(&strm_, 0, sizeof(strm_));
2300 strm_.zalloc = Z_NULL;
2301 strm_.zfree = Z_NULL;
2302 strm_.opaque = Z_NULL;
1672 2303
1673 // 15 is the value of wbits, which should be at the maximum possible value 2304 // 15 is the value of wbits, which should be at the maximum possible value
1674 // to ensure that any gzip stream can be decoded. The offset of 16 specifies 2305 // to ensure that any gzip stream can be decoded. The offset of 32 specifies
1675 // that the stream to decompress will be formatted with a gzip wrapper. 2306 // that the stream type should be automatically detected either gzip or
1676 is_valid_ = inflateInit2(&strm, 16 + 15) == Z_OK; 2307 // deflate.
2308 is_valid_ = inflateInit2(&strm_, 32 + 15) == Z_OK;
1677 } 2309 }
1678 2310
1679 ~decompressor() { inflateEnd(&strm); } 2311 ~gzip_decompressor() { inflateEnd(&strm_); }
1680 2312
1681 bool is_valid() const { return is_valid_; } 2313 bool is_valid() const override { return is_valid_; }
2314
2315 bool decompress(const char *data, size_t data_length,
2316 Callback callback) override {
2317 assert(is_valid_);
1682 2318
1683 template <typename T>
1684 bool decompress(const char *data, size_t data_length, T callback) {
1685 int ret = Z_OK; 2319 int ret = Z_OK;
1686 2320
1687 strm.avail_in = data_length; 2321 strm_.avail_in = static_cast<decltype(strm_.avail_in)>(data_length);
1688 strm.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data)); 2322 strm_.next_in = const_cast<Bytef *>(reinterpret_cast<const Bytef *>(data));
1689 2323
1690 std::array<char, 16384> buff{}; 2324 std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
1691 do { 2325 while (strm_.avail_in > 0) {
1692 strm.avail_out = buff.size(); 2326 strm_.avail_out = buff.size();
1693 strm.next_out = reinterpret_cast<Bytef *>(buff.data()); 2327 strm_.next_out = reinterpret_cast<Bytef *>(buff.data());
1694 2328
1695 ret = inflate(&strm, Z_NO_FLUSH); 2329 ret = inflate(&strm_, Z_NO_FLUSH);
1696 assert(ret != Z_STREAM_ERROR); 2330 assert(ret != Z_STREAM_ERROR);
1697 switch (ret) { 2331 switch (ret) {
1698 case Z_NEED_DICT: 2332 case Z_NEED_DICT:
1699 case Z_DATA_ERROR: 2333 case Z_DATA_ERROR:
1700 case Z_MEM_ERROR: inflateEnd(&strm); return false; 2334 case Z_MEM_ERROR: inflateEnd(&strm_); return false;
1701 } 2335 }
1702 2336
1703 if (!callback(buff.data(), buff.size() - strm.avail_out)) { 2337 if (!callback(buff.data(), buff.size() - strm_.avail_out)) {
1704 return false; 2338 return false;
1705 } 2339 }
1706 } while (strm.avail_out == 0); 2340 }
1707 2341
1708 return ret == Z_OK || ret == Z_STREAM_END; 2342 return ret == Z_OK || ret == Z_STREAM_END;
1709 } 2343 }
1710 2344
1711private: 2345private:
1712 bool is_valid_; 2346 bool is_valid_ = false;
1713 z_stream strm; 2347 z_stream strm_;
2348};
2349#endif
2350
2351#ifdef CPPHTTPLIB_BROTLI_SUPPORT
2352class brotli_compressor : public compressor {
2353public:
2354 brotli_compressor() {
2355 state_ = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
2356 }
2357
2358 ~brotli_compressor() { BrotliEncoderDestroyInstance(state_); }
2359
2360 bool compress(const char *data, size_t data_length, bool last,
2361 Callback callback) override {
2362 std::array<uint8_t, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2363
2364 auto operation = last ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS;
2365 auto available_in = data_length;
2366 auto next_in = reinterpret_cast<const uint8_t *>(data);
2367
2368 for (;;) {
2369 if (last) {
2370 if (BrotliEncoderIsFinished(state_)) { break; }
2371 } else {
2372 if (!available_in) { break; }
2373 }
2374
2375 auto available_out = buff.size();
2376 auto next_out = buff.data();
2377
2378 if (!BrotliEncoderCompressStream(state_, operation, &available_in,
2379 &next_in, &available_out, &next_out,
2380 nullptr)) {
2381 return false;
2382 }
2383
2384 auto output_bytes = buff.size() - available_out;
2385 if (output_bytes) {
2386 callback(reinterpret_cast<const char *>(buff.data()), output_bytes);
2387 }
2388 }
2389
2390 return true;
2391 }
2392
2393private:
2394 BrotliEncoderState *state_ = nullptr;
2395};
2396
2397class brotli_decompressor : public decompressor {
2398public:
2399 brotli_decompressor() {
2400 decoder_s = BrotliDecoderCreateInstance(0, 0, 0);
2401 decoder_r = decoder_s ? BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT
2402 : BROTLI_DECODER_RESULT_ERROR;
2403 }
2404
2405 ~brotli_decompressor() {
2406 if (decoder_s) { BrotliDecoderDestroyInstance(decoder_s); }
2407 }
2408
2409 bool is_valid() const override { return decoder_s; }
2410
2411 bool decompress(const char *data, size_t data_length,
2412 Callback callback) override {
2413 if (decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
2414 decoder_r == BROTLI_DECODER_RESULT_ERROR) {
2415 return 0;
2416 }
2417
2418 const uint8_t *next_in = (const uint8_t *)data;
2419 size_t avail_in = data_length;
2420 size_t total_out;
2421
2422 decoder_r = BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT;
2423
2424 std::array<char, CPPHTTPLIB_COMPRESSION_BUFSIZ> buff{};
2425 while (decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
2426 char *next_out = buff.data();
2427 size_t avail_out = buff.size();
2428
2429 decoder_r = BrotliDecoderDecompressStream(
2430 decoder_s, &avail_in, &next_in, &avail_out,
2431 reinterpret_cast<uint8_t **>(&next_out), &total_out);
2432
2433 if (decoder_r == BROTLI_DECODER_RESULT_ERROR) { return false; }
2434
2435 if (!callback(buff.data(), buff.size() - avail_out)) { return false; }
2436 }
2437
2438 return decoder_r == BROTLI_DECODER_RESULT_SUCCESS ||
2439 decoder_r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT;
2440 }
2441
2442private:
2443 BrotliDecoderResult decoder_r;
2444 BrotliDecoderState *decoder_s = nullptr;
1714}; 2445};
1715#endif 2446#endif
1716 2447
@@ -1720,21 +2451,60 @@ inline bool has_header(const Headers &headers, const char *key) {
1720 2451
1721inline const char *get_header_value(const Headers &headers, const char *key, 2452inline const char *get_header_value(const Headers &headers, const char *key,
1722 size_t id = 0, const char *def = nullptr) { 2453 size_t id = 0, const char *def = nullptr) {
1723 auto it = headers.find(key); 2454 auto rng = headers.equal_range(key);
1724 std::advance(it, id); 2455 auto it = rng.first;
1725 if (it != headers.end()) { return it->second.c_str(); } 2456 std::advance(it, static_cast<ssize_t>(id));
2457 if (it != rng.second) { return it->second.c_str(); }
1726 return def; 2458 return def;
1727} 2459}
1728 2460
1729inline uint64_t get_header_value_uint64(const Headers &headers, const char *key, 2461template <typename T>
1730 int def = 0) { 2462inline T get_header_value(const Headers & /*headers*/, const char * /*key*/,
1731 auto it = headers.find(key); 2463 size_t /*id*/ = 0, uint64_t /*def*/ = 0) {}
1732 if (it != headers.end()) { 2464
2465template <>
2466inline uint64_t get_header_value<uint64_t>(const Headers &headers,
2467 const char *key, size_t id,
2468 uint64_t def) {
2469 auto rng = headers.equal_range(key);
2470 auto it = rng.first;
2471 std::advance(it, static_cast<ssize_t>(id));
2472 if (it != rng.second) {
1733 return std::strtoull(it->second.data(), nullptr, 10); 2473 return std::strtoull(it->second.data(), nullptr, 10);
1734 } 2474 }
1735 return def; 2475 return def;
1736} 2476}
1737 2477
2478template <typename T>
2479inline bool parse_header(const char *beg, const char *end, T fn) {
2480 // Skip trailing spaces and tabs.
2481 while (beg < end && is_space_or_tab(end[-1])) {
2482 end--;
2483 }
2484
2485 auto p = beg;
2486 while (p < end && *p != ':') {
2487 p++;
2488 }
2489
2490 if (p == end) { return false; }
2491
2492 auto key_end = p;
2493
2494 if (*p++ != ':') { return false; }
2495
2496 while (p < end && is_space_or_tab(*p)) {
2497 p++;
2498 }
2499
2500 if (p < end) {
2501 fn(std::string(beg, key_end), decode_url(std::string(p, end), false));
2502 return true;
2503 }
2504
2505 return false;
2506}
2507
1738inline bool read_headers(Stream &strm, Headers &headers) { 2508inline bool read_headers(Stream &strm, Headers &headers) {
1739 const auto bufsiz = 2048; 2509 const auto bufsiz = 2048;
1740 char buf[bufsiz]; 2510 char buf[bufsiz];
@@ -1751,42 +2521,31 @@ inline bool read_headers(Stream &strm, Headers &headers) {
1751 continue; // Skip invalid line. 2521 continue; // Skip invalid line.
1752 } 2522 }
1753 2523
1754 // Skip trailing spaces and tabs. 2524 // Exclude CRLF
1755 auto end = line_reader.ptr() + line_reader.size() - 2; 2525 auto end = line_reader.ptr() + line_reader.size() - 2;
1756 while (line_reader.ptr() < end && (end[-1] == ' ' || end[-1] == '\t')) {
1757 end--;
1758 }
1759 2526
1760 // Horizontal tab and ' ' are considered whitespace and are ignored when on 2527 parse_header(line_reader.ptr(), end,
1761 // the left or right side of the header value: 2528 [&](std::string &&key, std::string &&val) {
1762 // - https://stackoverflow.com/questions/50179659/ 2529 headers.emplace(std::move(key), std::move(val));
1763 // - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html 2530 });
1764 static const std::regex re(R"((.+?):[\t ]*(.+))");
1765
1766 std::cmatch m;
1767 if (std::regex_match(line_reader.ptr(), end, m, re)) {
1768 auto key = std::string(m[1]);
1769 auto val = std::string(m[2]);
1770 headers.emplace(key, val);
1771 }
1772 } 2531 }
1773 2532
1774 return true; 2533 return true;
1775} 2534}
1776 2535
1777inline bool read_content_with_length(Stream &strm, uint64_t len, 2536inline bool read_content_with_length(Stream &strm, uint64_t len,
1778 Progress progress, ContentReceiver out) { 2537 Progress progress,
2538 ContentReceiverWithProgress out) {
1779 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 2539 char buf[CPPHTTPLIB_RECV_BUFSIZ];
1780 2540
1781 uint64_t r = 0; 2541 uint64_t r = 0;
1782 while (r < len) { 2542 while (r < len) {
1783 auto read_len = static_cast<size_t>(len - r); 2543 auto read_len = static_cast<size_t>(len - r);
1784 auto n = strm.read(buf, std::min(read_len, CPPHTTPLIB_RECV_BUFSIZ)); 2544 auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ));
1785 if (n <= 0) { return false; } 2545 if (n <= 0) { return false; }
1786 2546
1787 if (!out(buf, n)) { return false; } 2547 if (!out(buf, static_cast<size_t>(n), r, len)) { return false; }
1788 2548 r += static_cast<uint64_t>(n);
1789 r += n;
1790 2549
1791 if (progress) { 2550 if (progress) {
1792 if (!progress(r, len)) { return false; } 2551 if (!progress(r, len)) { return false; }
@@ -1801,14 +2560,16 @@ inline void skip_content_with_length(Stream &strm, uint64_t len) {
1801 uint64_t r = 0; 2560 uint64_t r = 0;
1802 while (r < len) { 2561 while (r < len) {
1803 auto read_len = static_cast<size_t>(len - r); 2562 auto read_len = static_cast<size_t>(len - r);
1804 auto n = strm.read(buf, std::min(read_len, CPPHTTPLIB_RECV_BUFSIZ)); 2563 auto n = strm.read(buf, (std::min)(read_len, CPPHTTPLIB_RECV_BUFSIZ));
1805 if (n <= 0) { return; } 2564 if (n <= 0) { return; }
1806 r += n; 2565 r += static_cast<uint64_t>(n);
1807 } 2566 }
1808} 2567}
1809 2568
1810inline bool read_content_without_length(Stream &strm, ContentReceiver out) { 2569inline bool read_content_without_length(Stream &strm,
2570 ContentReceiverWithProgress out) {
1811 char buf[CPPHTTPLIB_RECV_BUFSIZ]; 2571 char buf[CPPHTTPLIB_RECV_BUFSIZ];
2572 uint64_t r = 0;
1812 for (;;) { 2573 for (;;) {
1813 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ); 2574 auto n = strm.read(buf, CPPHTTPLIB_RECV_BUFSIZ);
1814 if (n < 0) { 2575 if (n < 0) {
@@ -1816,13 +2577,16 @@ inline bool read_content_without_length(Stream &strm, ContentReceiver out) {
1816 } else if (n == 0) { 2577 } else if (n == 0) {
1817 return true; 2578 return true;
1818 } 2579 }
1819 if (!out(buf, n)) { return false; } 2580
2581 if (!out(buf, static_cast<size_t>(n), r, 0)) { return false; }
2582 r += static_cast<uint64_t>(n);
1820 } 2583 }
1821 2584
1822 return true; 2585 return true;
1823} 2586}
1824 2587
1825inline bool read_content_chunked(Stream &strm, ContentReceiver out) { 2588inline bool read_content_chunked(Stream &strm,
2589 ContentReceiverWithProgress out) {
1826 const auto bufsiz = 16; 2590 const auto bufsiz = 16;
1827 char buf[bufsiz]; 2591 char buf[bufsiz];
1828 2592
@@ -1830,9 +2594,17 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
1830 2594
1831 if (!line_reader.getline()) { return false; } 2595 if (!line_reader.getline()) { return false; }
1832 2596
1833 auto chunk_len = std::stoi(line_reader.ptr(), 0, 16); 2597 unsigned long chunk_len;
2598 while (true) {
2599 char *end_ptr;
2600
2601 chunk_len = std::strtoul(line_reader.ptr(), &end_ptr, 16);
2602
2603 if (end_ptr == line_reader.ptr()) { return false; }
2604 if (chunk_len == ULONG_MAX) { return false; }
2605
2606 if (chunk_len == 0) { break; }
1834 2607
1835 while (chunk_len > 0) {
1836 if (!read_content_with_length(strm, chunk_len, nullptr, out)) { 2608 if (!read_content_with_length(strm, chunk_len, nullptr, out)) {
1837 return false; 2609 return false;
1838 } 2610 }
@@ -1842,8 +2614,6 @@ inline bool read_content_chunked(Stream &strm, ContentReceiver out) {
1842 if (strcmp(line_reader.ptr(), "\r\n")) { break; } 2614 if (strcmp(line_reader.ptr(), "\r\n")) { break; }
1843 2615
1844 if (!line_reader.getline()) { return false; } 2616 if (!line_reader.getline()) { return false; }
1845
1846 chunk_len = std::stoi(line_reader.ptr(), 0, 16);
1847 } 2617 }
1848 2618
1849 if (chunk_len == 0) { 2619 if (chunk_len == 0) {
@@ -1860,62 +2630,91 @@ inline bool is_chunked_transfer_encoding(const Headers &headers) {
1860 "chunked"); 2630 "chunked");
1861} 2631}
1862 2632
1863template <typename T> 2633template <typename T, typename U>
1864bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, 2634bool prepare_content_receiver(T &x, int &status,
1865 Progress progress, ContentReceiver receiver) { 2635 ContentReceiverWithProgress receiver,
1866 2636 bool decompress, U callback) {
1867 ContentReceiver out = [&](const char *buf, size_t n) { 2637 if (decompress) {
1868 return receiver(buf, n); 2638 std::string encoding = x.get_header_value("Content-Encoding");
1869 }; 2639 std::unique_ptr<decompressor> decompressor;
1870 2640
2641 if (encoding.find("gzip") != std::string::npos ||
2642 encoding.find("deflate") != std::string::npos) {
1871#ifdef CPPHTTPLIB_ZLIB_SUPPORT 2643#ifdef CPPHTTPLIB_ZLIB_SUPPORT
1872 decompressor decompressor; 2644 decompressor = detail::make_unique<gzip_decompressor>();
1873
1874 if (!decompressor.is_valid()) {
1875 status = 500;
1876 return false;
1877 }
1878
1879 if (x.get_header_value("Content-Encoding") == "gzip") {
1880 out = [&](const char *buf, size_t n) {
1881 return decompressor.decompress(
1882 buf, n, [&](const char *buf, size_t n) { return receiver(buf, n); });
1883 };
1884 }
1885#else 2645#else
1886 if (x.get_header_value("Content-Encoding") == "gzip") { 2646 status = 415;
1887 status = 415; 2647 return false;
1888 return false;
1889 }
1890#endif 2648#endif
2649 } else if (encoding.find("br") != std::string::npos) {
2650#ifdef CPPHTTPLIB_BROTLI_SUPPORT
2651 decompressor = detail::make_unique<brotli_decompressor>();
2652#else
2653 status = 415;
2654 return false;
2655#endif
2656 }
1891 2657
1892 auto ret = true; 2658 if (decompressor) {
1893 auto exceed_payload_max_length = false; 2659 if (decompressor->is_valid()) {
1894 2660 ContentReceiverWithProgress out = [&](const char *buf, size_t n,
1895 if (is_chunked_transfer_encoding(x.headers)) { 2661 uint64_t off, uint64_t len) {
1896 ret = read_content_chunked(strm, out); 2662 return decompressor->decompress(buf, n,
1897 } else if (!has_header(x.headers, "Content-Length")) { 2663 [&](const char *buf, size_t n) {
1898 ret = read_content_without_length(strm, out); 2664 return receiver(buf, n, off, len);
1899 } else { 2665 });
1900 auto len = get_header_value_uint64(x.headers, "Content-Length", 0); 2666 };
1901 if (len > payload_max_length) { 2667 return callback(std::move(out));
1902 exceed_payload_max_length = true; 2668 } else {
1903 skip_content_with_length(strm, len); 2669 status = 500;
1904 ret = false; 2670 return false;
1905 } else if (len > 0) { 2671 }
1906 ret = read_content_with_length(strm, len, progress, out);
1907 } 2672 }
1908 } 2673 }
1909 2674
1910 if (!ret) { status = exceed_payload_max_length ? 413 : 400; } 2675 ContentReceiverWithProgress out = [&](const char *buf, size_t n, uint64_t off,
2676 uint64_t len) {
2677 return receiver(buf, n, off, len);
2678 };
2679 return callback(std::move(out));
2680}
1911 2681
1912 return ret; 2682template <typename T>
2683bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
2684 Progress progress, ContentReceiverWithProgress receiver,
2685 bool decompress) {
2686 return prepare_content_receiver(
2687 x, status, std::move(receiver), decompress,
2688 [&](const ContentReceiverWithProgress &out) {
2689 auto ret = true;
2690 auto exceed_payload_max_length = false;
2691
2692 if (is_chunked_transfer_encoding(x.headers)) {
2693 ret = read_content_chunked(strm, out);
2694 } else if (!has_header(x.headers, "Content-Length")) {
2695 ret = read_content_without_length(strm, out);
2696 } else {
2697 auto len = get_header_value<uint64_t>(x.headers, "Content-Length");
2698 if (len > payload_max_length) {
2699 exceed_payload_max_length = true;
2700 skip_content_with_length(strm, len);
2701 ret = false;
2702 } else if (len > 0) {
2703 ret = read_content_with_length(strm, len, std::move(progress), out);
2704 }
2705 }
2706
2707 if (!ret) { status = exceed_payload_max_length ? 413 : 400; }
2708 return ret;
2709 });
1913} 2710}
1914 2711
1915template <typename T> 2712template <typename T>
1916inline int write_headers(Stream &strm, const T &info, const Headers &headers) { 2713inline ssize_t write_headers(Stream &strm, const T &info,
1917 auto write_len = 0; 2714 const Headers &headers) {
2715 ssize_t write_len = 0;
1918 for (const auto &x : info.headers) { 2716 for (const auto &x : info.headers) {
2717 if (x.first == "EXCEPTION_WHAT") { continue; }
1919 auto len = 2718 auto len =
1920 strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); 2719 strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str());
1921 if (len < 0) { return len; } 2720 if (len < 0) { return len; }
@@ -1933,57 +2732,150 @@ inline int write_headers(Stream &strm, const T &info, const Headers &headers) {
1933 return write_len; 2732 return write_len;
1934} 2733}
1935 2734
2735inline bool write_data(Stream &strm, const char *d, size_t l) {
2736 size_t offset = 0;
2737 while (offset < l) {
2738 auto length = strm.write(d + offset, l - offset);
2739 if (length < 0) { return false; }
2740 offset += static_cast<size_t>(length);
2741 }
2742 return true;
2743}
2744
2745template <typename T>
1936inline ssize_t write_content(Stream &strm, ContentProvider content_provider, 2746inline ssize_t write_content(Stream &strm, ContentProvider content_provider,
1937 size_t offset, size_t length) { 2747 size_t offset, size_t length, T is_shutting_down) {
1938 size_t begin_offset = offset; 2748 size_t begin_offset = offset;
1939 size_t end_offset = offset + length; 2749 size_t end_offset = offset + length;
1940 while (offset < end_offset) { 2750 auto ok = true;
1941 ssize_t written_length = 0; 2751 DataSink data_sink;
1942 2752
1943 DataSink data_sink; 2753 data_sink.write = [&](const char *d, size_t l) {
1944 data_sink.write = [&](const char *d, size_t l) { 2754 if (ok) {
1945 offset += l; 2755 offset += l;
1946 written_length = strm.write(d, l); 2756 if (!write_data(strm, d, l)) { ok = false; }
1947 }; 2757 }
1948 data_sink.done = [&](void) { written_length = -1; }; 2758 };
1949 data_sink.is_writable = [&](void) { return strm.is_writable(); }; 2759
2760 data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
1950 2761
1951 content_provider(offset, end_offset - offset, data_sink); 2762 while (offset < end_offset && !is_shutting_down()) {
1952 if (written_length < 0) { return written_length; } 2763 if (!content_provider(offset, end_offset - offset, data_sink)) {
2764 return -1;
2765 }
2766 if (!ok) { return -1; }
1953 } 2767 }
2768
1954 return static_cast<ssize_t>(offset - begin_offset); 2769 return static_cast<ssize_t>(offset - begin_offset);
1955} 2770}
1956 2771
1957template <typename T> 2772template <typename T>
2773inline ssize_t write_content_without_length(Stream &strm,
2774 ContentProvider content_provider,
2775 T is_shutting_down) {
2776 size_t offset = 0;
2777 auto data_available = true;
2778 auto ok = true;
2779 DataSink data_sink;
2780
2781 data_sink.write = [&](const char *d, size_t l) {
2782 if (ok) {
2783 offset += l;
2784 if (!write_data(strm, d, l)) { ok = false; }
2785 }
2786 };
2787
2788 data_sink.done = [&](void) { data_available = false; };
2789
2790 data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
2791
2792 while (data_available && !is_shutting_down()) {
2793 if (!content_provider(offset, 0, data_sink)) { return -1; }
2794 if (!ok) { return -1; }
2795 }
2796
2797 return static_cast<ssize_t>(offset);
2798}
2799
2800template <typename T, typename U>
1958inline ssize_t write_content_chunked(Stream &strm, 2801inline ssize_t write_content_chunked(Stream &strm,
1959 ContentProvider content_provider, 2802 ContentProvider content_provider,
1960 T is_shutting_down) { 2803 T is_shutting_down, U &compressor) {
1961 size_t offset = 0; 2804 size_t offset = 0;
1962 auto data_available = true; 2805 auto data_available = true;
1963 ssize_t total_written_length = 0; 2806 ssize_t total_written_length = 0;
1964 while (data_available && !is_shutting_down()) { 2807 auto ok = true;
1965 ssize_t written_length = 0; 2808 DataSink data_sink;
2809
2810 data_sink.write = [&](const char *d, size_t l) {
2811 if (!ok) { return; }
2812
2813 data_available = l > 0;
2814 offset += l;
2815
2816 std::string payload;
2817 if (!compressor.compress(d, l, false,
2818 [&](const char *data, size_t data_len) {
2819 payload.append(data, data_len);
2820 return true;
2821 })) {
2822 ok = false;
2823 return;
2824 }
1966 2825
1967 DataSink data_sink; 2826 if (!payload.empty()) {
1968 data_sink.write = [&](const char *d, size_t l) { 2827 // Emit chunked response header and footer for each chunk
1969 data_available = l > 0; 2828 auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
1970 offset += l; 2829 if (write_data(strm, chunk.data(), chunk.size())) {
2830 total_written_length += chunk.size();
2831 } else {
2832 ok = false;
2833 return;
2834 }
2835 }
2836 };
2837
2838 data_sink.done = [&](void) {
2839 if (!ok) { return; }
1971 2840
2841 data_available = false;
2842
2843 std::string payload;
2844 if (!compressor.compress(nullptr, 0, true,
2845 [&](const char *data, size_t data_len) {
2846 payload.append(data, data_len);
2847 return true;
2848 })) {
2849 ok = false;
2850 return;
2851 }
2852
2853 if (!payload.empty()) {
1972 // Emit chunked response header and footer for each chunk 2854 // Emit chunked response header and footer for each chunk
1973 auto chunk = from_i_to_hex(l) + "\r\n" + std::string(d, l) + "\r\n"; 2855 auto chunk = from_i_to_hex(payload.size()) + "\r\n" + payload + "\r\n";
1974 written_length = strm.write(chunk); 2856 if (write_data(strm, chunk.data(), chunk.size())) {
1975 }; 2857 total_written_length += chunk.size();
1976 data_sink.done = [&](void) { 2858 } else {
1977 data_available = false; 2859 ok = false;
1978 written_length = strm.write("0\r\n\r\n"); 2860 return;
1979 }; 2861 }
1980 data_sink.is_writable = [&](void) { return strm.is_writable(); }; 2862 }
2863
2864 static const std::string done_marker("0\r\n\r\n");
2865 if (write_data(strm, done_marker.data(), done_marker.size())) {
2866 total_written_length += done_marker.size();
2867 } else {
2868 ok = false;
2869 }
2870 };
1981 2871
1982 content_provider(offset, 0, data_sink); 2872 data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
1983 2873
1984 if (written_length < 0) { return written_length; } 2874 while (data_available && !is_shutting_down()) {
1985 total_written_length += written_length; 2875 if (!content_provider(offset, 0, data_sink)) { return -1; }
2876 if (!ok) { return -1; }
1986 } 2877 }
2878
1987 return total_written_length; 2879 return total_written_length;
1988} 2880}
1989 2881
@@ -1994,6 +2886,12 @@ inline bool redirect(T &cli, const Request &req, Response &res,
1994 new_req.path = path; 2886 new_req.path = path;
1995 new_req.redirect_count -= 1; 2887 new_req.redirect_count -= 1;
1996 2888
2889 if (res.status == 303 && (req.method != "GET" && req.method != "HEAD")) {
2890 new_req.method = "GET";
2891 new_req.body.clear();
2892 new_req.headers.clear();
2893 }
2894
1997 Response new_res; 2895 Response new_res;
1998 2896
1999 auto ret = cli.send(new_req, new_res); 2897 auto ret = cli.send(new_req, new_res);
@@ -2001,75 +2899,20 @@ inline bool redirect(T &cli, const Request &req, Response &res,
2001 return ret; 2899 return ret;
2002} 2900}
2003 2901
2004inline std::string encode_url(const std::string &s) { 2902inline std::string params_to_query_str(const Params &params) {
2005 std::string result; 2903 std::string query;
2006
2007 for (auto i = 0; s[i]; i++) {
2008 switch (s[i]) {
2009 case ' ': result += "%20"; break;
2010 case '+': result += "%2B"; break;
2011 case '\r': result += "%0D"; break;
2012 case '\n': result += "%0A"; break;
2013 case '\'': result += "%27"; break;
2014 case ',': result += "%2C"; break;
2015 // case ':': result += "%3A"; break; // ok? probably...
2016 case ';': result += "%3B"; break;
2017 default:
2018 auto c = static_cast<uint8_t>(s[i]);
2019 if (c >= 0x80) {
2020 result += '%';
2021 char hex[4];
2022 size_t len = snprintf(hex, sizeof(hex) - 1, "%02X", c);
2023 assert(len == 2);
2024 result.append(hex, len);
2025 } else {
2026 result += s[i];
2027 }
2028 break;
2029 }
2030 }
2031
2032 return result;
2033}
2034
2035inline std::string decode_url(const std::string &s) {
2036 std::string result;
2037 2904
2038 for (size_t i = 0; i < s.size(); i++) { 2905 for (auto it = params.begin(); it != params.end(); ++it) {
2039 if (s[i] == '%' && i + 1 < s.size()) { 2906 if (it != params.begin()) { query += "&"; }
2040 if (s[i + 1] == 'u') { 2907 query += it->first;
2041 int val = 0; 2908 query += "=";
2042 if (from_hex_to_i(s, i + 2, 4, val)) { 2909 query += encode_url(it->second);
2043 // 4 digits Unicode codes
2044 char buff[4];
2045 size_t len = to_utf8(val, buff);
2046 if (len > 0) { result.append(buff, len); }
2047 i += 5; // 'u0000'
2048 } else {
2049 result += s[i];
2050 }
2051 } else {
2052 int val = 0;
2053 if (from_hex_to_i(s, i + 1, 2, val)) {
2054 // 2 digits hex codes
2055 result += static_cast<char>(val);
2056 i += 2; // '00'
2057 } else {
2058 result += s[i];
2059 }
2060 }
2061 } else if (s[i] == '+') {
2062 result += ' ';
2063 } else {
2064 result += s[i];
2065 }
2066 } 2910 }
2067 2911 return query;
2068 return result;
2069} 2912}
2070 2913
2071inline void parse_query_text(const std::string &s, Params &params) { 2914inline void parse_query_text(const std::string &s, Params &params) {
2072 split(&s[0], &s[s.size()], '&', [&](const char *b, const char *e) { 2915 split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
2073 std::string key; 2916 std::string key;
2074 std::string val; 2917 std::string val;
2075 split(b, e, '=', [&](const char *b2, const char *e2) { 2918 split(b, e, '=', [&](const char *b2, const char *e2) {
@@ -2079,7 +2922,10 @@ inline void parse_query_text(const std::string &s, Params &params) {
2079 val.assign(b2, e2); 2922 val.assign(b2, e2);
2080 } 2923 }
2081 }); 2924 });
2082 params.emplace(key, decode_url(val)); 2925
2926 if (!key.empty()) {
2927 params.emplace(decode_url(key, true), decode_url(val, true));
2928 }
2083 }); 2929 });
2084} 2930}
2085 2931
@@ -2087,17 +2933,20 @@ inline bool parse_multipart_boundary(const std::string &content_type,
2087 std::string &boundary) { 2933 std::string &boundary) {
2088 auto pos = content_type.find("boundary="); 2934 auto pos = content_type.find("boundary=");
2089 if (pos == std::string::npos) { return false; } 2935 if (pos == std::string::npos) { return false; }
2090
2091 boundary = content_type.substr(pos + 9); 2936 boundary = content_type.substr(pos + 9);
2092 return true; 2937 if (boundary.length() >= 2 && boundary.front() == '"' &&
2938 boundary.back() == '"') {
2939 boundary = boundary.substr(1, boundary.size() - 2);
2940 }
2941 return !boundary.empty();
2093} 2942}
2094 2943
2095inline bool parse_range_header(const std::string &s, Ranges &ranges) { 2944inline bool parse_range_header(const std::string &s, Ranges &ranges) try {
2096 static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); 2945 static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
2097 std::smatch m; 2946 std::smatch m;
2098 if (std::regex_match(s, m, re_first_range)) { 2947 if (std::regex_match(s, m, re_first_range)) {
2099 auto pos = m.position(1); 2948 auto pos = static_cast<size_t>(m.position(1));
2100 auto len = m.length(1); 2949 auto len = static_cast<size_t>(m.length(1));
2101 bool all_valid_ranges = true; 2950 bool all_valid_ranges = true;
2102 split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) { 2951 split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
2103 if (!all_valid_ranges) return; 2952 if (!all_valid_ranges) return;
@@ -2124,25 +2973,26 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) {
2124 return all_valid_ranges; 2973 return all_valid_ranges;
2125 } 2974 }
2126 return false; 2975 return false;
2127} 2976} catch (...) { return false; }
2128 2977
2129class MultipartFormDataParser { 2978class MultipartFormDataParser {
2130public: 2979public:
2131 MultipartFormDataParser() {} 2980 MultipartFormDataParser() = default;
2132 2981
2133 void set_boundary(const std::string &boundary) { boundary_ = boundary; } 2982 void set_boundary(std::string &&boundary) { boundary_ = boundary; }
2134 2983
2135 bool is_valid() const { return is_valid_; } 2984 bool is_valid() const { return is_valid_; }
2136 2985
2137 template <typename T, typename U> 2986 template <typename T, typename U>
2138 bool parse(const char *buf, size_t n, T content_callback, U header_callback) { 2987 bool parse(const char *buf, size_t n, const T &content_callback,
2139 static const std::regex re_content_type(R"(^Content-Type:\s*(.*?)\s*$)", 2988 const U &header_callback) {
2140 std::regex_constants::icase);
2141 2989
2142 static const std::regex re_content_disposition( 2990 static const std::regex re_content_disposition(
2143 "^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename=" 2991 "^Content-Disposition:\\s*form-data;\\s*name=\"(.*?)\"(?:;\\s*filename="
2144 "\"(.*?)\")?\\s*$", 2992 "\"(.*?)\")?\\s*$",
2145 std::regex_constants::icase); 2993 std::regex_constants::icase);
2994 static const std::string dash_ = "--";
2995 static const std::string crlf_ = "\r\n";
2146 2996
2147 buf_.append(buf, n); // TODO: performance improvement 2997 buf_.append(buf, n); // TODO: performance improvement
2148 2998
@@ -2152,10 +3002,7 @@ public:
2152 auto pattern = dash_ + boundary_ + crlf_; 3002 auto pattern = dash_ + boundary_ + crlf_;
2153 if (pattern.size() > buf_.size()) { return true; } 3003 if (pattern.size() > buf_.size()) { return true; }
2154 auto pos = buf_.find(pattern); 3004 auto pos = buf_.find(pattern);
2155 if (pos != 0) { 3005 if (pos != 0) { return false; }
2156 is_done_ = true;
2157 return false;
2158 }
2159 buf_.erase(0, pattern.size()); 3006 buf_.erase(0, pattern.size());
2160 off_ += pattern.size(); 3007 off_ += pattern.size();
2161 state_ = 1; 3008 state_ = 1;
@@ -2173,7 +3020,6 @@ public:
2173 if (pos == 0) { 3020 if (pos == 0) {
2174 if (!header_callback(file_)) { 3021 if (!header_callback(file_)) {
2175 is_valid_ = false; 3022 is_valid_ = false;
2176 is_done_ = false;
2177 return false; 3023 return false;
2178 } 3024 }
2179 buf_.erase(0, crlf_.size()); 3025 buf_.erase(0, crlf_.size());
@@ -2182,12 +3028,13 @@ public:
2182 break; 3028 break;
2183 } 3029 }
2184 3030
2185 auto header = buf_.substr(0, pos); 3031 static const std::string header_name = "content-type:";
2186 { 3032 const auto header = buf_.substr(0, pos);
3033 if (start_with(header, header_name)) {
3034 file_.content_type = trim_copy(header.substr(header_name.size()));
3035 } else {
2187 std::smatch m; 3036 std::smatch m;
2188 if (std::regex_match(header, m, re_content_type)) { 3037 if (std::regex_match(header, m, re_content_disposition)) {
2189 file_.content_type = m[1];
2190 } else if (std::regex_match(header, m, re_content_disposition)) {
2191 file_.name = m[1]; 3038 file_.name = m[1];
2192 file_.filename = m[2]; 3039 file_.filename = m[2];
2193 } 3040 }
@@ -2197,6 +3044,7 @@ public:
2197 off_ += pos + crlf_.size(); 3044 off_ += pos + crlf_.size();
2198 pos = buf_.find(crlf_); 3045 pos = buf_.find(crlf_);
2199 } 3046 }
3047 if (state_ != 3) { return true; }
2200 break; 3048 break;
2201 } 3049 }
2202 case 3: { // Body 3050 case 3: { // Body
@@ -2205,10 +3053,17 @@ public:
2205 if (pattern.size() > buf_.size()) { return true; } 3053 if (pattern.size() > buf_.size()) { return true; }
2206 3054
2207 auto pos = buf_.find(pattern); 3055 auto pos = buf_.find(pattern);
2208 if (pos == std::string::npos) { pos = buf_.size(); } 3056 if (pos == std::string::npos) {
3057 pos = buf_.size();
3058 while (pos > 0) {
3059 auto c = buf_[pos - 1];
3060 if (c != '\r' && c != '\n' && c != '-') { break; }
3061 pos--;
3062 }
3063 }
3064
2209 if (!content_callback(buf_.data(), pos)) { 3065 if (!content_callback(buf_.data(), pos)) {
2210 is_valid_ = false; 3066 is_valid_ = false;
2211 is_done_ = false;
2212 return false; 3067 return false;
2213 } 3068 }
2214 3069
@@ -2224,7 +3079,6 @@ public:
2224 if (pos != std::string::npos) { 3079 if (pos != std::string::npos) {
2225 if (!content_callback(buf_.data(), pos)) { 3080 if (!content_callback(buf_.data(), pos)) {
2226 is_valid_ = false; 3081 is_valid_ = false;
2227 is_done_ = false;
2228 return false; 3082 return false;
2229 } 3083 }
2230 3084
@@ -2234,7 +3088,6 @@ public:
2234 } else { 3088 } else {
2235 if (!content_callback(buf_.data(), pattern.size())) { 3089 if (!content_callback(buf_.data(), pattern.size())) {
2236 is_valid_ = false; 3090 is_valid_ = false;
2237 is_done_ = false;
2238 return false; 3091 return false;
2239 } 3092 }
2240 3093
@@ -2246,20 +3099,19 @@ public:
2246 } 3099 }
2247 case 4: { // Boundary 3100 case 4: { // Boundary
2248 if (crlf_.size() > buf_.size()) { return true; } 3101 if (crlf_.size() > buf_.size()) { return true; }
2249 if (buf_.find(crlf_) == 0) { 3102 if (buf_.compare(0, crlf_.size(), crlf_) == 0) {
2250 buf_.erase(0, crlf_.size()); 3103 buf_.erase(0, crlf_.size());
2251 off_ += crlf_.size(); 3104 off_ += crlf_.size();
2252 state_ = 1; 3105 state_ = 1;
2253 } else { 3106 } else {
2254 auto pattern = dash_ + crlf_; 3107 auto pattern = dash_ + crlf_;
2255 if (pattern.size() > buf_.size()) { return true; } 3108 if (pattern.size() > buf_.size()) { return true; }
2256 if (buf_.find(pattern) == 0) { 3109 if (buf_.compare(0, pattern.size(), pattern) == 0) {
2257 buf_.erase(0, pattern.size()); 3110 buf_.erase(0, pattern.size());
2258 off_ += pattern.size(); 3111 off_ += pattern.size();
2259 is_valid_ = true; 3112 is_valid_ = true;
2260 state_ = 5; 3113 state_ = 5;
2261 } else { 3114 } else {
2262 is_done_ = true;
2263 return true; 3115 return true;
2264 } 3116 }
2265 } 3117 }
@@ -2282,14 +3134,11 @@ private:
2282 file_.content_type.clear(); 3134 file_.content_type.clear();
2283 } 3135 }
2284 3136
2285 const std::string dash_ = "--";
2286 const std::string crlf_ = "\r\n";
2287 std::string boundary_; 3137 std::string boundary_;
2288 3138
2289 std::string buf_; 3139 std::string buf_;
2290 size_t state_ = 0; 3140 size_t state_ = 0;
2291 size_t is_valid_ = false; 3141 bool is_valid_ = false;
2292 size_t is_done_ = false;
2293 size_t off_ = 0; 3142 size_t off_ = 0;
2294 MultipartFormData file_; 3143 MultipartFormData file_;
2295}; 3144};
@@ -2308,8 +3157,13 @@ inline std::string make_multipart_data_boundary() {
2308 static const char data[] = 3157 static const char data[] =
2309 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 3158 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
2310 3159
3160 // std::random_device might actually be deterministic on some
3161 // platforms, but due to lack of support in the c++ standard library,
3162 // doing better requires either some ugly hacks or breaking portability.
2311 std::random_device seed_gen; 3163 std::random_device seed_gen;
2312 std::mt19937 engine(seed_gen()); 3164 // Request 128 bits of entropy for initialization
3165 std::seed_seq seed_sequence{seed_gen(), seed_gen(), seed_gen(), seed_gen()};
3166 std::mt19937 engine(seed_sequence);
2313 3167
2314 std::string result = "--cpp-httplib-multipart-data-"; 3168 std::string result = "--cpp-httplib-multipart-data-";
2315 3169
@@ -2329,12 +3183,14 @@ get_range_offset_and_length(const Request &req, size_t content_length,
2329 return std::make_pair(0, content_length); 3183 return std::make_pair(0, content_length);
2330 } 3184 }
2331 3185
3186 auto slen = static_cast<ssize_t>(content_length);
3187
2332 if (r.first == -1) { 3188 if (r.first == -1) {
2333 r.first = content_length - r.second; 3189 r.first = (std::max)(static_cast<ssize_t>(0), slen - r.second);
2334 r.second = content_length - 1; 3190 r.second = slen - 1;
2335 } 3191 }
2336 3192
2337 if (r.second == -1) { r.second = content_length - 1; } 3193 if (r.second == -1) { r.second = slen - 1; }
2338 3194
2339 return std::make_pair(r.first, r.second - r.first + 1); 3195 return std::make_pair(r.first, r.second - r.first + 1);
2340} 3196}
@@ -2420,16 +3276,19 @@ get_multipart_ranges_data_length(const Request &req, Response &res,
2420 return data_length; 3276 return data_length;
2421} 3277}
2422 3278
3279template <typename T>
2423inline bool write_multipart_ranges_data(Stream &strm, const Request &req, 3280inline bool write_multipart_ranges_data(Stream &strm, const Request &req,
2424 Response &res, 3281 Response &res,
2425 const std::string &boundary, 3282 const std::string &boundary,
2426 const std::string &content_type) { 3283 const std::string &content_type,
3284 T is_shutting_down) {
2427 return process_multipart_ranges_data( 3285 return process_multipart_ranges_data(
2428 req, res, boundary, content_type, 3286 req, res, boundary, content_type,
2429 [&](const std::string &token) { strm.write(token); }, 3287 [&](const std::string &token) { strm.write(token); },
2430 [&](const char *token) { strm.write(token); }, 3288 [&](const char *token) { strm.write(token); },
2431 [&](size_t offset, size_t length) { 3289 [&](size_t offset, size_t length) {
2432 return write_content(strm, res.content_provider, offset, length) >= 0; 3290 return write_content(strm, res.content_provider_, offset, length,
3291 is_shutting_down) >= 0;
2433 }); 3292 });
2434} 3293}
2435 3294
@@ -2438,20 +3297,31 @@ get_range_offset_and_length(const Request &req, const Response &res,
2438 size_t index) { 3297 size_t index) {
2439 auto r = req.ranges[index]; 3298 auto r = req.ranges[index];
2440 3299
2441 if (r.second == -1) { r.second = res.content_length - 1; } 3300 if (r.second == -1) {
3301 r.second = static_cast<ssize_t>(res.content_length_) - 1;
3302 }
2442 3303
2443 return std::make_pair(r.first, r.second - r.first + 1); 3304 return std::make_pair(r.first, r.second - r.first + 1);
2444} 3305}
2445 3306
2446inline bool expect_content(const Request &req) { 3307inline bool expect_content(const Request &req) {
2447 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" || 3308 if (req.method == "POST" || req.method == "PUT" || req.method == "PATCH" ||
2448 req.method == "PRI") { 3309 req.method == "PRI" || req.method == "DELETE") {
2449 return true; 3310 return true;
2450 } 3311 }
2451 // TODO: check if Content-Length is set 3312 // TODO: check if Content-Length is set
2452 return false; 3313 return false;
2453} 3314}
2454 3315
3316inline bool has_crlf(const char *s) {
3317 auto p = s;
3318 while (*p) {
3319 if (*p == '\r' || *p == '\n') { return true; }
3320 p++;
3321 }
3322 return false;
3323}
3324
2455#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 3325#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2456template <typename CTX, typename Init, typename Update, typename Final> 3326template <typename CTX, typename Init, typename Update, typename Final>
2457inline std::string message_digest(const std::string &s, Init init, 3327inline std::string message_digest(const std::string &s, Init init,
@@ -2489,6 +3359,33 @@ inline std::string SHA_512(const std::string &s) {
2489#endif 3359#endif
2490 3360
2491#ifdef _WIN32 3361#ifdef _WIN32
3362#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3363// NOTE: This code came up with the following stackoverflow post:
3364// https://stackoverflow.com/questions/9507184/can-openssl-on-windows-use-the-system-certificate-store
3365inline bool load_system_certs_on_windows(X509_STORE *store) {
3366 auto hStore = CertOpenSystemStoreW((HCRYPTPROV_LEGACY)NULL, L"ROOT");
3367
3368 if (!hStore) { return false; }
3369
3370 PCCERT_CONTEXT pContext = NULL;
3371 while (pContext = CertEnumCertificatesInStore(hStore, pContext)) {
3372 auto encoded_cert =
3373 static_cast<const unsigned char *>(pContext->pbCertEncoded);
3374
3375 auto x509 = d2i_X509(NULL, &encoded_cert, pContext->cbCertEncoded);
3376 if (x509) {
3377 X509_STORE_add_cert(store, x509);
3378 X509_free(x509);
3379 }
3380 }
3381
3382 CertFreeCertificateContext(pContext);
3383 CertCloseStore(hStore, 0);
3384
3385 return true;
3386}
3387#endif
3388
2492class WSInit { 3389class WSInit {
2493public: 3390public:
2494 WSInit() { 3391 WSInit() {
@@ -2502,31 +3399,6 @@ public:
2502static WSInit wsinit_; 3399static WSInit wsinit_;
2503#endif 3400#endif
2504 3401
2505} // namespace detail
2506
2507// Header utilities
2508inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
2509 std::string field = "bytes=";
2510 auto i = 0;
2511 for (auto r : ranges) {
2512 if (i != 0) { field += ", "; }
2513 if (r.first != -1) { field += std::to_string(r.first); }
2514 field += '-';
2515 if (r.second != -1) { field += std::to_string(r.second); }
2516 i++;
2517 }
2518 return std::make_pair("Range", field);
2519}
2520
2521inline std::pair<std::string, std::string>
2522make_basic_authentication_header(const std::string &username,
2523 const std::string &password,
2524 bool is_proxy = false) {
2525 auto field = "Basic " + detail::base64_encode(username + ":" + password);
2526 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2527 return std::make_pair(key, field);
2528}
2529
2530#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 3402#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2531inline std::pair<std::string, std::string> make_digest_authentication_header( 3403inline std::pair<std::string, std::string> make_digest_authentication_header(
2532 const Request &req, const std::map<std::string, std::string> &auth, 3404 const Request &req, const std::map<std::string, std::string> &auth,
@@ -2566,17 +3438,18 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
2566 ":" + qop + ":" + H(A2)); 3438 ":" + qop + ":" + H(A2));
2567 } 3439 }
2568 3440
2569 auto field = "Digest username=\"hello\", realm=\"" + auth.at("realm") + 3441 auto field = "Digest username=\"" + username + "\", realm=\"" +
2570 "\", nonce=\"" + auth.at("nonce") + "\", uri=\"" + req.path + 3442 auth.at("realm") + "\", nonce=\"" + auth.at("nonce") +
2571 "\", algorithm=" + algo + ", qop=" + qop + ", nc=\"" + nc + 3443 "\", uri=\"" + req.path + "\", algorithm=" + algo +
2572 "\", cnonce=\"" + cnonce + "\", response=\"" + response + "\""; 3444 ", qop=" + qop + ", nc=\"" + nc + "\", cnonce=\"" + cnonce +
3445 "\", response=\"" + response + "\"";
2573 3446
2574 auto key = is_proxy ? "Proxy-Authorization" : "Authorization"; 3447 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
2575 return std::make_pair(key, field); 3448 return std::make_pair(key, field);
2576} 3449}
2577#endif 3450#endif
2578 3451
2579inline bool parse_www_authenticate(const httplib::Response &res, 3452inline bool parse_www_authenticate(const Response &res,
2580 std::map<std::string, std::string> &auth, 3453 std::map<std::string, std::string> &auth,
2581 bool is_proxy) { 3454 bool is_proxy) {
2582 auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate"; 3455 auto auth_key = is_proxy ? "Proxy-Authenticate" : "WWW-Authenticate";
@@ -2593,9 +3466,13 @@ inline bool parse_www_authenticate(const httplib::Response &res,
2593 auto beg = std::sregex_iterator(s.begin(), s.end(), re); 3466 auto beg = std::sregex_iterator(s.begin(), s.end(), re);
2594 for (auto i = beg; i != std::sregex_iterator(); ++i) { 3467 for (auto i = beg; i != std::sregex_iterator(); ++i) {
2595 auto m = *i; 3468 auto m = *i;
2596 auto key = s.substr(m.position(1), m.length(1)); 3469 auto key = s.substr(static_cast<size_t>(m.position(1)),
2597 auto val = m.length(2) > 0 ? s.substr(m.position(2), m.length(2)) 3470 static_cast<size_t>(m.length(1)));
2598 : s.substr(m.position(3), m.length(3)); 3471 auto val = m.length(2) > 0
3472 ? s.substr(static_cast<size_t>(m.position(2)),
3473 static_cast<size_t>(m.length(2)))
3474 : s.substr(static_cast<size_t>(m.position(3)),
3475 static_cast<size_t>(m.length(3)));
2599 auth[key] = val; 3476 auth[key] = val;
2600 } 3477 }
2601 return true; 3478 return true;
@@ -2612,13 +3489,60 @@ inline std::string random_string(size_t length) {
2612 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 3489 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2613 "abcdefghijklmnopqrstuvwxyz"; 3490 "abcdefghijklmnopqrstuvwxyz";
2614 const size_t max_index = (sizeof(charset) - 1); 3491 const size_t max_index = (sizeof(charset) - 1);
2615 return charset[rand() % max_index]; 3492 return charset[static_cast<size_t>(rand()) % max_index];
2616 }; 3493 };
2617 std::string str(length, 0); 3494 std::string str(length, 0);
2618 std::generate_n(str.begin(), length, randchar); 3495 std::generate_n(str.begin(), length, randchar);
2619 return str; 3496 return str;
2620} 3497}
2621 3498
3499class ContentProviderAdapter {
3500public:
3501 explicit ContentProviderAdapter(
3502 ContentProviderWithoutLength &&content_provider)
3503 : content_provider_(content_provider) {}
3504
3505 bool operator()(size_t offset, size_t, DataSink &sink) {
3506 return content_provider_(offset, sink);
3507 }
3508
3509private:
3510 ContentProviderWithoutLength content_provider_;
3511};
3512
3513} // namespace detail
3514
3515// Header utilities
3516inline std::pair<std::string, std::string> make_range_header(Ranges ranges) {
3517 std::string field = "bytes=";
3518 auto i = 0;
3519 for (auto r : ranges) {
3520 if (i != 0) { field += ", "; }
3521 if (r.first != -1) { field += std::to_string(r.first); }
3522 field += '-';
3523 if (r.second != -1) { field += std::to_string(r.second); }
3524 i++;
3525 }
3526 return std::make_pair("Range", std::move(field));
3527}
3528
3529inline std::pair<std::string, std::string>
3530make_basic_authentication_header(const std::string &username,
3531 const std::string &password,
3532 bool is_proxy = false) {
3533 auto field = "Basic " + detail::base64_encode(username + ":" + password);
3534 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
3535 return std::make_pair(key, std::move(field));
3536}
3537
3538inline std::pair<std::string, std::string>
3539make_bearer_token_authentication_header(const std::string &token,
3540 bool is_proxy = false) {
3541 auto field = "Bearer " + token;
3542 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
3543 return std::make_pair(key, std::move(field));
3544}
3545
2622// Request implementation 3546// Request implementation
2623inline bool Request::has_header(const char *key) const { 3547inline bool Request::has_header(const char *key) const {
2624 return detail::has_header(headers, key); 3548 return detail::has_header(headers, key);
@@ -2628,17 +3552,26 @@ inline std::string Request::get_header_value(const char *key, size_t id) const {
2628 return detail::get_header_value(headers, key, id, ""); 3552 return detail::get_header_value(headers, key, id, "");
2629} 3553}
2630 3554
3555template <typename T>
3556inline T Request::get_header_value(const char *key, size_t id) const {
3557 return detail::get_header_value<T>(headers, key, id, 0);
3558}
3559
2631inline size_t Request::get_header_value_count(const char *key) const { 3560inline size_t Request::get_header_value_count(const char *key) const {
2632 auto r = headers.equal_range(key); 3561 auto r = headers.equal_range(key);
2633 return std::distance(r.first, r.second); 3562 return static_cast<size_t>(std::distance(r.first, r.second));
2634} 3563}
2635 3564
2636inline void Request::set_header(const char *key, const char *val) { 3565inline void Request::set_header(const char *key, const char *val) {
2637 headers.emplace(key, val); 3566 if (!detail::has_crlf(key) && !detail::has_crlf(val)) {
3567 headers.emplace(key, val);
3568 }
2638} 3569}
2639 3570
2640inline void Request::set_header(const char *key, const std::string &val) { 3571inline void Request::set_header(const char *key, const std::string &val) {
2641 headers.emplace(key, val); 3572 if (!detail::has_crlf(key) && !detail::has_crlf(val.c_str())) {
3573 headers.emplace(key, val);
3574 }
2642} 3575}
2643 3576
2644inline bool Request::has_param(const char *key) const { 3577inline bool Request::has_param(const char *key) const {
@@ -2646,15 +3579,16 @@ inline bool Request::has_param(const char *key) const {
2646} 3579}
2647 3580
2648inline std::string Request::get_param_value(const char *key, size_t id) const { 3581inline std::string Request::get_param_value(const char *key, size_t id) const {
2649 auto it = params.find(key); 3582 auto rng = params.equal_range(key);
2650 std::advance(it, id); 3583 auto it = rng.first;
2651 if (it != params.end()) { return it->second; } 3584 std::advance(it, static_cast<ssize_t>(id));
3585 if (it != rng.second) { return it->second; }
2652 return std::string(); 3586 return std::string();
2653} 3587}
2654 3588
2655inline size_t Request::get_param_value_count(const char *key) const { 3589inline size_t Request::get_param_value_count(const char *key) const {
2656 auto r = params.equal_range(key); 3590 auto r = params.equal_range(key);
2657 return std::distance(r.first, r.second); 3591 return static_cast<size_t>(std::distance(r.first, r.second));
2658} 3592}
2659 3593
2660inline bool Request::is_multipart_form_data() const { 3594inline bool Request::is_multipart_form_data() const {
@@ -2682,22 +3616,41 @@ inline std::string Response::get_header_value(const char *key,
2682 return detail::get_header_value(headers, key, id, ""); 3616 return detail::get_header_value(headers, key, id, "");
2683} 3617}
2684 3618
3619template <typename T>
3620inline T Response::get_header_value(const char *key, size_t id) const {
3621 return detail::get_header_value<T>(headers, key, id, 0);
3622}
3623
2685inline size_t Response::get_header_value_count(const char *key) const { 3624inline size_t Response::get_header_value_count(const char *key) const {
2686 auto r = headers.equal_range(key); 3625 auto r = headers.equal_range(key);
2687 return std::distance(r.first, r.second); 3626 return static_cast<size_t>(std::distance(r.first, r.second));
2688} 3627}
2689 3628
2690inline void Response::set_header(const char *key, const char *val) { 3629inline void Response::set_header(const char *key, const char *val) {
2691 headers.emplace(key, val); 3630 if (!detail::has_crlf(key) && !detail::has_crlf(val)) {
3631 headers.emplace(key, val);
3632 }
2692} 3633}
2693 3634
2694inline void Response::set_header(const char *key, const std::string &val) { 3635inline void Response::set_header(const char *key, const std::string &val) {
2695 headers.emplace(key, val); 3636 if (!detail::has_crlf(key) && !detail::has_crlf(val.c_str())) {
3637 headers.emplace(key, val);
3638 }
3639}
3640
3641inline void Response::set_redirect(const char *url, int stat) {
3642 if (!detail::has_crlf(url)) {
3643 set_header("Location", url);
3644 if (300 <= stat && stat < 400) {
3645 this->status = stat;
3646 } else {
3647 this->status = 302;
3648 }
3649 }
2696} 3650}
2697 3651
2698inline void Response::set_redirect(const char *url) { 3652inline void Response::set_redirect(const std::string &url, int stat) {
2699 set_header("Location", url); 3653 set_redirect(url.c_str(), stat);
2700 status = 302;
2701} 3654}
2702 3655
2703inline void Response::set_content(const char *s, size_t n, 3656inline void Response::set_content(const char *s, size_t n,
@@ -2706,62 +3659,79 @@ inline void Response::set_content(const char *s, size_t n,
2706 set_header("Content-Type", content_type); 3659 set_header("Content-Type", content_type);
2707} 3660}
2708 3661
2709inline void Response::set_content(const std::string &s, 3662inline void Response::set_content(std::string s, const char *content_type) {
2710 const char *content_type) { 3663 body = std::move(s);
2711 body = s;
2712 set_header("Content-Type", content_type); 3664 set_header("Content-Type", content_type);
2713} 3665}
2714 3666
2715inline void Response::set_content_provider( 3667inline void
2716 size_t in_length, 3668Response::set_content_provider(size_t in_length, const char *content_type,
2717 std::function<void(size_t offset, size_t length, DataSink &sink)> provider, 3669 ContentProvider provider,
2718 std::function<void()> resource_releaser) { 3670 const std::function<void()> &resource_releaser) {
2719 assert(in_length > 0); 3671 assert(in_length > 0);
2720 content_length = in_length; 3672 set_header("Content-Type", content_type);
2721 content_provider = [provider](size_t offset, size_t length, DataSink &sink) { 3673 content_length_ = in_length;
2722 provider(offset, length, sink); 3674 content_provider_ = std::move(provider);
2723 }; 3675 content_provider_resource_releaser_ = resource_releaser;
2724 content_provider_resource_releaser = resource_releaser; 3676 is_chunked_content_provider = false;
3677}
3678
3679inline void
3680Response::set_content_provider(const char *content_type,
3681 ContentProviderWithoutLength provider,
3682 const std::function<void()> &resource_releaser) {
3683 set_header("Content-Type", content_type);
3684 content_length_ = 0;
3685 content_provider_ = detail::ContentProviderAdapter(std::move(provider));
3686 content_provider_resource_releaser_ = resource_releaser;
3687 is_chunked_content_provider = false;
2725} 3688}
2726 3689
2727inline void Response::set_chunked_content_provider( 3690inline void Response::set_chunked_content_provider(
2728 std::function<void(size_t offset, DataSink &sink)> provider, 3691 const char *content_type, ContentProviderWithoutLength provider,
2729 std::function<void()> resource_releaser) { 3692 const std::function<void()> &resource_releaser) {
2730 content_length = 0; 3693 set_header("Content-Type", content_type);
2731 content_provider = [provider](size_t offset, size_t, DataSink &sink) { 3694 content_length_ = 0;
2732 provider(offset, sink); 3695 content_provider_ = detail::ContentProviderAdapter(std::move(provider));
2733 }; 3696 content_provider_resource_releaser_ = resource_releaser;
2734 content_provider_resource_releaser = resource_releaser; 3697 is_chunked_content_provider = true;
2735} 3698}
2736 3699
2737// Rstream implementation 3700// Rstream implementation
2738inline int Stream::write(const char *ptr) { return write(ptr, strlen(ptr)); } 3701inline ssize_t Stream::write(const char *ptr) {
3702 return write(ptr, strlen(ptr));
3703}
2739 3704
2740inline int Stream::write(const std::string &s) { 3705inline ssize_t Stream::write(const std::string &s) {
2741 return write(s.data(), s.size()); 3706 return write(s.data(), s.size());
2742} 3707}
2743 3708
2744template <typename... Args> 3709template <typename... Args>
2745inline int Stream::write_format(const char *fmt, const Args &... args) { 3710inline ssize_t Stream::write_format(const char *fmt, const Args &... args) {
2746 std::array<char, 2048> buf; 3711 const auto bufsiz = 2048;
3712 std::array<char, bufsiz> buf;
2747 3713
2748#if defined(_MSC_VER) && _MSC_VER < 1900 3714#if defined(_MSC_VER) && _MSC_VER < 1900
2749 auto n = _snprintf_s(buf, bufsiz, buf.size() - 1, fmt, args...); 3715 auto sn = _snprintf_s(buf.data(), bufsiz - 1, buf.size() - 1, fmt, args...);
2750#else 3716#else
2751 auto n = snprintf(buf.data(), buf.size() - 1, fmt, args...); 3717 auto sn = snprintf(buf.data(), buf.size() - 1, fmt, args...);
2752#endif 3718#endif
2753 if (n <= 0) { return n; } 3719 if (sn <= 0) { return sn; }
2754 3720
2755 if (n >= static_cast<int>(buf.size()) - 1) { 3721 auto n = static_cast<size_t>(sn);
3722
3723 if (n >= buf.size() - 1) {
2756 std::vector<char> glowable_buf(buf.size()); 3724 std::vector<char> glowable_buf(buf.size());
2757 3725
2758 while (n >= static_cast<int>(glowable_buf.size() - 1)) { 3726 while (n >= glowable_buf.size() - 1) {
2759 glowable_buf.resize(glowable_buf.size() * 2); 3727 glowable_buf.resize(glowable_buf.size() * 2);
2760#if defined(_MSC_VER) && _MSC_VER < 1900 3728#if defined(_MSC_VER) && _MSC_VER < 1900
2761 n = _snprintf_s(&glowable_buf[0], glowable_buf.size(), 3729 n = static_cast<size_t>(_snprintf_s(&glowable_buf[0], glowable_buf.size(),
2762 glowable_buf.size() - 1, fmt, args...); 3730 glowable_buf.size() - 1, fmt,
3731 args...));
2763#else 3732#else
2764 n = snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...); 3733 n = static_cast<size_t>(
3734 snprintf(&glowable_buf[0], glowable_buf.size() - 1, fmt, args...));
2765#endif 3735#endif
2766 } 3736 }
2767 return write(&glowable_buf[0], n); 3737 return write(&glowable_buf[0], n);
@@ -2774,32 +3744,53 @@ namespace detail {
2774 3744
2775// Socket stream implementation 3745// Socket stream implementation
2776inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec, 3746inline SocketStream::SocketStream(socket_t sock, time_t read_timeout_sec,
2777 time_t read_timeout_usec) 3747 time_t read_timeout_usec,
3748 time_t write_timeout_sec,
3749 time_t write_timeout_usec)
2778 : sock_(sock), read_timeout_sec_(read_timeout_sec), 3750 : sock_(sock), read_timeout_sec_(read_timeout_sec),
2779 read_timeout_usec_(read_timeout_usec) {} 3751 read_timeout_usec_(read_timeout_usec),
3752 write_timeout_sec_(write_timeout_sec),
3753 write_timeout_usec_(write_timeout_usec) {}
2780 3754
2781inline SocketStream::~SocketStream() {} 3755inline SocketStream::~SocketStream() {}
2782 3756
2783inline bool SocketStream::is_readable() const { 3757inline bool SocketStream::is_readable() const {
2784 return detail::select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0; 3758 return select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0;
2785} 3759}
2786 3760
2787inline bool SocketStream::is_writable() const { 3761inline bool SocketStream::is_writable() const {
2788 return detail::select_write(sock_, 0, 0) > 0; 3762 return select_write(sock_, write_timeout_sec_, write_timeout_usec_) > 0;
2789} 3763}
2790 3764
2791inline int SocketStream::read(char *ptr, size_t size) { 3765inline ssize_t SocketStream::read(char *ptr, size_t size) {
2792 if (is_readable()) { return recv(sock_, ptr, static_cast<int>(size), 0); } 3766 if (!is_readable()) { return -1; }
2793 return -1; 3767
3768#ifdef _WIN32
3769 if (size > static_cast<size_t>((std::numeric_limits<int>::max)())) {
3770 return -1;
3771 }
3772 return recv(sock_, ptr, static_cast<int>(size), 0);
3773#else
3774 return handle_EINTR([&]() { return recv(sock_, ptr, size, 0); });
3775#endif
2794} 3776}
2795 3777
2796inline int SocketStream::write(const char *ptr, size_t size) { 3778inline ssize_t SocketStream::write(const char *ptr, size_t size) {
2797 if (is_writable()) { return send(sock_, ptr, static_cast<int>(size), 0); } 3779 if (!is_writable()) { return -1; }
2798 return -1; 3780
3781#ifdef _WIN32
3782 if (size > static_cast<size_t>((std::numeric_limits<int>::max)())) {
3783 return -1;
3784 }
3785 return send(sock_, ptr, static_cast<int>(size), 0);
3786#else
3787 return handle_EINTR([&]() { return send(sock_, ptr, size, 0); });
3788#endif
2799} 3789}
2800 3790
2801inline std::string SocketStream::get_remote_addr() const { 3791inline void SocketStream::get_remote_ip_and_port(std::string &ip,
2802 return detail::get_remote_addr(sock_); 3792 int &port) const {
3793 return detail::get_remote_ip_and_port(sock_, ip, port);
2803} 3794}
2804 3795
2805// Buffer stream implementation 3796// Buffer stream implementation
@@ -2807,22 +3798,23 @@ inline bool BufferStream::is_readable() const { return true; }
2807 3798
2808inline bool BufferStream::is_writable() const { return true; } 3799inline bool BufferStream::is_writable() const { return true; }
2809 3800
2810inline int BufferStream::read(char *ptr, size_t size) { 3801inline ssize_t BufferStream::read(char *ptr, size_t size) {
2811#if defined(_MSC_VER) && _MSC_VER < 1900 3802#if defined(_MSC_VER) && _MSC_VER <= 1900
2812 int len_read = static_cast<int>(buffer._Copy_s(ptr, size, size, position)); 3803 auto len_read = buffer._Copy_s(ptr, size, size, position);
2813#else 3804#else
2814 int len_read = static_cast<int>(buffer.copy(ptr, size, position)); 3805 auto len_read = buffer.copy(ptr, size, position);
2815#endif 3806#endif
2816 position += len_read; 3807 position += static_cast<size_t>(len_read);
2817 return len_read; 3808 return static_cast<ssize_t>(len_read);
2818} 3809}
2819 3810
2820inline int BufferStream::write(const char *ptr, size_t size) { 3811inline ssize_t BufferStream::write(const char *ptr, size_t size) {
2821 buffer.append(ptr, size); 3812 buffer.append(ptr, size);
2822 return static_cast<int>(size); 3813 return static_cast<ssize_t>(size);
2823} 3814}
2824 3815
2825inline std::string BufferStream::get_remote_addr() const { return ""; } 3816inline void BufferStream::get_remote_ip_and_port(std::string & /*ip*/,
3817 int & /*port*/) const {}
2826 3818
2827inline const std::string &BufferStream::get_buffer() const { return buffer; } 3819inline const std::string &BufferStream::get_buffer() const { return buffer; }
2828 3820
@@ -2830,67 +3822,77 @@ inline const std::string &BufferStream::get_buffer() const { return buffer; }
2830 3822
2831// HTTP server implementation 3823// HTTP server implementation
2832inline Server::Server() 3824inline Server::Server()
2833 : keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT), 3825 : new_task_queue(
2834 read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND), 3826 [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); }),
2835 read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND), 3827 svr_sock_(INVALID_SOCKET), is_running_(false) {
2836 payload_max_length_(CPPHTTPLIB_PAYLOAD_MAX_LENGTH), is_running_(false),
2837 svr_sock_(INVALID_SOCKET) {
2838#ifndef _WIN32 3828#ifndef _WIN32
2839 signal(SIGPIPE, SIG_IGN); 3829 signal(SIGPIPE, SIG_IGN);
2840#endif 3830#endif
2841 new_task_queue = [] { return new ThreadPool(CPPHTTPLIB_THREAD_POOL_COUNT); };
2842} 3831}
2843 3832
2844inline Server::~Server() {} 3833inline Server::~Server() {}
2845 3834
2846inline Server &Server::Get(const char *pattern, Handler handler) { 3835inline Server &Server::Get(const char *pattern, Handler handler) {
2847 get_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3836 get_handlers_.push_back(
3837 std::make_pair(std::regex(pattern), std::move(handler)));
2848 return *this; 3838 return *this;
2849} 3839}
2850 3840
2851inline Server &Server::Post(const char *pattern, Handler handler) { 3841inline Server &Server::Post(const char *pattern, Handler handler) {
2852 post_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3842 post_handlers_.push_back(
3843 std::make_pair(std::regex(pattern), std::move(handler)));
2853 return *this; 3844 return *this;
2854} 3845}
2855 3846
2856inline Server &Server::Post(const char *pattern, 3847inline Server &Server::Post(const char *pattern,
2857 HandlerWithContentReader handler) { 3848 HandlerWithContentReader handler) {
2858 post_handlers_for_content_reader_.push_back( 3849 post_handlers_for_content_reader_.push_back(
2859 std::make_pair(std::regex(pattern), handler)); 3850 std::make_pair(std::regex(pattern), std::move(handler)));
2860 return *this; 3851 return *this;
2861} 3852}
2862 3853
2863inline Server &Server::Put(const char *pattern, Handler handler) { 3854inline Server &Server::Put(const char *pattern, Handler handler) {
2864 put_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3855 put_handlers_.push_back(
3856 std::make_pair(std::regex(pattern), std::move(handler)));
2865 return *this; 3857 return *this;
2866} 3858}
2867 3859
2868inline Server &Server::Put(const char *pattern, 3860inline Server &Server::Put(const char *pattern,
2869 HandlerWithContentReader handler) { 3861 HandlerWithContentReader handler) {
2870 put_handlers_for_content_reader_.push_back( 3862 put_handlers_for_content_reader_.push_back(
2871 std::make_pair(std::regex(pattern), handler)); 3863 std::make_pair(std::regex(pattern), std::move(handler)));
2872 return *this; 3864 return *this;
2873} 3865}
2874 3866
2875inline Server &Server::Patch(const char *pattern, Handler handler) { 3867inline Server &Server::Patch(const char *pattern, Handler handler) {
2876 patch_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3868 patch_handlers_.push_back(
3869 std::make_pair(std::regex(pattern), std::move(handler)));
2877 return *this; 3870 return *this;
2878} 3871}
2879 3872
2880inline Server &Server::Patch(const char *pattern, 3873inline Server &Server::Patch(const char *pattern,
2881 HandlerWithContentReader handler) { 3874 HandlerWithContentReader handler) {
2882 patch_handlers_for_content_reader_.push_back( 3875 patch_handlers_for_content_reader_.push_back(
2883 std::make_pair(std::regex(pattern), handler)); 3876 std::make_pair(std::regex(pattern), std::move(handler)));
2884 return *this; 3877 return *this;
2885} 3878}
2886 3879
2887inline Server &Server::Delete(const char *pattern, Handler handler) { 3880inline Server &Server::Delete(const char *pattern, Handler handler) {
2888 delete_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3881 delete_handlers_.push_back(
3882 std::make_pair(std::regex(pattern), std::move(handler)));
3883 return *this;
3884}
3885
3886inline Server &Server::Delete(const char *pattern,
3887 HandlerWithContentReader handler) {
3888 delete_handlers_for_content_reader_.push_back(
3889 std::make_pair(std::regex(pattern), std::move(handler)));
2889 return *this; 3890 return *this;
2890} 3891}
2891 3892
2892inline Server &Server::Options(const char *pattern, Handler handler) { 3893inline Server &Server::Options(const char *pattern, Handler handler) {
2893 options_handlers_.push_back(std::make_pair(std::regex(pattern), handler)); 3894 options_handlers_.push_back(
3895 std::make_pair(std::regex(pattern), std::move(handler)));
2894 return *this; 3896 return *this;
2895} 3897}
2896 3898
@@ -2898,11 +3900,12 @@ inline bool Server::set_base_dir(const char *dir, const char *mount_point) {
2898 return set_mount_point(mount_point, dir); 3900 return set_mount_point(mount_point, dir);
2899} 3901}
2900 3902
2901inline bool Server::set_mount_point(const char *mount_point, const char *dir) { 3903inline bool Server::set_mount_point(const char *mount_point, const char *dir,
3904 Headers headers) {
2902 if (detail::is_dir(dir)) { 3905 if (detail::is_dir(dir)) {
2903 std::string mnt = mount_point ? mount_point : "/"; 3906 std::string mnt = mount_point ? mount_point : "/";
2904 if (!mnt.empty() && mnt[0] == '/') { 3907 if (!mnt.empty() && mnt[0] == '/') {
2905 base_dirs_.emplace_back(mnt, dir); 3908 base_dirs_.push_back({mnt, dir, std::move(headers)});
2906 return true; 3909 return true;
2907 } 3910 }
2908 } 3911 }
@@ -2911,7 +3914,7 @@ inline bool Server::set_mount_point(const char *mount_point, const char *dir) {
2911 3914
2912inline bool Server::remove_mount_point(const char *mount_point) { 3915inline bool Server::remove_mount_point(const char *mount_point) {
2913 for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) { 3916 for (auto it = base_dirs_.begin(); it != base_dirs_.end(); ++it) {
2914 if (it->first == mount_point) { 3917 if (it->mount_point == mount_point) {
2915 base_dirs_.erase(it); 3918 base_dirs_.erase(it);
2916 return true; 3919 return true;
2917 } 3920 }
@@ -2932,6 +3935,12 @@ inline void Server::set_error_handler(Handler handler) {
2932 error_handler_ = std::move(handler); 3935 error_handler_ = std::move(handler);
2933} 3936}
2934 3937
3938inline void Server::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
3939
3940inline void Server::set_socket_options(SocketOptions socket_options) {
3941 socket_options_ = std::move(socket_options);
3942}
3943
2935inline void Server::set_logger(Logger logger) { logger_ = std::move(logger); } 3944inline void Server::set_logger(Logger logger) { logger_ = std::move(logger); }
2936 3945
2937inline void 3946inline void
@@ -2943,11 +3952,25 @@ inline void Server::set_keep_alive_max_count(size_t count) {
2943 keep_alive_max_count_ = count; 3952 keep_alive_max_count_ = count;
2944} 3953}
2945 3954
3955inline void Server::set_keep_alive_timeout(time_t sec) {
3956 keep_alive_timeout_sec_ = sec;
3957}
3958
2946inline void Server::set_read_timeout(time_t sec, time_t usec) { 3959inline void Server::set_read_timeout(time_t sec, time_t usec) {
2947 read_timeout_sec_ = sec; 3960 read_timeout_sec_ = sec;
2948 read_timeout_usec_ = usec; 3961 read_timeout_usec_ = usec;
2949} 3962}
2950 3963
3964inline void Server::set_write_timeout(time_t sec, time_t usec) {
3965 write_timeout_sec_ = sec;
3966 write_timeout_usec_ = usec;
3967}
3968
3969inline void Server::set_idle_interval(time_t sec, time_t usec) {
3970 idle_interval_sec_ = sec;
3971 idle_interval_usec_ = usec;
3972}
3973
2951inline void Server::set_payload_max_length(size_t length) { 3974inline void Server::set_payload_max_length(size_t length) {
2952 payload_max_length_ = length; 3975 payload_max_length_ = length;
2953} 3976}
@@ -2987,7 +4010,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) {
2987 req.version = std::string(m[5]); 4010 req.version = std::string(m[5]);
2988 req.method = std::string(m[1]); 4011 req.method = std::string(m[1]);
2989 req.target = std::string(m[2]); 4012 req.target = std::string(m[2]);
2990 req.path = detail::decode_url(m[3]); 4013 req.path = detail::decode_url(m[3], false);
2991 4014
2992 // Parse query text 4015 // Parse query text
2993 auto len = std::distance(m[4].first, m[4].second); 4016 auto len = std::distance(m[4].first, m[4].second);
@@ -2999,7 +4022,7 @@ inline bool Server::parse_request_line(const char *s, Request &req) {
2999 return false; 4022 return false;
3000} 4023}
3001 4024
3002inline bool Server::write_response(Stream &strm, bool last_connection, 4025inline bool Server::write_response(Stream &strm, bool close_connection,
3003 const Request &req, Response &res) { 4026 const Request &req, Response &res) {
3004 assert(res.status != -1); 4027 assert(res.status != -1);
3005 4028
@@ -3014,16 +4037,17 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3014 } 4037 }
3015 4038
3016 // Headers 4039 // Headers
3017 if (last_connection || req.get_header_value("Connection") == "close") { 4040 if (close_connection || req.get_header_value("Connection") == "close") {
3018 res.set_header("Connection", "close"); 4041 res.set_header("Connection", "close");
3019 } 4042 } else {
3020 4043 std::stringstream ss;
3021 if (!last_connection && req.get_header_value("Connection") == "Keep-Alive") { 4044 ss << "timeout=" << keep_alive_timeout_sec_
3022 res.set_header("Connection", "Keep-Alive"); 4045 << ", max=" << keep_alive_max_count_;
4046 res.set_header("Keep-Alive", ss.str());
3023 } 4047 }
3024 4048
3025 if (!res.has_header("Content-Type") && 4049 if (!res.has_header("Content-Type") &&
3026 (!res.body.empty() || res.content_length > 0)) { 4050 (!res.body.empty() || res.content_length_ > 0 || res.content_provider_)) {
3027 res.set_header("Content-Type", "text/plain"); 4051 res.set_header("Content-Type", "text/plain");
3028 } 4052 }
3029 4053
@@ -3047,18 +4071,20 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3047 "multipart/byteranges; boundary=" + boundary); 4071 "multipart/byteranges; boundary=" + boundary);
3048 } 4072 }
3049 4073
4074 auto type = detail::encoding_type(req, res);
4075
3050 if (res.body.empty()) { 4076 if (res.body.empty()) {
3051 if (res.content_length > 0) { 4077 if (res.content_length_ > 0) {
3052 size_t length = 0; 4078 size_t length = 0;
3053 if (req.ranges.empty()) { 4079 if (req.ranges.empty()) {
3054 length = res.content_length; 4080 length = res.content_length_;
3055 } else if (req.ranges.size() == 1) { 4081 } else if (req.ranges.size() == 1) {
3056 auto offsets = 4082 auto offsets =
3057 detail::get_range_offset_and_length(req, res.content_length, 0); 4083 detail::get_range_offset_and_length(req, res.content_length_, 0);
3058 auto offset = offsets.first; 4084 auto offset = offsets.first;
3059 length = offsets.second; 4085 length = offsets.second;
3060 auto content_range = detail::make_content_range_header_field( 4086 auto content_range = detail::make_content_range_header_field(
3061 offset, length, res.content_length); 4087 offset, length, res.content_length_);
3062 res.set_header("Content-Range", content_range); 4088 res.set_header("Content-Range", content_range);
3063 } else { 4089 } else {
3064 length = detail::get_multipart_ranges_data_length(req, res, boundary, 4090 length = detail::get_multipart_ranges_data_length(req, res, boundary,
@@ -3066,8 +4092,15 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3066 } 4092 }
3067 res.set_header("Content-Length", std::to_string(length)); 4093 res.set_header("Content-Length", std::to_string(length));
3068 } else { 4094 } else {
3069 if (res.content_provider) { 4095 if (res.content_provider_) {
3070 res.set_header("Transfer-Encoding", "chunked"); 4096 if (res.is_chunked_content_provider) {
4097 res.set_header("Transfer-Encoding", "chunked");
4098 if (type == detail::EncodingType::Gzip) {
4099 res.set_header("Content-Encoding", "gzip");
4100 } else if (type == detail::EncodingType::Brotli) {
4101 res.set_header("Content-Encoding", "br");
4102 }
4103 }
3071 } else { 4104 } else {
3072 res.set_header("Content-Length", "0"); 4105 res.set_header("Content-Length", "0");
3073 } 4106 }
@@ -3089,16 +4122,35 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3089 detail::make_multipart_ranges_data(req, res, boundary, content_type); 4122 detail::make_multipart_ranges_data(req, res, boundary, content_type);
3090 } 4123 }
3091 4124
4125 if (type != detail::EncodingType::None) {
4126 std::unique_ptr<detail::compressor> compressor;
4127
4128 if (type == detail::EncodingType::Gzip) {
3092#ifdef CPPHTTPLIB_ZLIB_SUPPORT 4129#ifdef CPPHTTPLIB_ZLIB_SUPPORT
3093 // TODO: 'Accept-Encoding' has gzip, not gzip;q=0 4130 compressor = detail::make_unique<detail::gzip_compressor>();
3094 const auto &encodings = req.get_header_value("Accept-Encoding");
3095 if (encodings.find("gzip") != std::string::npos &&
3096 detail::can_compress(res.get_header_value("Content-Type"))) {
3097 if (detail::compress(res.body)) {
3098 res.set_header("Content-Encoding", "gzip"); 4131 res.set_header("Content-Encoding", "gzip");
4132#endif
4133 } else if (type == detail::EncodingType::Brotli) {
4134#ifdef CPPHTTPLIB_BROTLI_SUPPORT
4135 compressor = detail::make_unique<detail::brotli_compressor>();
4136 res.set_header("Content-Encoding", "brotli");
4137#endif
4138 }
4139
4140 if (compressor) {
4141 std::string compressed;
4142
4143 if (!compressor->compress(res.body.data(), res.body.size(), true,
4144 [&](const char *data, size_t data_len) {
4145 compressed.append(data, data_len);
4146 return true;
4147 })) {
4148 return false;
4149 }
4150
4151 res.body.swap(compressed);
3099 } 4152 }
3100 } 4153 }
3101#endif
3102 4154
3103 auto length = std::to_string(res.body.size()); 4155 auto length = std::to_string(res.body.size());
3104 res.set_header("Content-Length", length); 4156 res.set_header("Content-Length", length);
@@ -3111,13 +4163,14 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3111 strm.write(data.data(), data.size()); 4163 strm.write(data.data(), data.size());
3112 4164
3113 // Body 4165 // Body
4166 auto ret = true;
3114 if (req.method != "HEAD") { 4167 if (req.method != "HEAD") {
3115 if (!res.body.empty()) { 4168 if (!res.body.empty()) {
3116 if (!strm.write(res.body)) { return false; } 4169 if (!strm.write(res.body)) { ret = false; }
3117 } else if (res.content_provider) { 4170 } else if (res.content_provider_) {
3118 if (!write_content_with_provider(strm, req, res, boundary, 4171 if (!write_content_with_provider(strm, req, res, boundary,
3119 content_type)) { 4172 content_type)) {
3120 return false; 4173 ret = false;
3121 } 4174 }
3122 } 4175 }
3123 } 4176 }
@@ -3125,119 +4178,158 @@ inline bool Server::write_response(Stream &strm, bool last_connection,
3125 // Log 4178 // Log
3126 if (logger_) { logger_(req, res); } 4179 if (logger_) { logger_(req, res); }
3127 4180
3128 return true; 4181 return ret;
3129} 4182}
3130 4183
3131inline bool 4184inline bool
3132Server::write_content_with_provider(Stream &strm, const Request &req, 4185Server::write_content_with_provider(Stream &strm, const Request &req,
3133 Response &res, const std::string &boundary, 4186 Response &res, const std::string &boundary,
3134 const std::string &content_type) { 4187 const std::string &content_type) {
3135 if (res.content_length) { 4188 auto is_shutting_down = [this]() {
4189 return this->svr_sock_ == INVALID_SOCKET;
4190 };
4191
4192 if (res.content_length_ > 0) {
3136 if (req.ranges.empty()) { 4193 if (req.ranges.empty()) {
3137 if (detail::write_content(strm, res.content_provider, 0, 4194 if (detail::write_content(strm, res.content_provider_, 0,
3138 res.content_length) < 0) { 4195 res.content_length_, is_shutting_down) < 0) {
3139 return false; 4196 return false;
3140 } 4197 }
3141 } else if (req.ranges.size() == 1) { 4198 } else if (req.ranges.size() == 1) {
3142 auto offsets = 4199 auto offsets =
3143 detail::get_range_offset_and_length(req, res.content_length, 0); 4200 detail::get_range_offset_and_length(req, res.content_length_, 0);
3144 auto offset = offsets.first; 4201 auto offset = offsets.first;
3145 auto length = offsets.second; 4202 auto length = offsets.second;
3146 if (detail::write_content(strm, res.content_provider, offset, length) < 4203 if (detail::write_content(strm, res.content_provider_, offset, length,
3147 0) { 4204 is_shutting_down) < 0) {
3148 return false; 4205 return false;
3149 } 4206 }
3150 } else { 4207 } else {
3151 if (!detail::write_multipart_ranges_data(strm, req, res, boundary, 4208 if (!detail::write_multipart_ranges_data(
3152 content_type)) { 4209 strm, req, res, boundary, content_type, is_shutting_down)) {
3153 return false; 4210 return false;
3154 } 4211 }
3155 } 4212 }
3156 } else { 4213 } else {
3157 auto is_shutting_down = [this]() { 4214 if (res.is_chunked_content_provider) {
3158 return this->svr_sock_ == INVALID_SOCKET; 4215 auto type = detail::encoding_type(req, res);
3159 }; 4216
3160 if (detail::write_content_chunked(strm, res.content_provider, 4217 std::unique_ptr<detail::compressor> compressor;
3161 is_shutting_down) < 0) { 4218 if (type == detail::EncodingType::Gzip) {
3162 return false; 4219#ifdef CPPHTTPLIB_ZLIB_SUPPORT
4220 compressor = detail::make_unique<detail::gzip_compressor>();
4221#endif
4222 } else if (type == detail::EncodingType::Brotli) {
4223#ifdef CPPHTTPLIB_BROTLI_SUPPORT
4224 compressor = detail::make_unique<detail::brotli_compressor>();
4225#endif
4226 } else {
4227 compressor = detail::make_unique<detail::nocompressor>();
4228 }
4229 assert(compressor != nullptr);
4230
4231 if (detail::write_content_chunked(strm, res.content_provider_,
4232 is_shutting_down, *compressor) < 0) {
4233 return false;
4234 }
4235 } else {
4236 if (detail::write_content_without_length(strm, res.content_provider_,
4237 is_shutting_down) < 0) {
4238 return false;
4239 }
3163 } 4240 }
3164 } 4241 }
3165 return true; 4242 return true;
3166} 4243}
3167 4244
3168inline bool Server::read_content(Stream &strm, bool last_connection, 4245inline bool Server::read_content(Stream &strm, Request &req, Response &res) {
3169 Request &req, Response &res) {
3170 MultipartFormDataMap::iterator cur; 4246 MultipartFormDataMap::iterator cur;
3171 auto ret = read_content_core( 4247 if (read_content_core(
3172 strm, last_connection, req, res, 4248 strm, req, res,
3173 // Regular 4249 // Regular
3174 [&](const char *buf, size_t n) { 4250 [&](const char *buf, size_t n) {
3175 if (req.body.size() + n > req.body.max_size()) { return false; } 4251 if (req.body.size() + n > req.body.max_size()) { return false; }
3176 req.body.append(buf, n); 4252 req.body.append(buf, n);
3177 return true; 4253 return true;
3178 }, 4254 },
3179 // Multipart 4255 // Multipart
3180 [&](const MultipartFormData &file) { 4256 [&](const MultipartFormData &file) {
3181 cur = req.files.emplace(file.name, file); 4257 cur = req.files.emplace(file.name, file);
3182 return true; 4258 return true;
3183 }, 4259 },
3184 [&](const char *buf, size_t n) { 4260 [&](const char *buf, size_t n) {
3185 auto &content = cur->second.content; 4261 auto &content = cur->second.content;
3186 if (content.size() + n > content.max_size()) { return false; } 4262 if (content.size() + n > content.max_size()) { return false; }
3187 content.append(buf, n); 4263 content.append(buf, n);
3188 return true; 4264 return true;
3189 }); 4265 })) {
3190 4266 const auto &content_type = req.get_header_value("Content-Type");
3191 const auto &content_type = req.get_header_value("Content-Type"); 4267 if (!content_type.find("application/x-www-form-urlencoded")) {
3192 if (!content_type.find("application/x-www-form-urlencoded")) { 4268 detail::parse_query_text(req.body, req.params);
3193 detail::parse_query_text(req.body, req.params); 4269 }
4270 return true;
3194 } 4271 }
3195 4272 return false;
3196 return ret;
3197} 4273}
3198 4274
3199inline bool Server::read_content_with_content_receiver( 4275inline bool Server::read_content_with_content_receiver(
3200 Stream &strm, bool last_connection, Request &req, Response &res, 4276 Stream &strm, Request &req, Response &res, ContentReceiver receiver,
3201 ContentReceiver receiver, MultipartContentHeader multipart_header, 4277 MultipartContentHeader multipart_header,
3202 ContentReceiver multipart_receiver) { 4278 ContentReceiver multipart_receiver) {
3203 return read_content_core(strm, last_connection, req, res, receiver, 4279 return read_content_core(strm, req, res, std::move(receiver),
3204 multipart_header, multipart_receiver); 4280 std::move(multipart_header),
4281 std::move(multipart_receiver));
3205} 4282}
3206 4283
3207inline bool Server::read_content_core(Stream &strm, bool last_connection, 4284inline bool Server::read_content_core(Stream &strm, Request &req, Response &res,
3208 Request &req, Response &res,
3209 ContentReceiver receiver, 4285 ContentReceiver receiver,
3210 MultipartContentHeader mulitpart_header, 4286 MultipartContentHeader mulitpart_header,
3211 ContentReceiver multipart_receiver) { 4287 ContentReceiver multipart_receiver) {
3212 detail::MultipartFormDataParser multipart_form_data_parser; 4288 detail::MultipartFormDataParser multipart_form_data_parser;
3213 ContentReceiver out; 4289 ContentReceiverWithProgress out;
3214 4290
3215 if (req.is_multipart_form_data()) { 4291 if (req.is_multipart_form_data()) {
3216 const auto &content_type = req.get_header_value("Content-Type"); 4292 const auto &content_type = req.get_header_value("Content-Type");
3217 std::string boundary; 4293 std::string boundary;
3218 if (!detail::parse_multipart_boundary(content_type, boundary)) { 4294 if (!detail::parse_multipart_boundary(content_type, boundary)) {
3219 res.status = 400; 4295 res.status = 400;
3220 return write_response(strm, last_connection, req, res); 4296 return false;
3221 } 4297 }
3222 4298
3223 multipart_form_data_parser.set_boundary(boundary); 4299 multipart_form_data_parser.set_boundary(std::move(boundary));
3224 out = [&](const char *buf, size_t n) { 4300 out = [&](const char *buf, size_t n, uint64_t /*off*/, uint64_t /*len*/) {
4301 /* For debug
4302 size_t pos = 0;
4303 while (pos < n) {
4304 auto read_size = std::min<size_t>(1, n - pos);
4305 auto ret = multipart_form_data_parser.parse(
4306 buf + pos, read_size, multipart_receiver, mulitpart_header);
4307 if (!ret) { return false; }
4308 pos += read_size;
4309 }
4310 return true;
4311 */
3225 return multipart_form_data_parser.parse(buf, n, multipart_receiver, 4312 return multipart_form_data_parser.parse(buf, n, multipart_receiver,
3226 mulitpart_header); 4313 mulitpart_header);
3227 }; 4314 };
3228 } else { 4315 } else {
3229 out = receiver; 4316 out = [receiver](const char *buf, size_t n, uint64_t /*off*/,
4317 uint64_t /*len*/) { return receiver(buf, n); };
3230 } 4318 }
3231 4319
3232 if (!detail::read_content(strm, req, payload_max_length_, res.status, 4320 if (req.method == "DELETE" && !req.has_header("Content-Length")) {
3233 Progress(), out)) { 4321 return true;
3234 return write_response(strm, last_connection, req, res); 4322 }
4323
4324 if (!detail::read_content(strm, req, payload_max_length_, res.status, nullptr,
4325 out, true)) {
4326 return false;
3235 } 4327 }
3236 4328
3237 if (req.is_multipart_form_data()) { 4329 if (req.is_multipart_form_data()) {
3238 if (!multipart_form_data_parser.is_valid()) { 4330 if (!multipart_form_data_parser.is_valid()) {
3239 res.status = 400; 4331 res.status = 400;
3240 return write_response(strm, last_connection, req, res); 4332 return false;
3241 } 4333 }
3242 } 4334 }
3243 4335
@@ -3246,15 +4338,12 @@ inline bool Server::read_content_core(Stream &strm, bool last_connection,
3246 4338
3247inline bool Server::handle_file_request(Request &req, Response &res, 4339inline bool Server::handle_file_request(Request &req, Response &res,
3248 bool head) { 4340 bool head) {
3249 for (const auto &kv : base_dirs_) { 4341 for (const auto &entry : base_dirs_) {
3250 const auto &mount_point = kv.first;
3251 const auto &base_dir = kv.second;
3252
3253 // Prefix match 4342 // Prefix match
3254 if (!req.path.find(mount_point)) { 4343 if (!req.path.compare(0, entry.mount_point.size(), entry.mount_point)) {
3255 std::string sub_path = "/" + req.path.substr(mount_point.size()); 4344 std::string sub_path = "/" + req.path.substr(entry.mount_point.size());
3256 if (detail::is_valid_path(sub_path)) { 4345 if (detail::is_valid_path(sub_path)) {
3257 auto path = base_dir + sub_path; 4346 auto path = entry.base_dir + sub_path;
3258 if (path.back() == '/') { path += "index.html"; } 4347 if (path.back() == '/') { path += "index.html"; }
3259 4348
3260 if (detail::is_file(path)) { 4349 if (detail::is_file(path)) {
@@ -3262,6 +4351,9 @@ inline bool Server::handle_file_request(Request &req, Response &res,
3262 auto type = 4351 auto type =
3263 detail::find_content_type(path, file_extension_and_mimetype_map_); 4352 detail::find_content_type(path, file_extension_and_mimetype_map_);
3264 if (type) { res.set_header("Content-Type", type); } 4353 if (type) { res.set_header("Content-Type", type); }
4354 for (const auto &kv : entry.headers) {
4355 res.set_header(kv.first.c_str(), kv.second);
4356 }
3265 res.status = 200; 4357 res.status = 200;
3266 if (!head && file_request_handler_) { 4358 if (!head && file_request_handler_) {
3267 file_request_handler_(req, res); 4359 file_request_handler_(req, res);
@@ -3274,40 +4366,39 @@ inline bool Server::handle_file_request(Request &req, Response &res,
3274 return false; 4366 return false;
3275} 4367}
3276 4368
3277inline socket_t Server::create_server_socket(const char *host, int port, 4369inline socket_t
3278 int socket_flags) const { 4370Server::create_server_socket(const char *host, int port, int socket_flags,
4371 SocketOptions socket_options) const {
3279 return detail::create_socket( 4372 return detail::create_socket(
3280 host, port, 4373 host, port, socket_flags, tcp_nodelay_, std::move(socket_options),
3281 [](socket_t sock, struct addrinfo &ai) -> bool { 4374 [](socket_t sock, struct addrinfo &ai) -> bool {
3282 if (::bind(sock, ai.ai_addr, static_cast<int>(ai.ai_addrlen))) { 4375 if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
3283 return false; 4376 return false;
3284 } 4377 }
3285 if (::listen(sock, 5)) { // Listen through 5 channels 4378 if (::listen(sock, 5)) { // Listen through 5 channels
3286 return false; 4379 return false;
3287 } 4380 }
3288 return true; 4381 return true;
3289 }, 4382 });
3290 socket_flags);
3291} 4383}
3292 4384
3293inline int Server::bind_internal(const char *host, int port, int socket_flags) { 4385inline int Server::bind_internal(const char *host, int port, int socket_flags) {
3294 if (!is_valid()) { return -1; } 4386 if (!is_valid()) { return -1; }
3295 4387
3296 svr_sock_ = create_server_socket(host, port, socket_flags); 4388 svr_sock_ = create_server_socket(host, port, socket_flags, socket_options_);
3297 if (svr_sock_ == INVALID_SOCKET) { return -1; } 4389 if (svr_sock_ == INVALID_SOCKET) { return -1; }
3298 4390
3299 if (port == 0) { 4391 if (port == 0) {
3300 struct sockaddr_storage address; 4392 struct sockaddr_storage addr;
3301 socklen_t len = sizeof(address); 4393 socklen_t addr_len = sizeof(addr);
3302 if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&address), 4394 if (getsockname(svr_sock_, reinterpret_cast<struct sockaddr *>(&addr),
3303 &len) == -1) { 4395 &addr_len) == -1) {
3304 return -1; 4396 return -1;
3305 } 4397 }
3306 if (address.ss_family == AF_INET) { 4398 if (addr.ss_family == AF_INET) {
3307 return ntohs(reinterpret_cast<struct sockaddr_in *>(&address)->sin_port); 4399 return ntohs(reinterpret_cast<struct sockaddr_in *>(&addr)->sin_port);
3308 } else if (address.ss_family == AF_INET6) { 4400 } else if (addr.ss_family == AF_INET6) {
3309 return ntohs( 4401 return ntohs(reinterpret_cast<struct sockaddr_in6 *>(&addr)->sin6_port);
3310 reinterpret_cast<struct sockaddr_in6 *>(&address)->sin6_port);
3311 } else { 4402 } else {
3312 return -1; 4403 return -1;
3313 } 4404 }
@@ -3323,18 +4414,19 @@ inline bool Server::listen_internal() {
3323 { 4414 {
3324 std::unique_ptr<TaskQueue> task_queue(new_task_queue()); 4415 std::unique_ptr<TaskQueue> task_queue(new_task_queue());
3325 4416
3326 for (;;) { 4417 while (svr_sock_ != INVALID_SOCKET) {
3327 if (svr_sock_ == INVALID_SOCKET) { 4418#ifndef _WIN32
3328 // The server socket was closed by 'stop' method. 4419 if (idle_interval_sec_ > 0 || idle_interval_usec_ > 0) {
3329 break; 4420#endif
3330 } 4421 auto val = detail::select_read(svr_sock_, idle_interval_sec_,
3331 4422 idle_interval_usec_);
3332 auto val = detail::select_read(svr_sock_, 0, 100000); 4423 if (val == 0) { // Timeout
3333 4424 task_queue->on_idle();
3334 if (val == 0) { // Timeout 4425 continue;
3335 continue; 4426 }
4427#ifndef _WIN32
3336 } 4428 }
3337 4429#endif
3338 socket_t sock = accept(svr_sock_, nullptr, nullptr); 4430 socket_t sock = accept(svr_sock_, nullptr, nullptr);
3339 4431
3340 if (sock == INVALID_SOCKET) { 4432 if (sock == INVALID_SOCKET) {
@@ -3353,7 +4445,11 @@ inline bool Server::listen_internal() {
3353 break; 4445 break;
3354 } 4446 }
3355 4447
4448#if __cplusplus > 201703L
4449 task_queue->enqueue([=, this]() { process_and_close_socket(sock); });
4450#else
3356 task_queue->enqueue([=]() { process_and_close_socket(sock); }); 4451 task_queue->enqueue([=]() { process_and_close_socket(sock); });
4452#endif
3357 } 4453 }
3358 4454
3359 task_queue->shutdown(); 4455 task_queue->shutdown();
@@ -3363,8 +4459,7 @@ inline bool Server::listen_internal() {
3363 return ret; 4459 return ret;
3364} 4460}
3365 4461
3366inline bool Server::routing(Request &req, Response &res, Stream &strm, 4462inline bool Server::routing(Request &req, Response &res, Stream &strm) {
3367 bool last_connection) {
3368 // File handler 4463 // File handler
3369 bool is_head_request = req.method == "HEAD"; 4464 bool is_head_request = req.method == "HEAD";
3370 if ((req.method == "GET" || is_head_request) && 4465 if ((req.method == "GET" || is_head_request) &&
@@ -3378,33 +4473,43 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm,
3378 ContentReader reader( 4473 ContentReader reader(
3379 [&](ContentReceiver receiver) { 4474 [&](ContentReceiver receiver) {
3380 return read_content_with_content_receiver( 4475 return read_content_with_content_receiver(
3381 strm, last_connection, req, res, receiver, nullptr, nullptr); 4476 strm, req, res, std::move(receiver), nullptr, nullptr);
3382 }, 4477 },
3383 [&](MultipartContentHeader header, ContentReceiver receiver) { 4478 [&](MultipartContentHeader header, ContentReceiver receiver) {
3384 return read_content_with_content_receiver( 4479 return read_content_with_content_receiver(strm, req, res, nullptr,
3385 strm, last_connection, req, res, nullptr, header, receiver); 4480 std::move(header),
4481 std::move(receiver));
3386 }); 4482 });
3387 4483
3388 if (req.method == "POST") { 4484 if (req.method == "POST") {
3389 if (dispatch_request_for_content_reader( 4485 if (dispatch_request_for_content_reader(
3390 req, res, reader, post_handlers_for_content_reader_)) { 4486 req, res, std::move(reader),
4487 post_handlers_for_content_reader_)) {
3391 return true; 4488 return true;
3392 } 4489 }
3393 } else if (req.method == "PUT") { 4490 } else if (req.method == "PUT") {
3394 if (dispatch_request_for_content_reader( 4491 if (dispatch_request_for_content_reader(
3395 req, res, reader, put_handlers_for_content_reader_)) { 4492 req, res, std::move(reader),
4493 put_handlers_for_content_reader_)) {
3396 return true; 4494 return true;
3397 } 4495 }
3398 } else if (req.method == "PATCH") { 4496 } else if (req.method == "PATCH") {
3399 if (dispatch_request_for_content_reader( 4497 if (dispatch_request_for_content_reader(
3400 req, res, reader, patch_handlers_for_content_reader_)) { 4498 req, res, std::move(reader),
4499 patch_handlers_for_content_reader_)) {
4500 return true;
4501 }
4502 } else if (req.method == "DELETE") {
4503 if (dispatch_request_for_content_reader(
4504 req, res, std::move(reader),
4505 delete_handlers_for_content_reader_)) {
3401 return true; 4506 return true;
3402 } 4507 }
3403 } 4508 }
3404 } 4509 }
3405 4510
3406 // Read content into `req.body` 4511 // Read content into `req.body`
3407 if (!read_content(strm, last_connection, req, res)) { return false; } 4512 if (!read_content(strm, req, res)) { return false; }
3408 } 4513 }
3409 4514
3410 // Regular handler 4515 // Regular handler
@@ -3427,22 +4532,30 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm,
3427} 4532}
3428 4533
3429inline bool Server::dispatch_request(Request &req, Response &res, 4534inline bool Server::dispatch_request(Request &req, Response &res,
3430 Handlers &handlers) { 4535 const Handlers &handlers) {
3431 for (const auto &x : handlers) { 4536 try {
3432 const auto &pattern = x.first; 4537 for (const auto &x : handlers) {
3433 const auto &handler = x.second; 4538 const auto &pattern = x.first;
3434 4539 const auto &handler = x.second;
3435 if (std::regex_match(req.path, req.matches, pattern)) { 4540
3436 handler(req, res); 4541 if (std::regex_match(req.path, req.matches, pattern)) {
3437 return true; 4542 handler(req, res);
4543 return true;
4544 }
3438 } 4545 }
4546 } catch (const std::exception &ex) {
4547 res.status = 500;
4548 res.set_header("EXCEPTION_WHAT", ex.what());
4549 } catch (...) {
4550 res.status = 500;
4551 res.set_header("EXCEPTION_WHAT", "UNKNOWN");
3439 } 4552 }
3440 return false; 4553 return false;
3441} 4554}
3442 4555
3443inline bool Server::dispatch_request_for_content_reader( 4556inline bool Server::dispatch_request_for_content_reader(
3444 Request &req, Response &res, ContentReader content_reader, 4557 Request &req, Response &res, ContentReader content_reader,
3445 HandlersForContentReader &handlers) { 4558 const HandlersForContentReader &handlers) {
3446 for (const auto &x : handlers) { 4559 for (const auto &x : handlers) {
3447 const auto &pattern = x.first; 4560 const auto &pattern = x.first;
3448 const auto &handler = x.second; 4561 const auto &handler = x.second;
@@ -3456,8 +4569,8 @@ inline bool Server::dispatch_request_for_content_reader(
3456} 4569}
3457 4570
3458inline bool 4571inline bool
3459Server::process_request(Stream &strm, bool last_connection, 4572Server::process_request(Stream &strm, bool close_connection,
3460 bool &connection_close, 4573 bool &connection_closed,
3461 const std::function<void(Request &)> &setup_request) { 4574 const std::function<void(Request &)> &setup_request) {
3462 std::array<char, 2048> buf{}; 4575 std::array<char, 2048> buf{};
3463 4576
@@ -3476,31 +4589,34 @@ Server::process_request(Stream &strm, bool last_connection,
3476 Headers dummy; 4589 Headers dummy;
3477 detail::read_headers(strm, dummy); 4590 detail::read_headers(strm, dummy);
3478 res.status = 414; 4591 res.status = 414;
3479 return write_response(strm, last_connection, req, res); 4592 return write_response(strm, close_connection, req, res);
3480 } 4593 }
3481 4594
3482 // Request line and headers 4595 // Request line and headers
3483 if (!parse_request_line(line_reader.ptr(), req) || 4596 if (!parse_request_line(line_reader.ptr(), req) ||
3484 !detail::read_headers(strm, req.headers)) { 4597 !detail::read_headers(strm, req.headers)) {
3485 res.status = 400; 4598 res.status = 400;
3486 return write_response(strm, last_connection, req, res); 4599 return write_response(strm, close_connection, req, res);
3487 } 4600 }
3488 4601
3489 if (req.get_header_value("Connection") == "close") { 4602 if (req.get_header_value("Connection") == "close") {
3490 connection_close = true; 4603 connection_closed = true;
3491 } 4604 }
3492 4605
3493 if (req.version == "HTTP/1.0" && 4606 if (req.version == "HTTP/1.0" &&
3494 req.get_header_value("Connection") != "Keep-Alive") { 4607 req.get_header_value("Connection") != "Keep-Alive") {
3495 connection_close = true; 4608 connection_closed = true;
3496 } 4609 }
3497 4610
3498 req.set_header("REMOTE_ADDR", strm.get_remote_addr()); 4611 strm.get_remote_ip_and_port(req.remote_addr, req.remote_port);
4612 req.set_header("REMOTE_ADDR", req.remote_addr);
4613 req.set_header("REMOTE_PORT", std::to_string(req.remote_port));
3499 4614
3500 if (req.has_header("Range")) { 4615 if (req.has_header("Range")) {
3501 const auto &range_header_value = req.get_header_value("Range"); 4616 const auto &range_header_value = req.get_header_value("Range");
3502 if (!detail::parse_range_header(range_header_value, req.ranges)) { 4617 if (!detail::parse_range_header(range_header_value, req.ranges)) {
3503 // TODO: error 4618 res.status = 416;
4619 return write_response(strm, close_connection, req, res);
3504 } 4620 }
3505 } 4621 }
3506 4622
@@ -3517,136 +4633,278 @@ Server::process_request(Stream &strm, bool last_connection,
3517 strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status, 4633 strm.write_format("HTTP/1.1 %d %s\r\n\r\n", status,
3518 detail::status_message(status)); 4634 detail::status_message(status));
3519 break; 4635 break;
3520 default: return write_response(strm, last_connection, req, res); 4636 default: return write_response(strm, close_connection, req, res);
3521 } 4637 }
3522 } 4638 }
3523 4639
3524 // Rounting 4640 // Rounting
3525 if (routing(req, res, strm, last_connection)) { 4641 if (routing(req, res, strm)) {
3526 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } 4642 if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
3527 } else { 4643 } else {
3528 if (res.status == -1) { res.status = 404; } 4644 if (res.status == -1) { res.status = 404; }
3529 } 4645 }
3530 4646
3531 return write_response(strm, last_connection, req, res); 4647 return write_response(strm, close_connection, req, res);
3532} 4648}
3533 4649
3534inline bool Server::is_valid() const { return true; } 4650inline bool Server::is_valid() const { return true; }
3535 4651
3536inline bool Server::process_and_close_socket(socket_t sock) { 4652inline bool Server::process_and_close_socket(socket_t sock) {
3537 return detail::process_and_close_socket( 4653 auto ret = detail::process_server_socket(
3538 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_, 4654 sock, keep_alive_max_count_, keep_alive_timeout_sec_, read_timeout_sec_,
3539 [this](Stream &strm, bool last_connection, bool &connection_close) { 4655 read_timeout_usec_, write_timeout_sec_, write_timeout_usec_,
3540 return process_request(strm, last_connection, connection_close, 4656 [this](Stream &strm, bool close_connection, bool &connection_closed) {
4657 return process_request(strm, close_connection, connection_closed,
3541 nullptr); 4658 nullptr);
3542 }); 4659 });
4660
4661 detail::shutdown_socket(sock);
4662 detail::close_socket(sock);
4663 return ret;
3543} 4664}
3544 4665
3545// HTTP client implementation 4666// HTTP client implementation
3546inline Client::Client(const std::string &host, int port, 4667inline ClientImpl::ClientImpl(const std::string &host)
3547 const std::string &client_cert_path, 4668 : ClientImpl(host, 80, std::string(), std::string()) {}
3548 const std::string &client_key_path) 4669
3549 : host_(host), port_(port), 4670inline ClientImpl::ClientImpl(const std::string &host, int port)
4671 : ClientImpl(host, port, std::string(), std::string()) {}
4672
4673inline ClientImpl::ClientImpl(const std::string &host, int port,
4674 const std::string &client_cert_path,
4675 const std::string &client_key_path)
4676 : error_(Error::Success), host_(host), port_(port),
3550 host_and_port_(host_ + ":" + std::to_string(port_)), 4677 host_and_port_(host_ + ":" + std::to_string(port_)),
3551 client_cert_path_(client_cert_path), client_key_path_(client_key_path) {} 4678 client_cert_path_(client_cert_path), client_key_path_(client_key_path) {}
3552 4679
3553inline Client::~Client() {} 4680inline ClientImpl::~ClientImpl() { lock_socket_and_shutdown_and_close(); }
4681
4682inline bool ClientImpl::is_valid() const { return true; }
3554 4683
3555inline bool Client::is_valid() const { return true; } 4684inline Error ClientImpl::get_last_error() const { return error_; }
3556 4685
3557inline socket_t Client::create_client_socket() const { 4686inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
3558 if (!proxy_host_.empty()) { 4687 client_cert_path_ = rhs.client_cert_path_;
3559 return detail::create_client_socket(proxy_host_.c_str(), proxy_port_, 4688 client_key_path_ = rhs.client_key_path_;
3560 timeout_sec_, interface_); 4689 connection_timeout_sec_ = rhs.connection_timeout_sec_;
4690 read_timeout_sec_ = rhs.read_timeout_sec_;
4691 read_timeout_usec_ = rhs.read_timeout_usec_;
4692 write_timeout_sec_ = rhs.write_timeout_sec_;
4693 write_timeout_usec_ = rhs.write_timeout_usec_;
4694 basic_auth_username_ = rhs.basic_auth_username_;
4695 basic_auth_password_ = rhs.basic_auth_password_;
4696 bearer_token_auth_token_ = rhs.bearer_token_auth_token_;
4697#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4698 digest_auth_username_ = rhs.digest_auth_username_;
4699 digest_auth_password_ = rhs.digest_auth_password_;
4700#endif
4701 keep_alive_ = rhs.keep_alive_;
4702 follow_location_ = rhs.follow_location_;
4703 tcp_nodelay_ = rhs.tcp_nodelay_;
4704 socket_options_ = rhs.socket_options_;
4705 compress_ = rhs.compress_;
4706 decompress_ = rhs.decompress_;
4707 interface_ = rhs.interface_;
4708 proxy_host_ = rhs.proxy_host_;
4709 proxy_port_ = rhs.proxy_port_;
4710 proxy_basic_auth_username_ = rhs.proxy_basic_auth_username_;
4711 proxy_basic_auth_password_ = rhs.proxy_basic_auth_password_;
4712 proxy_bearer_token_auth_token_ = rhs.proxy_bearer_token_auth_token_;
4713#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4714 proxy_digest_auth_username_ = rhs.proxy_digest_auth_username_;
4715 proxy_digest_auth_password_ = rhs.proxy_digest_auth_password_;
4716#endif
4717#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4718 server_certificate_verification_ = rhs.server_certificate_verification_;
4719#endif
4720 logger_ = rhs.logger_;
4721}
4722
4723inline socket_t ClientImpl::create_client_socket() const {
4724 if (!proxy_host_.empty() && proxy_port_ != -1) {
4725 return detail::create_client_socket(
4726 proxy_host_.c_str(), proxy_port_, tcp_nodelay_, socket_options_,
4727 connection_timeout_sec_, connection_timeout_usec_, interface_, error_);
3561 } 4728 }
3562 return detail::create_client_socket(host_.c_str(), port_, timeout_sec_, 4729 return detail::create_client_socket(
3563 interface_); 4730 host_.c_str(), port_, tcp_nodelay_, socket_options_,
4731 connection_timeout_sec_, connection_timeout_usec_, interface_, error_);
4732}
4733
4734inline bool ClientImpl::create_and_connect_socket(Socket &socket) {
4735 auto sock = create_client_socket();
4736 if (sock == INVALID_SOCKET) { return false; }
4737 socket.sock = sock;
4738 return true;
4739}
4740
4741inline void ClientImpl::shutdown_ssl(Socket &socket, bool shutdown_gracefully) {
4742 (void)socket;
4743 (void)shutdown_gracefully;
4744 //If there are any requests in flight from threads other than us, then it's
4745 //a thread-unsafe race because individual ssl* objects are not thread-safe.
4746 assert(socket_requests_in_flight_ == 0 ||
4747 socket_requests_are_from_thread_ == std::this_thread::get_id());
4748}
4749
4750inline void ClientImpl::shutdown_socket(Socket &socket) {
4751 if (socket.sock == INVALID_SOCKET)
4752 return;
4753 detail::shutdown_socket(socket.sock);
4754}
4755
4756inline void ClientImpl::close_socket(Socket &socket) {
4757 // If there are requests in flight in another thread, usually closing
4758 // the socket will be fine and they will simply receive an error when
4759 // using the closed socket, but it is still a bug since rarely the OS
4760 // may reassign the socket id to be used for a new socket, and then
4761 // suddenly they will be operating on a live socket that is different
4762 // than the one they intended!
4763 assert(socket_requests_in_flight_ == 0 ||
4764 socket_requests_are_from_thread_ == std::this_thread::get_id());
4765 // It is also a bug if this happens while SSL is still active
4766#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4767 assert(socket.ssl == nullptr);
4768#endif
4769 if (socket.sock == INVALID_SOCKET)
4770 return;
4771 detail::close_socket(socket.sock);
4772 socket.sock = INVALID_SOCKET;
3564} 4773}
3565 4774
3566inline bool Client::read_response_line(Stream &strm, Response &res) { 4775inline void ClientImpl::lock_socket_and_shutdown_and_close() {
4776 std::lock_guard<std::mutex> guard(socket_mutex_);
4777 shutdown_ssl(socket_, true);
4778 shutdown_socket(socket_);
4779 close_socket(socket_);
4780}
4781
4782inline bool ClientImpl::read_response_line(Stream &strm, Response &res) {
3567 std::array<char, 2048> buf; 4783 std::array<char, 2048> buf;
3568 4784
3569 detail::stream_line_reader line_reader(strm, buf.data(), buf.size()); 4785 detail::stream_line_reader line_reader(strm, buf.data(), buf.size());
3570 4786
3571 if (!line_reader.getline()) { return false; } 4787 if (!line_reader.getline()) { return false; }
3572 4788
3573 const static std::regex re("(HTTP/1\\.[01]) (\\d+?) .*\r\n"); 4789 const static std::regex re("(HTTP/1\\.[01]) (\\d+) (.*?)\r\n");
3574 4790
3575 std::cmatch m; 4791 std::cmatch m;
3576 if (std::regex_match(line_reader.ptr(), m, re)) { 4792 if (!std::regex_match(line_reader.ptr(), m, re)) { return false; }
4793 res.version = std::string(m[1]);
4794 res.status = std::stoi(std::string(m[2]));
4795 res.reason = std::string(m[3]);
4796
4797 // Ignore '100 Continue'
4798 while (res.status == 100) {
4799 if (!line_reader.getline()) { return false; } // CRLF
4800 if (!line_reader.getline()) { return false; } // next response line
4801
4802 if (!std::regex_match(line_reader.ptr(), m, re)) { return false; }
3577 res.version = std::string(m[1]); 4803 res.version = std::string(m[1]);
3578 res.status = std::stoi(std::string(m[2])); 4804 res.status = std::stoi(std::string(m[2]));
4805 res.reason = std::string(m[3]);
3579 } 4806 }
3580 4807
3581 return true; 4808 return true;
3582} 4809}
3583 4810
3584inline bool Client::send(const Request &req, Response &res) { 4811inline bool ClientImpl::send(const Request &req, Response &res) {
3585 auto sock = create_client_socket(); 4812 std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
3586 if (sock == INVALID_SOCKET) { return false; } 4813
4814 {
4815 std::lock_guard<std::mutex> guard(socket_mutex_);
4816 // Set this to false immediately - if it ever gets set to true by the end of the
4817 // request, we know another thread instructed us to close the socket.
4818 socket_should_be_closed_when_request_is_done_ = false;
4819
4820 auto is_alive = false;
4821 if (socket_.is_open()) {
4822 is_alive = detail::select_write(socket_.sock, 0, 0) > 0;
4823 if (!is_alive) {
4824 // Attempt to avoid sigpipe by shutting down nongracefully if it seems like
4825 // the other side has already closed the connection
4826 // Also, there cannot be any requests in flight from other threads since we locked
4827 // request_mutex_, so safe to close everything immediately
4828 const bool shutdown_gracefully = false;
4829 shutdown_ssl(socket_, shutdown_gracefully);
4830 shutdown_socket(socket_);
4831 close_socket(socket_);
4832 }
4833 }
4834
4835 if (!is_alive) {
4836 if (!create_and_connect_socket(socket_)) { return false; }
3587 4837
3588#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 4838#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3589 if (is_ssl() && !proxy_host_.empty()) { 4839 // TODO: refactoring
3590 bool error; 4840 if (is_ssl()) {
3591 if (!connect(sock, res, error)) { return error; } 4841 auto &scli = static_cast<SSLClient &>(*this);
3592 } 4842 if (!proxy_host_.empty() && proxy_port_ != -1) {
4843 bool success = false;
4844 if (!scli.connect_with_proxy(socket_, res, success)) {
4845 return success;
4846 }
4847 }
4848
4849 if (!scli.initialize_ssl(socket_)) { return false; }
4850 }
3593#endif 4851#endif
4852 }
3594 4853
3595 return process_and_close_socket( 4854 // Mark the current socket as being in use so that it cannot be closed by anyone
3596 sock, 1, [&](Stream &strm, bool last_connection, bool &connection_close) { 4855 // else while this request is ongoing, even though we will be releasing the mutex.
3597 return handle_request(strm, req, res, last_connection, 4856 if (socket_requests_in_flight_ > 1) {
3598 connection_close); 4857 assert(socket_requests_are_from_thread_ == std::this_thread::get_id());
3599 }); 4858 }
3600} 4859 socket_requests_in_flight_ += 1;
4860 socket_requests_are_from_thread_ = std::this_thread::get_id();
4861 }
3601 4862
3602inline bool Client::send(const std::vector<Request> &requests, 4863 auto close_connection = !keep_alive_;
3603 std::vector<Response> &responses) { 4864 auto ret = process_socket(socket_, [&](Stream &strm) {
3604 size_t i = 0; 4865 return handle_request(strm, req, res, close_connection);
3605 while (i < requests.size()) { 4866 });
3606 auto sock = create_client_socket();
3607 if (sock == INVALID_SOCKET) { return false; }
3608 4867
3609#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 4868 //Briefly lock mutex in order to mark that a request is no longer ongoing
3610 if (is_ssl() && !proxy_host_.empty()) { 4869 {
3611 Response res; 4870 std::lock_guard<std::mutex> guard(socket_mutex_);
3612 bool error; 4871 socket_requests_in_flight_ -= 1;
3613 if (!connect(sock, res, error)) { return false; } 4872 if (socket_requests_in_flight_ <= 0) {
4873 assert(socket_requests_in_flight_ == 0);
4874 socket_requests_are_from_thread_ = std::thread::id();
3614 } 4875 }
3615#endif
3616 4876
3617 if (!process_and_close_socket(sock, requests.size() - i, 4877 if (socket_should_be_closed_when_request_is_done_ ||
3618 [&](Stream &strm, bool last_connection, 4878 close_connection ||
3619 bool &connection_close) -> bool { 4879 !ret ) {
3620 auto &req = requests[i++]; 4880 shutdown_ssl(socket_, true);
3621 auto res = Response(); 4881 shutdown_socket(socket_);
3622 auto ret = handle_request(strm, req, res, 4882 close_socket(socket_);
3623 last_connection,
3624 connection_close);
3625 if (ret) {
3626 responses.emplace_back(std::move(res));
3627 }
3628 return ret;
3629 })) {
3630 return false;
3631 } 4883 }
3632 } 4884 }
3633 4885
3634 return true; 4886 if (!ret) {
4887 if (error_ == Error::Success) { error_ = Error::Unknown; }
4888 }
4889
4890 return ret;
3635} 4891}
3636 4892
3637inline bool Client::handle_request(Stream &strm, const Request &req, 4893inline bool ClientImpl::handle_request(Stream &strm, const Request &req,
3638 Response &res, bool last_connection, 4894 Response &res, bool close_connection) {
3639 bool &connection_close) { 4895 if (req.path.empty()) {
3640 if (req.path.empty()) { return false; } 4896 error_ = Error::Connection;
4897 return false;
4898 }
3641 4899
3642 bool ret; 4900 bool ret;
3643 4901
3644 if (!is_ssl() && !proxy_host_.empty()) { 4902 if (!is_ssl() && !proxy_host_.empty() && proxy_port_ != -1) {
3645 auto req2 = req; 4903 auto req2 = req;
3646 req2.path = "http://" + host_and_port_ + req.path; 4904 req2.path = "http://" + host_and_port_ + req.path;
3647 ret = process_request(strm, req2, res, last_connection, connection_close); 4905 ret = process_request(strm, req2, res, close_connection);
3648 } else { 4906 } else {
3649 ret = process_request(strm, req, res, last_connection, connection_close); 4907 ret = process_request(strm, req, res, close_connection);
3650 } 4908 }
3651 4909
3652 if (!ret) { return false; } 4910 if (!ret) { return false; }
@@ -3656,7 +4914,8 @@ inline bool Client::handle_request(Stream &strm, const Request &req,
3656 } 4914 }
3657 4915
3658#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 4916#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3659 if (res.status == 401 || res.status == 407) { 4917 if ((res.status == 401 || res.status == 407) &&
4918 req.authorization_count_ < 5) {
3660 auto is_proxy = res.status == 407; 4919 auto is_proxy = res.status == 407;
3661 const auto &username = 4920 const auto &username =
3662 is_proxy ? proxy_digest_auth_username_ : digest_auth_username_; 4921 is_proxy ? proxy_digest_auth_username_ : digest_auth_username_;
@@ -3665,12 +4924,14 @@ inline bool Client::handle_request(Stream &strm, const Request &req,
3665 4924
3666 if (!username.empty() && !password.empty()) { 4925 if (!username.empty() && !password.empty()) {
3667 std::map<std::string, std::string> auth; 4926 std::map<std::string, std::string> auth;
3668 if (parse_www_authenticate(res, auth, is_proxy)) { 4927 if (detail::parse_www_authenticate(res, auth, is_proxy)) {
3669 Request new_req = req; 4928 Request new_req = req;
3670 auto key = is_proxy ? "Proxy-Authorization" : "WWW-Authorization"; 4929 new_req.authorization_count_ += 1;
4930 auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
3671 new_req.headers.erase(key); 4931 new_req.headers.erase(key);
3672 new_req.headers.insert(make_digest_authentication_header( 4932 new_req.headers.insert(detail::make_digest_authentication_header(
3673 req, auth, 1, random_string(10), username, password, is_proxy)); 4933 req, auth, new_req.authorization_count_, detail::random_string(10),
4934 username, password, is_proxy));
3674 4935
3675 Response new_res; 4936 Response new_res;
3676 4937
@@ -3684,102 +4945,64 @@ inline bool Client::handle_request(Stream &strm, const Request &req,
3684 return ret; 4945 return ret;
3685} 4946}
3686 4947
3687#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 4948inline bool ClientImpl::redirect(const Request &req, Response &res) {
3688inline bool Client::connect(socket_t sock, Response &res, bool &error) { 4949 if (req.redirect_count == 0) {
3689 error = true; 4950 error_ = Error::ExceedRedirectCount;
3690 Response res2;
3691
3692 if (!detail::process_socket(
3693 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3694 [&](Stream &strm, bool /*last_connection*/, bool &connection_close) {
3695 Request req2;
3696 req2.method = "CONNECT";
3697 req2.path = host_and_port_;
3698 return process_request(strm, req2, res2, false, connection_close);
3699 })) {
3700 detail::close_socket(sock);
3701 error = false;
3702 return false; 4951 return false;
3703 } 4952 }
3704 4953
3705 if (res2.status == 407) { 4954 auto location = detail::decode_url(res.get_header_value("location"), true);
3706 if (!proxy_digest_auth_username_.empty() &&
3707 !proxy_digest_auth_password_.empty()) {
3708 std::map<std::string, std::string> auth;
3709 if (parse_www_authenticate(res2, auth, true)) {
3710 Response res3;
3711 if (!detail::process_socket(
3712 true, sock, 1, read_timeout_sec_, read_timeout_usec_,
3713 [&](Stream &strm, bool /*last_connection*/,
3714 bool &connection_close) {
3715 Request req3;
3716 req3.method = "CONNECT";
3717 req3.path = host_and_port_;
3718 req3.headers.insert(make_digest_authentication_header(
3719 req3, auth, 1, random_string(10),
3720 proxy_digest_auth_username_, proxy_digest_auth_password_,
3721 true));
3722 return process_request(strm, req3, res3, false,
3723 connection_close);
3724 })) {
3725 detail::close_socket(sock);
3726 error = false;
3727 return false;
3728 }
3729 }
3730 } else {
3731 res = res2;
3732 return false;
3733 }
3734 }
3735
3736 return true;
3737}
3738#endif
3739
3740inline bool Client::redirect(const Request &req, Response &res) {
3741 if (req.redirect_count == 0) { return false; }
3742
3743 auto location = res.get_header_value("location");
3744 if (location.empty()) { return false; } 4955 if (location.empty()) { return false; }
3745 4956
3746 const static std::regex re( 4957 const static std::regex re(
3747 R"(^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*(?:\?[^#]*)?)(?:#.*)?)"); 4958 R"(^(?:(https?):)?(?://([^:/?#]*)(?::(\d+))?)?([^?#]*(?:\?[^#]*)?)(?:#.*)?)");
3748 4959
3749 std::smatch m; 4960 std::smatch m;
3750 if (!regex_match(location, m, re)) { return false; } 4961 if (!std::regex_match(location, m, re)) { return false; }
3751 4962
3752 auto scheme = is_ssl() ? "https" : "http"; 4963 auto scheme = is_ssl() ? "https" : "http";
3753 4964
3754 auto next_scheme = m[1].str(); 4965 auto next_scheme = m[1].str();
3755 auto next_host = m[2].str(); 4966 auto next_host = m[2].str();
3756 auto next_path = m[3].str(); 4967 auto port_str = m[3].str();
3757 if (next_scheme.empty()) { next_scheme = scheme; } 4968 auto next_path = m[4].str();
4969
4970 auto next_port = port_;
4971 if (!port_str.empty()) {
4972 next_port = std::stoi(port_str);
4973 } else if (!next_scheme.empty()) {
4974 next_port = next_scheme == "https" ? 443 : 80;
4975 }
4976
3758 if (next_scheme.empty()) { next_scheme = scheme; } 4977 if (next_scheme.empty()) { next_scheme = scheme; }
3759 if (next_host.empty()) { next_host = host_; } 4978 if (next_host.empty()) { next_host = host_; }
3760 if (next_path.empty()) { next_path = "/"; } 4979 if (next_path.empty()) { next_path = "/"; }
3761 4980
3762 if (next_scheme == scheme && next_host == host_) { 4981 if (next_scheme == scheme && next_host == host_ && next_port == port_) {
3763 return detail::redirect(*this, req, res, next_path); 4982 return detail::redirect(*this, req, res, next_path);
3764 } else { 4983 } else {
3765 if (next_scheme == "https") { 4984 if (next_scheme == "https") {
3766#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 4985#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3767 SSLClient cli(next_host.c_str()); 4986 SSLClient cli(next_host.c_str(), next_port);
3768 cli.copy_settings(*this); 4987 cli.copy_settings(*this);
3769 return detail::redirect(cli, req, res, next_path); 4988 auto ret = detail::redirect(cli, req, res, next_path);
4989 if (!ret) { error_ = cli.get_last_error(); }
4990 return ret;
3770#else 4991#else
3771 return false; 4992 return false;
3772#endif 4993#endif
3773 } else { 4994 } else {
3774 Client cli(next_host.c_str()); 4995 ClientImpl cli(next_host.c_str(), next_port);
3775 cli.copy_settings(*this); 4996 cli.copy_settings(*this);
3776 return detail::redirect(cli, req, res, next_path); 4997 auto ret = detail::redirect(cli, req, res, next_path);
4998 if (!ret) { error_ = cli.get_last_error(); }
4999 return ret;
3777 } 5000 }
3778 } 5001 }
3779} 5002}
3780 5003
3781inline bool Client::write_request(Stream &strm, const Request &req, 5004inline bool ClientImpl::write_request(Stream &strm, const Request &req,
3782 bool last_connection) { 5005 bool close_connection) {
3783 detail::BufferStream bstrm; 5006 detail::BufferStream bstrm;
3784 5007
3785 // Request line 5008 // Request line
@@ -3789,7 +5012,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3789 5012
3790 // Additonal headers 5013 // Additonal headers
3791 Headers headers; 5014 Headers headers;
3792 if (last_connection) { headers.emplace("Connection", "close"); } 5015 if (close_connection) { headers.emplace("Connection", "close"); }
3793 5016
3794 if (!req.has_header("Host")) { 5017 if (!req.has_header("Host")) {
3795 if (is_ssl()) { 5018 if (is_ssl()) {
@@ -3810,7 +5033,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3810 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); } 5033 if (!req.has_header("Accept")) { headers.emplace("Accept", "*/*"); }
3811 5034
3812 if (!req.has_header("User-Agent")) { 5035 if (!req.has_header("User-Agent")) {
3813 headers.emplace("User-Agent", "cpp-httplib/0.5"); 5036 headers.emplace("User-Agent", "cpp-httplib/0.7");
3814 } 5037 }
3815 5038
3816 if (req.body.empty()) { 5039 if (req.body.empty()) {
@@ -3818,7 +5041,10 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3818 auto length = std::to_string(req.content_length); 5041 auto length = std::to_string(req.content_length);
3819 headers.emplace("Content-Length", length); 5042 headers.emplace("Content-Length", length);
3820 } else { 5043 } else {
3821 headers.emplace("Content-Length", "0"); 5044 if (req.method == "POST" || req.method == "PUT" ||
5045 req.method == "PATCH") {
5046 headers.emplace("Content-Length", "0");
5047 }
3822 } 5048 }
3823 } else { 5049 } else {
3824 if (!req.has_header("Content-Type")) { 5050 if (!req.has_header("Content-Type")) {
@@ -3831,7 +5057,7 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3831 } 5057 }
3832 } 5058 }
3833 5059
3834 if (!basic_auth_username_.empty() && !basic_auth_password_.empty()) { 5060 if (!basic_auth_password_.empty()) {
3835 headers.insert(make_basic_authentication_header( 5061 headers.insert(make_basic_authentication_header(
3836 basic_auth_username_, basic_auth_password_, false)); 5062 basic_auth_username_, basic_auth_password_, false));
3837 } 5063 }
@@ -3842,11 +5068,24 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3842 proxy_basic_auth_username_, proxy_basic_auth_password_, true)); 5068 proxy_basic_auth_username_, proxy_basic_auth_password_, true));
3843 } 5069 }
3844 5070
5071 if (!bearer_token_auth_token_.empty()) {
5072 headers.insert(make_bearer_token_authentication_header(
5073 bearer_token_auth_token_, false));
5074 }
5075
5076 if (!proxy_bearer_token_auth_token_.empty()) {
5077 headers.insert(make_bearer_token_authentication_header(
5078 proxy_bearer_token_auth_token_, true));
5079 }
5080
3845 detail::write_headers(bstrm, req, headers); 5081 detail::write_headers(bstrm, req, headers);
3846 5082
3847 // Flush buffer 5083 // Flush buffer
3848 auto &data = bstrm.get_buffer(); 5084 auto &data = bstrm.get_buffer();
3849 strm.write(data.data(), data.size()); 5085 if (!detail::write_data(strm, data.data(), data.size())) {
5086 error_ = Error::Write;
5087 return false;
5088 }
3850 5089
3851 // Body 5090 // Body
3852 if (req.body.empty()) { 5091 if (req.body.empty()) {
@@ -3854,282 +5093,370 @@ inline bool Client::write_request(Stream &strm, const Request &req,
3854 size_t offset = 0; 5093 size_t offset = 0;
3855 size_t end_offset = req.content_length; 5094 size_t end_offset = req.content_length;
3856 5095
5096 bool ok = true;
5097
3857 DataSink data_sink; 5098 DataSink data_sink;
3858 data_sink.write = [&](const char *d, size_t l) { 5099 data_sink.write = [&](const char *d, size_t l) {
3859 auto written_length = strm.write(d, l); 5100 if (ok) {
3860 offset += written_length; 5101 if (detail::write_data(strm, d, l)) {
5102 offset += l;
5103 } else {
5104 ok = false;
5105 }
5106 }
3861 }; 5107 };
3862 data_sink.is_writable = [&](void) { return strm.is_writable(); }; 5108 data_sink.is_writable = [&](void) { return ok && strm.is_writable(); };
3863 5109
3864 while (offset < end_offset) { 5110 while (offset < end_offset) {
3865 req.content_provider(offset, end_offset - offset, data_sink); 5111 if (!req.content_provider(offset, end_offset - offset, data_sink)) {
5112 error_ = Error::Canceled;
5113 return false;
5114 }
5115 if (!ok) {
5116 error_ = Error::Write;
5117 return false;
5118 }
3866 } 5119 }
3867 } 5120 }
3868 } else { 5121 } else {
3869 strm.write(req.body); 5122 return detail::write_data(strm, req.body.data(), req.body.size());
3870 } 5123 }
3871 5124
3872 return true; 5125 return true;
3873} 5126}
3874 5127
3875inline std::shared_ptr<Response> Client::send_with_content_provider( 5128inline std::unique_ptr<Response> ClientImpl::send_with_content_provider(
3876 const char *method, const char *path, const Headers &headers, 5129 const char *method, const char *path, const Headers &headers,
3877 const std::string &body, size_t content_length, 5130 const std::string &body, size_t content_length,
3878 ContentProvider content_provider, const char *content_type) { 5131 ContentProvider content_provider, const char *content_type) {
5132
3879 Request req; 5133 Request req;
3880 req.method = method; 5134 req.method = method;
3881 req.headers = headers; 5135 req.headers = default_headers_;
5136 req.headers.insert(headers.begin(), headers.end());
3882 req.path = path; 5137 req.path = path;
3883 5138
3884 req.headers.emplace("Content-Type", content_type); 5139 if (content_type) { req.headers.emplace("Content-Type", content_type); }
3885 5140
3886#ifdef CPPHTTPLIB_ZLIB_SUPPORT 5141#ifdef CPPHTTPLIB_ZLIB_SUPPORT
3887 if (compress_) { 5142 if (compress_) {
5143 detail::gzip_compressor compressor;
5144
3888 if (content_provider) { 5145 if (content_provider) {
5146 auto ok = true;
3889 size_t offset = 0; 5147 size_t offset = 0;
3890 5148
3891 DataSink data_sink; 5149 DataSink data_sink;
3892 data_sink.write = [&](const char *data, size_t data_len) { 5150 data_sink.write = [&](const char *data, size_t data_len) {
3893 req.body.append(data, data_len); 5151 if (ok) {
3894 offset += data_len; 5152 auto last = offset + data_len == content_length;
5153
5154 auto ret = compressor.compress(
5155 data, data_len, last, [&](const char *data, size_t data_len) {
5156 req.body.append(data, data_len);
5157 return true;
5158 });
5159
5160 if (ret) {
5161 offset += data_len;
5162 } else {
5163 ok = false;
5164 }
5165 }
3895 }; 5166 };
3896 data_sink.is_writable = [&](void) { return true; }; 5167 data_sink.is_writable = [&](void) { return ok && true; };
3897 5168
3898 while (offset < content_length) { 5169 while (ok && offset < content_length) {
3899 content_provider(offset, content_length - offset, data_sink); 5170 if (!content_provider(offset, content_length - offset, data_sink)) {
5171 error_ = Error::Canceled;
5172 return nullptr;
5173 }
3900 } 5174 }
3901 } else { 5175 } else {
3902 req.body = body; 5176 if (!compressor.compress(body.data(), body.size(), true,
5177 [&](const char *data, size_t data_len) {
5178 req.body.append(data, data_len);
5179 return true;
5180 })) {
5181 return nullptr;
5182 }
3903 } 5183 }
3904 5184
3905 if (!detail::compress(req.body)) { return nullptr; }
3906 req.headers.emplace("Content-Encoding", "gzip"); 5185 req.headers.emplace("Content-Encoding", "gzip");
3907 } else 5186 } else
3908#endif 5187#endif
3909 { 5188 {
3910 if (content_provider) { 5189 if (content_provider) {
3911 req.content_length = content_length; 5190 req.content_length = content_length;
3912 req.content_provider = content_provider; 5191 req.content_provider = std::move(content_provider);
3913 } else { 5192 } else {
3914 req.body = body; 5193 req.body = body;
3915 } 5194 }
3916 } 5195 }
3917 5196
3918 auto res = std::make_shared<Response>(); 5197 auto res = detail::make_unique<Response>();
3919 5198
3920 return send(req, *res) ? res : nullptr; 5199 return send(req, *res) ? std::move(res) : nullptr;
3921} 5200}
3922 5201
3923inline bool Client::process_request(Stream &strm, const Request &req, 5202inline bool ClientImpl::process_request(Stream &strm, const Request &req,
3924 Response &res, bool last_connection, 5203 Response &res, bool close_connection) {
3925 bool &connection_close) {
3926 // Send request 5204 // Send request
3927 if (!write_request(strm, req, last_connection)) { return false; } 5205 if (!write_request(strm, req, close_connection)) { return false; }
3928 5206
3929 // Receive response and headers 5207 // Receive response and headers
3930 if (!read_response_line(strm, res) || 5208 if (!read_response_line(strm, res) ||
3931 !detail::read_headers(strm, res.headers)) { 5209 !detail::read_headers(strm, res.headers)) {
5210 error_ = Error::Read;
3932 return false; 5211 return false;
3933 } 5212 }
3934 5213
3935 if (res.get_header_value("Connection") == "close" ||
3936 res.version == "HTTP/1.0") {
3937 connection_close = true;
3938 }
3939
3940 if (req.response_handler) { 5214 if (req.response_handler) {
3941 if (!req.response_handler(res)) { return false; } 5215 if (!req.response_handler(res)) {
5216 error_ = Error::Canceled;
5217 return false;
5218 }
3942 } 5219 }
3943 5220
3944 // Body 5221 // Body
3945 if (req.method != "HEAD" && req.method != "CONNECT") { 5222 if (req.method != "HEAD" && req.method != "CONNECT") {
3946 ContentReceiver out = [&](const char *buf, size_t n) { 5223 auto out =
3947 if (res.body.size() + n > res.body.max_size()) { return false; } 5224 req.content_receiver
3948 res.body.append(buf, n); 5225 ? static_cast<ContentReceiverWithProgress>(
3949 return true; 5226 [&](const char *buf, size_t n, uint64_t off, uint64_t len) {
5227 auto ret = req.content_receiver(buf, n, off, len);
5228 if (!ret) { error_ = Error::Canceled; }
5229 return ret;
5230 })
5231 : static_cast<ContentReceiverWithProgress>(
5232 [&](const char *buf, size_t n, uint64_t /*off*/,
5233 uint64_t /*len*/) {
5234 if (res.body.size() + n > res.body.max_size()) {
5235 return false;
5236 }
5237 res.body.append(buf, n);
5238 return true;
5239 });
5240
5241 auto progress = [&](uint64_t current, uint64_t total) {
5242 if (!req.progress) { return true; }
5243 auto ret = req.progress(current, total);
5244 if (!ret) { error_ = Error::Canceled; }
5245 return ret;
3950 }; 5246 };
3951 5247
3952 if (req.content_receiver) {
3953 out = [&](const char *buf, size_t n) {
3954 return req.content_receiver(buf, n);
3955 };
3956 }
3957
3958 int dummy_status; 5248 int dummy_status;
3959 if (!detail::read_content(strm, res, std::numeric_limits<size_t>::max(), 5249 if (!detail::read_content(strm, res, (std::numeric_limits<size_t>::max)(),
3960 dummy_status, req.progress, out)) { 5250 dummy_status, std::move(progress), std::move(out),
5251 decompress_)) {
5252 if (error_ != Error::Canceled) { error_ = Error::Read; }
3961 return false; 5253 return false;
3962 } 5254 }
3963 } 5255 }
3964 5256
5257 if (res.get_header_value("Connection") == "close" ||
5258 (res.version == "HTTP/1.0" && res.reason != "Connection established")) {
5259 // TODO this requires a not-entirely-obvious chain of calls to be correct
5260 // for this to be safe. Maybe a code refactor (such as moving this out to
5261 // the send function and getting rid of the recursiveness of the mutex)
5262 // could make this more obvious.
5263
5264 // This is safe to call because process_request is only called by handle_request
5265 // which is only called by send, which locks the request mutex during the process.
5266 // It would be a bug to call it from a different thread since it's a thread-safety
5267 // issue to do these things to the socket if another thread is using the socket.
5268 lock_socket_and_shutdown_and_close();
5269 }
5270
3965 // Log 5271 // Log
3966 if (logger_) { logger_(req, res); } 5272 if (logger_) { logger_(req, res); }
3967 5273
3968 return true; 5274 return true;
3969} 5275}
3970 5276
3971inline bool Client::process_and_close_socket( 5277inline bool
3972 socket_t sock, size_t request_count, 5278ClientImpl::process_socket(const Socket &socket,
3973 std::function<bool(Stream &strm, bool last_connection, 5279 std::function<bool(Stream &strm)> callback) {
3974 bool &connection_close)> 5280 return detail::process_client_socket(
3975 callback) { 5281 socket.sock, read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
3976 request_count = std::min(request_count, keep_alive_max_count_); 5282 write_timeout_usec_, std::move(callback));
3977 return detail::process_and_close_socket(true, sock, request_count,
3978 read_timeout_sec_, read_timeout_usec_,
3979 callback);
3980} 5283}
3981 5284
3982inline bool Client::is_ssl() const { return false; } 5285inline bool ClientImpl::is_ssl() const { return false; }
3983 5286
3984inline std::shared_ptr<Response> Client::Get(const char *path) { 5287inline Result ClientImpl::Get(const char *path) {
3985 return Get(path, Headers(), Progress()); 5288 return Get(path, Headers(), Progress());
3986} 5289}
3987 5290
3988inline std::shared_ptr<Response> Client::Get(const char *path, 5291inline Result ClientImpl::Get(const char *path, Progress progress) {
3989 Progress progress) {
3990 return Get(path, Headers(), std::move(progress)); 5292 return Get(path, Headers(), std::move(progress));
3991} 5293}
3992 5294
3993inline std::shared_ptr<Response> Client::Get(const char *path, 5295inline Result ClientImpl::Get(const char *path, const Headers &headers) {
3994 const Headers &headers) {
3995 return Get(path, headers, Progress()); 5296 return Get(path, headers, Progress());
3996} 5297}
3997 5298
3998inline std::shared_ptr<Response> 5299inline Result ClientImpl::Get(const char *path, const Headers &headers,
3999Client::Get(const char *path, const Headers &headers, Progress progress) { 5300 Progress progress) {
4000 Request req; 5301 Request req;
4001 req.method = "GET"; 5302 req.method = "GET";
4002 req.path = path; 5303 req.path = path;
4003 req.headers = headers; 5304 req.headers = default_headers_;
5305 req.headers.insert(headers.begin(), headers.end());
4004 req.progress = std::move(progress); 5306 req.progress = std::move(progress);
4005 5307
4006 auto res = std::make_shared<Response>(); 5308 auto res = detail::make_unique<Response>();
4007 return send(req, *res) ? res : nullptr; 5309 auto ret = send(req, *res);
5310 return Result{ret ? std::move(res) : nullptr, get_last_error()};
4008} 5311}
4009 5312
4010inline std::shared_ptr<Response> Client::Get(const char *path, 5313inline Result ClientImpl::Get(const char *path,
4011 ContentReceiver content_receiver) { 5314 ContentReceiver content_receiver) {
4012 return Get(path, Headers(), nullptr, std::move(content_receiver), Progress()); 5315 return Get(path, Headers(), nullptr, std::move(content_receiver), nullptr);
4013} 5316}
4014 5317
4015inline std::shared_ptr<Response> Client::Get(const char *path, 5318inline Result ClientImpl::Get(const char *path,
4016 ContentReceiver content_receiver, 5319 ContentReceiver content_receiver,
4017 Progress progress) { 5320 Progress progress) {
4018 return Get(path, Headers(), nullptr, std::move(content_receiver), 5321 return Get(path, Headers(), nullptr, std::move(content_receiver),
4019 std::move(progress)); 5322 std::move(progress));
4020} 5323}
4021 5324
4022inline std::shared_ptr<Response> Client::Get(const char *path, 5325inline Result ClientImpl::Get(const char *path, const Headers &headers,
4023 const Headers &headers, 5326 ContentReceiver content_receiver) {
4024 ContentReceiver content_receiver) { 5327 return Get(path, headers, nullptr, std::move(content_receiver), nullptr);
4025 return Get(path, headers, nullptr, std::move(content_receiver), Progress());
4026} 5328}
4027 5329
4028inline std::shared_ptr<Response> Client::Get(const char *path, 5330inline Result ClientImpl::Get(const char *path, const Headers &headers,
4029 const Headers &headers, 5331 ContentReceiver content_receiver,
4030 ContentReceiver content_receiver, 5332 Progress progress) {
4031 Progress progress) {
4032 return Get(path, headers, nullptr, std::move(content_receiver), 5333 return Get(path, headers, nullptr, std::move(content_receiver),
4033 std::move(progress)); 5334 std::move(progress));
4034} 5335}
4035 5336
4036inline std::shared_ptr<Response> Client::Get(const char *path, 5337inline Result ClientImpl::Get(const char *path,
4037 const Headers &headers, 5338 ResponseHandler response_handler,
4038 ResponseHandler response_handler, 5339 ContentReceiver content_receiver) {
4039 ContentReceiver content_receiver) { 5340 return Get(path, Headers(), std::move(response_handler),
4040 return Get(path, headers, std::move(response_handler), content_receiver, 5341 std::move(content_receiver), nullptr);
4041 Progress()); 5342}
5343
5344inline Result ClientImpl::Get(const char *path, const Headers &headers,
5345 ResponseHandler response_handler,
5346 ContentReceiver content_receiver) {
5347 return Get(path, headers, std::move(response_handler),
5348 std::move(content_receiver), nullptr);
5349}
5350
5351inline Result ClientImpl::Get(const char *path,
5352 ResponseHandler response_handler,
5353 ContentReceiver content_receiver,
5354 Progress progress) {
5355 return Get(path, Headers(), std::move(response_handler),
5356 std::move(content_receiver), std::move(progress));
4042} 5357}
4043 5358
4044inline std::shared_ptr<Response> Client::Get(const char *path, 5359inline Result ClientImpl::Get(const char *path, const Headers &headers,
4045 const Headers &headers, 5360 ResponseHandler response_handler,
4046 ResponseHandler response_handler, 5361 ContentReceiver content_receiver,
4047 ContentReceiver content_receiver, 5362 Progress progress) {
4048 Progress progress) {
4049 Request req; 5363 Request req;
4050 req.method = "GET"; 5364 req.method = "GET";
4051 req.path = path; 5365 req.path = path;
4052 req.headers = headers; 5366 req.headers = default_headers_;
5367 req.headers.insert(headers.begin(), headers.end());
4053 req.response_handler = std::move(response_handler); 5368 req.response_handler = std::move(response_handler);
4054 req.content_receiver = std::move(content_receiver); 5369 req.content_receiver =
5370 [content_receiver](const char *data, size_t data_length,
5371 uint64_t /*offset*/, uint64_t /*total_length*/) {
5372 return content_receiver(data, data_length);
5373 };
4055 req.progress = std::move(progress); 5374 req.progress = std::move(progress);
4056 5375
4057 auto res = std::make_shared<Response>(); 5376 auto res = detail::make_unique<Response>();
4058 return send(req, *res) ? res : nullptr; 5377 auto ret = send(req, *res);
5378 return Result{ret ? std::move(res) : nullptr, get_last_error()};
4059} 5379}
4060 5380
4061inline std::shared_ptr<Response> Client::Head(const char *path) { 5381inline Result ClientImpl::Head(const char *path) {
4062 return Head(path, Headers()); 5382 return Head(path, Headers());
4063} 5383}
4064 5384
4065inline std::shared_ptr<Response> Client::Head(const char *path, 5385inline Result ClientImpl::Head(const char *path, const Headers &headers) {
4066 const Headers &headers) {
4067 Request req; 5386 Request req;
4068 req.method = "HEAD"; 5387 req.method = "HEAD";
4069 req.headers = headers; 5388 req.headers = default_headers_;
5389 req.headers.insert(headers.begin(), headers.end());
4070 req.path = path; 5390 req.path = path;
4071 5391
4072 auto res = std::make_shared<Response>(); 5392 auto res = detail::make_unique<Response>();
5393 auto ret = send(req, *res);
5394 return Result{ret ? std::move(res) : nullptr, get_last_error()};
5395}
4073 5396
4074 return send(req, *res) ? res : nullptr; 5397inline Result ClientImpl::Post(const char *path) {
5398 return Post(path, std::string(), nullptr);
4075} 5399}
4076 5400
4077inline std::shared_ptr<Response> Client::Post(const char *path, 5401inline Result ClientImpl::Post(const char *path, const std::string &body,
4078 const std::string &body, 5402 const char *content_type) {
4079 const char *content_type) {
4080 return Post(path, Headers(), body, content_type); 5403 return Post(path, Headers(), body, content_type);
4081} 5404}
4082 5405
4083inline std::shared_ptr<Response> Client::Post(const char *path, 5406inline Result ClientImpl::Post(const char *path, const Headers &headers,
4084 const Headers &headers, 5407 const std::string &body,
4085 const std::string &body, 5408 const char *content_type) {
4086 const char *content_type) { 5409 auto ret = send_with_content_provider("POST", path, headers, body, 0, nullptr,
4087 return send_with_content_provider("POST", path, headers, body, 0, nullptr, 5410 content_type);
4088 content_type); 5411 return Result{std::move(ret), get_last_error()};
4089} 5412}
4090 5413
4091inline std::shared_ptr<Response> Client::Post(const char *path, 5414inline Result ClientImpl::Post(const char *path, const Params &params) {
4092 const Params &params) {
4093 return Post(path, Headers(), params); 5415 return Post(path, Headers(), params);
4094} 5416}
4095 5417
4096inline std::shared_ptr<Response> Client::Post(const char *path, 5418inline Result ClientImpl::Post(const char *path, size_t content_length,
4097 size_t content_length, 5419 ContentProvider content_provider,
4098 ContentProvider content_provider, 5420 const char *content_type) {
4099 const char *content_type) { 5421 return Post(path, Headers(), content_length, std::move(content_provider),
4100 return Post(path, Headers(), content_length, content_provider, content_type); 5422 content_type);
4101} 5423}
4102 5424
4103inline std::shared_ptr<Response> 5425inline Result ClientImpl::Post(const char *path, const Headers &headers,
4104Client::Post(const char *path, const Headers &headers, size_t content_length, 5426 size_t content_length,
4105 ContentProvider content_provider, const char *content_type) { 5427 ContentProvider content_provider,
4106 return send_with_content_provider("POST", path, headers, std::string(), 5428 const char *content_type) {
4107 content_length, content_provider, 5429 auto ret = send_with_content_provider(
4108 content_type); 5430 "POST", path, headers, std::string(), content_length,
5431 std::move(content_provider), content_type);
5432 return Result{std::move(ret), get_last_error()};
4109} 5433}
4110 5434
4111inline std::shared_ptr<Response> 5435inline Result ClientImpl::Post(const char *path, const Headers &headers,
4112Client::Post(const char *path, const Headers &headers, const Params &params) { 5436 const Params &params) {
4113 std::string query; 5437 auto query = detail::params_to_query_str(params);
4114 for (auto it = params.begin(); it != params.end(); ++it) {
4115 if (it != params.begin()) { query += "&"; }
4116 query += it->first;
4117 query += "=";
4118 query += detail::encode_url(it->second);
4119 }
4120
4121 return Post(path, headers, query, "application/x-www-form-urlencoded"); 5438 return Post(path, headers, query, "application/x-www-form-urlencoded");
4122} 5439}
4123 5440
4124inline std::shared_ptr<Response> 5441inline Result ClientImpl::Post(const char *path,
4125Client::Post(const char *path, const MultipartFormDataItems &items) { 5442 const MultipartFormDataItems &items) {
4126 return Post(path, Headers(), items); 5443 return Post(path, Headers(), items);
4127} 5444}
4128 5445
4129inline std::shared_ptr<Response> 5446inline Result ClientImpl::Post(const char *path, const Headers &headers,
4130Client::Post(const char *path, const Headers &headers, 5447 const MultipartFormDataItems &items) {
4131 const MultipartFormDataItems &items) { 5448 return Post(path, headers, items, detail::make_multipart_data_boundary());
4132 auto boundary = detail::make_multipart_data_boundary(); 5449}
5450inline Result ClientImpl::Post(const char *path, const Headers &headers,
5451 const MultipartFormDataItems &items,
5452 const std::string &boundary) {
5453 for (size_t i = 0; i < boundary.size(); i++) {
5454 char c = boundary[i];
5455 if (!std::isalnum(c) && c != '-' && c != '_') {
5456 error_ = Error::UnsupportedMultipartBoundaryChars;
5457 return Result{nullptr, error_};
5458 }
5459 }
4133 5460
4134 std::string body; 5461 std::string body;
4135 5462
@@ -4153,182 +5480,240 @@ Client::Post(const char *path, const Headers &headers,
4153 return Post(path, headers, body, content_type.c_str()); 5480 return Post(path, headers, body, content_type.c_str());
4154} 5481}
4155 5482
4156inline std::shared_ptr<Response> Client::Put(const char *path, 5483inline Result ClientImpl::Put(const char *path) {
4157 const std::string &body, 5484 return Put(path, std::string(), nullptr);
4158 const char *content_type) { 5485}
5486
5487inline Result ClientImpl::Put(const char *path, const std::string &body,
5488 const char *content_type) {
4159 return Put(path, Headers(), body, content_type); 5489 return Put(path, Headers(), body, content_type);
4160} 5490}
4161 5491
4162inline std::shared_ptr<Response> Client::Put(const char *path, 5492inline Result ClientImpl::Put(const char *path, const Headers &headers,
4163 const Headers &headers, 5493 const std::string &body,
4164 const std::string &body, 5494 const char *content_type) {
4165 const char *content_type) { 5495 auto ret = send_with_content_provider("PUT", path, headers, body, 0, nullptr,
4166 return send_with_content_provider("PUT", path, headers, body, 0, nullptr, 5496 content_type);
4167 content_type); 5497 return Result{std::move(ret), get_last_error()};
4168} 5498}
4169 5499
4170inline std::shared_ptr<Response> Client::Put(const char *path, 5500inline Result ClientImpl::Put(const char *path, size_t content_length,
4171 size_t content_length, 5501 ContentProvider content_provider,
4172 ContentProvider content_provider, 5502 const char *content_type) {
4173 const char *content_type) { 5503 return Put(path, Headers(), content_length, std::move(content_provider),
4174 return Put(path, Headers(), content_length, content_provider, content_type); 5504 content_type);
4175} 5505}
4176 5506
4177inline std::shared_ptr<Response> 5507inline Result ClientImpl::Put(const char *path, const Headers &headers,
4178Client::Put(const char *path, const Headers &headers, size_t content_length, 5508 size_t content_length,
4179 ContentProvider content_provider, const char *content_type) { 5509 ContentProvider content_provider,
4180 return send_with_content_provider("PUT", path, headers, std::string(), 5510 const char *content_type) {
4181 content_length, content_provider, 5511 auto ret = send_with_content_provider(
4182 content_type); 5512 "PUT", path, headers, std::string(), content_length,
5513 std::move(content_provider), content_type);
5514 return Result{std::move(ret), get_last_error()};
4183} 5515}
4184 5516
4185inline std::shared_ptr<Response> Client::Put(const char *path, 5517inline Result ClientImpl::Put(const char *path, const Params &params) {
4186 const Params &params) {
4187 return Put(path, Headers(), params); 5518 return Put(path, Headers(), params);
4188} 5519}
4189 5520
4190inline std::shared_ptr<Response> 5521inline Result ClientImpl::Put(const char *path, const Headers &headers,
4191Client::Put(const char *path, const Headers &headers, const Params &params) { 5522 const Params &params) {
4192 std::string query; 5523 auto query = detail::params_to_query_str(params);
4193 for (auto it = params.begin(); it != params.end(); ++it) {
4194 if (it != params.begin()) { query += "&"; }
4195 query += it->first;
4196 query += "=";
4197 query += detail::encode_url(it->second);
4198 }
4199
4200 return Put(path, headers, query, "application/x-www-form-urlencoded"); 5524 return Put(path, headers, query, "application/x-www-form-urlencoded");
4201} 5525}
4202 5526
4203inline std::shared_ptr<Response> Client::Patch(const char *path, 5527inline Result ClientImpl::Patch(const char *path, const std::string &body,
4204 const std::string &body, 5528 const char *content_type) {
4205 const char *content_type) {
4206 return Patch(path, Headers(), body, content_type); 5529 return Patch(path, Headers(), body, content_type);
4207} 5530}
4208 5531
4209inline std::shared_ptr<Response> Client::Patch(const char *path, 5532inline Result ClientImpl::Patch(const char *path, const Headers &headers,
4210 const Headers &headers, 5533 const std::string &body,
4211 const std::string &body, 5534 const char *content_type) {
4212 const char *content_type) { 5535 auto ret = send_with_content_provider("PATCH", path, headers, body, 0,
4213 return send_with_content_provider("PATCH", path, headers, body, 0, nullptr, 5536 nullptr, content_type);
4214 content_type); 5537 return Result{std::move(ret), get_last_error()};
4215} 5538}
4216 5539
4217inline std::shared_ptr<Response> Client::Patch(const char *path, 5540inline Result ClientImpl::Patch(const char *path, size_t content_length,
4218 size_t content_length, 5541 ContentProvider content_provider,
4219 ContentProvider content_provider, 5542 const char *content_type) {
4220 const char *content_type) { 5543 return Patch(path, Headers(), content_length, std::move(content_provider),
4221 return Patch(path, Headers(), content_length, content_provider, content_type); 5544 content_type);
4222} 5545}
4223 5546
4224inline std::shared_ptr<Response> 5547inline Result ClientImpl::Patch(const char *path, const Headers &headers,
4225Client::Patch(const char *path, const Headers &headers, size_t content_length, 5548 size_t content_length,
4226 ContentProvider content_provider, const char *content_type) { 5549 ContentProvider content_provider,
4227 return send_with_content_provider("PATCH", path, headers, std::string(), 5550 const char *content_type) {
4228 content_length, content_provider, 5551 auto ret = send_with_content_provider(
4229 content_type); 5552 "PATCH", path, headers, std::string(), content_length,
5553 std::move(content_provider), content_type);
5554 return Result{std::move(ret), get_last_error()};
4230} 5555}
4231 5556
4232inline std::shared_ptr<Response> Client::Delete(const char *path) { 5557inline Result ClientImpl::Delete(const char *path) {
4233 return Delete(path, Headers(), std::string(), nullptr); 5558 return Delete(path, Headers(), std::string(), nullptr);
4234} 5559}
4235 5560
4236inline std::shared_ptr<Response> Client::Delete(const char *path, 5561inline Result ClientImpl::Delete(const char *path, const std::string &body,
4237 const std::string &body, 5562 const char *content_type) {
4238 const char *content_type) {
4239 return Delete(path, Headers(), body, content_type); 5563 return Delete(path, Headers(), body, content_type);
4240} 5564}
4241 5565
4242inline std::shared_ptr<Response> Client::Delete(const char *path, 5566inline Result ClientImpl::Delete(const char *path, const Headers &headers) {
4243 const Headers &headers) {
4244 return Delete(path, headers, std::string(), nullptr); 5567 return Delete(path, headers, std::string(), nullptr);
4245} 5568}
4246 5569
4247inline std::shared_ptr<Response> Client::Delete(const char *path, 5570inline Result ClientImpl::Delete(const char *path, const Headers &headers,
4248 const Headers &headers, 5571 const std::string &body,
4249 const std::string &body, 5572 const char *content_type) {
4250 const char *content_type) {
4251 Request req; 5573 Request req;
4252 req.method = "DELETE"; 5574 req.method = "DELETE";
4253 req.headers = headers; 5575 req.headers = default_headers_;
5576 req.headers.insert(headers.begin(), headers.end());
4254 req.path = path; 5577 req.path = path;
4255 5578
4256 if (content_type) { req.headers.emplace("Content-Type", content_type); } 5579 if (content_type) { req.headers.emplace("Content-Type", content_type); }
4257 req.body = body; 5580 req.body = body;
4258 5581
4259 auto res = std::make_shared<Response>(); 5582 auto res = detail::make_unique<Response>();
4260 5583 auto ret = send(req, *res);
4261 return send(req, *res) ? res : nullptr; 5584 return Result{ret ? std::move(res) : nullptr, get_last_error()};
4262} 5585}
4263 5586
4264inline std::shared_ptr<Response> Client::Options(const char *path) { 5587inline Result ClientImpl::Options(const char *path) {
4265 return Options(path, Headers()); 5588 return Options(path, Headers());
4266} 5589}
4267 5590
4268inline std::shared_ptr<Response> Client::Options(const char *path, 5591inline Result ClientImpl::Options(const char *path, const Headers &headers) {
4269 const Headers &headers) {
4270 Request req; 5592 Request req;
4271 req.method = "OPTIONS"; 5593 req.method = "OPTIONS";
5594 req.headers = default_headers_;
5595 req.headers.insert(headers.begin(), headers.end());
4272 req.path = path; 5596 req.path = path;
4273 req.headers = headers;
4274 5597
4275 auto res = std::make_shared<Response>(); 5598 auto res = detail::make_unique<Response>();
5599 auto ret = send(req, *res);
5600 return Result{ret ? std::move(res) : nullptr, get_last_error()};
5601}
4276 5602
4277 return send(req, *res) ? res : nullptr; 5603inline size_t ClientImpl::is_socket_open() const {
5604 std::lock_guard<std::mutex> guard(socket_mutex_);
5605 return socket_.is_open();
4278} 5606}
4279 5607
4280inline void Client::set_timeout_sec(time_t timeout_sec) { 5608inline void ClientImpl::stop() {
4281 timeout_sec_ = timeout_sec; 5609 std::lock_guard<std::mutex> guard(socket_mutex_);
5610 // There is no guarantee that this doesn't get overwritten later, but set it so that
5611 // there is a good chance that any threads stopping as a result pick up this error.
5612 error_ = Error::Canceled;
5613
5614 // If there is anything ongoing right now, the ONLY thread-safe thing we can do
5615 // is to shutdown_socket, so that threads using this socket suddenly discover
5616 // they can't read/write any more and error out.
5617 // Everything else (closing the socket, shutting ssl down) is unsafe because these
5618 // actions are not thread-safe.
5619 if (socket_requests_in_flight_ > 0) {
5620 shutdown_socket(socket_);
5621 // Aside from that, we set a flag for the socket to be closed when we're done.
5622 socket_should_be_closed_when_request_is_done_ = true;
5623 return;
5624 }
5625
5626 //Otherwise, sitll holding the mutex, we can shut everything down ourselves
5627 shutdown_ssl(socket_, true);
5628 shutdown_socket(socket_);
5629 close_socket(socket_);
4282} 5630}
4283 5631
4284inline void Client::set_read_timeout(time_t sec, time_t usec) { 5632inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) {
5633 connection_timeout_sec_ = sec;
5634 connection_timeout_usec_ = usec;
5635}
5636
5637inline void ClientImpl::set_read_timeout(time_t sec, time_t usec) {
4285 read_timeout_sec_ = sec; 5638 read_timeout_sec_ = sec;
4286 read_timeout_usec_ = usec; 5639 read_timeout_usec_ = usec;
4287} 5640}
4288 5641
4289inline void Client::set_keep_alive_max_count(size_t count) { 5642inline void ClientImpl::set_write_timeout(time_t sec, time_t usec) {
4290 keep_alive_max_count_ = count; 5643 write_timeout_sec_ = sec;
5644 write_timeout_usec_ = usec;
4291} 5645}
4292 5646
4293inline void Client::set_basic_auth(const char *username, const char *password) { 5647inline void ClientImpl::set_basic_auth(const char *username,
5648 const char *password) {
4294 basic_auth_username_ = username; 5649 basic_auth_username_ = username;
4295 basic_auth_password_ = password; 5650 basic_auth_password_ = password;
4296} 5651}
4297 5652
5653inline void ClientImpl::set_bearer_token_auth(const char *token) {
5654 bearer_token_auth_token_ = token;
5655}
5656
4298#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 5657#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4299inline void Client::set_digest_auth(const char *username, 5658inline void ClientImpl::set_digest_auth(const char *username,
4300 const char *password) { 5659 const char *password) {
4301 digest_auth_username_ = username; 5660 digest_auth_username_ = username;
4302 digest_auth_password_ = password; 5661 digest_auth_password_ = password;
4303} 5662}
4304#endif 5663#endif
4305 5664
4306inline void Client::set_follow_location(bool on) { follow_location_ = on; } 5665inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; }
4307 5666
4308inline void Client::set_compress(bool on) { compress_ = on; } 5667inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; }
4309 5668
4310inline void Client::set_interface(const char *intf) { interface_ = intf; } 5669inline void ClientImpl::set_default_headers(Headers headers) {
5670 default_headers_ = std::move(headers);
5671}
4311 5672
4312inline void Client::set_proxy(const char *host, int port) { 5673inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
5674
5675inline void ClientImpl::set_socket_options(SocketOptions socket_options) {
5676 socket_options_ = std::move(socket_options);
5677}
5678
5679inline void ClientImpl::set_compress(bool on) { compress_ = on; }
5680
5681inline void ClientImpl::set_decompress(bool on) { decompress_ = on; }
5682
5683inline void ClientImpl::set_interface(const char *intf) { interface_ = intf; }
5684
5685inline void ClientImpl::set_proxy(const char *host, int port) {
4313 proxy_host_ = host; 5686 proxy_host_ = host;
4314 proxy_port_ = port; 5687 proxy_port_ = port;
4315} 5688}
4316 5689
4317inline void Client::set_proxy_basic_auth(const char *username, 5690inline void ClientImpl::set_proxy_basic_auth(const char *username,
4318 const char *password) { 5691 const char *password) {
4319 proxy_basic_auth_username_ = username; 5692 proxy_basic_auth_username_ = username;
4320 proxy_basic_auth_password_ = password; 5693 proxy_basic_auth_password_ = password;
4321} 5694}
4322 5695
5696inline void ClientImpl::set_proxy_bearer_token_auth(const char *token) {
5697 proxy_bearer_token_auth_token_ = token;
5698}
5699
4323#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 5700#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4324inline void Client::set_proxy_digest_auth(const char *username, 5701inline void ClientImpl::set_proxy_digest_auth(const char *username,
4325 const char *password) { 5702 const char *password) {
4326 proxy_digest_auth_username_ = username; 5703 proxy_digest_auth_username_ = username;
4327 proxy_digest_auth_password_ = password; 5704 proxy_digest_auth_password_ = password;
4328} 5705}
4329#endif 5706#endif
4330 5707
4331inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); } 5708#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
5709inline void ClientImpl::enable_server_certificate_verification(bool enabled) {
5710 server_certificate_verification_ = enabled;
5711}
5712#endif
5713
5714inline void ClientImpl::set_logger(Logger logger) {
5715 logger_ = std::move(logger);
5716}
4332 5717
4333/* 5718/*
4334 * SSL Implementation 5719 * SSL Implementation
@@ -4336,72 +5721,69 @@ inline void Client::set_logger(Logger logger) { logger_ = std::move(logger); }
4336#ifdef CPPHTTPLIB_OPENSSL_SUPPORT 5721#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
4337namespace detail { 5722namespace detail {
4338 5723
4339template <typename U, typename V, typename T> 5724template <typename U, typename V>
4340inline bool process_and_close_socket_ssl( 5725inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
4341 bool is_client_request, socket_t sock, size_t keep_alive_max_count, 5726 U SSL_connect_or_accept, V setup) {
4342 time_t read_timeout_sec, time_t read_timeout_usec, SSL_CTX *ctx,
4343 std::mutex &ctx_mutex, U SSL_connect_or_accept, V setup, T callback) {
4344 assert(keep_alive_max_count > 0);
4345
4346 SSL *ssl = nullptr; 5727 SSL *ssl = nullptr;
4347 { 5728 {
4348 std::lock_guard<std::mutex> guard(ctx_mutex); 5729 std::lock_guard<std::mutex> guard(ctx_mutex);
4349 ssl = SSL_new(ctx); 5730 ssl = SSL_new(ctx);
4350 } 5731 }
4351 5732
4352 if (!ssl) { 5733 if (ssl) {
4353 close_socket(sock); 5734 auto bio = BIO_new_socket(static_cast<int>(sock), BIO_NOCLOSE);
4354 return false; 5735 SSL_set_bio(ssl, bio, bio);
4355 }
4356 5736
4357 auto bio = BIO_new_socket(static_cast<int>(sock), BIO_NOCLOSE); 5737 if (!setup(ssl) || SSL_connect_or_accept(ssl) != 1) {
4358 SSL_set_bio(ssl, bio, bio); 5738 SSL_shutdown(ssl);
4359 5739 {
4360 if (!setup(ssl)) { 5740 std::lock_guard<std::mutex> guard(ctx_mutex);
4361 SSL_shutdown(ssl); 5741 SSL_free(ssl);
4362 { 5742 }
4363 std::lock_guard<std::mutex> guard(ctx_mutex); 5743 return nullptr;
4364 SSL_free(ssl);
4365 } 5744 }
4366
4367 close_socket(sock);
4368 return false;
4369 } 5745 }
4370 5746
4371 auto ret = false; 5747 return ssl;
5748}
4372 5749
4373 if (SSL_connect_or_accept(ssl) == 1) { 5750inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
4374 if (keep_alive_max_count > 1) { 5751 bool shutdown_gracefully) {
4375 auto count = keep_alive_max_count; 5752 // sometimes we may want to skip this to try to avoid SIGPIPE if we know
4376 while (count > 0 && 5753 // the remote has closed the network connection
4377 (is_client_request || 5754 // Note that it is not always possible to avoid SIGPIPE, this is merely a best-efforts.
4378 detail::select_read(sock, CPPHTTPLIB_KEEPALIVE_TIMEOUT_SECOND, 5755 if (shutdown_gracefully) {
4379 CPPHTTPLIB_KEEPALIVE_TIMEOUT_USECOND) > 0)) { 5756 SSL_shutdown(ssl);
4380 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
4381 auto last_connection = count == 1;
4382 auto connection_close = false;
4383
4384 ret = callback(ssl, strm, last_connection, connection_close);
4385 if (!ret || connection_close) { break; }
4386
4387 count--;
4388 }
4389 } else {
4390 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec);
4391 auto dummy_connection_close = false;
4392 ret = callback(ssl, strm, true, dummy_connection_close);
4393 }
4394 } 5757 }
4395 5758
4396 SSL_shutdown(ssl); 5759 std::lock_guard<std::mutex> guard(ctx_mutex);
4397 { 5760 SSL_free(ssl);
4398 std::lock_guard<std::mutex> guard(ctx_mutex); 5761}
4399 SSL_free(ssl);
4400 }
4401 5762
4402 close_socket(sock); 5763template <typename T>
5764inline bool
5765process_server_socket_ssl(SSL *ssl, socket_t sock, size_t keep_alive_max_count,
5766 time_t keep_alive_timeout_sec,
5767 time_t read_timeout_sec, time_t read_timeout_usec,
5768 time_t write_timeout_sec, time_t write_timeout_usec,
5769 T callback) {
5770 return process_server_socket_core(
5771 sock, keep_alive_max_count, keep_alive_timeout_sec,
5772 [&](bool close_connection, bool &connection_closed) {
5773 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
5774 write_timeout_sec, write_timeout_usec);
5775 return callback(strm, close_connection, connection_closed);
5776 });
5777}
4403 5778
4404 return ret; 5779template <typename T>
5780inline bool
5781process_client_socket_ssl(SSL *ssl, socket_t sock, time_t read_timeout_sec,
5782 time_t read_timeout_usec, time_t write_timeout_sec,
5783 time_t write_timeout_usec, T callback) {
5784 SSLSocketStream strm(sock, ssl, read_timeout_sec, read_timeout_usec,
5785 write_timeout_sec, write_timeout_usec);
5786 return callback(strm);
4405} 5787}
4406 5788
4407#if OPENSSL_VERSION_NUMBER < 0x10100000L 5789#if OPENSSL_VERSION_NUMBER < 0x10100000L
@@ -4420,11 +5802,11 @@ public:
4420private: 5802private:
4421 static void locking_callback(int mode, int type, const char * /*file*/, 5803 static void locking_callback(int mode, int type, const char * /*file*/,
4422 int /*line*/) { 5804 int /*line*/) {
4423 auto &locks = *openSSL_locks_; 5805 auto &lk = (*openSSL_locks_)[static_cast<size_t>(type)];
4424 if (mode & CRYPTO_LOCK) { 5806 if (mode & CRYPTO_LOCK) {
4425 locks[type].lock(); 5807 lk.lock();
4426 } else { 5808 } else {
4427 locks[type].unlock(); 5809 lk.unlock();
4428 } 5810 }
4429 } 5811 }
4430}; 5812};
@@ -4458,9 +5840,15 @@ private:
4458// SSL socket stream implementation 5840// SSL socket stream implementation
4459inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl, 5841inline SSLSocketStream::SSLSocketStream(socket_t sock, SSL *ssl,
4460 time_t read_timeout_sec, 5842 time_t read_timeout_sec,
4461 time_t read_timeout_usec) 5843 time_t read_timeout_usec,
5844 time_t write_timeout_sec,
5845 time_t write_timeout_usec)
4462 : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec), 5846 : sock_(sock), ssl_(ssl), read_timeout_sec_(read_timeout_sec),
4463 read_timeout_usec_(read_timeout_usec) {} 5847 read_timeout_usec_(read_timeout_usec),
5848 write_timeout_sec_(write_timeout_sec),
5849 write_timeout_usec_(write_timeout_usec) {
5850 SSL_clear_mode(ssl, SSL_MODE_AUTO_RETRY);
5851}
4464 5852
4465inline SSLSocketStream::~SSLSocketStream() {} 5853inline SSLSocketStream::~SSLSocketStream() {}
4466 5854
@@ -4469,24 +5857,44 @@ inline bool SSLSocketStream::is_readable() const {
4469} 5857}
4470 5858
4471inline bool SSLSocketStream::is_writable() const { 5859inline bool SSLSocketStream::is_writable() const {
4472 return detail::select_write(sock_, 0, 0) > 0; 5860 return detail::select_write(sock_, write_timeout_sec_, write_timeout_usec_) >
5861 0;
4473} 5862}
4474 5863
4475inline int SSLSocketStream::read(char *ptr, size_t size) { 5864inline ssize_t SSLSocketStream::read(char *ptr, size_t size) {
4476 if (SSL_pending(ssl_) > 0 || 5865 if (SSL_pending(ssl_) > 0) {
4477 select_read(sock_, read_timeout_sec_, read_timeout_usec_) > 0) {
4478 return SSL_read(ssl_, ptr, static_cast<int>(size)); 5866 return SSL_read(ssl_, ptr, static_cast<int>(size));
5867 } else if (is_readable()) {
5868 auto ret = SSL_read(ssl_, ptr, static_cast<int>(size));
5869 if (ret < 0) {
5870 auto err = SSL_get_error(ssl_, ret);
5871 while (err == SSL_ERROR_WANT_READ) {
5872 if (SSL_pending(ssl_) > 0) {
5873 return SSL_read(ssl_, ptr, static_cast<int>(size));
5874 } else if (is_readable()) {
5875 ret = SSL_read(ssl_, ptr, static_cast<int>(size));
5876 if (ret >= 0) {
5877 return ret;
5878 }
5879 err = SSL_get_error(ssl_, ret);
5880 } else {
5881 return -1;
5882 }
5883 }
5884 }
5885 return ret;
4479 } 5886 }
4480 return -1; 5887 return -1;
4481} 5888}
4482 5889
4483inline int SSLSocketStream::write(const char *ptr, size_t size) { 5890inline ssize_t SSLSocketStream::write(const char *ptr, size_t size) {
4484 if (is_writable()) { return SSL_write(ssl_, ptr, static_cast<int>(size)); } 5891 if (is_writable()) { return SSL_write(ssl_, ptr, static_cast<int>(size)); }
4485 return -1; 5892 return -1;
4486} 5893}
4487 5894
4488inline std::string SSLSocketStream::get_remote_addr() const { 5895inline void SSLSocketStream::get_remote_ip_and_port(std::string &ip,
4489 return detail::get_remote_addr(sock_); 5896 int &port) const {
5897 detail::get_remote_ip_and_port(sock_, ip, port);
4490} 5898}
4491 5899
4492static SSLInit sslinit_; 5900static SSLInit sslinit_;
@@ -4532,6 +5940,33 @@ inline SSLServer::SSLServer(const char *cert_path, const char *private_key_path,
4532 } 5940 }
4533} 5941}
4534 5942
5943inline SSLServer::SSLServer(X509 *cert, EVP_PKEY *private_key,
5944 X509_STORE *client_ca_cert_store) {
5945 ctx_ = SSL_CTX_new(SSLv23_server_method());
5946
5947 if (ctx_) {
5948 SSL_CTX_set_options(ctx_,
5949 SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 |
5950 SSL_OP_NO_COMPRESSION |
5951 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
5952
5953 if (SSL_CTX_use_certificate(ctx_, cert) != 1 ||
5954 SSL_CTX_use_PrivateKey(ctx_, private_key) != 1) {
5955 SSL_CTX_free(ctx_);
5956 ctx_ = nullptr;
5957 } else if (client_ca_cert_store) {
5958
5959 SSL_CTX_set_cert_store(ctx_, client_ca_cert_store);
5960
5961 SSL_CTX_set_verify(
5962 ctx_,
5963 SSL_VERIFY_PEER |
5964 SSL_VERIFY_FAIL_IF_NO_PEER_CERT, // SSL_VERIFY_CLIENT_ONCE,
5965 nullptr);
5966 }
5967 }
5968}
5969
4535inline SSLServer::~SSLServer() { 5970inline SSLServer::~SSLServer() {
4536 if (ctx_) { SSL_CTX_free(ctx_); } 5971 if (ctx_) { SSL_CTX_free(ctx_); }
4537} 5972}
@@ -4539,21 +5974,42 @@ inline SSLServer::~SSLServer() {
4539inline bool SSLServer::is_valid() const { return ctx_; } 5974inline bool SSLServer::is_valid() const { return ctx_; }
4540 5975
4541inline bool SSLServer::process_and_close_socket(socket_t sock) { 5976inline bool SSLServer::process_and_close_socket(socket_t sock) {
4542 return detail::process_and_close_socket_ssl( 5977 auto ssl = detail::ssl_new(sock, ctx_, ctx_mutex_, SSL_accept,
4543 false, sock, keep_alive_max_count_, read_timeout_sec_, read_timeout_usec_, 5978 [](SSL * /*ssl*/) { return true; });
4544 ctx_, ctx_mutex_, SSL_accept, [](SSL * /*ssl*/) { return true; }, 5979
4545 [this](SSL *ssl, Stream &strm, bool last_connection, 5980 if (ssl) {
4546 bool &connection_close) { 5981 auto ret = detail::process_server_socket_ssl(
4547 return process_request(strm, last_connection, connection_close, 5982 ssl, sock, keep_alive_max_count_, keep_alive_timeout_sec_,
4548 [&](Request &req) { req.ssl = ssl; }); 5983 read_timeout_sec_, read_timeout_usec_, write_timeout_sec_,
4549 }); 5984 write_timeout_usec_,
5985 [this, ssl](Stream &strm, bool close_connection,
5986 bool &connection_closed) {
5987 return process_request(strm, close_connection, connection_closed,
5988 [&](Request &req) { req.ssl = ssl; });
5989 });
5990
5991 detail::ssl_delete(ctx_mutex_, ssl, ret);
5992 detail::shutdown_socket(sock);
5993 detail::close_socket(sock);
5994 return ret;
5995 }
5996
5997 detail::shutdown_socket(sock);
5998 detail::close_socket(sock);
5999 return false;
4550} 6000}
4551 6001
4552// SSL HTTP client implementation 6002// SSL HTTP client implementation
6003inline SSLClient::SSLClient(const std::string &host)
6004 : SSLClient(host, 443, std::string(), std::string()) {}
6005
6006inline SSLClient::SSLClient(const std::string &host, int port)
6007 : SSLClient(host, port, std::string(), std::string()) {}
6008
4553inline SSLClient::SSLClient(const std::string &host, int port, 6009inline SSLClient::SSLClient(const std::string &host, int port,
4554 const std::string &client_cert_path, 6010 const std::string &client_cert_path,
4555 const std::string &client_key_path) 6011 const std::string &client_key_path)
4556 : Client(host, port, client_cert_path, client_key_path) { 6012 : ClientImpl(host, port, client_cert_path, client_key_path) {
4557 ctx_ = SSL_CTX_new(SSLv23_client_method()); 6013 ctx_ = SSL_CTX_new(SSLv23_client_method());
4558 6014
4559 detail::split(&host_[0], &host_[host_.size()], '.', 6015 detail::split(&host_[0], &host_[host_.size()], '.',
@@ -4571,8 +6027,30 @@ inline SSLClient::SSLClient(const std::string &host, int port,
4571 } 6027 }
4572} 6028}
4573 6029
6030inline SSLClient::SSLClient(const std::string &host, int port,
6031 X509 *client_cert, EVP_PKEY *client_key)
6032 : ClientImpl(host, port) {
6033 ctx_ = SSL_CTX_new(SSLv23_client_method());
6034
6035 detail::split(&host_[0], &host_[host_.size()], '.',
6036 [&](const char *b, const char *e) {
6037 host_components_.emplace_back(std::string(b, e));
6038 });
6039 if (client_cert != nullptr && client_key != nullptr) {
6040 if (SSL_CTX_use_certificate(ctx_, client_cert) != 1 ||
6041 SSL_CTX_use_PrivateKey(ctx_, client_key) != 1) {
6042 SSL_CTX_free(ctx_);
6043 ctx_ = nullptr;
6044 }
6045 }
6046}
6047
4574inline SSLClient::~SSLClient() { 6048inline SSLClient::~SSLClient() {
4575 if (ctx_) { SSL_CTX_free(ctx_); } 6049 if (ctx_) { SSL_CTX_free(ctx_); }
6050 // Make sure to shut down SSL since shutdown_ssl will resolve to the
6051 // base function rather than the derived function once we get to the
6052 // base class destructor, and won't free the SSL (causing a leak).
6053 SSLClient::shutdown_ssl(socket_, true);
4576} 6054}
4577 6055
4578inline bool SSLClient::is_valid() const { return ctx_; } 6056inline bool SSLClient::is_valid() const { return ctx_; }
@@ -4583,67 +6061,188 @@ inline void SSLClient::set_ca_cert_path(const char *ca_cert_file_path,
4583 if (ca_cert_dir_path) { ca_cert_dir_path_ = ca_cert_dir_path; } 6061 if (ca_cert_dir_path) { ca_cert_dir_path_ = ca_cert_dir_path; }
4584} 6062}
4585 6063
4586inline void SSLClient::enable_server_certificate_verification(bool enabled) { 6064inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
4587 server_certificate_verification_ = enabled; 6065 if (ca_cert_store) {
6066 if (ctx_) {
6067 if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
6068 // Free memory allocated for old cert and use new store `ca_cert_store`
6069 SSL_CTX_set_cert_store(ctx_, ca_cert_store);
6070 }
6071 } else {
6072 X509_STORE_free(ca_cert_store);
6073 }
6074 }
4588} 6075}
4589 6076
4590inline long SSLClient::get_openssl_verify_result() const { 6077inline long SSLClient::get_openssl_verify_result() const {
4591 return verify_result_; 6078 return verify_result_;
4592} 6079}
4593 6080
4594inline SSL_CTX *SSLClient::ssl_context() const noexcept { return ctx_; } 6081inline SSL_CTX *SSLClient::ssl_context() const { return ctx_; }
4595 6082
4596inline bool SSLClient::process_and_close_socket( 6083inline bool SSLClient::create_and_connect_socket(Socket &socket) {
4597 socket_t sock, size_t request_count, 6084 return is_valid() && ClientImpl::create_and_connect_socket(socket);
4598 std::function<bool(Stream &strm, bool last_connection, 6085}
4599 bool &connection_close)>
4600 callback) {
4601 6086
4602 request_count = std::min(request_count, keep_alive_max_count_); 6087// Assumes that socket_mutex_ is locked and that there are no requests in flight
6088inline bool SSLClient::connect_with_proxy(Socket &socket, Response &res,
6089 bool &success) {
6090 success = true;
6091 Response res2;
6092 if (!detail::process_client_socket(
6093 socket.sock, read_timeout_sec_, read_timeout_usec_,
6094 write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) {
6095 Request req2;
6096 req2.method = "CONNECT";
6097 req2.path = host_and_port_;
6098 return process_request(strm, req2, res2, false);
6099 })) {
6100 // Thread-safe to close everything because we are assuming there are no requests in flight
6101 shutdown_ssl(socket, true);
6102 shutdown_socket(socket);
6103 close_socket(socket);
6104 success = false;
6105 return false;
6106 }
6107
6108 if (res2.status == 407) {
6109 if (!proxy_digest_auth_username_.empty() &&
6110 !proxy_digest_auth_password_.empty()) {
6111 std::map<std::string, std::string> auth;
6112 if (detail::parse_www_authenticate(res2, auth, true)) {
6113 Response res3;
6114 if (!detail::process_client_socket(
6115 socket.sock, read_timeout_sec_, read_timeout_usec_,
6116 write_timeout_sec_, write_timeout_usec_, [&](Stream &strm) {
6117 Request req3;
6118 req3.method = "CONNECT";
6119 req3.path = host_and_port_;
6120 req3.headers.insert(detail::make_digest_authentication_header(
6121 req3, auth, 1, detail::random_string(10),
6122 proxy_digest_auth_username_, proxy_digest_auth_password_,
6123 true));
6124 return process_request(strm, req3, res3, false);
6125 })) {
6126 // Thread-safe to close everything because we are assuming there are no requests in flight
6127 shutdown_ssl(socket, true);
6128 shutdown_socket(socket);
6129 close_socket(socket);
6130 success = false;
6131 return false;
6132 }
6133 }
6134 } else {
6135 res = res2;
6136 return false;
6137 }
6138 }
4603 6139
4604 return is_valid() && 6140 return true;
4605 detail::process_and_close_socket_ssl( 6141}
4606 true, sock, request_count, read_timeout_sec_, read_timeout_usec_,
4607 ctx_, ctx_mutex_,
4608 [&](SSL *ssl) {
4609 if (ca_cert_file_path_.empty()) {
4610 SSL_CTX_set_verify(ctx_, SSL_VERIFY_NONE, nullptr);
4611 } else {
4612 if (!SSL_CTX_load_verify_locations(
4613 ctx_, ca_cert_file_path_.c_str(), nullptr)) {
4614 return false;
4615 }
4616 SSL_CTX_set_verify(ctx_, SSL_VERIFY_PEER, nullptr);
4617 }
4618 6142
4619 if (SSL_connect(ssl) != 1) { return false; } 6143inline bool SSLClient::load_certs() {
6144 bool ret = true;
4620 6145
4621 if (server_certificate_verification_) { 6146 std::call_once(initialize_cert_, [&]() {
4622 verify_result_ = SSL_get_verify_result(ssl); 6147 std::lock_guard<std::mutex> guard(ctx_mutex_);
6148 if (!ca_cert_file_path_.empty()) {
6149 if (!SSL_CTX_load_verify_locations(ctx_, ca_cert_file_path_.c_str(),
6150 nullptr)) {
6151 ret = false;
6152 }
6153 } else if (!ca_cert_dir_path_.empty()) {
6154 if (!SSL_CTX_load_verify_locations(ctx_, nullptr,
6155 ca_cert_dir_path_.c_str())) {
6156 ret = false;
6157 }
6158 } else {
6159#ifdef _WIN32
6160 detail::load_system_certs_on_windows(SSL_CTX_get_cert_store(ctx_));
6161#else
6162 SSL_CTX_set_default_verify_paths(ctx_);
6163#endif
6164 }
6165 });
4623 6166
4624 if (verify_result_ != X509_V_OK) { return false; } 6167 return ret;
6168}
4625 6169
4626 auto server_cert = SSL_get_peer_certificate(ssl); 6170inline bool SSLClient::initialize_ssl(Socket &socket) {
6171 auto ssl = detail::ssl_new(
6172 socket.sock, ctx_, ctx_mutex_,
6173 [&](SSL *ssl) {
6174 if (server_certificate_verification_) {
6175 if (!load_certs()) {
6176 error_ = Error::SSLLoadingCerts;
6177 return false;
6178 }
6179 SSL_set_verify(ssl, SSL_VERIFY_NONE, nullptr);
6180 }
4627 6181
4628 if (server_cert == nullptr) { return false; } 6182 if (SSL_connect(ssl) != 1) {
6183 error_ = Error::SSLConnection;
6184 return false;
6185 }
4629 6186
4630 if (!verify_host(server_cert)) { 6187 if (server_certificate_verification_) {
4631 X509_free(server_cert); 6188 verify_result_ = SSL_get_verify_result(ssl);
4632 return false;
4633 }
4634 X509_free(server_cert);
4635 }
4636 6189
4637 return true; 6190 if (verify_result_ != X509_V_OK) {
4638 }, 6191 error_ = Error::SSLServerVerification;
4639 [&](SSL *ssl) { 6192 return false;
4640 SSL_set_tlsext_host_name(ssl, host_.c_str()); 6193 }
4641 return true; 6194
4642 }, 6195 auto server_cert = SSL_get_peer_certificate(ssl);
4643 [&](SSL * /*ssl*/, Stream &strm, bool last_connection, 6196
4644 bool &connection_close) { 6197 if (server_cert == nullptr) {
4645 return callback(strm, last_connection, connection_close); 6198 error_ = Error::SSLServerVerification;
4646 }); 6199 return false;
6200 }
6201
6202 if (!verify_host(server_cert)) {
6203 X509_free(server_cert);
6204 error_ = Error::SSLServerVerification;
6205 return false;
6206 }
6207 X509_free(server_cert);
6208 }
6209
6210 return true;
6211 },
6212 [&](SSL *ssl) {
6213 SSL_set_tlsext_host_name(ssl, host_.c_str());
6214 return true;
6215 });
6216
6217 if (ssl) {
6218 socket.ssl = ssl;
6219 return true;
6220 }
6221
6222 shutdown_socket(socket);
6223 close_socket(socket);
6224 return false;
6225}
6226
6227inline void SSLClient::shutdown_ssl(Socket &socket, bool shutdown_gracefully) {
6228 if (socket.sock == INVALID_SOCKET) {
6229 assert(socket.ssl == nullptr);
6230 return;
6231 }
6232 if (socket.ssl) {
6233 detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully);
6234 socket.ssl = nullptr;
6235 }
6236 assert(socket.ssl == nullptr);
6237}
6238
6239inline bool
6240SSLClient::process_socket(const Socket &socket,
6241 std::function<bool(Stream &strm)> callback) {
6242 assert(socket.ssl);
6243 return detail::process_client_socket_ssl(
6244 socket.ssl, socket.sock, read_timeout_sec_, read_timeout_usec_,
6245 write_timeout_sec_, write_timeout_usec_, std::move(callback));
4647} 6246}
4648 6247
4649inline bool SSLClient::is_ssl() const { return true; } 6248inline bool SSLClient::is_ssl() const { return true; }
@@ -4703,7 +6302,7 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const {
4703 6302
4704 auto count = sk_GENERAL_NAME_num(alt_names); 6303 auto count = sk_GENERAL_NAME_num(alt_names);
4705 6304
4706 for (auto i = 0; i < count && !dsn_matched; i++) { 6305 for (decltype(count) i = 0; i < count && !dsn_matched; i++) {
4707 auto val = sk_GENERAL_NAME_value(alt_names, i); 6306 auto val = sk_GENERAL_NAME_value(alt_names, i);
4708 if (val->type == type) { 6307 if (val->type == type) {
4709 auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5); 6308 auto name = (const char *)ASN1_STRING_get0_data(val->d.ia5);
@@ -4728,7 +6327,6 @@ SSLClient::verify_host_with_subject_alt_name(X509 *server_cert) const {
4728 } 6327 }
4729 6328
4730 GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names); 6329 GENERAL_NAMES_free((STACK_OF(GENERAL_NAME) *)alt_names);
4731
4732 return ret; 6330 return ret;
4733} 6331}
4734 6332
@@ -4740,7 +6338,9 @@ inline bool SSLClient::verify_host_with_common_name(X509 *server_cert) const {
4740 auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName, 6338 auto name_len = X509_NAME_get_text_by_NID(subject_name, NID_commonName,
4741 name, sizeof(name)); 6339 name, sizeof(name));
4742 6340
4743 if (name_len != -1) { return check_host_name(name, name_len); } 6341 if (name_len != -1) {
6342 return check_host_name(name, static_cast<size_t>(name_len));
6343 }
4744 } 6344 }
4745 6345
4746 return false; 6346 return false;
@@ -4775,6 +6375,338 @@ inline bool SSLClient::check_host_name(const char *pattern,
4775} 6375}
4776#endif 6376#endif
4777 6377
6378// Universal client implementation
6379inline Client::Client(const char *scheme_host_port)
6380 : Client(scheme_host_port, std::string(), std::string()) {}
6381
6382inline Client::Client(const char *scheme_host_port,
6383 const std::string &client_cert_path,
6384 const std::string &client_key_path) {
6385 const static std::regex re(R"(^(?:([a-z]+)://)?([^:/?#]+)(?::(\d+))?)");
6386
6387 std::cmatch m;
6388 if (std::regex_match(scheme_host_port, m, re)) {
6389 auto scheme = m[1].str();
6390
6391#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6392 if (!scheme.empty() && (scheme != "http" && scheme != "https")) {
6393#else
6394 if (!scheme.empty() && scheme != "http") {
6395#endif
6396 std::string msg = "'" + scheme + "' scheme is not supported.";
6397 throw std::invalid_argument(msg);
6398 return;
6399 }
6400
6401 auto is_ssl = scheme == "https";
6402
6403 auto host = m[2].str();
6404
6405 auto port_str = m[3].str();
6406 auto port = !port_str.empty() ? std::stoi(port_str) : (is_ssl ? 443 : 80);
6407
6408 if (is_ssl) {
6409#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6410 cli_ = detail::make_unique<SSLClient>(host.c_str(), port,
6411 client_cert_path, client_key_path);
6412 is_ssl_ = is_ssl;
6413#endif
6414 } else {
6415 cli_ = detail::make_unique<ClientImpl>(host.c_str(), port,
6416 client_cert_path, client_key_path);
6417 }
6418 } else {
6419 cli_ = detail::make_unique<ClientImpl>(scheme_host_port, 80,
6420 client_cert_path, client_key_path);
6421 }
6422}
6423
6424inline Client::Client(const std::string &host, int port)
6425 : cli_(detail::make_unique<ClientImpl>(host, port)) {}
6426
6427inline Client::Client(const std::string &host, int port,
6428 const std::string &client_cert_path,
6429 const std::string &client_key_path)
6430 : cli_(detail::make_unique<ClientImpl>(host, port, client_cert_path,
6431 client_key_path)) {}
6432
6433inline Client::~Client() {}
6434
6435inline bool Client::is_valid() const {
6436 return cli_ != nullptr && cli_->is_valid();
6437}
6438
6439inline Result Client::Get(const char *path) { return cli_->Get(path); }
6440inline Result Client::Get(const char *path, const Headers &headers) {
6441 return cli_->Get(path, headers);
6442}
6443inline Result Client::Get(const char *path, Progress progress) {
6444 return cli_->Get(path, std::move(progress));
6445}
6446inline Result Client::Get(const char *path, const Headers &headers,
6447 Progress progress) {
6448 return cli_->Get(path, headers, std::move(progress));
6449}
6450inline Result Client::Get(const char *path, ContentReceiver content_receiver) {
6451 return cli_->Get(path, std::move(content_receiver));
6452}
6453inline Result Client::Get(const char *path, const Headers &headers,
6454 ContentReceiver content_receiver) {
6455 return cli_->Get(path, headers, std::move(content_receiver));
6456}
6457inline Result Client::Get(const char *path, ContentReceiver content_receiver,
6458 Progress progress) {
6459 return cli_->Get(path, std::move(content_receiver), std::move(progress));
6460}
6461inline Result Client::Get(const char *path, const Headers &headers,
6462 ContentReceiver content_receiver, Progress progress) {
6463 return cli_->Get(path, headers, std::move(content_receiver),
6464 std::move(progress));
6465}
6466inline Result Client::Get(const char *path, ResponseHandler response_handler,
6467 ContentReceiver content_receiver) {
6468 return cli_->Get(path, std::move(response_handler),
6469 std::move(content_receiver));
6470}
6471inline Result Client::Get(const char *path, const Headers &headers,
6472 ResponseHandler response_handler,
6473 ContentReceiver content_receiver) {
6474 return cli_->Get(path, headers, std::move(response_handler),
6475 std::move(content_receiver));
6476}
6477inline Result Client::Get(const char *path, ResponseHandler response_handler,
6478 ContentReceiver content_receiver, Progress progress) {
6479 return cli_->Get(path, std::move(response_handler),
6480 std::move(content_receiver), std::move(progress));
6481}
6482inline Result Client::Get(const char *path, const Headers &headers,
6483 ResponseHandler response_handler,
6484 ContentReceiver content_receiver, Progress progress) {
6485 return cli_->Get(path, headers, std::move(response_handler),
6486 std::move(content_receiver), std::move(progress));
6487}
6488
6489inline Result Client::Head(const char *path) { return cli_->Head(path); }
6490inline Result Client::Head(const char *path, const Headers &headers) {
6491 return cli_->Head(path, headers);
6492}
6493
6494inline Result Client::Post(const char *path) { return cli_->Post(path); }
6495inline Result Client::Post(const char *path, const std::string &body,
6496 const char *content_type) {
6497 return cli_->Post(path, body, content_type);
6498}
6499inline Result Client::Post(const char *path, const Headers &headers,
6500 const std::string &body, const char *content_type) {
6501 return cli_->Post(path, headers, body, content_type);
6502}
6503inline Result Client::Post(const char *path, size_t content_length,
6504 ContentProvider content_provider,
6505 const char *content_type) {
6506 return cli_->Post(path, content_length, std::move(content_provider),
6507 content_type);
6508}
6509inline Result Client::Post(const char *path, const Headers &headers,
6510 size_t content_length,
6511 ContentProvider content_provider,
6512 const char *content_type) {
6513 return cli_->Post(path, headers, content_length, std::move(content_provider),
6514 content_type);
6515}
6516inline Result Client::Post(const char *path, const Params &params) {
6517 return cli_->Post(path, params);
6518}
6519inline Result Client::Post(const char *path, const Headers &headers,
6520 const Params &params) {
6521 return cli_->Post(path, headers, params);
6522}
6523inline Result Client::Post(const char *path,
6524 const MultipartFormDataItems &items) {
6525 return cli_->Post(path, items);
6526}
6527inline Result Client::Post(const char *path, const Headers &headers,
6528 const MultipartFormDataItems &items) {
6529 return cli_->Post(path, headers, items);
6530}
6531inline Result Client::Post(const char *path, const Headers &headers,
6532 const MultipartFormDataItems &items,
6533 const std::string &boundary) {
6534 return cli_->Post(path, headers, items, boundary);
6535}
6536inline Result Client::Put(const char *path) { return cli_->Put(path); }
6537inline Result Client::Put(const char *path, const std::string &body,
6538 const char *content_type) {
6539 return cli_->Put(path, body, content_type);
6540}
6541inline Result Client::Put(const char *path, const Headers &headers,
6542 const std::string &body, const char *content_type) {
6543 return cli_->Put(path, headers, body, content_type);
6544}
6545inline Result Client::Put(const char *path, size_t content_length,
6546 ContentProvider content_provider,
6547 const char *content_type) {
6548 return cli_->Put(path, content_length, std::move(content_provider),
6549 content_type);
6550}
6551inline Result Client::Put(const char *path, const Headers &headers,
6552 size_t content_length,
6553 ContentProvider content_provider,
6554 const char *content_type) {
6555 return cli_->Put(path, headers, content_length, std::move(content_provider),
6556 content_type);
6557}
6558inline Result Client::Put(const char *path, const Params &params) {
6559 return cli_->Put(path, params);
6560}
6561inline Result Client::Put(const char *path, const Headers &headers,
6562 const Params &params) {
6563 return cli_->Put(path, headers, params);
6564}
6565inline Result Client::Patch(const char *path, const std::string &body,
6566 const char *content_type) {
6567 return cli_->Patch(path, body, content_type);
6568}
6569inline Result Client::Patch(const char *path, const Headers &headers,
6570 const std::string &body, const char *content_type) {
6571 return cli_->Patch(path, headers, body, content_type);
6572}
6573inline Result Client::Patch(const char *path, size_t content_length,
6574 ContentProvider content_provider,
6575 const char *content_type) {
6576 return cli_->Patch(path, content_length, std::move(content_provider),
6577 content_type);
6578}
6579inline Result Client::Patch(const char *path, const Headers &headers,
6580 size_t content_length,
6581 ContentProvider content_provider,
6582 const char *content_type) {
6583 return cli_->Patch(path, headers, content_length, std::move(content_provider),
6584 content_type);
6585}
6586inline Result Client::Delete(const char *path) { return cli_->Delete(path); }
6587inline Result Client::Delete(const char *path, const std::string &body,
6588 const char *content_type) {
6589 return cli_->Delete(path, body, content_type);
6590}
6591inline Result Client::Delete(const char *path, const Headers &headers) {
6592 return cli_->Delete(path, headers);
6593}
6594inline Result Client::Delete(const char *path, const Headers &headers,
6595 const std::string &body,
6596 const char *content_type) {
6597 return cli_->Delete(path, headers, body, content_type);
6598}
6599inline Result Client::Options(const char *path) { return cli_->Options(path); }
6600inline Result Client::Options(const char *path, const Headers &headers) {
6601 return cli_->Options(path, headers);
6602}
6603
6604inline bool Client::send(const Request &req, Response &res) {
6605 return cli_->send(req, res);
6606}
6607
6608inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
6609
6610inline void Client::stop() { cli_->stop(); }
6611
6612inline void Client::set_default_headers(Headers headers) {
6613 cli_->set_default_headers(std::move(headers));
6614}
6615
6616inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
6617inline void Client::set_socket_options(SocketOptions socket_options) {
6618 cli_->set_socket_options(std::move(socket_options));
6619}
6620
6621inline void Client::set_connection_timeout(time_t sec, time_t usec) {
6622 cli_->set_connection_timeout(sec, usec);
6623}
6624inline void Client::set_read_timeout(time_t sec, time_t usec) {
6625 cli_->set_read_timeout(sec, usec);
6626}
6627inline void Client::set_write_timeout(time_t sec, time_t usec) {
6628 cli_->set_write_timeout(sec, usec);
6629}
6630
6631inline void Client::set_basic_auth(const char *username, const char *password) {
6632 cli_->set_basic_auth(username, password);
6633}
6634inline void Client::set_bearer_token_auth(const char *token) {
6635 cli_->set_bearer_token_auth(token);
6636}
6637#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6638inline void Client::set_digest_auth(const char *username,
6639 const char *password) {
6640 cli_->set_digest_auth(username, password);
6641}
6642#endif
6643
6644inline void Client::set_keep_alive(bool on) { cli_->set_keep_alive(on); }
6645inline void Client::set_follow_location(bool on) {
6646 cli_->set_follow_location(on);
6647}
6648
6649inline void Client::set_compress(bool on) { cli_->set_compress(on); }
6650
6651inline void Client::set_decompress(bool on) { cli_->set_decompress(on); }
6652
6653inline void Client::set_interface(const char *intf) {
6654 cli_->set_interface(intf);
6655}
6656
6657inline void Client::set_proxy(const char *host, int port) {
6658 cli_->set_proxy(host, port);
6659}
6660inline void Client::set_proxy_basic_auth(const char *username,
6661 const char *password) {
6662 cli_->set_proxy_basic_auth(username, password);
6663}
6664inline void Client::set_proxy_bearer_token_auth(const char *token) {
6665 cli_->set_proxy_bearer_token_auth(token);
6666}
6667#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6668inline void Client::set_proxy_digest_auth(const char *username,
6669 const char *password) {
6670 cli_->set_proxy_digest_auth(username, password);
6671}
6672#endif
6673
6674#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6675inline void Client::enable_server_certificate_verification(bool enabled) {
6676 cli_->enable_server_certificate_verification(enabled);
6677}
6678#endif
6679
6680inline void Client::set_logger(Logger logger) { cli_->set_logger(logger); }
6681
6682#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
6683inline void Client::set_ca_cert_path(const char *ca_cert_file_path,
6684 const char *ca_cert_dir_path) {
6685 if (is_ssl_) {
6686 static_cast<SSLClient &>(*cli_).set_ca_cert_path(ca_cert_file_path,
6687 ca_cert_dir_path);
6688 }
6689}
6690
6691inline void Client::set_ca_cert_store(X509_STORE *ca_cert_store) {
6692 if (is_ssl_) {
6693 static_cast<SSLClient &>(*cli_).set_ca_cert_store(ca_cert_store);
6694 }
6695}
6696
6697inline long Client::get_openssl_verify_result() const {
6698 if (is_ssl_) {
6699 return static_cast<SSLClient &>(*cli_).get_openssl_verify_result();
6700 }
6701 return -1; // NOTE: -1 doesn't match any of X509_V_ERR_???
6702}
6703
6704inline SSL_CTX *Client::ssl_context() const {
6705 if (is_ssl_) { return static_cast<SSLClient &>(*cli_).ssl_context(); }
6706 return nullptr;
6707}
6708#endif
6709
4778// ---------------------------------------------------------------------------- 6710// ----------------------------------------------------------------------------
4779 6711
4780} // namespace httplib 6712} // namespace httplib
diff --git a/externals/inih/inih b/externals/inih/inih
Subproject 603729dec89aaca42d7bd08f08bc333165b7d5d Subproject 1e80a47dffbda813604f0913e2ad68c7054c14e
diff --git a/externals/libressl b/externals/libressl
Subproject 7d01cb01cb1a926ecb4c9c98b107ef3c26f59df Subproject 8289d0d07de6553bf4b900bf60e808ea3f7f59d
diff --git a/externals/lurlparser/CMakeLists.txt b/externals/lurlparser/CMakeLists.txt
deleted file mode 100644
index 45046ffd3..000000000
--- a/externals/lurlparser/CMakeLists.txt
+++ /dev/null
@@ -1,8 +0,0 @@
1add_library(lurlparser
2 LUrlParser.cpp
3 LUrlParser.h
4)
5
6create_target_directory_groups(lurlparser)
7
8target_include_directories(lurlparser INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
diff --git a/externals/lurlparser/LUrlParser.cpp b/externals/lurlparser/LUrlParser.cpp
deleted file mode 100644
index 9c134e330..000000000
--- a/externals/lurlparser/LUrlParser.cpp
+++ /dev/null
@@ -1,265 +0,0 @@
1/*
2 * Lightweight URL & URI parser (RFC 1738, RFC 3986)
3 * https://github.com/corporateshark/LUrlParser
4 *
5 * The MIT License (MIT)
6 *
7 * Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28#include "LUrlParser.h"
29
30#include <algorithm>
31#include <cstring>
32#include <stdlib.h>
33
34// check if the scheme name is valid
35static bool IsSchemeValid( const std::string& SchemeName )
36{
37 for ( auto c : SchemeName )
38 {
39 if ( !isalpha( c ) && c != '+' && c != '-' && c != '.' ) return false;
40 }
41
42 return true;
43}
44
45bool LUrlParser::clParseURL::GetPort( int* OutPort ) const
46{
47 if ( !IsValid() ) { return false; }
48
49 int Port = atoi( m_Port.c_str() );
50
51 if ( Port <= 0 || Port > 65535 ) { return false; }
52
53 if ( OutPort ) { *OutPort = Port; }
54
55 return true;
56}
57
58// based on RFC 1738 and RFC 3986
59LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL( const std::string& URL )
60{
61 LUrlParser::clParseURL Result;
62
63 const char* CurrentString = URL.c_str();
64
65 /*
66 * <scheme>:<scheme-specific-part>
67 * <scheme> := [a-z\+\-\.]+
68 * For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names
69 */
70
71 // try to read scheme
72 {
73 const char* LocalString = strchr( CurrentString, ':' );
74
75 if ( !LocalString )
76 {
77 return clParseURL( LUrlParserError_NoUrlCharacter );
78 }
79
80 // save the scheme name
81 Result.m_Scheme = std::string( CurrentString, LocalString - CurrentString );
82
83 if ( !IsSchemeValid( Result.m_Scheme ) )
84 {
85 return clParseURL( LUrlParserError_InvalidSchemeName );
86 }
87
88 // scheme should be lowercase
89 std::transform( Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower );
90
91 // skip ':'
92 CurrentString = LocalString+1;
93 }
94
95 /*
96 * //<user>:<password>@<host>:<port>/<url-path>
97 * any ":", "@" and "/" must be normalized
98 */
99
100 // skip "//"
101 if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
102 if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
103
104 // check if the user name and password are specified
105 bool bHasUserName = false;
106
107 const char* LocalString = CurrentString;
108
109 while ( *LocalString )
110 {
111 if ( *LocalString == '@' )
112 {
113 // user name and password are specified
114 bHasUserName = true;
115 break;
116 }
117 else if ( *LocalString == '/' )
118 {
119 // end of <host>:<port> specification
120 bHasUserName = false;
121 break;
122 }
123
124 LocalString++;
125 }
126
127 // user name and password
128 LocalString = CurrentString;
129
130 if ( bHasUserName )
131 {
132 // read user name
133 while ( *LocalString && *LocalString != ':' && *LocalString != '@' ) LocalString++;
134
135 Result.m_UserName = std::string( CurrentString, LocalString - CurrentString );
136
137 // proceed with the current pointer
138 CurrentString = LocalString;
139
140 if ( *CurrentString == ':' )
141 {
142 // skip ':'
143 CurrentString++;
144
145 // read password
146 LocalString = CurrentString;
147
148 while ( *LocalString && *LocalString != '@' ) LocalString++;
149
150 Result.m_Password = std::string( CurrentString, LocalString - CurrentString );
151
152 CurrentString = LocalString;
153 }
154
155 // skip '@'
156 if ( *CurrentString != '@' )
157 {
158 return clParseURL( LUrlParserError_NoAtSign );
159 }
160
161 CurrentString++;
162 }
163
164 bool bHasBracket = ( *CurrentString == '[' );
165
166 // go ahead, read the host name
167 LocalString = CurrentString;
168
169 while ( *LocalString )
170 {
171 if ( bHasBracket && *LocalString == ']' )
172 {
173 // end of IPv6 address
174 LocalString++;
175 break;
176 }
177 else if ( !bHasBracket && ( *LocalString == ':' || *LocalString == '/' ) )
178 {
179 // port number is specified
180 break;
181 }
182
183 LocalString++;
184 }
185
186 Result.m_Host = std::string( CurrentString, LocalString - CurrentString );
187
188 CurrentString = LocalString;
189
190 // is port number specified?
191 if ( *CurrentString == ':' )
192 {
193 CurrentString++;
194
195 // read port number
196 LocalString = CurrentString;
197
198 while ( *LocalString && *LocalString != '/' ) LocalString++;
199
200 Result.m_Port = std::string( CurrentString, LocalString - CurrentString );
201
202 CurrentString = LocalString;
203 }
204
205 // end of string
206 if ( !*CurrentString )
207 {
208 Result.m_ErrorCode = LUrlParserError_Ok;
209
210 return Result;
211 }
212
213 // skip '/'
214 if ( *CurrentString != '/' )
215 {
216 return clParseURL( LUrlParserError_NoSlash );
217 }
218
219 CurrentString++;
220
221 // parse the path
222 LocalString = CurrentString;
223
224 while ( *LocalString && *LocalString != '#' && *LocalString != '?' ) LocalString++;
225
226 Result.m_Path = std::string( CurrentString, LocalString - CurrentString );
227
228 CurrentString = LocalString;
229
230 // check for query
231 if ( *CurrentString == '?' )
232 {
233 // skip '?'
234 CurrentString++;
235
236 // read query
237 LocalString = CurrentString;
238
239 while ( *LocalString && *LocalString != '#' ) LocalString++;
240
241 Result.m_Query = std::string( CurrentString, LocalString - CurrentString );
242
243 CurrentString = LocalString;
244 }
245
246 // check for fragment
247 if ( *CurrentString == '#' )
248 {
249 // skip '#'
250 CurrentString++;
251
252 // read fragment
253 LocalString = CurrentString;
254
255 while ( *LocalString ) LocalString++;
256
257 Result.m_Fragment = std::string( CurrentString, LocalString - CurrentString );
258
259 CurrentString = LocalString;
260 }
261
262 Result.m_ErrorCode = LUrlParserError_Ok;
263
264 return Result;
265}
diff --git a/externals/lurlparser/LUrlParser.h b/externals/lurlparser/LUrlParser.h
deleted file mode 100644
index 25d210981..000000000
--- a/externals/lurlparser/LUrlParser.h
+++ /dev/null
@@ -1,78 +0,0 @@
1/*
2 * Lightweight URL & URI parser (RFC 1738, RFC 3986)
3 * https://github.com/corporateshark/LUrlParser
4 *
5 * The MIT License (MIT)
6 *
7 * Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28#pragma once
29
30#include <string>
31
32namespace LUrlParser
33{
34enum LUrlParserError
35{
36 LUrlParserError_Ok = 0,
37 LUrlParserError_Uninitialized = 1,
38 LUrlParserError_NoUrlCharacter = 2,
39 LUrlParserError_InvalidSchemeName = 3,
40 LUrlParserError_NoDoubleSlash = 4,
41 LUrlParserError_NoAtSign = 5,
42 LUrlParserError_UnexpectedEndOfLine = 6,
43 LUrlParserError_NoSlash = 7,
44};
45
46class clParseURL
47{
48public:
49 LUrlParserError m_ErrorCode;
50 std::string m_Scheme;
51 std::string m_Host;
52 std::string m_Port;
53 std::string m_Path;
54 std::string m_Query;
55 std::string m_Fragment;
56 std::string m_UserName;
57 std::string m_Password;
58
59 clParseURL()
60 : m_ErrorCode( LUrlParserError_Uninitialized )
61 {}
62
63 /// return 'true' if the parsing was successful
64 bool IsValid() const { return m_ErrorCode == LUrlParserError_Ok; }
65
66 /// helper to convert the port number to int, return 'true' if the port is valid (within the 0..65535 range)
67 bool GetPort( int* OutPort ) const;
68
69 /// parse the URL
70 static clParseURL ParseURL( const std::string& URL );
71
72private:
73 explicit clParseURL( LUrlParserError ErrorCode )
74 : m_ErrorCode( ErrorCode )
75 {}
76};
77
78} // namespace LUrlParser
diff --git a/externals/lurlparser/README.md b/externals/lurlparser/README.md
deleted file mode 100644
index be7f0135a..000000000
--- a/externals/lurlparser/README.md
+++ /dev/null
@@ -1,19 +0,0 @@
1From https://github.com/corporateshark/LUrlParser/commit/455d5e2d27e3946f11ad0328fee9ee2628e6a8e2
2
3MIT License
4
5===
6
7Lightweight URL & URI parser (RFC 1738, RFC 3986)
8
9(C) Sergey Kosarevsky, 2015
10
11@corporateshark sk@linderdaum.com
12
13http://www.linderdaum.com
14
15http://blog.linderdaum.com
16
17=============================
18
19A tiny and lightweight URL & URI parser (RFC 1738, RFC 3986) written in C++.
diff --git a/externals/microprofile/microprofile.h b/externals/microprofile/microprofile.h
index 85d5bd5de..a06f6457d 100644
--- a/externals/microprofile/microprofile.h
+++ b/externals/microprofile/microprofile.h
@@ -902,8 +902,10 @@ inline uint16_t MicroProfileGetGroupIndex(MicroProfileToken t)
902#include <windows.h> 902#include <windows.h>
903#define snprintf _snprintf 903#define snprintf _snprintf
904 904
905#ifdef _MSC_VER
905#pragma warning(push) 906#pragma warning(push)
906#pragma warning(disable: 4244) 907#pragma warning(disable: 4244)
908#endif
907int64_t MicroProfileTicksPerSecondCpu() 909int64_t MicroProfileTicksPerSecondCpu()
908{ 910{
909 static int64_t nTicksPerSecond = 0; 911 static int64_t nTicksPerSecond = 0;
@@ -946,7 +948,11 @@ typedef HANDLE MicroProfileThread;
946DWORD _stdcall ThreadTrampoline(void* pFunc) 948DWORD _stdcall ThreadTrampoline(void* pFunc)
947{ 949{
948 MicroProfileThreadFunc F = (MicroProfileThreadFunc)pFunc; 950 MicroProfileThreadFunc F = (MicroProfileThreadFunc)pFunc;
949 return (uint32_t)F(0); 951
952 // The return value of F will always return a void*, however, this is for
953 // compatibility with pthreads. The underlying "address" of the pointer
954 // is always a 32-bit value, so this cast is safe to perform.
955 return static_cast<DWORD>(reinterpret_cast<uint64_t>(F(0)));
950} 956}
951 957
952inline void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func) 958inline void MicroProfileThreadStart(MicroProfileThread* pThread, MicroProfileThreadFunc Func)
@@ -1742,10 +1748,10 @@ void MicroProfileFlip()
1742 } 1748 }
1743 } 1749 }
1744 } 1750 }
1745 for(uint32_t i = 0; i < MICROPROFILE_MAX_GROUPS; ++i) 1751 for(uint32_t j = 0; j < MICROPROFILE_MAX_GROUPS; ++j)
1746 { 1752 {
1747 pLog->nGroupTicks[i] += nGroupTicks[i]; 1753 pLog->nGroupTicks[j] += nGroupTicks[j];
1748 pFrameGroup[i] += nGroupTicks[i]; 1754 pFrameGroup[j] += nGroupTicks[j];
1749 } 1755 }
1750 pLog->nStackPos = nStackPos; 1756 pLog->nStackPos = nStackPos;
1751 } 1757 }
@@ -3328,7 +3334,7 @@ bool MicroProfileIsLocalThread(uint32_t nThreadId)
3328#endif 3334#endif
3329#else 3335#else
3330 3336
3331bool MicroProfileIsLocalThread(uint32_t nThreadId){return false;} 3337bool MicroProfileIsLocalThread([[maybe_unused]] uint32_t nThreadId) { return false; }
3332void MicroProfileStopContextSwitchTrace(){} 3338void MicroProfileStopContextSwitchTrace(){}
3333void MicroProfileStartContextSwitchTrace(){} 3339void MicroProfileStartContextSwitchTrace(){}
3334 3340
@@ -3576,7 +3582,7 @@ int MicroProfileGetGpuTickReference(int64_t* pOutCpu, int64_t* pOutGpu)
3576 3582
3577#undef S 3583#undef S
3578 3584
3579#ifdef _WIN32 3585#ifdef _MSC_VER
3580#pragma warning(pop) 3586#pragma warning(pop)
3581#endif 3587#endif
3582 3588
diff --git a/externals/unicorn b/externals/unicorn
deleted file mode 160000
Subproject 73f45735354396766a4bfb26d0b96b06e5cf31b