Support combined host and port.

master
Adam Gu 7 years ago
parent e368ad2efb
commit 3ba0ddbba1

@ -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";

@ -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;

@ -35,12 +35,7 @@ void HttpAsyncClient::Request(std::shared_ptr<HttpRequest> 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,

@ -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);

@ -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.

@ -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);

@ -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,

@ -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;

@ -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,

@ -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;

Loading…
Cancel
Save