mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-06-12 09:37:15 +00:00
Compare commits
59 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1133a2dcb | ||
|
|
e273fec93c | ||
|
|
95d0b073bd | ||
|
|
9c7d841b37 | ||
|
|
f086bf5310 | ||
|
|
6613d7b7ad | ||
|
|
6adf130bf3 | ||
|
|
b6b2eaf5bc | ||
|
|
eb4b7c70a9 | ||
|
|
84661ea6ed | ||
|
|
041122908c | ||
|
|
401de608df | ||
|
|
726c64cf10 | ||
|
|
e1f781a21a | ||
|
|
72b81badad | ||
|
|
17428a8fbf | ||
|
|
6e1879dfae | ||
|
|
eb1d2e04bc | ||
|
|
c909ffa758 | ||
|
|
ff5677ad19 | ||
|
|
8b1b31ac20 | ||
|
|
953600c177 | ||
|
|
536e7eb7f2 | ||
|
|
6d66721ba1 | ||
|
|
3b29cd0bdc | ||
|
|
109b624dfe | ||
|
|
0ed70c4d9f | ||
|
|
a50b7591ca | ||
|
|
bf8fc11b53 | ||
|
|
bc4a613b6d | ||
|
|
4bb001351c | ||
|
|
e155ba44bb | ||
|
|
a4a9637738 | ||
|
|
5292142046 | ||
|
|
cc5147ad72 | ||
|
|
fffbf1a669 | ||
|
|
d37bc0fb4d | ||
|
|
6d60dc8839 | ||
|
|
b713a3a651 | ||
|
|
7e8db1dc68 | ||
|
|
316add860b | ||
|
|
79ce6f1745 | ||
|
|
09fdf4eacd | ||
|
|
143b2dd15a | ||
|
|
e2c4e9d95c | ||
|
|
d87082f04b | ||
|
|
cc14855ba0 | ||
|
|
56c418745f | ||
|
|
4ce9911837 | ||
|
|
559c407552 | ||
|
|
a2f4e29a7b | ||
|
|
b8cf739d27 | ||
|
|
aec2f9521d | ||
|
|
7b55ecdc59 | ||
|
|
e9575bcb78 | ||
|
|
308aeb187b | ||
|
|
05d18f2bc5 | ||
|
|
3da4a0ac69 | ||
|
|
9d12b3f20e |
7
.github/workflows/test.yaml
vendored
7
.github/workflows/test.yaml
vendored
@@ -20,16 +20,19 @@ jobs:
|
||||
uses: actions/checkout@v2
|
||||
- name: install brotli library on ubuntu
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
run: sudo apt-get install -y libbrotli-dev
|
||||
run: sudo apt update && sudo apt-get install -y libbrotli-dev
|
||||
- name: install brotli library on macOS
|
||||
if: matrix.os == 'macOS-latest'
|
||||
run: brew install brotli
|
||||
- name: make
|
||||
if: matrix.os != 'windows-latest'
|
||||
run: cd test && make
|
||||
- name: check fuzz test target
|
||||
if: matrix.os == 'ubuntu-latest'
|
||||
run: cd test && make -f Makefile.fuzz_test
|
||||
- name: setup msbuild on windows
|
||||
if: matrix.os == 'windows-latest'
|
||||
uses: warrenbuckley/Setup-MSBuild@v1
|
||||
uses: microsoft/setup-msbuild@v1.0.2
|
||||
- name: make-windows
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: |
|
||||
|
||||
@@ -236,9 +236,9 @@ target_link_libraries(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
|
||||
# Set the definitions to enable optional features
|
||||
target_compile_definitions(${PROJECT_NAME} ${_INTERFACE_OR_PUBLIC}
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:"CPPHTTPLIB_BROTLI_SUPPORT">
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:"CPPHTTPLIB_ZLIB_SUPPORT">
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:"CPPHTTPLIB_OPENSSL_SUPPORT">
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_BROTLI}>:CPPHTTPLIB_BROTLI_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_ZLIB}>:CPPHTTPLIB_ZLIB_SUPPORT>
|
||||
$<$<BOOL:${HTTPLIB_IS_USING_OPENSSL}>:CPPHTTPLIB_OPENSSL_SUPPORT>
|
||||
)
|
||||
|
||||
# Cmake's find_package search path is different based on the system
|
||||
|
||||
30
README.md
30
README.md
@@ -297,13 +297,18 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e
|
||||
|
||||
### Default thread pool support
|
||||
|
||||
`ThreadPool` is used as a **default** task queue, and the default thread count is 8, or `std::thread::hardware_concurrency()`. You can change it with `CPPHTTPLIB_THREAD_POOL_COUNT`.
|
||||
|
||||
`ThreadPool` is used as a default task queue, and the default thread count is set to value from `std::thread::hardware_concurrency()`.
|
||||
If you want to set the thread count at runtime, there is no convenient way... But here is how.
|
||||
|
||||
You can change the thread count by setting `CPPHTTPLIB_THREAD_POOL_COUNT`.
|
||||
```cpp
|
||||
svr.new_task_queue = [] { return new ThreadPool(12); };
|
||||
```
|
||||
|
||||
### Override the default thread pool with yours
|
||||
|
||||
You can supply your own thread pool implementation according to your need.
|
||||
|
||||
```cpp
|
||||
class YourThreadPoolTaskQueue : public TaskQueue {
|
||||
public:
|
||||
@@ -360,6 +365,27 @@ httplib::Client cli("http://localhost:8080");
|
||||
httplib::Client cli("https://localhost");
|
||||
```
|
||||
|
||||
### Error code
|
||||
|
||||
Here is the list of errors from `Result::error()`.
|
||||
|
||||
```c++
|
||||
enum Error {
|
||||
Success = 0,
|
||||
Unknown,
|
||||
Connection,
|
||||
BindIPAddress,
|
||||
Read,
|
||||
Write,
|
||||
ExceedRedirectCount,
|
||||
Canceled,
|
||||
SSLConnection,
|
||||
SSLLoadingCerts,
|
||||
SSLServerVerification,
|
||||
UnsupportedMultipartBoundaryChars
|
||||
};
|
||||
```
|
||||
|
||||
### GET with HTTP headers
|
||||
|
||||
```c++
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
|
||||
#CXX = clang++
|
||||
CXXFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion
|
||||
CXXFLAGS = -g -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion #-fsanitize=address
|
||||
|
||||
OPENSSL_DIR = /usr/local/opt/openssl@1.1
|
||||
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
|
||||
@@ -25,6 +24,7 @@ test_proxy : test_proxy.cc ../httplib.h Makefile cert.pem
|
||||
cert.pem:
|
||||
openssl genrsa 2048 > key.pem
|
||||
openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||
openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
|
||||
openssl genrsa 2048 > rootCA.key.pem
|
||||
openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem
|
||||
openssl genrsa 2048 > client.key.pem
|
||||
|
||||
36
test/Makefile.fuzz_test
Normal file
36
test/Makefile.fuzz_test
Normal file
@@ -0,0 +1,36 @@
|
||||
|
||||
#CXX = clang++
|
||||
CXXFLAGS += -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion
|
||||
|
||||
OPENSSL_DIR = /usr/local/opt/openssl@1.1
|
||||
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto
|
||||
|
||||
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
||||
|
||||
BROTLI_DIR = /usr/local/opt/brotli
|
||||
BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||
|
||||
# By default, use standalone_fuzz_target_runner.
|
||||
# This runner does no fuzzing, but simply executes the inputs
|
||||
# provided via parameters.
|
||||
# Run e.g. "make all LIB_FUZZING_ENGINE=/path/to/libFuzzer.a"
|
||||
# to link the fuzzer(s) against a real fuzzing engine.
|
||||
# OSS-Fuzz will define its own value for LIB_FUZZING_ENGINE.
|
||||
LIB_FUZZING_ENGINE ?= standalone_fuzz_target_runner.o
|
||||
|
||||
# Runs server_fuzzer.cc based on value of $(LIB_FUZZING_ENGINE).
|
||||
# Usage: make fuzz_test LIB_FUZZING_ENGINE=/path/to/libFuzzer
|
||||
all fuzz_test: server_fuzzer
|
||||
./server_fuzzer fuzzing/corpus/*
|
||||
|
||||
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
||||
server_fuzzer : fuzzing/server_fuzzer.cc ../httplib.h standalone_fuzz_target_runner.o
|
||||
$(CXX) $(CXXFLAGS) -o $@ $< $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) $(BROTLI_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
||||
|
||||
# Standalone fuzz runner, which just reads inputs from fuzzing/corpus/ dir and
|
||||
# feeds it to server_fuzzer.
|
||||
standalone_fuzz_target_runner.o : fuzzing/standalone_fuzz_target_runner.cpp
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
||||
27
test/fuzzing/Makefile
Normal file
27
test/fuzzing/Makefile
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#CXX = clang++
|
||||
# Do not add default sanitizer flags here as OSS-fuzz adds its own sanitizer flags.
|
||||
CXXFLAGS += -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I../.. -I. -Wall -Wextra -Wtype-limits -Wconversion
|
||||
|
||||
OPENSSL_DIR = /usr/local/opt/openssl@1.1
|
||||
|
||||
# Using full path to libssl and libcrypto to avoid accidentally picking openssl libs brought in by msan.
|
||||
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -I$(OPENSSL_DIR)/lib /usr/local/lib/libssl.a /usr/local/lib/libcrypto.a
|
||||
|
||||
ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
||||
|
||||
BROTLI_DIR = /usr/local/opt/brotli
|
||||
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||
|
||||
# Runs all the tests and also fuzz tests against seed corpus.
|
||||
all : server_fuzzer
|
||||
./server_fuzzer corpus/*
|
||||
|
||||
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
||||
server_fuzzer : server_fuzzer.cc ../../httplib.h
|
||||
# $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
||||
zip -q -r server_fuzzer_seed_corpus.zip corpus
|
||||
|
||||
clean:
|
||||
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
||||
1
test/fuzzing/corpus/1
Normal file
1
test/fuzzing/corpus/1
Normal file
@@ -0,0 +1 @@
|
||||
PUT /search/sample?a=12 HTTP/1.1
|
||||
5
test/fuzzing/corpus/2
Normal file
5
test/fuzzing/corpus/2
Normal file
@@ -0,0 +1,5 @@
|
||||
GET /hello.htm HTTP/1.1
|
||||
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
|
||||
Accept-Language: en-us
|
||||
Accept-Encoding: gzip, deflate
|
||||
Connection: Keep-Alive
|
||||
90
test/fuzzing/server_fuzzer.cc
Normal file
90
test/fuzzing/server_fuzzer.cc
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <memory>
|
||||
#include <httplib.h>
|
||||
|
||||
class FuzzedStream : public httplib::Stream {
|
||||
public:
|
||||
FuzzedStream(const uint8_t* data, size_t size)
|
||||
: data_(data), size_(size), read_pos_(0) {}
|
||||
|
||||
ssize_t read(char* ptr, size_t size) override {
|
||||
if (size + read_pos_ > size_) {
|
||||
size = size_ - read_pos_;
|
||||
}
|
||||
memcpy(ptr, data_ + read_pos_, size);
|
||||
read_pos_ += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
ssize_t write(const char* ptr, size_t size) override {
|
||||
response_.append(ptr, size);
|
||||
return static_cast<int>(size);
|
||||
}
|
||||
|
||||
int write(const char* ptr) { return write(ptr, strlen(ptr)); }
|
||||
|
||||
int write(const std::string& s) { return write(s.data(), s.size()); }
|
||||
|
||||
std::string get_remote_addr() const { return ""; }
|
||||
|
||||
bool is_readable() const override { return true; }
|
||||
|
||||
bool is_writable() const override { return true; }
|
||||
|
||||
void get_remote_ip_and_port(std::string &ip, int &port) const override {
|
||||
ip = "127.0.0.1";
|
||||
port = 8080;
|
||||
}
|
||||
|
||||
socket_t socket() const override { return 0; }
|
||||
|
||||
private:
|
||||
const uint8_t* data_;
|
||||
size_t size_;
|
||||
size_t read_pos_;
|
||||
std::string response_;
|
||||
};
|
||||
|
||||
class FuzzableServer : public httplib::Server {
|
||||
public:
|
||||
void ProcessFuzzedRequest(FuzzedStream& stream) {
|
||||
bool connection_close = false;
|
||||
process_request(stream, /*last_connection=*/false, connection_close,
|
||||
nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
static FuzzableServer g_server;
|
||||
|
||||
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
|
||||
g_server.Get(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
g_server.Post(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
g_server.Put(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
g_server.Patch(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
g_server.Delete(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
g_server.Options(R"(.*)",
|
||||
[&](const httplib::Request& req, httplib::Response& res) {
|
||||
res.set_content("response content", "text/plain");
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
|
||||
FuzzedStream stream{data, size};
|
||||
g_server.ProcessFuzzedRequest(stream);
|
||||
return 0;
|
||||
}
|
||||
224
test/fuzzing/server_fuzzer.dict
Normal file
224
test/fuzzing/server_fuzzer.dict
Normal file
@@ -0,0 +1,224 @@
|
||||
# Sources: https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
|
||||
|
||||
# misc
|
||||
"HTTP/1.1"
|
||||
|
||||
# verbs
|
||||
"CONNECT"
|
||||
"DELETE"
|
||||
"GET"
|
||||
"HEAD"
|
||||
"OPTIONS"
|
||||
"PATCH"
|
||||
"POST"
|
||||
"PUT"
|
||||
"TRACE"
|
||||
|
||||
|
||||
# Webdav/caldav verbs
|
||||
"ACL"
|
||||
"BASELINE-CONTROL"
|
||||
"BIND"
|
||||
"CHECKIN"
|
||||
"CHECKOUT"
|
||||
"COPY"
|
||||
"LABEL"
|
||||
"LINK"
|
||||
"LOCK"
|
||||
"MERGE"
|
||||
"MKACTIVITY"
|
||||
"MKCALENDAR"
|
||||
"MKCOL"
|
||||
"MKREDIRECTREF"
|
||||
"MKWORKSPACE"
|
||||
"MOVE"
|
||||
"ORDERPATCH"
|
||||
"PRI"
|
||||
"PROPFIND"
|
||||
"PROPPATCH"
|
||||
"REBIND"
|
||||
"REPORT"
|
||||
"SEARCH"
|
||||
"UNBIND"
|
||||
"UNCHECKOUT"
|
||||
"UNLINK"
|
||||
"UNLOCK"
|
||||
"UPDATE"
|
||||
"UPDATEREDIRECTREF"
|
||||
"VERSION-CONTROL"
|
||||
|
||||
|
||||
# Fields
|
||||
"A-IM"
|
||||
"Accept"
|
||||
"Accept-Charset"
|
||||
"Accept-Datetime"
|
||||
"Accept-Encoding"
|
||||
"Accept-Language"
|
||||
"Accept-Patch"
|
||||
"Accept-Ranges"
|
||||
"Access-Control-Allow-Credentials"
|
||||
"Access-Control-Allow-Headers"
|
||||
"Access-Control-Allow-Methods"
|
||||
"Access-Control-Allow-Origin"
|
||||
"Access-Control-Expose-Headers"
|
||||
"Access-Control-Max-Age"
|
||||
"Access-Control-Request-Headers"
|
||||
"Access-Control-Request-Method"
|
||||
"Age"
|
||||
"Allow"
|
||||
"Alt-Svc"
|
||||
"Authorization"
|
||||
"Cache-Control"
|
||||
"Connection"
|
||||
"Connection:"
|
||||
"Content-Disposition"
|
||||
"Content-Encoding"
|
||||
"Content-Language"
|
||||
"Content-Length"
|
||||
"Content-Location"
|
||||
"Content-MD5"
|
||||
"Content-Range"
|
||||
"Content-Security-Policy"
|
||||
"Content-Type"
|
||||
"Cookie"
|
||||
"DNT"
|
||||
"Date"
|
||||
"Delta-Base"
|
||||
"ETag"
|
||||
"Expect"
|
||||
"Expires"
|
||||
"Forwarded"
|
||||
"From"
|
||||
"Front-End-Https"
|
||||
"HTTP2-Settings"
|
||||
"Host"
|
||||
"IM"
|
||||
"If-Match"
|
||||
"If-Modified-Since"
|
||||
"If-None-Match"
|
||||
"If-Range"
|
||||
"If-Unmodified-Since"
|
||||
"Last-Modified"
|
||||
"Link"
|
||||
"Location"
|
||||
"Max-Forwards"
|
||||
"Origin"
|
||||
"P3P"
|
||||
"Pragma"
|
||||
"Proxy-Authenticate"
|
||||
"Proxy-Authorization"
|
||||
"Proxy-Connection"
|
||||
"Public-Key-Pins"
|
||||
"Range"
|
||||
"Referer"
|
||||
"Refresh"
|
||||
"Retry-After"
|
||||
"Save-Data"
|
||||
"Server"
|
||||
"Set-Cookie"
|
||||
"Status"
|
||||
"Strict-Transport-Security"
|
||||
"TE"
|
||||
"Timing-Allow-Origin"
|
||||
"Tk"
|
||||
"Trailer"
|
||||
"Transfer-Encoding"
|
||||
"Upgrade"
|
||||
"Upgrade-Insecure-Requests"
|
||||
"User-Agent"
|
||||
"Vary"
|
||||
"Via"
|
||||
"WWW-Authenticate"
|
||||
"Warning"
|
||||
"X-ATT-DeviceId"
|
||||
"X-Content-Duration"
|
||||
"X-Content-Security-Policy"
|
||||
"X-Content-Type-Options"
|
||||
"X-Correlation-ID"
|
||||
"X-Csrf-Token"
|
||||
"X-Forwarded-For"
|
||||
"X-Forwarded-Host"
|
||||
"X-Forwarded-Proto"
|
||||
"X-Frame-Options"
|
||||
"X-Http-Method-Override"
|
||||
"X-Powered-By"
|
||||
"X-Request-ID"
|
||||
"X-Requested-With"
|
||||
"X-UA-Compatible"
|
||||
"X-UIDH"
|
||||
"X-Wap-Profile"
|
||||
"X-WebKit-CSP"
|
||||
"X-XSS-Protection"
|
||||
|
||||
# Source: string and character literals in httplib.h
|
||||
" "
|
||||
"&"
|
||||
", "
|
||||
"-"
|
||||
"--"
|
||||
"."
|
||||
".."
|
||||
":"
|
||||
"="
|
||||
" = = "
|
||||
"0123456789abcdef"
|
||||
"%02X"
|
||||
"%0A"
|
||||
"\\x0a\\x0d"
|
||||
"%0D"
|
||||
"%20"
|
||||
"%27"
|
||||
"%2B"
|
||||
"%2C"
|
||||
"%3A"
|
||||
"%3B"
|
||||
"application/javascript"
|
||||
"application/json"
|
||||
"application/pdf"
|
||||
"application/xhtml+xml"
|
||||
"application/xml"
|
||||
"application/x-www-form-urlencoded"
|
||||
"Bad Request"
|
||||
"boundary="
|
||||
"bytes="
|
||||
"chunked"
|
||||
"close"
|
||||
"CONNECT"
|
||||
"css"
|
||||
"Forbidden"
|
||||
"Found"
|
||||
"gif"
|
||||
"gzip"
|
||||
"html"
|
||||
"ico"
|
||||
"image/gif"
|
||||
"image/jpg"
|
||||
"image/png"
|
||||
"image/svg+xml"
|
||||
"image/x-icon"
|
||||
"index.html"
|
||||
"Internal Server Error"
|
||||
"jpeg"
|
||||
"js"
|
||||
"json"
|
||||
"Location"
|
||||
"Moved Permanently"
|
||||
"multipart/form-data"
|
||||
"Not Found"
|
||||
"Not Modified"
|
||||
"OK"
|
||||
"pdf"
|
||||
"png"
|
||||
"Range"
|
||||
"REMOTE_ADDR"
|
||||
"See Other"
|
||||
"svg"
|
||||
"text/"
|
||||
"text/css"
|
||||
"text/html"
|
||||
"text/plain"
|
||||
"txt"
|
||||
"Unsupported Media Type"
|
||||
"xhtml"
|
||||
"xml"
|
||||
35
test/fuzzing/standalone_fuzz_target_runner.cpp
Normal file
35
test/fuzzing/standalone_fuzz_target_runner.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
||||
// This runner does not do any fuzzing, but allows us to run the fuzz target
|
||||
// on the test corpus or on a single file,
|
||||
// e.g. the one that comes from a bug report.
|
||||
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
// Forward declare the "fuzz target" interface.
|
||||
// We deliberately keep this inteface simple and header-free.
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
||||
|
||||
// It reads all files passed as parameters and feeds their contents
|
||||
// one by one into the fuzz target (LLVMFuzzerTestOneInput).
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 1; i < argc; i++) {
|
||||
std::ifstream in(argv[i]);
|
||||
in.seekg(0, in.end);
|
||||
size_t length = in.tellg();
|
||||
in.seekg (0, in.beg);
|
||||
std::cout << "Reading " << length << " bytes from " << argv[i] << std::endl;
|
||||
// Allocate exactly length bytes so that we reliably catch buffer overflows.
|
||||
std::vector<char> bytes(length);
|
||||
in.read(bytes.data(), bytes.size());
|
||||
LLVMFuzzerTestOneInput(reinterpret_cast<const uint8_t *>(bytes.data()),
|
||||
bytes.size());
|
||||
std::cout << "Execution successful" << std::endl;
|
||||
}
|
||||
std::cout << "Execution finished" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
357
test/test.cc
357
test/test.cc
@@ -7,6 +7,7 @@
|
||||
#include <thread>
|
||||
|
||||
#define SERVER_CERT_FILE "./cert.pem"
|
||||
#define SERVER_CERT2_FILE "./cert2.pem"
|
||||
#define SERVER_PRIVATE_KEY_FILE "./key.pem"
|
||||
#define CA_CERT_FILE "./ca-bundle.crt"
|
||||
#define CLIENT_CA_CERT_FILE "./rootCA.cert.pem"
|
||||
@@ -43,22 +44,11 @@ TEST(StartupTest, WSAStartup) {
|
||||
ASSERT_EQ(0, ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(TrimTests, TrimStringTests) {
|
||||
{
|
||||
std::string s = "abc";
|
||||
detail::trim(s);
|
||||
EXPECT_EQ("abc", s);
|
||||
}
|
||||
{
|
||||
std::string s = " abc ";
|
||||
detail::trim(s);
|
||||
EXPECT_EQ("abc", s);
|
||||
}
|
||||
{
|
||||
std::string s = "";
|
||||
detail::trim(s);
|
||||
EXPECT_TRUE( s.empty() );
|
||||
}
|
||||
EXPECT_EQ("abc", detail::trim_copy("abc"));
|
||||
EXPECT_EQ("abc", detail::trim_copy(" abc "));
|
||||
EXPECT_TRUE(detail::trim_copy("").empty());
|
||||
}
|
||||
|
||||
TEST(SplitTest, ParseQueryString) {
|
||||
@@ -100,7 +90,6 @@ TEST(SplitTest, ParseInvalidQueryTests) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(ParseQueryTest, ParseQueryString) {
|
||||
string s = "key1=val1&key2=val2&key3=val3";
|
||||
Params dic;
|
||||
@@ -146,6 +135,17 @@ TEST(GetHeaderValueTest, RegularValue) {
|
||||
EXPECT_STREQ("text/html", val);
|
||||
}
|
||||
|
||||
TEST(GetHeaderValueTest, SetContent) {
|
||||
Response res;
|
||||
|
||||
res.set_content("html", "text/html");
|
||||
EXPECT_EQ("text/html", res.get_header_value("Content-Type"));
|
||||
|
||||
res.set_content("text", "text/plain");
|
||||
EXPECT_EQ(1, res.get_header_value_count("Content-Type"));
|
||||
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
|
||||
}
|
||||
|
||||
TEST(GetHeaderValueTest, RegularValueInt) {
|
||||
Headers headers = {{"Content-Length", "100"}, {"Dummy", "Dummy"}};
|
||||
auto val =
|
||||
@@ -841,7 +841,7 @@ TEST(UrlWithSpace, Redirect) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(Server, BindDualStack) {
|
||||
TEST(BindServerTest, BindDualStack) {
|
||||
Server svr;
|
||||
|
||||
svr.Get("/1", [&](const Request & /*req*/, Response &res) {
|
||||
@@ -874,7 +874,7 @@ TEST(Server, BindDualStack) {
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
}
|
||||
|
||||
TEST(Server, BindAndListenSeparately) {
|
||||
TEST(BindServerTest, BindAndListenSeparately) {
|
||||
Server svr;
|
||||
int port = svr.bind_to_any_port("0.0.0.0");
|
||||
ASSERT_TRUE(svr.is_valid());
|
||||
@@ -883,7 +883,7 @@ TEST(Server, BindAndListenSeparately) {
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
TEST(SSLServer, BindAndListenSeparately) {
|
||||
TEST(BindServerTest, BindAndListenSeparatelySSL) {
|
||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
|
||||
CLIENT_CA_CERT_DIR);
|
||||
int port = svr.bind_to_any_port("0.0.0.0");
|
||||
@@ -893,6 +893,41 @@ TEST(SSLServer, BindAndListenSeparately) {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(ErrorHandlerTest, ContentLength) {
|
||||
Server svr;
|
||||
|
||||
svr.set_error_handler([](const Request & /*req*/, Response &res) {
|
||||
res.status = 200;
|
||||
res.set_content("abcdefghijklmnopqrstuvwxyz",
|
||||
"text/html"); // <= Content-Length still 13
|
||||
});
|
||||
|
||||
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
|
||||
res.set_content("Hello World!\n", "text/plain");
|
||||
res.status = 524;
|
||||
});
|
||||
|
||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||
|
||||
// Give GET time to get a few messages.
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
{
|
||||
Client cli(HOST, PORT);
|
||||
|
||||
auto res = cli.Get("/hi");
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
EXPECT_EQ("text/html", res->get_header_value("Content-Type"));
|
||||
EXPECT_EQ("26", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ("abcdefghijklmnopqrstuvwxyz", res->body);
|
||||
}
|
||||
|
||||
svr.stop();
|
||||
thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
}
|
||||
|
||||
class ServerTest : public ::testing::Test {
|
||||
protected:
|
||||
ServerTest()
|
||||
@@ -1345,8 +1380,11 @@ protected:
|
||||
|
||||
virtual void TearDown() {
|
||||
svr_.stop();
|
||||
for (auto &t : request_threads_) {
|
||||
t.join();
|
||||
if (!request_threads_.empty()) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
for (auto &t : request_threads_) {
|
||||
t.join();
|
||||
}
|
||||
}
|
||||
t_.join();
|
||||
}
|
||||
@@ -1827,8 +1865,7 @@ TEST_F(ServerTest, MultipartFormData) {
|
||||
{"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
|
||||
{"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
|
||||
{"file3", "", "", "application/octet-stream"},
|
||||
{"file4", "", "", " application/json tmp-string "}
|
||||
};
|
||||
{"file4", "", "", " application/json tmp-string "}};
|
||||
|
||||
auto res = cli_.Post("/multipart", items);
|
||||
|
||||
@@ -1907,6 +1944,41 @@ TEST_F(ServerTest, GetStreamedWithRange2) {
|
||||
EXPECT_EQ(std::string("bcdefg"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedWithRangeSuffix1) {
|
||||
auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-3"}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("3", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
EXPECT_EQ(std::string("efg"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedWithRangeSuffix2) {
|
||||
auto res = cli_.Get("/streamed-with-range", {{"Range", "bytes=-9999"}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("7", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
EXPECT_EQ(std::string("abcdefg"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedWithRangeError) {
|
||||
auto res = cli_.Get("/streamed-with-range",
|
||||
{{"Range", "bytes=92233720368547758079223372036854775806-"
|
||||
"92233720368547758079223372036854775807"}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(416, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetRangeWithMaxLongLength) {
|
||||
auto res =
|
||||
cli_.Get("/with-range", {{"Range", "bytes=0-9223372036854775807"}});
|
||||
EXPECT_EQ(206, res->status);
|
||||
EXPECT_EQ("7", res->get_header_value("Content-Length"));
|
||||
EXPECT_EQ(true, res->has_header("Content-Range"));
|
||||
EXPECT_EQ(std::string("abcdefg"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedWithRangeMultipart) {
|
||||
auto res =
|
||||
cli_.Get("/streamed-with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
||||
@@ -1942,8 +2014,7 @@ TEST_F(ServerTest, ClientStop) {
|
||||
}));
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
while (cli_.is_socket_open()) {
|
||||
cli_.stop();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
@@ -1989,6 +2060,12 @@ TEST_F(ServerTest, GetWithRange4) {
|
||||
EXPECT_EQ(std::string("fg"), res->body);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetWithRangeOffsetGreaterThanContent) {
|
||||
auto res = cli_.Get("/with-range", {{make_range_header({{10000, 20000}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(416, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetWithRangeMultipart) {
|
||||
auto res = cli_.Get("/with-range", {{make_range_header({{1, 2}, {4, 5}})}});
|
||||
ASSERT_TRUE(res);
|
||||
@@ -1998,6 +2075,13 @@ TEST_F(ServerTest, GetWithRangeMultipart) {
|
||||
EXPECT_EQ(269, res->body.size());
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetWithRangeMultipartOffsetGreaterThanContent) {
|
||||
auto res =
|
||||
cli_.Get("/with-range", {{make_range_header({{-1, 2}, {10000, 30000}})}});
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(416, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, GetStreamedChunked) {
|
||||
auto res = cli_.Get("/streamed-chunked");
|
||||
ASSERT_TRUE(res);
|
||||
@@ -2064,7 +2148,6 @@ TEST_F(ServerTest, SlowRequest) {
|
||||
std::thread([=]() { auto res = cli_.Get("/slow"); }));
|
||||
request_threads_.push_back(
|
||||
std::thread([=]() { auto res = cli_.Get("/slow"); }));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, SlowPost) {
|
||||
@@ -2081,9 +2164,14 @@ TEST_F(ServerTest, SlowPost) {
|
||||
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, SlowPostFail) {
|
||||
char buffer[64 * 1024];
|
||||
memset(buffer, 0x42, sizeof(buffer));
|
||||
|
||||
cli_.set_write_timeout(0, 0);
|
||||
res = cli_.Post(
|
||||
auto res = cli_.Post(
|
||||
"/slowpost", 64 * 1024 * 1024,
|
||||
[&](size_t /*offset*/, size_t /*length*/, DataSink &sink) {
|
||||
sink.write(buffer, sizeof(buffer));
|
||||
@@ -2201,7 +2289,6 @@ TEST_F(ServerTest, GetStreamedChunkedWithGzip2) {
|
||||
EXPECT_EQ(std::string("123456789"), res->body);
|
||||
}
|
||||
|
||||
|
||||
TEST(GzipDecompressor, ChunkedDecompression) {
|
||||
std::string data;
|
||||
for (size_t i = 0; i < 32 * 1024; ++i) {
|
||||
@@ -2212,10 +2299,8 @@ TEST(GzipDecompressor, ChunkedDecompression) {
|
||||
{
|
||||
httplib::detail::gzip_compressor compressor;
|
||||
bool result = compressor.compress(
|
||||
data.data(),
|
||||
data.size(),
|
||||
/*last=*/true,
|
||||
[&] (const char* data, size_t size) {
|
||||
data.data(), data.size(),
|
||||
/*last=*/true, [&](const char *data, size_t size) {
|
||||
compressed_data.insert(compressed_data.size(), data, size);
|
||||
return true;
|
||||
});
|
||||
@@ -2226,15 +2311,16 @@ TEST(GzipDecompressor, ChunkedDecompression) {
|
||||
{
|
||||
httplib::detail::gzip_decompressor decompressor;
|
||||
|
||||
// Chunk size is chosen specificaly to have a decompressed chunk size equal to 16384 bytes
|
||||
// 16384 bytes is the size of decompressor output buffer
|
||||
// Chunk size is chosen specificaly to have a decompressed chunk size equal
|
||||
// to 16384 bytes 16384 bytes is the size of decompressor output buffer
|
||||
size_t chunk_size = 130;
|
||||
for (size_t chunk_begin = 0; chunk_begin < compressed_data.size(); chunk_begin += chunk_size) {
|
||||
size_t current_chunk_size = std::min(compressed_data.size() - chunk_begin, chunk_size);
|
||||
for (size_t chunk_begin = 0; chunk_begin < compressed_data.size();
|
||||
chunk_begin += chunk_size) {
|
||||
size_t current_chunk_size =
|
||||
std::min(compressed_data.size() - chunk_begin, chunk_size);
|
||||
bool result = decompressor.decompress(
|
||||
compressed_data.data() + chunk_begin,
|
||||
current_chunk_size,
|
||||
[&] (const char* data, size_t size) {
|
||||
compressed_data.data() + chunk_begin, current_chunk_size,
|
||||
[&](const char *data, size_t size) {
|
||||
decompressed_data.insert(decompressed_data.size(), data, size);
|
||||
return true;
|
||||
});
|
||||
@@ -2338,6 +2424,41 @@ TEST_F(ServerTest, PostMulitpartFilsContentReceiver) {
|
||||
EXPECT_EQ(200, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, PostMulitpartPlusBoundary) {
|
||||
MultipartFormDataItems items = {
|
||||
{"text1", "text default", "", ""},
|
||||
{"text2", "aωb", "", ""},
|
||||
{"file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain"},
|
||||
{"file2", "{\n \"world\", true\n}\n", "world.json", "application/json"},
|
||||
{"file3", "", "", "application/octet-stream"},
|
||||
};
|
||||
|
||||
auto boundary = std::string("+++++");
|
||||
|
||||
std::string body;
|
||||
|
||||
for (const auto &item : items) {
|
||||
body += "--" + boundary + "\r\n";
|
||||
body += "Content-Disposition: form-data; name=\"" + item.name + "\"";
|
||||
if (!item.filename.empty()) {
|
||||
body += "; filename=\"" + item.filename + "\"";
|
||||
}
|
||||
body += "\r\n";
|
||||
if (!item.content_type.empty()) {
|
||||
body += "Content-Type: " + item.content_type + "\r\n";
|
||||
}
|
||||
body += "\r\n";
|
||||
body += item.content + "\r\n";
|
||||
}
|
||||
body += "--" + boundary + "--\r\n";
|
||||
|
||||
std::string content_type = "multipart/form-data; boundary=" + boundary;
|
||||
auto res = cli_.Post("/content_receiver", body, content_type.c_str());
|
||||
|
||||
ASSERT_TRUE(res);
|
||||
EXPECT_EQ(200, res->status);
|
||||
}
|
||||
|
||||
TEST_F(ServerTest, PostContentReceiverGzip) {
|
||||
cli_.set_compress(true);
|
||||
auto res = cli_.Post("/content_receiver", "content", "text/plain");
|
||||
@@ -2819,15 +2940,14 @@ TEST(StreamingTest, NoContentLengthStreaming) {
|
||||
Server svr;
|
||||
|
||||
svr.Get("/stream", [](const Request & /*req*/, Response &res) {
|
||||
res.set_content_provider(
|
||||
"text/plain", [](size_t offset, DataSink &sink) {
|
||||
if (offset < 6) {
|
||||
sink.os << (offset < 3 ? "a" : "b");
|
||||
} else {
|
||||
sink.done();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
res.set_content_provider("text/plain", [](size_t offset, DataSink &sink) {
|
||||
if (offset < 6) {
|
||||
sink.os << (offset < 3 ? "a" : "b");
|
||||
} else {
|
||||
sink.done();
|
||||
}
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
|
||||
@@ -2965,6 +3085,48 @@ TEST(KeepAliveTest, ReadTimeout) {
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
TEST(KeepAliveTest, ReadTimeoutSSL) {
|
||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||
ASSERT_TRUE(svr.is_valid());
|
||||
|
||||
svr.Get("/a", [&](const Request & /*req*/, Response &res) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||
res.set_content("a", "text/plain");
|
||||
});
|
||||
|
||||
svr.Get("/b", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_content("b", "text/plain");
|
||||
});
|
||||
|
||||
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
|
||||
while (!svr.is_running()) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
}
|
||||
|
||||
// Give GET time to get a few messages.
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
|
||||
SSLClient cli("localhost", PORT);
|
||||
cli.enable_server_certificate_verification(false);
|
||||
cli.set_keep_alive(true);
|
||||
cli.set_read_timeout(1);
|
||||
|
||||
auto resa = cli.Get("/a");
|
||||
ASSERT_TRUE(!resa);
|
||||
EXPECT_EQ(Error::Read, resa.error());
|
||||
|
||||
auto resb = cli.Get("/b");
|
||||
ASSERT_TRUE(resb);
|
||||
EXPECT_EQ(200, resb->status);
|
||||
EXPECT_EQ("b", resb->body);
|
||||
|
||||
svr.stop();
|
||||
listen_thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
}
|
||||
#endif
|
||||
|
||||
class ServerTestWithAI_PASSIVE : public ::testing::Test {
|
||||
protected:
|
||||
ServerTestWithAI_PASSIVE()
|
||||
@@ -3099,6 +3261,19 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) {
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
TEST(SSLClientTest, UpdateCAStore) {
|
||||
httplib::SSLClient httplib_client("www.google.com");
|
||||
auto ca_store_1 = X509_STORE_new();
|
||||
X509_STORE_load_locations(ca_store_1, "/etc/ssl/certs/ca-certificates.crt",
|
||||
nullptr);
|
||||
httplib_client.set_ca_cert_store(ca_store_1);
|
||||
|
||||
auto ca_store_2 = X509_STORE_new();
|
||||
X509_STORE_load_locations(ca_store_2, "/etc/ssl/certs/ca-certificates.crt",
|
||||
nullptr);
|
||||
httplib_client.set_ca_cert_store(ca_store_2);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerNameIndication) {
|
||||
SSLClient cli("httpbin.org", 443);
|
||||
auto res = cli.Get("/get");
|
||||
@@ -3130,6 +3305,31 @@ TEST(SSLClientTest, ServerCertificateVerification3) {
|
||||
ASSERT_EQ(301, res->status);
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, ServerCertificateVerification4) {
|
||||
SSLServer svr(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||
ASSERT_TRUE(svr.is_valid());
|
||||
|
||||
svr.Get("/test", [&](const Request &, Response &res) {
|
||||
res.set_content("test", "text/plain");
|
||||
svr.stop();
|
||||
ASSERT_TRUE(true);
|
||||
});
|
||||
|
||||
thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); });
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
|
||||
SSLClient cli("127.0.0.1", PORT);
|
||||
cli.set_ca_cert_path(SERVER_CERT2_FILE);
|
||||
cli.enable_server_certificate_verification(true);
|
||||
cli.set_connection_timeout(30);
|
||||
|
||||
auto res = cli.Get("/test");
|
||||
ASSERT_TRUE(res);
|
||||
ASSERT_EQ(200, res->status);
|
||||
|
||||
t.join();
|
||||
}
|
||||
|
||||
TEST(SSLClientTest, WildcardHostNameMatch) {
|
||||
SSLClient cli("www.youtube.com");
|
||||
|
||||
@@ -3184,6 +3384,7 @@ TEST(SSLClientServerTest, ClientCertPresent) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
#if !defined(_WIN32) || defined(OPENSSL_USE_APPLINK)
|
||||
TEST(SSLClientServerTest, MemoryClientCertPresent) {
|
||||
X509 *server_cert;
|
||||
EVP_PKEY *server_private_key;
|
||||
@@ -3259,6 +3460,7 @@ TEST(SSLClientServerTest, MemoryClientCertPresent) {
|
||||
|
||||
t.join();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(SSLClientServerTest, ClientCertMissing) {
|
||||
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE, CLIENT_CA_CERT_FILE,
|
||||
@@ -3303,6 +3505,51 @@ TEST(SSLClientServerTest, TrustDirOptional) {
|
||||
|
||||
t.join();
|
||||
}
|
||||
|
||||
TEST(SSLClientServerTest, SSLConnectTimeout) {
|
||||
class NoListenSSLServer : public SSLServer {
|
||||
public:
|
||||
NoListenSSLServer(const char *cert_path, const char *private_key_path,
|
||||
const char *client_ca_cert_file_path,
|
||||
const char *client_ca_cert_dir_path = nullptr)
|
||||
: SSLServer(cert_path, private_key_path, client_ca_cert_file_path,
|
||||
client_ca_cert_dir_path),
|
||||
stop_(false) {}
|
||||
|
||||
bool stop_;
|
||||
|
||||
private:
|
||||
bool process_and_close_socket(socket_t /*sock*/) override {
|
||||
// Don't create SSL context
|
||||
while (!stop_) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
NoListenSSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
|
||||
CLIENT_CA_CERT_FILE);
|
||||
ASSERT_TRUE(svr.is_valid());
|
||||
|
||||
svr.Get("/test", [&](const Request &, Response &res) {
|
||||
res.set_content("test", "text/plain");
|
||||
});
|
||||
|
||||
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
|
||||
SSLClient cli(HOST, PORT, CLIENT_CERT_FILE, CLIENT_PRIVATE_KEY_FILE);
|
||||
cli.enable_server_certificate_verification(false);
|
||||
cli.set_connection_timeout(1);
|
||||
|
||||
auto res = cli.Get("/test");
|
||||
ASSERT_TRUE(!res);
|
||||
EXPECT_EQ(Error::SSLConnection, res.error());
|
||||
|
||||
svr.stop_ = true;
|
||||
svr.stop();
|
||||
t.join();
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -3312,17 +3559,15 @@ TEST(CleanupTest, WSACleanup) {
|
||||
}
|
||||
#endif
|
||||
|
||||
// #ifndef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
// TEST(NoSSLSupport, SimpleInterface) {
|
||||
// Client cli("https://yahoo.com");
|
||||
// ASSERT_FALSE(cli.is_valid());
|
||||
// }
|
||||
// #endif
|
||||
#ifndef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
TEST(NoSSLSupport, SimpleInterface) {
|
||||
ASSERT_ANY_THROW(Client cli("https://yahoo.com"));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
TEST(InvalidScheme, SimpleInterface) {
|
||||
Client cli("scheme://yahoo.com");
|
||||
ASSERT_FALSE(cli.is_valid());
|
||||
ASSERT_ANY_THROW(Client cli("scheme://yahoo.com"));
|
||||
}
|
||||
|
||||
TEST(NoScheme, SimpleInterface) {
|
||||
|
||||
@@ -16,3 +16,6 @@ emailAddress = test@email.address
|
||||
|
||||
[req_attributes]
|
||||
challengePassword = 1234
|
||||
|
||||
[SAN]
|
||||
subjectAltName=IP:127.0.0.1
|
||||
|
||||
Reference in New Issue
Block a user