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