Commit da6759a9 authored by knowledge4igor's avatar knowledge4igor

Refactoring and getting rid of magic number

parent 1a90268a
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <pistache/peer.h> #include <pistache/peer.h>
#include <pistache/tcp.h> #include <pistache/tcp.h>
#include <pistache/transport.h> #include <pistache/transport.h>
#include <pistache/view.h>
namespace Pistache { namespace Pistache {
namespace Http { namespace Http {
...@@ -810,5 +811,19 @@ std::shared_ptr<H> make_handler(Args&& ...args) { ...@@ -810,5 +811,19 @@ std::shared_ptr<H> make_handler(Args&& ...args) {
return std::make_shared<H>(std::forward<Args>(args)...); return std::make_shared<H>(std::forward<Args>(args)...);
} }
namespace helpers
{
inline Address httpAddr(const StringView& view) {
auto const str = view.toString();
auto const pos = str.find(':');
if (pos == std::string::npos) {
return Address(std::move(str), HTTP_STANDARD_PORT);
}
auto const host = str.substr(0, pos);
auto const port = std::stoi(str.substr(pos + 1));
return Address(std::move(host), port);
}
} // namespace helpers
} // namespace Http } // namespace Http
} // namespace Pistache } // namespace Pistache
...@@ -118,6 +118,8 @@ namespace Http { ...@@ -118,6 +118,8 @@ namespace Http {
CHARSET(Utf32-LE, "utf-32le") \ CHARSET(Utf32-LE, "utf-32le") \
CHARSET(Unicode-11, "unicode-1-1") CHARSET(Unicode-11, "unicode-1-1")
const uint16_t HTTP_STANDARD_PORT = 80;
enum class Method { enum class Method {
#define METHOD(m, _) m, #define METHOD(m, _) m,
HTTP_METHODS HTTP_METHODS
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <netdb.h> #include <netdb.h>
#include <pistache/client.h> #include <pistache/client.h>
#include <pistache/http.h>
#include <pistache/stream.h> #include <pistache/stream.h>
...@@ -19,20 +20,6 @@ using namespace Polling; ...@@ -19,20 +20,6 @@ using namespace Polling;
namespace Http { namespace Http {
namespace {
Address httpAddr(const StringView& view) {
auto str = view.toString();
auto pos = str.find(':');
if (pos == std::string::npos) {
return Address(std::move(str), 80);
}
auto host = str.substr(0, pos);
auto port = std::stoi(str.substr(pos + 1));
return Address(std::move(host), port);
}
}
static constexpr const char* UA = "pistache/0.1"; static constexpr const char* UA = "pistache/0.1";
std::pair<StringView, StringView> std::pair<StringView, StringView>
...@@ -872,7 +859,7 @@ Client::doRequest( ...@@ -872,7 +859,7 @@ Client::doRequest(
} }
if (!conn->isConnected()) { if (!conn->isConnected()) {
conn->connect(httpAddr(s.first)); conn->connect(helpers::httpAddr(s.first));
} }
return conn->perform(request, timeout, [=]() { return conn->perform(request, timeout, [=]() {
......
...@@ -346,7 +346,7 @@ Host::parse(const std::string& data) { ...@@ -346,7 +346,7 @@ Host::parse(const std::string& data) {
port_ = p; port_ = p;
} else { } else {
host_ = data; host_ = data;
port_ = 80; port_ = HTTP_STANDARD_PORT;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment