Commit 352745b4 authored by Mathieu Stefani's avatar Mathieu Stefani

Headers now live in their own namespace.

The reason is to avoid some headers name like Server or Date to collide
with some other generic classes that might have the same name
parent ddf9c22c
......@@ -16,8 +16,8 @@ class MyHandler : public Net::Http::Handler {
Net::Http::Response response(Net::Http::Code::Ok, "PONG");
response.headers
.add(std::make_shared<Server>("lys"))
.add(std::make_shared<ContentType>(MIME(Text, Plain)));
.add(std::make_shared<Header::Server>("lys"))
.add(std::make_shared<Header::ContentType>(MIME(Text, Plain)));
response.writeTo(peer);
......
......@@ -228,8 +228,8 @@ namespace Private {
if (!cursor.advance(1)) return State::Again;
}
if (HeaderRegistry::isRegistered(name)) {
std::shared_ptr<Header> header = HeaderRegistry::makeHeader(name);
if (Header::Registry::isRegistered(name)) {
std::shared_ptr<Header::Header> header = Header::Registry::makeHeader(name);
header->parseRaw(cursor.offset(start), cursor.diff(start));
request->headers.add(header);
}
......@@ -246,7 +246,7 @@ namespace Private {
Parser::State
Parser::BodyStep::apply(Cursor& cursor) {
auto cl = request->headers.tryGet<ContentLength>();
auto cl = request->headers.tryGet<Header::ContentLength>();
if (!cl) return Parser::State::Done;
auto contentLength = cl->value();
......@@ -483,7 +483,7 @@ Endpoint::serve()
if (!handler_)
throw std::runtime_error("Must call setHandler() prior to serve()");
listener.init(8, Tcp::Options::InstallSignalHandler);
listener.init(24, Tcp::Options::InstallSignalHandler);
listener.setHandler(handler_);
if (listener.bind()) {
......
......@@ -112,7 +112,7 @@ public:
Message();
Version version;
Headers headers;
Header::Collection headers;
std::string body;
};
......
......@@ -18,6 +18,8 @@ namespace Net {
namespace Http {
namespace Header {
const char* encodingString(Encoding encoding) {
switch (encoding) {
case Encoding::Gzip:
......@@ -169,6 +171,8 @@ ContentType::write(std::ostream& os) const {
os << mime_.toString();
}
} // namespace Header
} // namespace Http
} // namespace Net
......@@ -20,6 +20,8 @@ namespace Net {
namespace Http {
namespace Header {
#ifdef SAFE_HEADER_CAST
namespace detail {
......@@ -245,6 +247,8 @@ private:
};
} // namespace Header
} // namespace Http
} // namespace Net
......
......@@ -14,12 +14,14 @@ namespace Net {
namespace Http {
namespace Header {
namespace {
std::unordered_map<std::string, HeaderRegistry::RegistryFunc> registry;
std::unordered_map<std::string, Registry::RegistryFunc> registry;
}
void
HeaderRegistry::registerHeader(std::string name, HeaderRegistry::RegistryFunc func)
Registry::registerHeader(std::string name, Registry::RegistryFunc func)
{
auto it = registry.find(name);
if (it != std::end(registry)) {
......@@ -30,7 +32,7 @@ HeaderRegistry::registerHeader(std::string name, HeaderRegistry::RegistryFunc fu
}
std::vector<std::string>
HeaderRegistry::headersList() {
Registry::headersList() {
std::vector<std::string> names;
names.reserve(registry.size());
......@@ -42,7 +44,7 @@ HeaderRegistry::headersList() {
}
std::unique_ptr<Header>
HeaderRegistry::makeHeader(const std::string& name) {
Registry::makeHeader(const std::string& name) {
auto it = registry.find(name);
if (it == std::end(registry)) {
throw std::runtime_error("Unknown header");
......@@ -52,20 +54,20 @@ HeaderRegistry::makeHeader(const std::string& name) {
}
bool
HeaderRegistry::isRegistered(const std::string& name) {
Registry::isRegistered(const std::string& name) {
auto it = registry.find(name);
return it != std::end(registry);
}
Headers&
Headers::add(const std::shared_ptr<Header>& header) {
Collection&
Collection::add(const std::shared_ptr<Header>& header) {
headers.insert(std::make_pair(header->name(), header));
return *this;
}
std::shared_ptr<const Header>
Headers::get(const std::string& name) const {
Collection::get(const std::string& name) const {
auto header = getImpl(name);
if (!header.first) {
throw std::runtime_error("Could not find header");
......@@ -75,7 +77,7 @@ Headers::get(const std::string& name) const {
}
std::shared_ptr<Header>
Headers::get(const std::string& name) {
Collection::get(const std::string& name) {
auto header = getImpl(name);
if (!header.first) {
throw std::runtime_error("Could not find header");
......@@ -85,7 +87,7 @@ Headers::get(const std::string& name) {
}
std::shared_ptr<const Header>
Headers::tryGet(const std::string& name) const {
Collection::tryGet(const std::string& name) const {
auto header = getImpl(name);
if (!header.first) return nullptr;
......@@ -93,7 +95,7 @@ Headers::tryGet(const std::string& name) const {
}
std::shared_ptr<Header>
Headers::tryGet(const std::string& name) {
Collection::tryGet(const std::string& name) {
auto header = getImpl(name);
if (!header.first) return nullptr;
......@@ -101,12 +103,12 @@ Headers::tryGet(const std::string& name) {
}
bool
Headers::has(const std::string& name) const {
Collection::has(const std::string& name) const {
return getImpl(name).first;
}
std::vector<std::shared_ptr<Header>>
Headers::list() const {
Collection::list() const {
std::vector<std::shared_ptr<Header>> ret;
ret.reserve(headers.size());
for (const auto& h: headers) {
......@@ -117,12 +119,12 @@ Headers::list() const {
}
void
Headers::clear() {
Collection::clear() {
headers.clear();
}
std::pair<bool, std::shared_ptr<Header>>
Headers::getImpl(const std::string& name) const {
Collection::getImpl(const std::string& name) const {
auto it = headers.find(name);
if (it == std::end(headers)) {
return std::make_pair(false, nullptr);
......@@ -134,16 +136,18 @@ Headers::getImpl(const std::string& name) const {
namespace {
struct AtInit {
AtInit() {
HeaderRegistry::registerHeader<ContentLength>();
HeaderRegistry::registerHeader<ContentType>();
HeaderRegistry::registerHeader<Host>();
HeaderRegistry::registerHeader<Accept>();
HeaderRegistry::registerHeader<UserAgent>();
HeaderRegistry::registerHeader<ContentEncoding>();
Registry::registerHeader<ContentLength>();
Registry::registerHeader<ContentType>();
Registry::registerHeader<Host>();
Registry::registerHeader<Accept>();
Registry::registerHeader<UserAgent>();
Registry::registerHeader<ContentEncoding>();
}
} atInit;
}
} // namespace Header
} // namespace Http
} // namespace Net
......@@ -15,7 +15,9 @@ namespace Net {
namespace Http {
class Headers {
namespace Header {
class Collection {
public:
template<typename H>
......@@ -48,7 +50,7 @@ public:
return std::static_pointer_cast<H>(tryGet(H::Name));
}
Headers& add(const std::shared_ptr<Header>& header);
Collection& add(const std::shared_ptr<Header>& header);
std::shared_ptr<const Header> get(const std::string& name) const;
std::shared_ptr<Header> get(const std::string& name);
......@@ -75,7 +77,7 @@ private:
std::unordered_map<std::string, std::shared_ptr<Header>> headers;
};
struct HeaderRegistry {
struct Registry {
typedef std::function<std::unique_ptr<Header>()> RegistryFunc;
......@@ -99,6 +101,8 @@ struct HeaderRegistry {
static bool isRegistered(const std::string& name);
};
} // namespace Header
} // namespace Http
} // namespace Net
......@@ -4,14 +4,14 @@
using namespace Net::Http;
TEST(headers_test, content_length) {
ContentLength cl;
Header::ContentLength cl;
cl.parse("3495");
ASSERT_EQ(cl.value(), 3495);
}
TEST(headers_test, host) {
Host host;
Header::Host host;
host.parse("www.w3.org");
ASSERT_EQ(host.host(), "www.w3.org");
......@@ -23,21 +23,21 @@ TEST(headers_test, host) {
}
TEST(headers_test, user_agent) {
UserAgent ua;
Header::UserAgent ua;
ua.parse("CERN-LineMode/2.15 libwww/2.17b3");
ASSERT_EQ(ua.ua(), "CERN-LineMode/2.15 libwww/2.17b3");
}
TEST(headers_test, content_encoding) {
ContentEncoding ce;
Header::ContentEncoding ce;
ce.parse("gzip");
ASSERT_EQ(ce.encoding(), Encoding::Gzip);
ASSERT_EQ(ce.encoding(), Header::Encoding::Gzip);
}
TEST(headers_test, content_type) {
ContentType ct;
Header::ContentType ct;
ct.parse("text/html; charset=ISO-8859-4");
const auto& mime = ct.mime();
......
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