mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-17 19:30:54 +00:00
Compare commits
25
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a11f530494 | ||
|
|
b23e16775d | ||
|
|
f57672b390 | ||
|
|
654fd3546c | ||
|
|
546e821c65 | ||
|
|
53a7f4964e | ||
|
|
8c5fd6aa08 | ||
|
|
a2ebab183d | ||
|
|
a39e081386 | ||
|
|
07142256ed | ||
|
|
cc7a30f12f | ||
|
|
cdcd2ac6e6 | ||
|
|
bd23a7b179 | ||
|
|
6d8dec3c3e | ||
|
|
7e5d0683ac | ||
|
|
6df971434e | ||
|
|
39c98d4671 | ||
|
|
a1ba73a256 | ||
|
|
453b1b8613 | ||
|
|
a18895f42e | ||
|
|
24e58ce5b2 | ||
|
|
a0ab09fc0a | ||
|
|
290c4c0bbe | ||
|
|
25ba40f6fe | ||
|
|
578e4815b7 |
No files matched your search
+22
-1
@@ -200,6 +200,10 @@ void close(CloseStatus status = CloseStatus::Normal,
|
|||||||
const std::string &reason = "");
|
const std::string &reason = "");
|
||||||
bool is_open() const;
|
bool is_open() const;
|
||||||
|
|
||||||
|
// Authentication - the same setters Client has (Basic wins over Bearer).
|
||||||
|
void set_basic_auth(const std::string &username, const std::string &password);
|
||||||
|
void set_bearer_token_auth(const std::string &token);
|
||||||
|
|
||||||
// Proxy - the same setters Client has. connect() opens a CONNECT tunnel
|
// Proxy - the same setters Client has. connect() opens a CONNECT tunnel
|
||||||
// through the proxy for ws:// as well as wss:// (RFC 6455 4.1); the proxy
|
// through the proxy for ws:// as well as wss:// (RFC 6455 4.1); the proxy
|
||||||
// credentials are sent only on the CONNECT request, never through the tunnel.
|
// credentials are sent only on the CONNECT request, never through the tunnel.
|
||||||
@@ -233,7 +237,8 @@ void set_address_family(int family);
|
|||||||
void set_ipv6_v6only(bool on);
|
void set_ipv6_v6only(bool on);
|
||||||
void set_socket_options(SocketOptions socket_options);
|
void set_socket_options(SocketOptions socket_options);
|
||||||
void set_interface(const std::string &intf);
|
void set_interface(const std::string &intf);
|
||||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
void set_direct_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void set_tunnel_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
|
||||||
// SSL configuration (wss:// only, SSL builds only)
|
// SSL configuration (wss:// only, SSL builds only)
|
||||||
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
||||||
@@ -403,6 +408,22 @@ svr.set_pre_request_handler([](const httplib::Request &req, httplib::Response &r
|
|||||||
|
|
||||||
The server accepts a handshake from any `Origin`. A browser sends its cookies with a handshake another site starts, so if cookies authenticate the connection, also check `req.get_header_value("Origin")` against your own origins in one of these handlers (cross-site WebSocket hijacking).
|
The server accepts a handshake from any `Origin`. A browser sends its cookies with a handshake another site starts, so if cookies authenticate the connection, also check `req.get_header_value("Origin")` against your own origins in one of these handlers (cross-site WebSocket hijacking).
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
httplib::ws::WebSocketClient ws("wss://example.com/ws");
|
||||||
|
ws.set_bearer_token_auth("token123"); // Authorization: Bearer token123
|
||||||
|
// or
|
||||||
|
ws.set_basic_auth("user", "pass"); // Authorization: Basic dXNlcjpwYXNz
|
||||||
|
|
||||||
|
if (ws.connect()) { /* ... */ }
|
||||||
|
```
|
||||||
|
|
||||||
|
The credentials are read on every `connect()`, so a new token takes effect on
|
||||||
|
the next reconnect. An `Authorization` header passed to the constructor wins
|
||||||
|
over them. Through a proxy they are sent only inside the tunnel, never on
|
||||||
|
`CONNECT`.
|
||||||
|
|
||||||
### Custom Headers and Timeouts
|
### Custom Headers and Timeouts
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|||||||
@@ -742,7 +742,7 @@ svr.Post("/multipart", [&](const Request& req, Response& res) {
|
|||||||
auto safe = httplib::sanitize_filename(file.filename);
|
auto safe = httplib::sanitize_filename(file.filename);
|
||||||
```
|
```
|
||||||
|
|
||||||
This function keeps only the part after the last `/` or `\`, removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.
|
This function keeps only the part after the last `/` or `\` (or, on Windows, `:`, which starts a drive prefix such as `C:` or an NTFS stream name), removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> `name` and `filename` are read as quoted-strings (RFC 9110 §5.6.4), where `\"` and `\\` stand for `"` and `\`. Browsers send a backslash as it is, so from a browser, a value with `\\` in it arrives with a single `\`, and one that ends in `\` is not read correctly.
|
> `name` and `filename` are read as quoted-strings (RFC 9110 §5.6.4), where `\"` and `\\` stand for `"` and `\`. Browsers send a backslash as it is, so from a browser, a value with `\\` in it arrives with a single `\`, and one that ends in `\` is not read correctly.
|
||||||
@@ -1477,17 +1477,17 @@ The same method is available on `httplib::ws::WebSocketClient`.
|
|||||||
|
|
||||||
### Override the connection target for a hostname
|
### Override the connection target for a hostname
|
||||||
|
|
||||||
`set_hostname_addr_map` redirects where the socket connects, without changing
|
`set_direct_hostname_addr_map` redirects where the socket connects, without
|
||||||
the identity of the request. The hostname the client was constructed with keeps
|
changing the identity of the request. The hostname the client was constructed
|
||||||
supplying the `Host` header, the SNI, and the name that the server certificate
|
with keeps supplying the `Host` header, the SNI, and the name that the server
|
||||||
is verified against, so this is a connection-level override only, not a way to
|
certificate is verified against, so this is a connection-level override only,
|
||||||
talk to a different origin.
|
not a way to talk to a different origin.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
httplib::Client cli("https://example.com");
|
httplib::Client cli("https://example.com");
|
||||||
|
|
||||||
// Connect to this IP address instead of resolving "example.com"
|
// Connect to this IP address instead of resolving "example.com"
|
||||||
cli.set_hostname_addr_map({{"example.com", "192.168.1.10"}});
|
cli.set_direct_hostname_addr_map({{"example.com", "192.168.1.10"}});
|
||||||
```
|
```
|
||||||
|
|
||||||
A mapped value may be an IP literal or another hostname. An IP literal is used
|
A mapped value may be an IP literal or another hostname. An IP literal is used
|
||||||
@@ -1495,13 +1495,37 @@ as-is; anything else is resolved as a name, so a host that is only reachable
|
|||||||
under a different name works too:
|
under a different name works too:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
cli.set_hostname_addr_map({{"example.com", "internal.example.lan"}});
|
cli.set_direct_hostname_addr_map({{"example.com", "internal.example.lan"}});
|
||||||
```
|
```
|
||||||
|
|
||||||
An empty value is ignored, leaving the original hostname as the connection
|
An empty value is ignored, leaving the original hostname as the connection
|
||||||
target.
|
target.
|
||||||
|
|
||||||
The same method is available on `httplib::ws::WebSocketClient`.
|
The direct map applies only when the client connects to the origin itself.
|
||||||
|
Through a proxy, use `set_tunnel_hostname_addr_map`: its value becomes the
|
||||||
|
`CONNECT` target (and that request's `Host`) that the proxy is asked to connect
|
||||||
|
to (HTTPS and WebSocket). The `Host` header, the SNI and the certificate check
|
||||||
|
inside the tunnel keep the original hostname:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
httplib::Client cli("https://sni.example");
|
||||||
|
|
||||||
|
cli.set_tunnel_hostname_addr_map({{"sni.example", "server.example"}});
|
||||||
|
cli.set_proxy("proxy.example", 3128);
|
||||||
|
// CONNECT server.example:443, SNI sni.example
|
||||||
|
```
|
||||||
|
|
||||||
|
The two maps are independent; to use the same target on both paths, set both.
|
||||||
|
They correspond to curl's `--resolve` and `--connect-to`.
|
||||||
|
|
||||||
|
Neither map applies to the proxy's own address ("direct" means not through a
|
||||||
|
proxy, as in a PAC file's `DIRECT`). To connect to the proxy at a fixed
|
||||||
|
address, pass an IP literal to `set_proxy()`.
|
||||||
|
|
||||||
|
`set_hostname_addr_map` is deprecated and works the same as
|
||||||
|
`set_direct_hostname_addr_map`.
|
||||||
|
|
||||||
|
These methods are also available on `httplib::ws::WebSocketClient`.
|
||||||
|
|
||||||
### Automatic Path Encoding
|
### Automatic Path Encoding
|
||||||
|
|
||||||
@@ -1692,6 +1716,8 @@ cli.set_address_family(AF_UNIX);
|
|||||||
|
|
||||||
This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior.
|
This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior.
|
||||||
|
|
||||||
|
With `set_follow_location(true)`, a redirect is followed over the same socket, whatever host the `Location` names, as curl's `--unix-socket` does. A host mapped with `set_direct_hostname_addr_map` goes to its mapped socket instead. Only a `Location` naming `localhost` counts as the same host; any other host is treated as cross-host, so `Authorization` and `Cookie` are not sent to it.
|
||||||
|
|
||||||
|
|
||||||
URI Encoding/Decoding Utilities
|
URI Encoding/Decoding Utilities
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
|
|||||||
|
|
||||||
[site]
|
[site]
|
||||||
title = "cpp-httplib"
|
title = "cpp-httplib"
|
||||||
version = "0.63.0"
|
version = "0.66.0"
|
||||||
hostname = "https://yhirose.github.io"
|
hostname = "https://yhirose.github.io"
|
||||||
base_path = "/cpp-httplib"
|
base_path = "/cpp-httplib"
|
||||||
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ svr.listen("/tmp/httplib.sock", 80); // The port is unused, but must not be 0
|
|||||||
// Client
|
// Client
|
||||||
httplib::Client cli("http://localhost");
|
httplib::Client cli("http://localhost");
|
||||||
cli.set_address_family(AF_UNIX);
|
cli.set_address_family(AF_UNIX);
|
||||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
cli.set_direct_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||||
|
|
||||||
auto res = cli.Get("/");
|
auto res = cli.Get("/");
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ svr.listen("/tmp/httplib.sock", 80); // ポート番号は使われない(0
|
|||||||
// クライアント
|
// クライアント
|
||||||
httplib::Client cli("http://localhost");
|
httplib::Client cli("http://localhost");
|
||||||
cli.set_address_family(AF_UNIX);
|
cli.set_address_family(AF_UNIX);
|
||||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
cli.set_direct_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||||
|
|
||||||
auto res = cli.Get("/");
|
auto res = cli.Get("/");
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
#ifndef CPPHTTPLIB_HTTPLIB_H
|
#ifndef CPPHTTPLIB_HTTPLIB_H
|
||||||
#define CPPHTTPLIB_HTTPLIB_H
|
#define CPPHTTPLIB_HTTPLIB_H
|
||||||
|
|
||||||
#define CPPHTTPLIB_VERSION "0.63.0"
|
#define CPPHTTPLIB_VERSION "0.66.0"
|
||||||
#define CPPHTTPLIB_VERSION_NUM "0x003f00"
|
#define CPPHTTPLIB_VERSION_NUM "0x004200"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
|
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
|
||||||
@@ -1029,18 +1029,27 @@ inline std::string resolve_relative_location(const std::string &location,
|
|||||||
// A ':' in the first segment means the value has a scheme.
|
// A ':' in the first segment means the value has a scheme.
|
||||||
if (location.find(':') < location.find_first_of("/?#")) { return location; }
|
if (location.find(':') < location.find_first_of("/?#")) { return location; }
|
||||||
|
|
||||||
if (location[0] == '#') { return base.substr(0, base.find('#')) + location; }
|
// A path starting with "//" would be re-parsed as an authority. Prefix "/."
|
||||||
|
// (as the URL Standard does); remove_dot_segments strips it later.
|
||||||
|
auto on_this_host = [](std::string path) -> std::string {
|
||||||
|
if (path.compare(0, 2, "//") == 0) { path.insert(0, "/."); }
|
||||||
|
return path;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (location[0] == '#') {
|
||||||
|
return on_this_host(base.substr(0, base.find('#')) + location);
|
||||||
|
}
|
||||||
|
|
||||||
auto base_path = base.substr(0, base.find_first_of("?#"));
|
auto base_path = base.substr(0, base.find_first_of("?#"));
|
||||||
if (location[0] == '?') { return base_path + location; }
|
if (location[0] == '?') { return on_this_host(base_path + location); }
|
||||||
|
|
||||||
if (base_path.empty() || base_path[0] != '/') { base_path = "/"; }
|
if (base_path.empty() || base_path[0] != '/') { base_path = "/"; }
|
||||||
auto merged = base_path.substr(0, base_path.rfind('/') + 1) + location;
|
auto merged = base_path.substr(0, base_path.rfind('/') + 1) + location;
|
||||||
|
|
||||||
// Remove "." and ".." segments from the merged path.
|
// Remove "." and ".." segments from the merged path.
|
||||||
auto path_end = (std::min)(merged.find_first_of("?#"), merged.size());
|
auto path_end = (std::min)(merged.find_first_of("?#"), merged.size());
|
||||||
return remove_dot_segments(merged.substr(0, path_end)) +
|
return on_this_host(remove_dot_segments(merged.substr(0, path_end)) +
|
||||||
merged.substr(path_end);
|
merged.substr(path_end));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
@@ -1335,7 +1344,7 @@ public:
|
|||||||
size_type erase(const std::string &key) {
|
size_type erase(const std::string &key) {
|
||||||
auto before = entries_.size();
|
auto before = entries_.size();
|
||||||
entries_.erase(std::remove_if(entries_.begin(), entries_.end(),
|
entries_.erase(std::remove_if(entries_.begin(), entries_.end(),
|
||||||
[&](const value_type &entry) {
|
[key](const value_type &entry) {
|
||||||
return keys_equal(entry.first, key);
|
return keys_equal(entry.first, key);
|
||||||
}),
|
}),
|
||||||
entries_.end());
|
entries_.end());
|
||||||
@@ -2927,7 +2936,12 @@ public:
|
|||||||
size_t is_socket_open() const;
|
size_t is_socket_open() const;
|
||||||
socket_t socket() const;
|
socket_t socket() const;
|
||||||
|
|
||||||
|
[[deprecated("Use set_direct_hostname_addr_map() instead.")]]
|
||||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_direct_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_tunnel_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
|
||||||
void set_default_headers(Headers headers);
|
void set_default_headers(Headers headers);
|
||||||
|
|
||||||
@@ -3017,8 +3031,8 @@ protected:
|
|||||||
void disconnect(bool gracefully);
|
void disconnect(bool gracefully);
|
||||||
|
|
||||||
// Under socket_mutex_: whether the connection kept alive can take another
|
// Under socket_mutex_: whether the connection kept alive can take another
|
||||||
// request. One the peer has closed, or one set up under TLS settings changed
|
// request. One with anything to read, or one set up under TLS settings
|
||||||
// since, is closed instead.
|
// changed since, is closed instead.
|
||||||
bool socket_reusable();
|
bool socket_reusable();
|
||||||
|
|
||||||
// TLS settings apply as a connection is set up, so a change makes the
|
// TLS settings apply as a connection is set up, so a change makes the
|
||||||
@@ -3053,12 +3067,19 @@ protected:
|
|||||||
bool socket_should_be_closed_when_request_is_done_ = false;
|
bool socket_should_be_closed_when_request_is_done_ = false;
|
||||||
size_t socket_tls_settings_version_ = 0;
|
size_t socket_tls_settings_version_ = 0;
|
||||||
|
|
||||||
|
// Set by stop() while a request is in flight. The request's reads poll it,
|
||||||
|
// because shutting the socket down does not wake a read on Windows.
|
||||||
|
std::atomic<bool> stop_requested_{false};
|
||||||
|
|
||||||
std::atomic<size_t> tls_settings_version_{0};
|
std::atomic<size_t> tls_settings_version_{0};
|
||||||
|
|
||||||
// Hostname to connection target map. The value is an IP literal or another
|
// Hostname to connection target map. The value is an IP literal or another
|
||||||
// hostname; only the connection target changes, never the identity.
|
// hostname; only the connection target changes, never the identity.
|
||||||
std::map<std::string, std::string> addr_map_;
|
std::map<std::string, std::string> addr_map_;
|
||||||
|
|
||||||
|
// Hostname to CONNECT target map, used only through a proxy.
|
||||||
|
std::map<std::string, std::string> tunnel_addr_map_;
|
||||||
|
|
||||||
// Default headers
|
// Default headers
|
||||||
Headers default_headers_;
|
Headers default_headers_;
|
||||||
|
|
||||||
@@ -3348,7 +3369,12 @@ public:
|
|||||||
size_t is_socket_open() const;
|
size_t is_socket_open() const;
|
||||||
socket_t socket() const;
|
socket_t socket() const;
|
||||||
|
|
||||||
|
[[deprecated("Use set_direct_hostname_addr_map() instead.")]]
|
||||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_direct_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_tunnel_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
|
||||||
void set_default_headers(Headers headers);
|
void set_default_headers(Headers headers);
|
||||||
|
|
||||||
@@ -4516,8 +4542,6 @@ public:
|
|||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool parse_sse_line(const std::string &line, SSEMessage &msg, int &retry_ms,
|
|
||||||
bool &has_data, bool &has_id);
|
|
||||||
void run_event_loop();
|
void run_event_loop();
|
||||||
void dispatch_event(const SSEMessage &msg);
|
void dispatch_event(const SSEMessage &msg);
|
||||||
bool should_reconnect(int count) const;
|
bool should_reconnect(int count) const;
|
||||||
@@ -4552,6 +4576,40 @@ private:
|
|||||||
|
|
||||||
} // namespace sse
|
} // namespace sse
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
// Interprets an event stream fed in chunks of any size
|
||||||
|
class SSEParser {
|
||||||
|
public:
|
||||||
|
using EventHandler = std::function<void(const sse::SSEMessage &msg)>;
|
||||||
|
using IdHandler = std::function<void(const std::string &id)>;
|
||||||
|
using RetryHandler = std::function<void(int retry_ms)>;
|
||||||
|
|
||||||
|
SSEParser(EventHandler on_event, IdHandler on_id, RetryHandler on_retry,
|
||||||
|
size_t max_event_length = CPPHTTPLIB_SSE_MAX_EVENT_LENGTH);
|
||||||
|
|
||||||
|
// Returns false once the line and the event still waiting for their end
|
||||||
|
// exceed max_event_length. 0 means no limit.
|
||||||
|
bool parse(const char *buf, size_t n);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void parse_line(const std::string &line);
|
||||||
|
|
||||||
|
EventHandler on_event_;
|
||||||
|
IdHandler on_id_;
|
||||||
|
RetryHandler on_retry_;
|
||||||
|
size_t max_event_length_;
|
||||||
|
|
||||||
|
std::string buffer_;
|
||||||
|
sse::SSEMessage msg_;
|
||||||
|
bool has_data_ = false;
|
||||||
|
bool has_id_ = false;
|
||||||
|
bool first_line_ = true;
|
||||||
|
bool after_cr_ = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
namespace ws {
|
namespace ws {
|
||||||
|
|
||||||
class WebSocketClient;
|
class WebSocketClient;
|
||||||
@@ -4748,7 +4806,15 @@ public:
|
|||||||
set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
|
set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||||
|
|
||||||
void set_interface(const std::string &intf);
|
void set_interface(const std::string &intf);
|
||||||
|
[[deprecated("Use set_direct_hostname_addr_map() instead.")]]
|
||||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_direct_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void
|
||||||
|
set_tunnel_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
|
||||||
|
void set_basic_auth(const std::string &username, const std::string &password);
|
||||||
|
void set_bearer_token_auth(const std::string &token);
|
||||||
|
|
||||||
void set_proxy(const std::string &host, int port);
|
void set_proxy(const std::string &host, int port);
|
||||||
void set_proxy_basic_auth(const std::string &username,
|
void set_proxy_basic_auth(const std::string &username,
|
||||||
@@ -4820,6 +4886,13 @@ private:
|
|||||||
// hostname; only the connection target changes, never the identity.
|
// hostname; only the connection target changes, never the identity.
|
||||||
std::map<std::string, std::string> addr_map_;
|
std::map<std::string, std::string> addr_map_;
|
||||||
|
|
||||||
|
// Hostname to CONNECT target map, used only through a proxy.
|
||||||
|
std::map<std::string, std::string> tunnel_addr_map_;
|
||||||
|
|
||||||
|
std::string basic_auth_username_;
|
||||||
|
std::string basic_auth_password_;
|
||||||
|
std::string bearer_token_auth_token_;
|
||||||
|
|
||||||
std::string proxy_host_;
|
std::string proxy_host_;
|
||||||
int proxy_port_ = -1;
|
int proxy_port_ = -1;
|
||||||
std::string proxy_basic_auth_username_;
|
std::string proxy_basic_auth_username_;
|
||||||
@@ -4982,6 +5055,110 @@ inline std::string Result::read_all() {
|
|||||||
|
|
||||||
} // namespace stream
|
} // namespace stream
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
inline SSEParser::SSEParser(EventHandler on_event, IdHandler on_id,
|
||||||
|
RetryHandler on_retry, size_t max_event_length)
|
||||||
|
: on_event_(std::move(on_event)), on_id_(std::move(on_id)),
|
||||||
|
on_retry_(std::move(on_retry)), max_event_length_(max_event_length) {}
|
||||||
|
|
||||||
|
inline bool SSEParser::parse(const char *buf, size_t n) {
|
||||||
|
// What is left in the buffer has no line ending
|
||||||
|
auto search_pos = buffer_.size();
|
||||||
|
buffer_.append(buf, n);
|
||||||
|
|
||||||
|
// A line ends with \r\n, \n or \r per the SSE spec
|
||||||
|
size_t line_start = 0;
|
||||||
|
size_t eol_pos;
|
||||||
|
|
||||||
|
while ((eol_pos = buffer_.find_first_of("\r\n", search_pos)) !=
|
||||||
|
std::string::npos) {
|
||||||
|
auto line = buffer_.substr(line_start, eol_pos - line_start);
|
||||||
|
line_start = eol_pos + 1;
|
||||||
|
search_pos = line_start;
|
||||||
|
|
||||||
|
// The \n of a \r\n may arrive in a later chunk than its \r
|
||||||
|
auto is_lf_of_crlf = after_cr_ && line.empty() && buffer_[eol_pos] == '\n';
|
||||||
|
after_cr_ = buffer_[eol_pos] == '\r';
|
||||||
|
if (is_lf_of_crlf) { continue; }
|
||||||
|
|
||||||
|
// A UTF-8 BOM at the start of the stream is ignored per the SSE spec
|
||||||
|
if (first_line_) {
|
||||||
|
first_line_ = false;
|
||||||
|
if (line.compare(0, 3, "\xEF\xBB\xBF") == 0) { line.erase(0, 3); }
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_line(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep unprocessed data in buffer
|
||||||
|
buffer_.erase(0, line_start);
|
||||||
|
|
||||||
|
return max_event_length_ == 0 ||
|
||||||
|
buffer_.size() + msg_.data.size() <= max_event_length_;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void SSEParser::parse_line(const std::string &line) {
|
||||||
|
// Blank line signals end of event
|
||||||
|
if (line.empty()) {
|
||||||
|
// The id counts for Last-Event-ID even for an event that has no data. An
|
||||||
|
// empty id clears it.
|
||||||
|
if (has_id_ && on_id_) { on_id_(msg_.id); }
|
||||||
|
|
||||||
|
// An event without a data field is not dispatched
|
||||||
|
if (has_data_ && on_event_) { on_event_(msg_); }
|
||||||
|
|
||||||
|
msg_.clear();
|
||||||
|
has_data_ = false;
|
||||||
|
has_id_ = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lines starting with ':' are comments (ignored)
|
||||||
|
if (line[0] == ':') { return; }
|
||||||
|
|
||||||
|
// Find the colon separator
|
||||||
|
auto colon_pos = line.find(':');
|
||||||
|
auto field = line.substr(0, colon_pos);
|
||||||
|
std::string value;
|
||||||
|
|
||||||
|
// Value starts after colon, skip optional single space
|
||||||
|
if (colon_pos != std::string::npos && colon_pos + 1 < line.size()) {
|
||||||
|
auto value_start = colon_pos + 1;
|
||||||
|
if (line[value_start] == ' ') { value_start++; }
|
||||||
|
value = line.substr(value_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle known fields
|
||||||
|
if (field == "event") {
|
||||||
|
// An empty event type is dispatched as "message" per the SSE spec
|
||||||
|
msg_.event = value.empty() ? std::string("message") : std::move(value);
|
||||||
|
} else if (field == "data") {
|
||||||
|
// Multiple data lines are concatenated with newlines
|
||||||
|
if (has_data_) { msg_.data += "\n"; }
|
||||||
|
msg_.data += value;
|
||||||
|
has_data_ = true;
|
||||||
|
} else if (field == "id") {
|
||||||
|
// An id that cannot be sent back as Last-Event-ID is ignored. Empty id is
|
||||||
|
// valid (clears the last event ID)
|
||||||
|
if (fields::is_field_value(value)) {
|
||||||
|
msg_.id = value;
|
||||||
|
has_id_ = true;
|
||||||
|
}
|
||||||
|
} else if (field == "retry") {
|
||||||
|
// Parse retry interval in milliseconds
|
||||||
|
// Per the SSE spec, a value that is not all ASCII digits is ignored.
|
||||||
|
if (is_numeric(value)) {
|
||||||
|
int v = 0;
|
||||||
|
auto res = from_chars(value.data(), value.data() + value.size(), v);
|
||||||
|
if (res.ec == std::errc{} && on_retry_) { on_retry_(v); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Unknown fields are ignored per SSE spec
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
namespace sse {
|
namespace sse {
|
||||||
|
|
||||||
// SSEMessage implementations
|
// SSEMessage implementations
|
||||||
@@ -5063,58 +5240,6 @@ inline void SSEClient::stop() {
|
|||||||
if (async_thread_.joinable()) { async_thread_.join(); }
|
if (async_thread_.joinable()) { async_thread_.join(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool SSEClient::parse_sse_line(const std::string &line, SSEMessage &msg,
|
|
||||||
int &retry_ms, bool &has_data,
|
|
||||||
bool &has_id) {
|
|
||||||
// Blank line signals end of event
|
|
||||||
if (line.empty()) { return true; }
|
|
||||||
|
|
||||||
// Lines starting with ':' are comments (ignored)
|
|
||||||
if (line[0] == ':') { return false; }
|
|
||||||
|
|
||||||
// Find the colon separator
|
|
||||||
auto colon_pos = line.find(':');
|
|
||||||
auto field = line.substr(0, colon_pos);
|
|
||||||
std::string value;
|
|
||||||
|
|
||||||
// Value starts after colon, skip optional single space
|
|
||||||
if (colon_pos != std::string::npos && colon_pos + 1 < line.size()) {
|
|
||||||
auto value_start = colon_pos + 1;
|
|
||||||
if (line[value_start] == ' ') { value_start++; }
|
|
||||||
value = line.substr(value_start);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle known fields
|
|
||||||
if (field == "event") {
|
|
||||||
// An empty event type is dispatched as "message" per the SSE spec
|
|
||||||
msg.event = value.empty() ? std::string("message") : std::move(value);
|
|
||||||
} else if (field == "data") {
|
|
||||||
// Multiple data lines are concatenated with newlines
|
|
||||||
if (has_data) { msg.data += "\n"; }
|
|
||||||
msg.data += value;
|
|
||||||
has_data = true;
|
|
||||||
} else if (field == "id") {
|
|
||||||
// An id that cannot be sent back as Last-Event-ID is ignored. Empty id is
|
|
||||||
// valid (clears the last event ID)
|
|
||||||
if (detail::fields::is_field_value(value)) {
|
|
||||||
msg.id = value;
|
|
||||||
has_id = true;
|
|
||||||
}
|
|
||||||
} else if (field == "retry") {
|
|
||||||
// Parse retry interval in milliseconds
|
|
||||||
// Per the SSE spec, a value that is not all ASCII digits is ignored.
|
|
||||||
if (detail::is_numeric(value)) {
|
|
||||||
int v = 0;
|
|
||||||
auto res =
|
|
||||||
detail::from_chars(value.data(), value.data() + value.size(), v);
|
|
||||||
if (res.ec == std::errc{}) { retry_ms = v; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Unknown fields are ignored per SSE spec
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void SSEClient::run_event_loop() {
|
inline void SSEClient::run_event_loop() {
|
||||||
auto reconnect_count = 0;
|
auto reconnect_count = 0;
|
||||||
|
|
||||||
@@ -5175,72 +5300,17 @@ inline void SSEClient::run_event_loop() {
|
|||||||
if (on_open_) { on_open_(); }
|
if (on_open_) { on_open_(); }
|
||||||
|
|
||||||
// Event receiving loop
|
// Event receiving loop
|
||||||
std::string buffer;
|
detail::SSEParser parser(
|
||||||
SSEMessage current_msg;
|
[this](const SSEMessage &msg) { dispatch_event(msg); },
|
||||||
auto has_data = false;
|
[this](const std::string &id) {
|
||||||
auto has_id = false;
|
std::lock_guard<std::mutex> lock(last_event_id_mutex_);
|
||||||
auto first_line = true;
|
last_event_id_ = id;
|
||||||
auto after_cr = false;
|
},
|
||||||
|
[this](int retry_ms) { reconnect_interval_ms_ = retry_ms; });
|
||||||
auto event_too_long = false;
|
auto event_too_long = false;
|
||||||
|
|
||||||
while (running_.load() && result.next()) {
|
while (running_.load() && result.next()) {
|
||||||
// What is left in the buffer has no line ending
|
if (!parser.parse(result.data(), result.size())) {
|
||||||
auto search_pos = buffer.size();
|
|
||||||
buffer.append(result.data(), result.size());
|
|
||||||
|
|
||||||
// Process complete lines in the buffer. A line ends with \r\n, \n or \r
|
|
||||||
// per the SSE spec
|
|
||||||
size_t line_start = 0;
|
|
||||||
size_t eol_pos;
|
|
||||||
|
|
||||||
while ((eol_pos = buffer.find_first_of("\r\n", search_pos)) !=
|
|
||||||
std::string::npos) {
|
|
||||||
auto line = buffer.substr(line_start, eol_pos - line_start);
|
|
||||||
line_start = eol_pos + 1;
|
|
||||||
search_pos = line_start;
|
|
||||||
|
|
||||||
// The \n of a \r\n may arrive in a later chunk than its \r
|
|
||||||
auto is_lf_of_crlf =
|
|
||||||
after_cr && line.empty() && buffer[eol_pos] == '\n';
|
|
||||||
after_cr = buffer[eol_pos] == '\r';
|
|
||||||
if (is_lf_of_crlf) { continue; }
|
|
||||||
|
|
||||||
// A UTF-8 BOM at the start of the stream is ignored per the SSE spec
|
|
||||||
if (first_line) {
|
|
||||||
first_line = false;
|
|
||||||
if (line.compare(0, 3, "\xEF\xBB\xBF") == 0) { line.erase(0, 3); }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse the line and check if event is complete
|
|
||||||
auto event_complete = parse_sse_line(
|
|
||||||
line, current_msg, reconnect_interval_ms_, has_data, has_id);
|
|
||||||
|
|
||||||
if (event_complete) {
|
|
||||||
// Update last_event_id for reconnection, even for an event that
|
|
||||||
// has no data. An empty id clears it.
|
|
||||||
if (has_id) {
|
|
||||||
std::lock_guard<std::mutex> lock(last_event_id_mutex_);
|
|
||||||
last_event_id_ = current_msg.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// An event without a data field is not dispatched
|
|
||||||
if (has_data) { dispatch_event(current_msg); }
|
|
||||||
|
|
||||||
// Reset the message for the next event either way
|
|
||||||
current_msg.clear();
|
|
||||||
has_data = false;
|
|
||||||
has_id = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Keep unprocessed data in buffer
|
|
||||||
buffer.erase(0, line_start);
|
|
||||||
|
|
||||||
// Neither an event nor a line still waiting for its end may grow
|
|
||||||
// without bound. 0 means no limit.
|
|
||||||
if (CPPHTTPLIB_SSE_MAX_EVENT_LENGTH > 0 &&
|
|
||||||
buffer.size() + current_msg.data.size() >
|
|
||||||
CPPHTTPLIB_SSE_MAX_EVENT_LENGTH) {
|
|
||||||
if (on_error_) { on_error_(Error::ExceedMaxPayloadSize); }
|
if (on_error_) { on_error_(Error::ExceedMaxPayloadSize); }
|
||||||
event_too_long = true;
|
event_too_long = true;
|
||||||
break;
|
break;
|
||||||
@@ -6041,9 +6111,35 @@ inline bool is_valid_path(const std::string &path) {
|
|||||||
|
|
||||||
inline bool canonicalize_path(const char *path, std::string &resolved) {
|
inline bool canonicalize_path(const char *path, std::string &resolved) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
char buf[_MAX_PATH];
|
// Resolve junctions and symlinks, as realpath() does
|
||||||
if (_fullpath(buf, path, _MAX_PATH) == nullptr) { return false; }
|
auto wpath = u8string_to_wstring(path);
|
||||||
resolved = buf;
|
if (wpath.empty()) { return false; }
|
||||||
|
|
||||||
|
CREATEFILE2_EXTENDED_PARAMETERS params{};
|
||||||
|
params.dwSize = sizeof(params);
|
||||||
|
params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS; // Needed to open a directory
|
||||||
|
auto h = ::CreateFile2(wpath.c_str(), 0,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||||
|
OPEN_EXISTING, ¶ms);
|
||||||
|
if (h == INVALID_HANDLE_VALUE) { return false; }
|
||||||
|
auto guard = scope_exit([&]() { ::CloseHandle(h); });
|
||||||
|
|
||||||
|
std::wstring wbuf(MAX_PATH, L'\0');
|
||||||
|
auto len = ::GetFinalPathNameByHandleW(
|
||||||
|
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
|
||||||
|
if (len >= wbuf.size()) {
|
||||||
|
wbuf.resize(len);
|
||||||
|
len = ::GetFinalPathNameByHandleW(
|
||||||
|
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
|
||||||
|
}
|
||||||
|
if (len == 0 || len >= wbuf.size()) { return false; }
|
||||||
|
|
||||||
|
auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
|
||||||
|
nullptr, 0, nullptr, nullptr);
|
||||||
|
if (n <= 0) { return false; }
|
||||||
|
resolved.assign(static_cast<size_t>(n), '\0');
|
||||||
|
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
|
||||||
|
&resolved[0], n, nullptr, nullptr);
|
||||||
#elif defined(PATH_MAX)
|
#elif defined(PATH_MAX)
|
||||||
char buf[PATH_MAX];
|
char buf[PATH_MAX];
|
||||||
if (realpath(path, buf) == nullptr) { return false; }
|
if (realpath(path, buf) == nullptr) { return false; }
|
||||||
@@ -6812,10 +6908,14 @@ select_read_interruptible(socket_t sock, time_t sec, time_t usec,
|
|||||||
while (!interrupted()) {
|
while (!interrupted()) {
|
||||||
auto left = std::chrono::duration_cast<std::chrono::microseconds>(
|
auto left = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||||
deadline - std::chrono::steady_clock::now());
|
deadline - std::chrono::steady_clock::now());
|
||||||
auto wait =
|
auto last = left < slice;
|
||||||
(std::max)((std::min)(left, slice), std::chrono::microseconds(0));
|
// poll() takes whole milliseconds, so round the last wait up to keep it
|
||||||
auto ret = select_impl(sock, POLLIN, 0, static_cast<time_t>(wait.count()));
|
// from ending before the deadline.
|
||||||
if (ret != 0 || wait < slice) { return ret; }
|
auto wait_usec = last ? (std::max)((left.count() + 999) / 1000 * 1000,
|
||||||
|
static_cast<decltype(left.count())>(0))
|
||||||
|
: slice.count();
|
||||||
|
auto ret = select_impl(sock, POLLIN, 0, static_cast<time_t>(wait_usec));
|
||||||
|
if (ret != 0 || last) { return ret; }
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -9307,6 +9407,7 @@ inline bool redirect(T &cli, Request &req, Response &res,
|
|||||||
Error &error) {
|
Error &error) {
|
||||||
Request new_req = req;
|
Request new_req = req;
|
||||||
new_req.path = path;
|
new_req.path = path;
|
||||||
|
new_req.params.clear();
|
||||||
new_req.redirect_count_ -= 1;
|
new_req.redirect_count_ -= 1;
|
||||||
|
|
||||||
if (res.status == StatusCode::SeeOther_303 &&
|
if (res.status == StatusCode::SeeOther_303 &&
|
||||||
@@ -11975,8 +12076,13 @@ inline std::string decode_query_component(const std::string &component,
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline std::string sanitize_filename(const std::string &filename) {
|
inline std::string sanitize_filename(const std::string &filename) {
|
||||||
// Extract basename: find the last path separator (/ or \)
|
// Extract basename: find the last path separator (/ or \) and, on Windows,
|
||||||
|
// ':', which ends a drive prefix ("C:..") or names an NTFS stream ("a:b")
|
||||||
|
#ifdef _WIN32
|
||||||
|
auto pos = filename.find_last_of("/\\:");
|
||||||
|
#else
|
||||||
auto pos = filename.find_last_of("/\\");
|
auto pos = filename.find_last_of("/\\");
|
||||||
|
#endif
|
||||||
auto result =
|
auto result =
|
||||||
(pos != std::string::npos) ? filename.substr(pos + 1) : filename;
|
(pos != std::string::npos) ? filename.substr(pos + 1) : filename;
|
||||||
|
|
||||||
@@ -12587,7 +12693,16 @@ inline bool ThreadPool::enqueue(std::function<void()> fn) {
|
|||||||
if (jobs_.size() > idle_thread_count_ &&
|
if (jobs_.size() > idle_thread_count_ &&
|
||||||
threads_.size() + dynamic_threads_.size() < max_thread_count_) {
|
threads_.size() + dynamic_threads_.size() < max_thread_count_) {
|
||||||
cleanup_finished_threads();
|
cleanup_finished_threads();
|
||||||
dynamic_threads_.emplace_back(std::thread([this]() { worker(true); }));
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
try {
|
||||||
|
#endif
|
||||||
|
dynamic_threads_.emplace_back(std::thread([this]() { worker(true); }));
|
||||||
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
} catch (const std::system_error &) {
|
||||||
|
// No thread budget left: keep the job queued for an existing worker
|
||||||
|
// instead of letting the exception escape the accept loop.
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12826,7 +12941,8 @@ inline ssize_t SocketStream::read(char *ptr, size_t size) {
|
|||||||
inline ssize_t SocketStream::write(const char *ptr, size_t size) {
|
inline ssize_t SocketStream::write(const char *ptr, size_t size) {
|
||||||
if (!wait_writable()) { return -1; }
|
if (!wait_writable()) { return -1; }
|
||||||
|
|
||||||
#if defined(_WIN32) && !defined(_WIN64)
|
// Winsock's send() takes an int length, even on 64-bit Windows
|
||||||
|
#ifdef _WIN32
|
||||||
size =
|
size =
|
||||||
(std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
|
(std::min)(size, static_cast<size_t>((std::numeric_limits<int>::max)()));
|
||||||
#endif
|
#endif
|
||||||
@@ -13041,6 +13157,25 @@ make_host_and_port_string_always_port(const std::string &host, int port) {
|
|||||||
return prepare_host_string(host) + ":" + std::to_string(port);
|
return prepare_host_string(host) + ":" + std::to_string(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Where a client connects for `host`, as one string: the mapped address or
|
||||||
|
// name, or `host` itself.
|
||||||
|
inline std::string
|
||||||
|
addr_map_target(const std::map<std::string, std::string> &addr_map,
|
||||||
|
const std::string &host) {
|
||||||
|
std::string connect_host;
|
||||||
|
std::string ip;
|
||||||
|
apply_addr_map(addr_map, host, connect_host, ip);
|
||||||
|
return ip.empty() ? connect_host : ip;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request target of a CONNECT for `host`, with `addr_map` applied.
|
||||||
|
inline std::string
|
||||||
|
make_connect_request_target(const std::map<std::string, std::string> &addr_map,
|
||||||
|
const std::string &host, int port) {
|
||||||
|
return make_host_and_port_string_always_port(addr_map_target(addr_map, host),
|
||||||
|
port);
|
||||||
|
}
|
||||||
|
|
||||||
// Value for the Host header a client sends when the caller supplied none.
|
// Value for the Host header a client sends when the caller supplied none.
|
||||||
// Only the value: callers decide where in their header list it goes.
|
// Only the value: callers decide where in their header list it goes.
|
||||||
inline std::string make_default_host_header_value(const std::string &host,
|
inline std::string make_default_host_header_value(const std::string &host,
|
||||||
@@ -15230,7 +15365,10 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
|||||||
if ((req.method == "GET" || req.method == "HEAD") &&
|
if ((req.method == "GET" || req.method == "HEAD") &&
|
||||||
req.has_header("Range")) {
|
req.has_header("Range")) {
|
||||||
const auto &range_header_value = req.get_header_value("Range");
|
const auto &range_header_value = req.get_header_value("Range");
|
||||||
if (!detail::parse_range_header(range_header_value, req.ranges)) {
|
// RFC 9110 14.2: a range unit other than bytes must be ignored
|
||||||
|
auto unit = range_header_value.substr(0, range_header_value.find('='));
|
||||||
|
if (detail::case_ignore::equal(unit, "bytes") &&
|
||||||
|
!detail::parse_range_header(range_header_value, req.ranges)) {
|
||||||
connection_closed = true;
|
connection_closed = true;
|
||||||
res.status = StatusCode::RangeNotSatisfiable_416;
|
res.status = StatusCode::RangeNotSatisfiable_416;
|
||||||
output_error_log(Error::InvalidRangeHeader, &req);
|
output_error_log(Error::InvalidRangeHeader, &req);
|
||||||
@@ -15663,14 +15801,8 @@ inline void ClientImpl::disconnect(bool gracefully) {
|
|||||||
inline bool ClientImpl::socket_reusable() {
|
inline bool ClientImpl::socket_reusable() {
|
||||||
if (!socket_.is_open()) { return false; }
|
if (!socket_.is_open()) { return false; }
|
||||||
|
|
||||||
auto is_alive = detail::is_socket_alive(socket_.sock);
|
// RFC 9112 9.2: data with no request outstanding is not a response
|
||||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
if (detail::select_read(socket_.sock, 0, 0) != 0) {
|
||||||
if (is_alive && is_ssl() && tls::is_peer_closed(socket_.ssl, socket_.sock)) {
|
|
||||||
is_alive = false;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (!is_alive) {
|
|
||||||
// Peer seems gone — non-graceful shutdown to avoid SIGPIPE.
|
|
||||||
disconnect(/*gracefully=*/false);
|
disconnect(/*gracefully=*/false);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -15759,6 +15891,9 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
|
|||||||
// socket.
|
// socket.
|
||||||
socket_should_be_closed_when_request_is_done_ = false;
|
socket_should_be_closed_when_request_is_done_ = false;
|
||||||
|
|
||||||
|
// A redirect or a retry sent inside a request keeps its stop()
|
||||||
|
if (socket_requests_in_flight_ == 0) { stop_requested_ = false; }
|
||||||
|
|
||||||
if (!socket_reusable()) {
|
if (!socket_reusable()) {
|
||||||
if (!ensure_socket_connection(socket_, error)) { return false; }
|
if (!ensure_socket_connection(socket_, error)) { return false; }
|
||||||
|
|
||||||
@@ -15806,6 +15941,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ret = process_socket(socket_, req.start_time_, [&](Stream &strm) {
|
ret = process_socket(socket_, req.start_time_, [&](Stream &strm) {
|
||||||
|
strm.set_read_interrupt([this] { return stop_requested_.load(); });
|
||||||
return handle_request(strm, req, res, close_connection, error);
|
return handle_request(strm, req, res, close_connection, error);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -15920,6 +16056,8 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
|
|||||||
handle.connection_ = detail::make_unique<ClientConnection>();
|
handle.connection_ = detail::make_unique<ClientConnection>();
|
||||||
|
|
||||||
{
|
{
|
||||||
|
// A request in flight uses socket_ without holding socket_mutex_
|
||||||
|
std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
|
||||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||||
|
|
||||||
if (!socket_reusable()) {
|
if (!socket_reusable()) {
|
||||||
@@ -16315,9 +16453,10 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
|
|||||||
// to do these things to the socket if another thread is using the socket.
|
// to do these things to the socket if another thread is using the socket.
|
||||||
std::lock_guard<std::mutex> guard(socket_mutex_);
|
std::lock_guard<std::mutex> guard(socket_mutex_);
|
||||||
// Also when the request asked for it (e.g. a body that was never sent,
|
// Also when the request asked for it (e.g. a body that was never sent,
|
||||||
// or "Connection: close"), so that a redirect or an auth retry below does
|
// or "Connection: close") or bytes follow the response (RFC 9112 9.2), so
|
||||||
// not reuse the socket.
|
// that a redirect or an auth retry below does not reuse the socket.
|
||||||
if (socket_should_be_closed_when_request_is_done_ || close_connection ||
|
if (socket_should_be_closed_when_request_is_done_ || close_connection ||
|
||||||
|
strm.is_readable() ||
|
||||||
detail::has_header_token(res.headers, "Connection", "close") ||
|
detail::has_header_token(res.headers, "Connection", "close") ||
|
||||||
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
|
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
|
||||||
disconnect(/*gracefully=*/true);
|
disconnect(/*gracefully=*/true);
|
||||||
@@ -16418,8 +16557,13 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
|
|||||||
|
|
||||||
auto path = std::move(next_path) + next_query;
|
auto path = std::move(next_path) + next_query;
|
||||||
|
|
||||||
|
// A Unix socket client sends "Host: localhost", so a Location naming
|
||||||
|
// localhost names this client too.
|
||||||
|
auto is_same_host = next_host == host_ ||
|
||||||
|
(address_family_ == AF_UNIX && next_host == "localhost");
|
||||||
|
|
||||||
// Same host redirect - use current client
|
// Same host redirect - use current client
|
||||||
if (next_scheme == scheme && next_host == host_ && next_port == port_) {
|
if (next_scheme == scheme && is_same_host && next_port == port_) {
|
||||||
return detail::redirect(*this, req, res, path, location, error);
|
return detail::redirect(*this, req, res, path, location, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16569,6 +16713,15 @@ inline void ClientImpl::setup_redirect_client(ClientType &client) {
|
|||||||
|
|
||||||
// Copy network and socket settings
|
// Copy network and socket settings
|
||||||
client.addr_map_ = addr_map_;
|
client.addr_map_ = addr_map_;
|
||||||
|
client.tunnel_addr_map_ = tunnel_addr_map_;
|
||||||
|
|
||||||
|
// A Unix socket client reaches every host through its own socket, as curl's
|
||||||
|
// --unix-socket does, unless the caller mapped that host elsewhere. The
|
||||||
|
// Location's host is never taken as a socket path.
|
||||||
|
if (address_family_ == AF_UNIX) {
|
||||||
|
auto &target = client.addr_map_[client.host()];
|
||||||
|
if (target.empty()) { target = detail::addr_map_target(addr_map_, host_); }
|
||||||
|
}
|
||||||
client.set_address_family(address_family_);
|
client.set_address_family(address_family_);
|
||||||
client.set_tcp_nodelay(tcp_nodelay_);
|
client.set_tcp_nodelay(tcp_nodelay_);
|
||||||
client.set_ipv6_v6only(ipv6_v6only_);
|
client.set_ipv6_v6only(ipv6_v6only_);
|
||||||
@@ -18023,6 +18176,9 @@ inline void ClientImpl::stop() {
|
|||||||
// are not thread-safe.
|
// are not thread-safe.
|
||||||
if (socket_requests_in_flight_ > 0) {
|
if (socket_requests_in_flight_ > 0) {
|
||||||
shutdown_socket(socket_);
|
shutdown_socket(socket_);
|
||||||
|
// shutdown() does not wake a read waiting in WSAPoll on Windows, so the
|
||||||
|
// request's reads also give up once they see this flag.
|
||||||
|
stop_requested_ = true;
|
||||||
|
|
||||||
// Aside from that, we set a flag for the socket to be closed when we're
|
// Aside from that, we set a flag for the socket to be closed when we're
|
||||||
// done.
|
// done.
|
||||||
@@ -18081,9 +18237,19 @@ inline void ClientImpl::set_path_encode(bool on) { path_encode_ = on; }
|
|||||||
|
|
||||||
inline void
|
inline void
|
||||||
ClientImpl::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
ClientImpl::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
||||||
|
set_direct_hostname_addr_map(std::move(addr_map));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void ClientImpl::set_direct_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
addr_map_ = std::move(addr_map);
|
addr_map_ = std::move(addr_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void ClientImpl::set_tunnel_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
|
tunnel_addr_map_ = std::move(addr_map);
|
||||||
|
}
|
||||||
|
|
||||||
inline void ClientImpl::set_default_headers(Headers headers) {
|
inline void ClientImpl::set_default_headers(Headers headers) {
|
||||||
default_headers_ = std::move(headers);
|
default_headers_ = std::move(headers);
|
||||||
}
|
}
|
||||||
@@ -18792,7 +18958,17 @@ inline socket_t Client::socket() const { return cli_->socket(); }
|
|||||||
|
|
||||||
inline void
|
inline void
|
||||||
Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
|
||||||
cli_->set_hostname_addr_map(std::move(addr_map));
|
cli_->set_direct_hostname_addr_map(std::move(addr_map));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Client::set_direct_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
|
cli_->set_direct_hostname_addr_map(std::move(addr_map));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Client::set_tunnel_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
|
cli_->set_tunnel_hostname_addr_map(std::move(addr_map));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Client::set_default_headers(Headers headers) {
|
inline void Client::set_default_headers(Headers headers) {
|
||||||
@@ -19146,6 +19322,8 @@ inline bool SSLClient::setup_proxy_connection(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!initialize_ssl(socket, error)) {
|
if (!initialize_ssl(socket, error)) {
|
||||||
|
shutdown_socket(socket);
|
||||||
|
close_socket(socket);
|
||||||
success = false;
|
success = false;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -19160,6 +19338,10 @@ inline bool SSLClient::connect_with_proxy(
|
|||||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||||
Response &res, bool &success, Error &error) {
|
Response &res, bool &success, Error &error) {
|
||||||
success = true;
|
success = true;
|
||||||
|
auto connect_target =
|
||||||
|
detail::make_connect_request_target(tunnel_addr_map_, host_, port_);
|
||||||
|
auto retargeted = connect_target !=
|
||||||
|
detail::make_host_and_port_string_always_port(host_, port_);
|
||||||
Response proxy_res;
|
Response proxy_res;
|
||||||
if (!detail::process_client_socket(
|
if (!detail::process_client_socket(
|
||||||
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
socket.sock, read_timeout_sec_, read_timeout_usec_,
|
||||||
@@ -19167,8 +19349,8 @@ inline bool SSLClient::connect_with_proxy(
|
|||||||
start_time, [&](Stream &strm) {
|
start_time, [&](Stream &strm) {
|
||||||
Request req2;
|
Request req2;
|
||||||
req2.method = "CONNECT";
|
req2.method = "CONNECT";
|
||||||
req2.path =
|
req2.path = connect_target;
|
||||||
detail::make_host_and_port_string_always_port(host_, port_);
|
if (retargeted) { req2.set_header("Host", connect_target); }
|
||||||
if (max_timeout_msec_ > 0) {
|
if (max_timeout_msec_ > 0) {
|
||||||
req2.start_time_ = std::chrono::steady_clock::now();
|
req2.start_time_ = std::chrono::steady_clock::now();
|
||||||
}
|
}
|
||||||
@@ -19207,8 +19389,8 @@ inline bool SSLClient::connect_with_proxy(
|
|||||||
start_time, [&](Stream &strm) {
|
start_time, [&](Stream &strm) {
|
||||||
Request req3;
|
Request req3;
|
||||||
req3.method = "CONNECT";
|
req3.method = "CONNECT";
|
||||||
req3.path = detail::make_host_and_port_string_always_port(
|
req3.path = connect_target;
|
||||||
host_, port_);
|
if (retargeted) { req3.set_header("Host", connect_target); }
|
||||||
req3.headers.insert(detail::make_digest_authentication_header(
|
req3.headers.insert(detail::make_digest_authentication_header(
|
||||||
req3, make_request_target(req3), auth, 1,
|
req3, make_request_target(req3), auth, 1,
|
||||||
detail::random_string(10), proxy_digest_auth_username_,
|
detail::random_string(10), proxy_digest_auth_username_,
|
||||||
@@ -21040,6 +21222,11 @@ inline ErrorCode map_mbedtls_error(int ret, int &out_errno,
|
|||||||
out_errno = errno;
|
out_errno = errno;
|
||||||
return ErrorCode::SyscallError;
|
return ErrorCode::SyscallError;
|
||||||
}
|
}
|
||||||
|
if (ret == MBEDTLS_ERR_SSL_CONN_EOF) {
|
||||||
|
// EOF without close_notify
|
||||||
|
out_errno = 0;
|
||||||
|
return ErrorCode::SyscallError;
|
||||||
|
}
|
||||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||||
// Unlike OpenSSL/wolfSSL, Mbed TLS folds the CN/SAN identity check into
|
// Unlike OpenSSL/wolfSSL, Mbed TLS folds the CN/SAN identity check into
|
||||||
// the handshake's chain verification (see set_sni()); a mismatch there
|
// the handshake's chain verification (see set_sni()); a mismatch there
|
||||||
@@ -21127,7 +21314,7 @@ inline int mbedtls_net_recv_cb(void *ctx, unsigned char *buf, size_t len) {
|
|||||||
return MBEDTLS_ERR_NET_RECV_FAILED;
|
return MBEDTLS_ERR_NET_RECV_FAILED;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (ret == 0) { return MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; }
|
// EOF is MBEDTLS_ERR_SSL_CONN_EOF; only a close_notify is a clean end
|
||||||
return static_cast<int>(ret);
|
return static_cast<int>(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21783,10 +21970,8 @@ inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) {
|
|||||||
return static_cast<ssize_t>(ret);
|
return static_cast<ssize_t>(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
// 0 means EOF without close_notify, which is an error as on OpenSSL
|
||||||
err.code = ErrorCode::PeerClosed;
|
if (ret == 0) { ret = MBEDTLS_ERR_SSL_CONN_EOF; }
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
err.code = impl::map_mbedtls_error(ret, err.sys_errno, 0);
|
err.code = impl::map_mbedtls_error(ret, err.sys_errno, 0);
|
||||||
err.backend_code = static_cast<uint64_t>(-ret);
|
err.backend_code = static_cast<uint64_t>(-ret);
|
||||||
@@ -22393,6 +22578,8 @@ inline ErrorCode map_wolfssl_error(WOLFSSL *ssl, int ssl_error,
|
|||||||
case SSL_ERROR_WANT_WRITE: return ErrorCode::WantWrite;
|
case SSL_ERROR_WANT_WRITE: return ErrorCode::WantWrite;
|
||||||
case SSL_ERROR_ZERO_RETURN: return ErrorCode::PeerClosed;
|
case SSL_ERROR_ZERO_RETURN: return ErrorCode::PeerClosed;
|
||||||
case SSL_ERROR_SYSCALL: out_errno = errno; return ErrorCode::SyscallError;
|
case SSL_ERROR_SYSCALL: out_errno = errno; return ErrorCode::SyscallError;
|
||||||
|
// EOF without close_notify
|
||||||
|
case SOCKET_PEER_CLOSED_E: out_errno = 0; return ErrorCode::SyscallError;
|
||||||
default:
|
default:
|
||||||
if (ssl) {
|
if (ssl) {
|
||||||
// wolfSSL stores the low-level error code as a negative value.
|
// wolfSSL stores the low-level error code as a negative value.
|
||||||
@@ -22956,6 +23143,9 @@ inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto wsession = static_cast<impl::WolfSSLSession *>(session);
|
auto wsession = static_cast<impl::WolfSSLSession *>(session);
|
||||||
|
constexpr auto max_len =
|
||||||
|
static_cast<size_t>((std::numeric_limits<int>::max)());
|
||||||
|
if (len > max_len) { len = max_len; }
|
||||||
int ret = wolfSSL_read(wsession->ssl, buf, static_cast<int>(len));
|
int ret = wolfSSL_read(wsession->ssl, buf, static_cast<int>(len));
|
||||||
|
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
@@ -22963,13 +23153,10 @@ inline ssize_t read(session_t session, void *buf, size_t len, TlsError &err) {
|
|||||||
return static_cast<ssize_t>(ret);
|
return static_cast<ssize_t>(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
// 0 is also returned for EOF without close_notify; get_error tells them apart
|
||||||
err.code = ErrorCode::PeerClosed;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
||||||
err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
|
err.code = impl::map_wolfssl_error(wsession->ssl, ssl_error, err.sys_errno);
|
||||||
|
if (err.code == ErrorCode::PeerClosed) { return 0; }
|
||||||
err.backend_code = static_cast<uint64_t>(ssl_error);
|
err.backend_code = static_cast<uint64_t>(ssl_error);
|
||||||
impl::wolfssl_last_error() = err.backend_code;
|
impl::wolfssl_last_error() = err.backend_code;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -23926,6 +24113,9 @@ inline void WebSocketClient::prepare_default_headers(Request &req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
detail::add_default_user_agent_header(req);
|
detail::add_default_user_agent_header(req);
|
||||||
|
detail::add_authorization_header(req, basic_auth_username_,
|
||||||
|
basic_auth_password_,
|
||||||
|
bearer_token_auth_token_, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Result WebSocketClient::connect() {
|
inline Result WebSocketClient::connect() {
|
||||||
@@ -24051,9 +24241,19 @@ inline void WebSocketClient::set_interface(const std::string &intf) {
|
|||||||
|
|
||||||
inline void WebSocketClient::set_hostname_addr_map(
|
inline void WebSocketClient::set_hostname_addr_map(
|
||||||
std::map<std::string, std::string> addr_map) {
|
std::map<std::string, std::string> addr_map) {
|
||||||
|
set_direct_hostname_addr_map(std::move(addr_map));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void WebSocketClient::set_direct_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
addr_map_ = std::move(addr_map);
|
addr_map_ = std::move(addr_map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void WebSocketClient::set_tunnel_hostname_addr_map(
|
||||||
|
std::map<std::string, std::string> addr_map) {
|
||||||
|
tunnel_addr_map_ = std::move(addr_map);
|
||||||
|
}
|
||||||
|
|
||||||
inline bool WebSocketClient::is_proxy_enabled() const {
|
inline bool WebSocketClient::is_proxy_enabled() const {
|
||||||
if (proxy_host_.empty() || proxy_port_ == -1) { return false; }
|
if (proxy_host_.empty() || proxy_port_ == -1) { return false; }
|
||||||
if (no_proxy_entries_.empty()) { return true; }
|
if (no_proxy_entries_.empty()) { return true; }
|
||||||
@@ -24103,7 +24303,8 @@ inline bool WebSocketClient::connect_with_proxy(Response &proxy_res,
|
|||||||
Error &error) {
|
Error &error) {
|
||||||
Request req;
|
Request req;
|
||||||
req.method = "CONNECT";
|
req.method = "CONNECT";
|
||||||
req.path = detail::make_host_and_port_string_always_port(host_, port_);
|
req.path =
|
||||||
|
detail::make_connect_request_target(tunnel_addr_map_, host_, port_);
|
||||||
req.headers.emplace("Host", req.path);
|
req.headers.emplace("Host", req.path);
|
||||||
detail::add_default_user_agent_header(req);
|
detail::add_default_user_agent_header(req);
|
||||||
detail::add_authorization_header(req, proxy_basic_auth_username_,
|
detail::add_authorization_header(req, proxy_basic_auth_username_,
|
||||||
@@ -24137,6 +24338,16 @@ inline bool WebSocketClient::connect_with_proxy(Response &proxy_res,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void WebSocketClient::set_basic_auth(const std::string &username,
|
||||||
|
const std::string &password) {
|
||||||
|
basic_auth_username_ = username;
|
||||||
|
basic_auth_password_ = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void WebSocketClient::set_bearer_token_auth(const std::string &token) {
|
||||||
|
bearer_token_auth_token_ = token;
|
||||||
|
}
|
||||||
|
|
||||||
inline void WebSocketClient::set_proxy(const std::string &host, int port) {
|
inline void WebSocketClient::set_proxy(const std::string &host, int port) {
|
||||||
proxy_host_ = host;
|
proxy_host_ = host;
|
||||||
proxy_port_ = port;
|
proxy_port_ = port;
|
||||||
|
|||||||
+13
-2
@@ -13,14 +13,15 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
|||||||
BROTLI_DIR = /usr/local/opt/brotli
|
BROTLI_DIR = /usr/local/opt/brotli
|
||||||
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||||
|
|
||||||
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer
|
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer
|
||||||
|
|
||||||
# Runs all the tests and also fuzz tests against seed corpus.
|
# Runs all the tests and also fuzz tests against seed corpus.
|
||||||
all : $(FUZZERS)
|
all : $(FUZZERS)
|
||||||
./server_fuzzer corpus/*
|
./server_fuzzer corpus/*
|
||||||
|
./server_mount_fuzzer corpus_mount/*
|
||||||
|
|
||||||
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
||||||
server_fuzzer : server_fuzzer.cc ../../httplib.h
|
server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
# $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
# $(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 -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
zip -q -r server_fuzzer_seed_corpus.zip corpus
|
zip -q -r server_fuzzer_seed_corpus.zip corpus
|
||||||
@@ -38,5 +39,15 @@ url_parser_fuzzer : url_parser_fuzzer.cc ../../httplib.h
|
|||||||
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
sse_parser_fuzzer : sse_parser_fuzzer.cc ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
server_mount_fuzzer : server_mount_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
HEAD /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
If-Modified-Since: Mon, 01 Jan 2024 00:00:00 GMT
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/index.html HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/index.html HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
If-None-Match: W/"bogusetag"
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-in.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-out.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-sibling.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/missing.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
Range: bytes=0-3
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/sub/ HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/../../../../etc/passwd HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/%2e%2e/secret.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
// Like SocketStream, pull in more than was asked for, which the line
|
||||||
|
// reader then takes through buffered_data(), so both of its paths run.
|
||||||
|
buffered_end_ =
|
||||||
|
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
|
||||||
|
return static_cast<ssize_t>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *buffered_data(size_t &size) const override {
|
||||||
|
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
|
||||||
|
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void consume_buffered(size_t size) override { read_pos_ += size; }
|
||||||
|
|
||||||
|
ssize_t write(const char *ptr, size_t size) override {
|
||||||
|
response_.append(ptr, size);
|
||||||
|
return static_cast<int>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
|
||||||
|
|
||||||
|
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
|
||||||
|
|
||||||
|
bool is_readable() const override { return true; }
|
||||||
|
|
||||||
|
bool wait_readable() const override { return true; }
|
||||||
|
|
||||||
|
bool wait_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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_local_ip_and_port(std::string &ip, int &port) const override {
|
||||||
|
ip = "127.0.0.1";
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
|
||||||
|
socket_t socket() const override { return 0; }
|
||||||
|
|
||||||
|
time_t duration() const override { return 0; };
|
||||||
|
|
||||||
|
const std::string &response() const { return response_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const uint8_t *data_;
|
||||||
|
size_t size_;
|
||||||
|
size_t read_pos_;
|
||||||
|
size_t buffered_end_ = 0;
|
||||||
|
std::string response_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FuzzableServer : public httplib::Server {
|
||||||
|
public:
|
||||||
|
// A server that is not listening counts as shutting down, and then writes
|
||||||
|
// no file. The socket is never used.
|
||||||
|
void MarkAsRunning() { svr_sock_ = 0; }
|
||||||
|
|
||||||
|
void ProcessFuzzedRequest(FuzzedStream &stream) {
|
||||||
|
bool connection_close = false;
|
||||||
|
process_request(stream,
|
||||||
|
/*remote_addr=*/"",
|
||||||
|
/*remote_port =*/0,
|
||||||
|
/*local_addr=*/"",
|
||||||
|
/*local_port =*/0,
|
||||||
|
/*last_connection=*/false, connection_close, nullptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,79 +1,4 @@
|
|||||||
#include <cstdint>
|
#include "fuzzed_stream.h"
|
||||||
|
|
||||||
#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;
|
|
||||||
// Like SocketStream, pull in more than was asked for, which the line
|
|
||||||
// reader then takes through buffered_data(), so both of its paths run.
|
|
||||||
buffered_end_ =
|
|
||||||
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
|
|
||||||
return static_cast<ssize_t>(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *buffered_data(size_t &size) const override {
|
|
||||||
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
|
|
||||||
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void consume_buffered(size_t size) override { read_pos_ += size; }
|
|
||||||
|
|
||||||
ssize_t write(const char *ptr, size_t size) override {
|
|
||||||
response_.append(ptr, size);
|
|
||||||
return static_cast<int>(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
|
|
||||||
|
|
||||||
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
|
|
||||||
|
|
||||||
bool is_readable() const override { return true; }
|
|
||||||
|
|
||||||
bool wait_readable() const override { return true; }
|
|
||||||
|
|
||||||
bool wait_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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_local_ip_and_port(std::string &ip, int &port) const override {
|
|
||||||
ip = "127.0.0.1";
|
|
||||||
port = 8080;
|
|
||||||
}
|
|
||||||
|
|
||||||
socket_t socket() const override { return 0; }
|
|
||||||
|
|
||||||
time_t duration() const override { return 0; };
|
|
||||||
|
|
||||||
private:
|
|
||||||
const uint8_t *data_;
|
|
||||||
size_t size_;
|
|
||||||
size_t read_pos_;
|
|
||||||
size_t buffered_end_ = 0;
|
|
||||||
std::string response_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FuzzableServer : public httplib::Server {
|
|
||||||
public:
|
|
||||||
void ProcessFuzzedRequest(FuzzedStream &stream) {
|
|
||||||
bool connection_close = false;
|
|
||||||
process_request(stream,
|
|
||||||
/*remote_addr=*/"",
|
|
||||||
/*remote_port =*/0,
|
|
||||||
/*local_addr=*/"",
|
|
||||||
/*local_port =*/0,
|
|
||||||
/*last_connection=*/false, connection_close, nullptr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static FuzzableServer g_server;
|
static FuzzableServer g_server;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <utime.h>
|
||||||
|
|
||||||
|
#include "fuzzed_stream.h"
|
||||||
|
|
||||||
|
// Fixture layout, with <token> unknown to the fuzzer:
|
||||||
|
// root/mnt/{index.html,a.txt,data.bin,sub/index.html} served at /mnt
|
||||||
|
// root/mnt/link-in.txt -> a.txt (stays inside, may be served)
|
||||||
|
// root/mnt/link-out.txt -> ../outside.txt (escapes, must be refused)
|
||||||
|
// root/mnt/link-sibling.txt -> ../mnt_secret/secret.txt (shares the "mnt"
|
||||||
|
// prefix, must be refused)
|
||||||
|
// The response must never contain <token>.
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
FuzzableServer g_server;
|
||||||
|
std::string g_root;
|
||||||
|
std::string g_token;
|
||||||
|
std::vector<std::string> g_paths; // In creation order
|
||||||
|
|
||||||
|
void check(bool ok, const char *what) {
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "fixture setup failed: %s\n", what);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_dir(const std::string &path) {
|
||||||
|
check(mkdir(path.c_str(), 0755) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_file(const std::string &path, const std::string &content) {
|
||||||
|
{
|
||||||
|
std::ofstream out(path, std::ios::binary);
|
||||||
|
out << content;
|
||||||
|
check(out.good(), path.c_str());
|
||||||
|
}
|
||||||
|
// Fixed, so that conditional requests behave the same in every run
|
||||||
|
utimbuf times = {1700000000, 1700000000};
|
||||||
|
check(utime(path.c_str(), ×) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_link(const std::string &target, const std::string &path) {
|
||||||
|
check(symlink(target.c_str(), path.c_str()) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string serve(const uint8_t *data, size_t size) {
|
||||||
|
FuzzedStream stream{data, size};
|
||||||
|
g_server.ProcessFuzzedRequest(stream);
|
||||||
|
return stream.response();
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_served(const char *path) {
|
||||||
|
auto req = std::string("GET ") + path + " HTTP/1.1\r\nHost: x\r\n\r\n";
|
||||||
|
auto res = serve(reinterpret_cast<const uint8_t *>(req.data()), req.size());
|
||||||
|
// Without this, a fuzzer that serves nothing would pass its check forever
|
||||||
|
check(res.find("abcdef") != std::string::npos, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_fixture() {
|
||||||
|
for (auto it = g_paths.rbegin(); it != g_paths.rend(); ++it) {
|
||||||
|
if (unlink(it->c_str()) != 0) { rmdir(it->c_str()); }
|
||||||
|
}
|
||||||
|
rmdir(g_root.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
|
||||||
|
std::random_device rd;
|
||||||
|
char token[33];
|
||||||
|
snprintf(token, sizeof(token), "%08x%08x%08x%08x", rd(), rd(), rd(), rd());
|
||||||
|
g_token = token;
|
||||||
|
|
||||||
|
auto tmpdir = getenv("TMPDIR");
|
||||||
|
auto tmpl = std::string(tmpdir && *tmpdir ? tmpdir : "/tmp") +
|
||||||
|
"/httplib-mount-XXXXXX";
|
||||||
|
check(mkdtemp(&tmpl[0]) != nullptr, tmpl.c_str());
|
||||||
|
g_root = tmpl;
|
||||||
|
atexit(remove_fixture);
|
||||||
|
|
||||||
|
auto mnt = g_root + "/mnt";
|
||||||
|
make_dir(mnt);
|
||||||
|
make_dir(mnt + "/sub");
|
||||||
|
make_dir(g_root + "/mnt_secret");
|
||||||
|
make_file(mnt + "/index.html", "<html>index</html>");
|
||||||
|
make_file(mnt + "/a.txt", "abcdef");
|
||||||
|
make_file(mnt + "/data.bin", std::string("\x01\x02\x03\x04\x05\x06", 6));
|
||||||
|
make_file(mnt + "/sub/index.html", "<html>sub</html>");
|
||||||
|
make_file(g_root + "/outside.txt", g_token);
|
||||||
|
make_file(g_root + "/mnt_secret/secret.txt", g_token);
|
||||||
|
make_link("a.txt", mnt + "/link-in.txt");
|
||||||
|
make_link("../outside.txt", mnt + "/link-out.txt");
|
||||||
|
make_link("../mnt_secret/secret.txt", mnt + "/link-sibling.txt");
|
||||||
|
|
||||||
|
g_server.MarkAsRunning();
|
||||||
|
check(g_server.set_mount_point("/mnt", mnt), "set_mount_point");
|
||||||
|
check_served("/mnt/a.txt");
|
||||||
|
check_served("/mnt/link-in.txt");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (serve(data, size).find(g_token) != std::string::npos) { abort(); }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
"GET"
|
||||||
|
"HEAD"
|
||||||
|
"HTTP/1.1"
|
||||||
|
|
||||||
|
"/mnt/"
|
||||||
|
"/mnt/index.html"
|
||||||
|
"/mnt/a.txt"
|
||||||
|
"/mnt/data.bin"
|
||||||
|
"/mnt/sub/"
|
||||||
|
"/mnt/link-in.txt"
|
||||||
|
"/mnt/link-out.txt"
|
||||||
|
"/mnt/link-sibling.txt"
|
||||||
|
".."
|
||||||
|
"%2e%2e"
|
||||||
|
"%2f"
|
||||||
|
"//"
|
||||||
|
|
||||||
|
"Range: bytes=0-"
|
||||||
|
"Range: bytes=0-3,5-"
|
||||||
|
"If-None-Match: "
|
||||||
|
"W/"
|
||||||
|
"If-Modified-Since: "
|
||||||
|
"If-Range: "
|
||||||
|
"Accept-Encoding: gzip"
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// Feeds the stream in chunks and records every handler call in order
|
||||||
|
std::string parse_in_chunks(const std::string &stream, size_t chunk,
|
||||||
|
size_t max_event_length) {
|
||||||
|
std::string trace;
|
||||||
|
httplib::detail::SSEParser parser(
|
||||||
|
[&](const httplib::sse::SSEMessage &msg) {
|
||||||
|
trace += "event[" + msg.event + "|" + msg.data + "|" + msg.id + "]";
|
||||||
|
},
|
||||||
|
[&](const std::string &id) { trace += "id[" + id + "]"; },
|
||||||
|
[&](int retry_ms) { trace += "retry[" + std::to_string(retry_ms) + "]"; },
|
||||||
|
max_event_length);
|
||||||
|
|
||||||
|
for (size_t off = 0; off < stream.size(); off += chunk) {
|
||||||
|
if (!parser.parse(stream.data() + off,
|
||||||
|
std::min(chunk, stream.size() - off))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return trace;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (size < 2 || size > 65536) { return 0; }
|
||||||
|
|
||||||
|
// First byte selects the chunk size, the rest is the event stream
|
||||||
|
auto chunk = static_cast<size_t>(data[0]) + 1;
|
||||||
|
std::string stream(reinterpret_cast<const char *>(data + 1), size - 1);
|
||||||
|
|
||||||
|
// Without a length limit, chunking must not change what is reported
|
||||||
|
auto whole = parse_in_chunks(stream, stream.size(), 0);
|
||||||
|
auto chunked = parse_in_chunks(stream, chunk, 0);
|
||||||
|
if (whole != chunked) { std::abort(); }
|
||||||
|
|
||||||
|
// With a limit, only check that the parser stops cleanly
|
||||||
|
parse_in_chunks(stream, chunk, 64);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|||||||
size_t payload_size = size - 1;
|
size_t payload_size = size - 1;
|
||||||
std::string input(payload, payload_size);
|
std::string input(payload, payload_size);
|
||||||
|
|
||||||
switch (selector % 6) {
|
switch (selector % 13) {
|
||||||
case 0: {
|
case 0: {
|
||||||
// parse_query_text
|
// parse_query_text
|
||||||
httplib::Params params;
|
httplib::Params params;
|
||||||
@@ -46,6 +46,48 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|||||||
httplib::detail::is_valid_path(input);
|
httplib::detail::is_valid_path(input);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 6: {
|
||||||
|
// parse_url
|
||||||
|
httplib::detail::UrlComponents uc;
|
||||||
|
httplib::detail::parse_url(input, uc);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 7: {
|
||||||
|
// remove_dot_segments
|
||||||
|
httplib::detail::remove_dot_segments(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 8: {
|
||||||
|
// resolve_relative_location, with the input as "<location>\n<base>"
|
||||||
|
auto nl = input.find('\n');
|
||||||
|
auto location = input.substr(0, nl);
|
||||||
|
auto base = nl == std::string::npos ? std::string("/index.html")
|
||||||
|
: input.substr(nl + 1);
|
||||||
|
httplib::detail::resolve_relative_location(location, base);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 9: {
|
||||||
|
// encode_path
|
||||||
|
httplib::detail::encode_path(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 10: {
|
||||||
|
// encode_uri_component, encode_uri
|
||||||
|
httplib::encode_uri_component(input);
|
||||||
|
httplib::encode_uri(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 11: {
|
||||||
|
// decode_uri_component, decode_uri
|
||||||
|
httplib::decode_uri_component(input);
|
||||||
|
httplib::decode_uri(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 12: {
|
||||||
|
// sanitize_filename
|
||||||
|
httplib::sanitize_filename(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (size < 2) { return 0; }
|
||||||
|
|
||||||
|
auto selector = data[0];
|
||||||
|
std::string input(reinterpret_cast<const char *>(data + 1), size - 1);
|
||||||
|
|
||||||
|
switch (selector % 4) {
|
||||||
|
case 0: {
|
||||||
|
// read_websocket_frame. WebSocket::read passes the macro, close() passes
|
||||||
|
// 125, and 0 means no limit
|
||||||
|
static const size_t max_lens[] = {0, 125,
|
||||||
|
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH};
|
||||||
|
auto expect_masked = (selector / 4) % 2 != 0;
|
||||||
|
auto max_len = max_lens[(selector / 8) % 3];
|
||||||
|
httplib::detail::BufferStream strm;
|
||||||
|
strm.write(input.data(), input.size());
|
||||||
|
auto opcode = httplib::ws::Opcode::Text;
|
||||||
|
std::string payload;
|
||||||
|
auto fin = false;
|
||||||
|
httplib::ws::impl::read_websocket_frame(strm, opcode, payload, fin,
|
||||||
|
expect_masked, max_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: {
|
||||||
|
// is_valid_utf8
|
||||||
|
httplib::ws::impl::is_valid_utf8(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
// is_valid_close_payload
|
||||||
|
httplib::ws::impl::is_valid_close_payload(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
// read_websocket_upgrade_response, with a fixed key since
|
||||||
|
// perform_websocket_handshake draws a random one
|
||||||
|
static const auto expected_accept =
|
||||||
|
httplib::detail::websocket_accept_key("dGhlIHNhbXBsZSBub25jZQ==");
|
||||||
|
httplib::detail::BufferStream strm;
|
||||||
|
strm.write(input.data(), input.size());
|
||||||
|
httplib::detail::WebSocketUpgradeResponse upgrade;
|
||||||
|
httplib::detail::read_websocket_upgrade_response(strm, expected_accept,
|
||||||
|
"chat, stream", upgrade);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
[libfuzzer]
|
||||||
|
max_len = 70000
|
||||||
+1341
-256
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user