diff --git a/webcc/globals.cc b/webcc/globals.cc index adbceb4..7e4980b 100644 --- a/webcc/globals.cc +++ b/webcc/globals.cc @@ -26,6 +26,9 @@ const std::string kAppJsonUtf8 = "application/json; charset=utf-8"; const std::string kTextXmlUtf8 = "text/xml; charset=utf-8"; #endif // WEBCC_ENABLE_SOAP +const std::string kHttpPort = "80"; +const std::string kHttpsPort = "443"; + const std::string kHttpHead = "HEAD"; const std::string kHttpGet = "GET"; const std::string kHttpPost = "POST"; diff --git a/webcc/globals.h b/webcc/globals.h index 1b14106..ff63213 100644 --- a/webcc/globals.h +++ b/webcc/globals.h @@ -43,6 +43,10 @@ extern const std::string kAppJsonUtf8; extern const std::string kTextXmlUtf8; #endif // WEBCC_ENABLE_SOAP +// Default ports. +extern const std::string kHttpPort; +extern const std::string kHttpsPort; + // HTTP methods (verbs) in string ("HEAD", "GET", etc.). // NOTE: Don't use enum to avoid converting back and forth. extern const std::string kHttpHead; diff --git a/webcc/http_async_client.cc b/webcc/http_async_client.cc index c0b6c40..774c248 100644 --- a/webcc/http_async_client.cc +++ b/webcc/http_async_client.cc @@ -35,12 +35,7 @@ void HttpAsyncClient::Request(std::shared_ptr request, request_ = request; response_handler_ = response_handler; - std::string port = request->port(); - if (port.empty()) { - port = "80"; - } - - resolver_->async_resolve(tcp::v4(), request->host(), port, + resolver_->async_resolve(tcp::v4(), request->host(), request->port(kHttpPort), std::bind(&HttpAsyncClient::ResolveHandler, shared_from_this(), std::placeholders::_1, diff --git a/webcc/http_client.cc b/webcc/http_client.cc index 5a9519a..0ffc7a2 100644 --- a/webcc/http_client.cc +++ b/webcc/http_client.cc @@ -75,10 +75,7 @@ Error HttpClient::Connect(const HttpRequest& request) { tcp::resolver resolver(io_context_); - std::string port = request.port(); - if (port.empty()) { - port = "80"; - } + std::string port = request.port(kHttpPort); boost::system::error_code ec; auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec); diff --git a/webcc/http_request.h b/webcc/http_request.h index 7642783..c500919 100644 --- a/webcc/http_request.h +++ b/webcc/http_request.h @@ -22,9 +22,15 @@ class HttpRequest : public HttpMessage { const std::string& host() const { return host_; } const std::string& port() const { return port_; } + std::string port(const std::string& default_port) const { + return port_.empty() ? default_port : port_; + } + // Set host name and port number. - // The |host| is a descriptive name or a numeric IP address. The |port| is - // a numeric number (e.g., "9000") and "80" will be used if it's empty. + // The |host| is a descriptive name (e.g., www.google.com) or a numeric IP + // address (127.0.0.1). + // The |port| is a numeric number (e.g., 9000), the default (80 for HTTP and + // 443 for HTTPS) will be used if it's empty. void SetHost(const std::string& host, const std::string& port); // Set start line according to HTTP method, URL, etc. diff --git a/webcc/http_ssl_client.cc b/webcc/http_ssl_client.cc index a3b7f32..9c9f8ee 100644 --- a/webcc/http_ssl_client.cc +++ b/webcc/http_ssl_client.cc @@ -67,10 +67,7 @@ Error HttpSslClient::Connect(const HttpRequest& request) { tcp::resolver resolver(io_context_); - std::string port = request.port(); - if (port.empty()) { - port = "443"; // 443 is the default port of HTTPs. - } + std::string port = request.port(kHttpsPort); boost::system::error_code ec; auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec); diff --git a/webcc/rest_client.cc b/webcc/rest_client.cc index e283b18..747adab 100644 --- a/webcc/rest_client.cc +++ b/webcc/rest_client.cc @@ -9,6 +9,13 @@ RestClient::RestClient(const std::string& host, const std::string& port) : host_(host), port_(port), timeout_seconds_(0), timed_out_(false), error_(kNoError) { + if (port_.empty()) { + std::size_t i = host_.find_last_of(':'); + if (i != std::string::npos) { + port_ = host_.substr(i + 1); + host_ = host_.substr(0, i); + } + } } bool RestClient::Request(const std::string& method, const std::string& url, diff --git a/webcc/rest_client.h b/webcc/rest_client.h index db1aa8c..68dcecf 100644 --- a/webcc/rest_client.h +++ b/webcc/rest_client.h @@ -12,7 +12,9 @@ namespace webcc { class RestClient { public: - RestClient(const std::string& host, const std::string& port); + // If |port| is empty, |host| will be checked to see if it contains port or + // not (separated by ':'). + RestClient(const std::string& host, const std::string& port = ""); ~RestClient() = default; diff --git a/webcc/soap_client.cc b/webcc/soap_client.cc index 5a11441..ea89f3b 100644 --- a/webcc/soap_client.cc +++ b/webcc/soap_client.cc @@ -16,6 +16,13 @@ SoapClient::SoapClient(const std::string& host, const std::string& port) soapenv_ns_(kSoapEnvNamespace), format_raw_(true), timeout_seconds_(0), timed_out_(false), error_(kNoError) { + if (port_.empty()) { + std::size_t i = host_.find_last_of(':'); + if (i != std::string::npos) { + port_ = host_.substr(i + 1); + host_ = host_.substr(0, i); + } + } } bool SoapClient::Request(const std::string& operation, diff --git a/webcc/soap_client.h b/webcc/soap_client.h index 2f1526d..d69506b 100644 --- a/webcc/soap_client.h +++ b/webcc/soap_client.h @@ -11,7 +11,9 @@ namespace webcc { class SoapClient { public: - SoapClient(const std::string& host, const std::string& port); + // If |port| is empty, |host| will be checked to see if it contains port or + // not (separated by ':'). + SoapClient(const std::string& host, const std::string& port = ""); ~SoapClient() = default;