Support combined host and port.
This commit is contained in:
@@ -26,6 +26,9 @@ const std::string kAppJsonUtf8 = "application/json; charset=utf-8";
|
|||||||
const std::string kTextXmlUtf8 = "text/xml; charset=utf-8";
|
const std::string kTextXmlUtf8 = "text/xml; charset=utf-8";
|
||||||
#endif // WEBCC_ENABLE_SOAP
|
#endif // WEBCC_ENABLE_SOAP
|
||||||
|
|
||||||
|
const std::string kHttpPort = "80";
|
||||||
|
const std::string kHttpsPort = "443";
|
||||||
|
|
||||||
const std::string kHttpHead = "HEAD";
|
const std::string kHttpHead = "HEAD";
|
||||||
const std::string kHttpGet = "GET";
|
const std::string kHttpGet = "GET";
|
||||||
const std::string kHttpPost = "POST";
|
const std::string kHttpPost = "POST";
|
||||||
|
|||||||
@@ -43,6 +43,10 @@ extern const std::string kAppJsonUtf8;
|
|||||||
extern const std::string kTextXmlUtf8;
|
extern const std::string kTextXmlUtf8;
|
||||||
#endif // WEBCC_ENABLE_SOAP
|
#endif // WEBCC_ENABLE_SOAP
|
||||||
|
|
||||||
|
// Default ports.
|
||||||
|
extern const std::string kHttpPort;
|
||||||
|
extern const std::string kHttpsPort;
|
||||||
|
|
||||||
// HTTP methods (verbs) in string ("HEAD", "GET", etc.).
|
// HTTP methods (verbs) in string ("HEAD", "GET", etc.).
|
||||||
// NOTE: Don't use enum to avoid converting back and forth.
|
// NOTE: Don't use enum to avoid converting back and forth.
|
||||||
extern const std::string kHttpHead;
|
extern const std::string kHttpHead;
|
||||||
|
|||||||
@@ -35,12 +35,7 @@ void HttpAsyncClient::Request(std::shared_ptr<HttpRequest> request,
|
|||||||
request_ = request;
|
request_ = request;
|
||||||
response_handler_ = response_handler;
|
response_handler_ = response_handler;
|
||||||
|
|
||||||
std::string port = request->port();
|
resolver_->async_resolve(tcp::v4(), request->host(), request->port(kHttpPort),
|
||||||
if (port.empty()) {
|
|
||||||
port = "80";
|
|
||||||
}
|
|
||||||
|
|
||||||
resolver_->async_resolve(tcp::v4(), request->host(), port,
|
|
||||||
std::bind(&HttpAsyncClient::ResolveHandler,
|
std::bind(&HttpAsyncClient::ResolveHandler,
|
||||||
shared_from_this(),
|
shared_from_this(),
|
||||||
std::placeholders::_1,
|
std::placeholders::_1,
|
||||||
|
|||||||
@@ -75,10 +75,7 @@ Error HttpClient::Connect(const HttpRequest& request) {
|
|||||||
|
|
||||||
tcp::resolver resolver(io_context_);
|
tcp::resolver resolver(io_context_);
|
||||||
|
|
||||||
std::string port = request.port();
|
std::string port = request.port(kHttpPort);
|
||||||
if (port.empty()) {
|
|
||||||
port = "80";
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
||||||
|
|||||||
@@ -22,9 +22,15 @@ class HttpRequest : public HttpMessage {
|
|||||||
const std::string& host() const { return host_; }
|
const std::string& host() const { return host_; }
|
||||||
const std::string& port() const { return port_; }
|
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.
|
// Set host name and port number.
|
||||||
// The |host| is a descriptive name or a numeric IP address. The |port| is
|
// The |host| is a descriptive name (e.g., www.google.com) or a numeric IP
|
||||||
// a numeric number (e.g., "9000") and "80" will be used if it's empty.
|
// 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);
|
void SetHost(const std::string& host, const std::string& port);
|
||||||
|
|
||||||
// Set start line according to HTTP method, URL, etc.
|
// Set start line according to HTTP method, URL, etc.
|
||||||
|
|||||||
@@ -67,10 +67,7 @@ Error HttpSslClient::Connect(const HttpRequest& request) {
|
|||||||
|
|
||||||
tcp::resolver resolver(io_context_);
|
tcp::resolver resolver(io_context_);
|
||||||
|
|
||||||
std::string port = request.port();
|
std::string port = request.port(kHttpsPort);
|
||||||
if (port.empty()) {
|
|
||||||
port = "443"; // 443 is the default port of HTTPs.
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
||||||
|
|||||||
@@ -9,6 +9,13 @@ RestClient::RestClient(const std::string& host, const std::string& port)
|
|||||||
: host_(host), port_(port),
|
: host_(host), port_(port),
|
||||||
timeout_seconds_(0), timed_out_(false),
|
timeout_seconds_(0), timed_out_(false),
|
||||||
error_(kNoError) {
|
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,
|
bool RestClient::Request(const std::string& method, const std::string& url,
|
||||||
|
|||||||
+3
-1
@@ -12,7 +12,9 @@ namespace webcc {
|
|||||||
|
|
||||||
class RestClient {
|
class RestClient {
|
||||||
public:
|
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;
|
~RestClient() = default;
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ SoapClient::SoapClient(const std::string& host, const std::string& port)
|
|||||||
soapenv_ns_(kSoapEnvNamespace),
|
soapenv_ns_(kSoapEnvNamespace),
|
||||||
format_raw_(true), timeout_seconds_(0), timed_out_(false),
|
format_raw_(true), timeout_seconds_(0), timed_out_(false),
|
||||||
error_(kNoError) {
|
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,
|
bool SoapClient::Request(const std::string& operation,
|
||||||
|
|||||||
+3
-1
@@ -11,7 +11,9 @@ namespace webcc {
|
|||||||
|
|
||||||
class SoapClient {
|
class SoapClient {
|
||||||
public:
|
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;
|
~SoapClient() = default;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user