Commit ef8a6a49 authored by Mathieu Stefani's avatar Mathieu Stefani

Experimenting with SSO-based string implementation

parent 79c71831
...@@ -15,7 +15,7 @@ class MyHandler : public Net::Http::Handler { ...@@ -15,7 +15,7 @@ class MyHandler : public Net::Http::Handler {
using namespace Net::Http; using namespace Net::Http;
Net::Http::Response response(Net::Http::Code::Ok, "PONG"); Net::Http::Response response(Net::Http::Code::Ok, "PONG");
#if 0 #if 1
response.headers response.headers
.add(make_header<Header::ContentEncoding>(Header::Encoding::Gzip)) .add(make_header<Header::ContentEncoding>(Header::Encoding::Gzip))
.add(make_header<Header::ContentType>(MIME(Text, Plain))); .add(make_header<Header::ContentType>(MIME(Text, Plain)));
......
...@@ -218,7 +218,7 @@ namespace Private { ...@@ -218,7 +218,7 @@ namespace Private {
// Skip the ':' // Skip the ':'
if (!cursor.advance(1)) return State::Again; if (!cursor.advance(1)) return State::Again;
std::string name = std::string(cursor.offset(start), cursor.diff(start) - 1); Header::string name = Header::string(cursor.offset(start), cursor.diff(start) - 1);
// Ignore spaces // Ignore spaces
while (cursor.current() == ' ') while (cursor.current() == ' ')
...@@ -236,8 +236,9 @@ namespace Private { ...@@ -236,8 +236,9 @@ namespace Private {
request->headers.add(std::move(header)); request->headers.add(std::move(header));
} }
else { else {
cout << "La chouma c'est qui ce con " << name << endl;
std::string value(cursor.offset(start), cursor.diff(start)); std::string value(cursor.offset(start), cursor.diff(start));
request->headers.addRaw(Header::Raw(std::move(name), std::move(value))); //request->headers.addRaw(Header::Raw(std::move(name), std::move(value)));
} }
// CRLF // CRLF
......
...@@ -243,6 +243,13 @@ CacheControl::addDirectives(const std::vector<Http::CacheDirective>& directives) ...@@ -243,6 +243,13 @@ CacheControl::addDirectives(const std::vector<Http::CacheDirective>& directives)
std::copy(std::begin(directives), std::end(directives), std::back_inserter(directives_)); std::copy(std::begin(directives), std::end(directives), std::back_inserter(directives_));
} }
void
Connection::parseRaw(const char* str, size_t len) {
}
void Connection::write(std::ostream& os) const {
}
void void
ContentLength::parse(const std::string& data) { ContentLength::parse(const std::string& data) {
try { try {
...@@ -271,16 +278,17 @@ Date::write(std::ostream& os) const { ...@@ -271,16 +278,17 @@ Date::write(std::ostream& os) const {
} }
void void
Host::parse(const std::string& data) { Host::parseRaw(const char* str, size_t size)
auto pos = data.find(':'); {
if (pos != std::string::npos) { const char *pos = static_cast<const char *>(memchr(str, ':', size));
std::string h = data.substr(0, pos); if (pos != nullptr) {
int16_t p = std::stoi(data.substr(pos + 1)); string h = string(str, pos - str);
int16_t p = 80;
host_ = h; host_ = std::move(h);
port_ = p; port_ = p;
} else { } else {
host_ = data; host_ = string(str, size);
port_ = 80; port_ = 80;
} }
} }
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#pragma once #pragma once
#define USE_SSO_STRING
#include "mime.h" #include "mime.h"
#include "net.h" #include "net.h"
#include "http_defs.h" #include "http_defs.h"
...@@ -14,6 +16,9 @@ ...@@ -14,6 +16,9 @@
#include <memory> #include <memory>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#ifdef USE_SSO_STRING
#include <ext/vstring.h>
#endif
#define SAFE_HEADER_CAST #define SAFE_HEADER_CAST
...@@ -23,6 +28,12 @@ namespace Http { ...@@ -23,6 +28,12 @@ namespace Http {
namespace Header { namespace Header {
#ifdef USE_SSO_STRING
typedef __gnu_cxx::__vstring string;
#else
typedef std::string string;
#endif
#ifdef SAFE_HEADER_CAST #ifdef SAFE_HEADER_CAST
namespace detail { namespace detail {
...@@ -180,6 +191,16 @@ private: ...@@ -180,6 +191,16 @@ private:
std::vector<Http::CacheDirective> directives_; std::vector<Http::CacheDirective> directives_;
}; };
class Connection : public Header {
public:
NAME("Connection")
Connection() { };
void parseRaw(const char* str, size_t len);
void write(std::ostream& os) const;
};
class ContentEncoding : public Header { class ContentEncoding : public Header {
public: public:
...@@ -272,19 +293,19 @@ public: ...@@ -272,19 +293,19 @@ public:
, port_(-1) , port_(-1)
{ } { }
explicit Host(const std::string& host, Net::Port port = 80) explicit Host(const string& host, Net::Port port = 80)
: host_(host) : host_(host)
, port_(port) , port_(port)
{ } { }
void parse(const std::string& data); void parseRaw(const char* str, size_t size);
void write(std::ostream& os) const; void write(std::ostream& os) const;
std::string host() const { return host_; } string host() const { return host_; }
Net::Port port() const { return port_; } Net::Port port() const { return port_; }
private: private:
std::string host_; string host_;
Net::Port port_; Net::Port port_;
}; };
......
...@@ -29,13 +29,13 @@ namespace { ...@@ -29,13 +29,13 @@ namespace {
return tid; return tid;
} }
std::unordered_map<std::string, std::unique_ptr<AllocatorBase>> allocators; std::unordered_map<string, std::unique_ptr<AllocatorBase>> allocators;
} }
namespace detail { namespace detail {
Header * Header *
allocate_header(const std::string& name) { allocate_header(const string& name) {
auto it = allocators.find(name); auto it = allocators.find(name);
if (it == std::end(allocators)) { if (it == std::end(allocators)) {
throw std::bad_alloc(); throw std::bad_alloc();
...@@ -54,6 +54,7 @@ header_delete::operator()(Header* header) { ...@@ -54,6 +54,7 @@ header_delete::operator()(Header* header) {
RegisterHeader(Accept); RegisterHeader(Accept);
RegisterHeader(CacheControl); RegisterHeader(CacheControl);
RegisterHeader(Connection);
RegisterHeader(ContentEncoding); RegisterHeader(ContentEncoding);
RegisterHeader(ContentLength); RegisterHeader(ContentLength);
RegisterHeader(ContentType); RegisterHeader(ContentType);
...@@ -63,14 +64,14 @@ RegisterHeader(Server); ...@@ -63,14 +64,14 @@ RegisterHeader(Server);
RegisterHeader(UserAgent); RegisterHeader(UserAgent);
void void
Registry::registerHeader(std::string name, std::unique_ptr<AllocatorBase>&& alloc) Registry::registerHeader(string name, std::unique_ptr<AllocatorBase>&& alloc)
{ {
allocators.insert(std::make_pair(name, std::move(alloc))); allocators.insert(std::make_pair(name, std::move(alloc)));
} }
std::vector<std::string> std::vector<string>
Registry::headersList() { Registry::headersList() {
std::vector<std::string> names; std::vector<string> names;
names.reserve(allocators.size()); names.reserve(allocators.size());
for (const auto &header: allocators) { for (const auto &header: allocators) {
...@@ -81,7 +82,7 @@ Registry::headersList() { ...@@ -81,7 +82,7 @@ Registry::headersList() {
} }
std::unique_ptr<Header, header_delete> std::unique_ptr<Header, header_delete>
Registry::makeHeader(const std::string& name) { Registry::makeHeader(const string& name) {
auto it = allocators.find(name); auto it = allocators.find(name);
if (it == std::end(allocators)) { if (it == std::end(allocators)) {
throw std::bad_alloc(); throw std::bad_alloc();
...@@ -91,7 +92,7 @@ Registry::makeHeader(const std::string& name) { ...@@ -91,7 +92,7 @@ Registry::makeHeader(const std::string& name) {
} }
bool bool
Registry::isRegistered(const std::string& name) { Registry::isRegistered(const string& name) {
auto it = allocators.find(name); auto it = allocators.find(name);
return it != std::end(allocators); return it != std::end(allocators);
} }
...@@ -111,11 +112,11 @@ Collection::add(std::shared_ptr<Header>&& header) { ...@@ -111,11 +112,11 @@ Collection::add(std::shared_ptr<Header>&& header) {
Collection& Collection&
Collection::addRaw(const Raw& raw) { Collection::addRaw(const Raw& raw) {
rawHeaders.insert(std::make_pair(raw.name(), raw)); //rawHeaders.insert(std::make_pair(raw.name(), raw));
} }
std::shared_ptr<const Header> std::shared_ptr<const Header>
Collection::get(const std::string& name) const { Collection::get(const string& name) const {
auto header = getImpl(name); auto header = getImpl(name);
if (!header.first) { if (!header.first) {
throw std::runtime_error("Could not find header"); throw std::runtime_error("Could not find header");
...@@ -125,7 +126,7 @@ Collection::get(const std::string& name) const { ...@@ -125,7 +126,7 @@ Collection::get(const std::string& name) const {
} }
std::shared_ptr<Header> std::shared_ptr<Header>
Collection::get(const std::string& name) { Collection::get(const string& name) {
auto header = getImpl(name); auto header = getImpl(name);
if (!header.first) { if (!header.first) {
throw std::runtime_error("Could not find header"); throw std::runtime_error("Could not find header");
...@@ -135,7 +136,7 @@ Collection::get(const std::string& name) { ...@@ -135,7 +136,7 @@ Collection::get(const std::string& name) {
} }
Raw Raw
Collection::getRaw(const std::string& name) const { Collection::getRaw(const string& name) const {
auto it = rawHeaders.find(name); auto it = rawHeaders.find(name);
if (it == std::end(rawHeaders)) { if (it == std::end(rawHeaders)) {
throw std::runtime_error("Could not find header"); throw std::runtime_error("Could not find header");
...@@ -145,7 +146,7 @@ Collection::getRaw(const std::string& name) const { ...@@ -145,7 +146,7 @@ Collection::getRaw(const std::string& name) const {
} }
std::shared_ptr<const Header> std::shared_ptr<const Header>
Collection::tryGet(const std::string& name) const { Collection::tryGet(const string& name) const {
auto header = getImpl(name); auto header = getImpl(name);
if (!header.first) return nullptr; if (!header.first) return nullptr;
...@@ -153,7 +154,7 @@ Collection::tryGet(const std::string& name) const { ...@@ -153,7 +154,7 @@ Collection::tryGet(const std::string& name) const {
} }
std::shared_ptr<Header> std::shared_ptr<Header>
Collection::tryGet(const std::string& name) { Collection::tryGet(const string& name) {
auto header = getImpl(name); auto header = getImpl(name);
if (!header.first) return nullptr; if (!header.first) return nullptr;
...@@ -161,7 +162,7 @@ Collection::tryGet(const std::string& name) { ...@@ -161,7 +162,7 @@ Collection::tryGet(const std::string& name) {
} }
Optional<Raw> Optional<Raw>
Collection::tryGetRaw(const std::string& name) const { Collection::tryGetRaw(const string& name) const {
auto it = rawHeaders.find(name); auto it = rawHeaders.find(name);
if (it == std::end(rawHeaders)) { if (it == std::end(rawHeaders)) {
return None(); return None();
...@@ -171,7 +172,7 @@ Collection::tryGetRaw(const std::string& name) const { ...@@ -171,7 +172,7 @@ Collection::tryGetRaw(const std::string& name) const {
} }
bool bool
Collection::has(const std::string& name) const { Collection::has(const string& name) const {
return getImpl(name).first; return getImpl(name).first;
} }
...@@ -193,7 +194,7 @@ Collection::clear() { ...@@ -193,7 +194,7 @@ Collection::clear() {
} }
std::pair<bool, std::shared_ptr<Header>> std::pair<bool, std::shared_ptr<Header>>
Collection::getImpl(const std::string& name) const { Collection::getImpl(const string& name) const {
auto it = headers.find(name); auto it = headers.find(name);
if (it == std::end(headers)) { if (it == std::end(headers)) {
return std::make_pair(false, nullptr); return std::make_pair(false, nullptr);
......
...@@ -57,13 +57,13 @@ public: ...@@ -57,13 +57,13 @@ public:
Collection& add(std::shared_ptr<Header>&& header); Collection& add(std::shared_ptr<Header>&& header);
Collection& addRaw(const Raw& raw); Collection& addRaw(const Raw& raw);
std::shared_ptr<const Header> get(const std::string& name) const; std::shared_ptr<const Header> get(const string& name) const;
std::shared_ptr<Header> get(const std::string& name); std::shared_ptr<Header> get(const string& name);
Raw getRaw(const std::string& name) const; Raw getRaw(const string& name) const;
std::shared_ptr<const Header> tryGet(const std::string& name) const; std::shared_ptr<const Header> tryGet(const string& name) const;
std::shared_ptr<Header> tryGet(const std::string& name); std::shared_ptr<Header> tryGet(const string& name);
Optional<Raw> tryGetRaw(const std::string& name) const; Optional<Raw> tryGetRaw(const string& name) const;
template<typename H> template<typename H>
typename std::enable_if< typename std::enable_if<
...@@ -72,17 +72,17 @@ public: ...@@ -72,17 +72,17 @@ public:
has() const { has() const {
return has(H::Name); return has(H::Name);
} }
bool has(const std::string& name) const; bool has(const string& name) const;
std::vector<std::shared_ptr<Header>> list() const; std::vector<std::shared_ptr<Header>> list() const;
void clear(); void clear();
private: private:
std::pair<bool, std::shared_ptr<Header>> getImpl(const std::string& name) const; std::pair<bool, std::shared_ptr<Header>> getImpl(const string& name) const;
std::unordered_map<std::string, std::shared_ptr<Header>> headers; std::unordered_map<string, std::shared_ptr<Header>> headers;
std::unordered_map<std::string, Raw> rawHeaders; std::unordered_map<string, Raw> rawHeaders;
}; };
struct header_delete { struct header_delete {
...@@ -157,12 +157,12 @@ struct Registry { ...@@ -157,12 +157,12 @@ struct Registry {
registerHeader(H::Name, std::move(alloc)); registerHeader(H::Name, std::move(alloc));
} }
static void registerHeader(std::string name, std::unique_ptr<AllocatorBase>&& alloc); static void registerHeader(string name, std::unique_ptr<AllocatorBase>&& alloc);
static std::vector<std::string> headersList(); static std::vector<string> headersList();
static std::unique_ptr<Header, header_delete> makeHeader(const std::string& name); static std::unique_ptr<Header, header_delete> makeHeader(const string& name);
static bool isRegistered(const std::string& name); static bool isRegistered(const string& name);
}; };
template<typename H> template<typename H>
...@@ -175,7 +175,7 @@ struct Registrar { ...@@ -175,7 +175,7 @@ struct Registrar {
}; };
namespace detail { namespace detail {
Header* allocate_header(const std::string& name); Header* allocate_header(const string& name);
} }
/* Crazy macro machinery to generate a unique variable name /* Crazy macro machinery to generate a unique variable name
......
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