Unverified Commit db3563a2 authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #299 from knowledge4igor/code_refactoring_2

Refactoring and getting rid of magic number
parents 1a90268a da6759a9
......@@ -26,6 +26,7 @@
#include <pistache/peer.h>
#include <pistache/tcp.h>
#include <pistache/transport.h>
#include <pistache/view.h>
namespace Pistache {
namespace Http {
......@@ -810,5 +811,19 @@ std::shared_ptr<H> make_handler(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 Pistache
......@@ -118,6 +118,8 @@ namespace Http {
CHARSET(Utf32-LE, "utf-32le") \
CHARSET(Unicode-11, "unicode-1-1")
const uint16_t HTTP_STANDARD_PORT = 80;
enum class Method {
#define METHOD(m, _) m,
HTTP_METHODS
......
......@@ -10,6 +10,7 @@
#include <netdb.h>
#include <pistache/client.h>
#include <pistache/http.h>
#include <pistache/stream.h>
......@@ -19,20 +20,6 @@ using namespace Polling;
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";
std::pair<StringView, StringView>
......@@ -872,7 +859,7 @@ Client::doRequest(
}
if (!conn->isConnected()) {
conn->connect(httpAddr(s.first));
conn->connect(helpers::httpAddr(s.first));
}
return conn->perform(request, timeout, [=]() {
......
......@@ -346,7 +346,7 @@ Host::parse(const std::string& data) {
port_ = p;
} else {
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