ipv6 support

This commit is contained in:
Chunting Gu
2020-08-24 13:50:23 +08:00
parent 12e3569a83
commit c392b57a55
10 changed files with 44 additions and 17 deletions
+3 -1
View File
@@ -102,7 +102,9 @@ void Client::DoConnect(RequestPtr request, const std::string& default_port) {
LOG_VERB("Resolve host (%s)...", request->host().c_str());
std::error_code ec;
auto endpoints = resolver.resolve(tcp::v4(), request->host(), port, ec);
// The protocol depends on the `host`, both V4 and V6 are supported.
auto endpoints = resolver.resolve(request->host(), port, ec);
if (ec) {
LOG_ERRO("Host resolve error (%s): %s, %s.", ec.message().c_str(),
+12 -6
View File
@@ -16,9 +16,15 @@ using tcp = asio::ip::tcp;
namespace webcc {
Server::Server(std::uint16_t port, const std::filesystem::path& doc_root)
: port_(port), doc_root_(doc_root), file_chunk_size_(1024), running_(false),
acceptor_(io_context_), signals_(io_context_) {
Server::Server(asio::ip::tcp protocol, std::uint16_t port,
const sfs::path& doc_root)
: protocol_(protocol),
port_(port),
doc_root_(doc_root),
file_chunk_size_(1024),
running_(false),
acceptor_(io_context_),
signals_(io_context_) {
AddSignals();
}
@@ -112,7 +118,7 @@ void Server::AsyncWaitSignals() {
bool Server::Listen(std::uint16_t port) {
std::error_code ec;
tcp::endpoint endpoint(tcp::v4(), port);
tcp::endpoint endpoint(protocol_, port);
// Open the acceptor.
acceptor_.open(endpoint.protocol(), ec);
@@ -296,7 +302,7 @@ bool Server::MatchViewOrStatic(const std::string& method,
// Try to match a static file.
if (method == methods::kGet && !doc_root_.empty()) {
std::filesystem::path path = doc_root_ / url;
sfs::path path = doc_root_ / url;
if (!sfs::is_directory(path) && sfs::exists(path)) {
return true;
}
@@ -313,7 +319,7 @@ ResponsePtr Server::ServeStatic(RequestPtr request) {
return {};
}
std::filesystem::path path = doc_root_ / request->url().path();
sfs::path path = doc_root_ / request->url().path();
try {
// NOTE: FileBody might throw Error::kFileError.
+5 -2
View File
@@ -20,8 +20,8 @@ namespace webcc {
class Server : public Router {
public:
explicit Server(std::uint16_t port,
const std::filesystem::path& doc_root = {});
Server(asio::ip::tcp protocol, std::uint16_t port,
const std::filesystem::path& doc_root = {});
~Server() = default;
@@ -94,6 +94,9 @@ private:
ResponsePtr ServeStatic(RequestPtr request);
private:
// tcp::v4() or tcp::v6()
asio::ip::tcp protocol_;
// Port number.
std::uint16_t port_;
+8 -3
View File
@@ -274,10 +274,15 @@ void Url::Parse(const std::string& str) {
}
if (!host_.empty()) {
p = host_.find(':');
// Check if there's a port.
p = host_.find_last_of(':');
if (p != std::string::npos) {
port_ = host_.substr(p + 1);
host_ = host_.substr(0, p);
// For IPv6: [::1]:8080
std::size_t bracket = host_.find_last_of(']');
if (bracket == std::string::npos || p > bracket) {
port_ = host_.substr(p + 1);
host_ = host_.substr(0, p);
}
}
}
}