Commit d673e215 authored by Dennis Jenkins's avatar Dennis Jenkins

Reformatted all .cc files with clang-format (default options). Reformatting...

Reformatted all .cc files with clang-format (default options).  Reformatting the .h files causes cppcheck to segfault, so I'll tackle those later.
parent 2447ef55
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -4,189 +4,167 @@ ...@@ -4,189 +4,167 @@
Implementation of http definitions Implementation of http definitions
*/ */
#include <iostream>
#include <iomanip> #include <iomanip>
#include <iostream>
#include <pistache/http_defs.h>
#include <pistache/common.h> #include <pistache/common.h>
#include <pistache/date.h> #include <pistache/date.h>
#include <pistache/http_defs.h>
namespace Pistache { namespace Pistache {
namespace Http { namespace Http {
namespace { namespace {
using time_point = FullDate::time_point; using time_point = FullDate::time_point;
bool parse_RFC_1123(const std::string& s, time_point &tp) bool parse_RFC_1123(const std::string &s, time_point &tp) {
{ std::istringstream in{s};
std::istringstream in{s}; in >> date::parse("%a, %d %b %Y %T %Z", tp);
in >> date::parse("%a, %d %b %Y %T %Z", tp); return !in.fail();
return !in.fail(); }
}
bool parse_RFC_850(const std::string &s, time_point &tp) {
bool parse_RFC_850(const std::string& s, time_point &tp) std::istringstream in{s};
{ in >> date::parse("%a, %d-%b-%y %T %Z", tp);
std::istringstream in{s}; return !in.fail();
in >> date::parse("%a, %d-%b-%y %T %Z", tp); }
return !in.fail();
} bool parse_asctime(const std::string &s, time_point &tp) {
std::istringstream in{s};
bool parse_asctime(const std::string& s, time_point &tp) in >> date::parse("%a %b %d %T %Y", tp);
{ return !in.fail();
std::istringstream in{s}; }
in >> date::parse("%a %b %d %T %Y", tp);
return !in.fail();
}
} // anonymous namespace } // anonymous namespace
CacheDirective::CacheDirective(Directive directive) CacheDirective::CacheDirective(Directive directive) : directive_(), data() {
: directive_() init(directive, std::chrono::seconds(0));
, data()
{
init(directive, std::chrono::seconds(0));
} }
CacheDirective::CacheDirective(Directive directive, std::chrono::seconds delta) CacheDirective::CacheDirective(Directive directive, std::chrono::seconds delta)
: directive_() : directive_(), data() {
, data() init(directive, delta);
{
init(directive, delta);
} }
std::chrono::seconds std::chrono::seconds CacheDirective::delta() const {
CacheDirective::delta() const switch (directive_) {
{ case MaxAge:
switch (directive_) { return std::chrono::seconds(data.maxAge);
case MaxAge: case SMaxAge:
return std::chrono::seconds(data.maxAge); return std::chrono::seconds(data.sMaxAge);
case SMaxAge: case MaxStale:
return std::chrono::seconds(data.sMaxAge); return std::chrono::seconds(data.maxStale);
case MaxStale: case MinFresh:
return std::chrono::seconds(data.maxStale); return std::chrono::seconds(data.minFresh);
case MinFresh: default:
return std::chrono::seconds(data.minFresh); throw std::domain_error("Invalid operation on cache directive");
default: }
throw std::domain_error("Invalid operation on cache directive");
}
} }
void void CacheDirective::init(Directive directive, std::chrono::seconds delta) {
CacheDirective::init(Directive directive, std::chrono::seconds delta) directive_ = directive;
{ switch (directive) {
directive_ = directive; case MaxAge:
switch (directive) { data.maxAge = delta.count();
case MaxAge: break;
data.maxAge = delta.count(); case SMaxAge:
break; data.sMaxAge = delta.count();
case SMaxAge: break;
data.sMaxAge = delta.count(); case MaxStale:
break; data.maxStale = delta.count();
case MaxStale: break;
data.maxStale = delta.count(); case MinFresh:
break; data.minFresh = delta.count();
case MinFresh: break;
data.minFresh = delta.count(); default:
break; break;
default: }
break;
}
} }
FullDate FullDate FullDate::fromString(const std::string &str) {
FullDate::fromString(const std::string& str) {
FullDate::time_point tp; FullDate::time_point tp;
if(parse_RFC_1123(str, tp)) if (parse_RFC_1123(str, tp))
return FullDate(tp); return FullDate(tp);
else if(parse_RFC_850(str, tp)) else if (parse_RFC_850(str, tp))
return FullDate(tp); return FullDate(tp);
else if(parse_asctime(str, tp)) else if (parse_asctime(str, tp))
return FullDate(tp); return FullDate(tp);
throw std::runtime_error("Invalid Date format"); throw std::runtime_error("Invalid Date format");
} }
void void FullDate::write(std::ostream &os, Type type) const {
FullDate::write(std::ostream& os, Type type) const switch (type) {
{ case Type::RFC1123:
switch (type) { date::to_stream(os, "%a, %d %b %Y %T %Z", date_);
case Type::RFC1123: break;
date::to_stream(os, "%a, %d %b %Y %T %Z", date_); case Type::RFC850:
break; date::to_stream(os, "%a, %d-%b-%y %T %Z", date_);
case Type::RFC850: break;
date::to_stream(os, "%a, %d-%b-%y %T %Z", date_); case Type::AscTime:
break; date::to_stream(os, "%a %b %d %T %Y", date_);
case Type::AscTime: break;
date::to_stream(os, "%a %b %d %T %Y", date_); default:
break; std::runtime_error("Invalid use of FullDate::write");
default: }
std::runtime_error("Invalid use of FullDate::write");
}
} }
const char *versionString(Version version) { const char *versionString(Version version) {
switch (version) { switch (version) {
case Version::Http10: case Version::Http10:
return "HTTP/1.0"; return "HTTP/1.0";
case Version::Http11: case Version::Http11:
return "HTTP/1.1"; return "HTTP/1.1";
} }
unreachable(); unreachable();
} }
const char* methodString(Method method) const char *methodString(Method method) {
{ switch (method) {
switch (method) { #define METHOD(name, str) \
#define METHOD(name, str) \ case Method::name: \
case Method::name: \ return str;
return str;
HTTP_METHODS HTTP_METHODS
#undef METHOD #undef METHOD
} }
unreachable(); unreachable();
} }
const char* codeString(Code code) const char *codeString(Code code) {
{ switch (code) {
switch (code) { #define CODE(_, name, str) \
#define CODE(_, name, str) \ case Code::name: \
case Code::name: \ return str;
return str;
STATUS_CODES STATUS_CODES
#undef CODE #undef CODE
} }
return ""; return "";
} }
std::ostream& operator<<(std::ostream& os, Version version) { std::ostream &operator<<(std::ostream &os, Version version) {
os << versionString(version); os << versionString(version);
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, Method method) { std::ostream &operator<<(std::ostream &os, Method method) {
os << methodString(method); os << methodString(method);
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, Code code) { std::ostream &operator<<(std::ostream &os, Code code) {
os << codeString(code); os << codeString(code);
return os; return os;
} }
HttpError::HttpError(Code code, std::string reason) HttpError::HttpError(Code code, std::string reason)
: code_(static_cast<int>(code)) : code_(static_cast<int>(code)), reason_(std::move(reason)) {}
, reason_(std::move(reason))
{ }
HttpError::HttpError(int code, std::string reason) HttpError::HttpError(int code, std::string reason)
: code_(code) : code_(code), reason_(std::move(reason)) {}
, reason_(std::move(reason))
{ }
} // namespace Http } // namespace Http
} // namespace Pistache } // namespace Pistache
This diff is collapsed.
...@@ -35,184 +35,168 @@ RegisterHeader(Location); ...@@ -35,184 +35,168 @@ RegisterHeader(Location);
RegisterHeader(Server); RegisterHeader(Server);
RegisterHeader(UserAgent); RegisterHeader(UserAgent);
std::string std::string toLowercase(std::string str) {
toLowercase(std::string str) { std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::transform(str.begin(), str.end(), str.begin(), ::tolower); return str;
return str;
} }
bool bool LowercaseEqualStatic(const std::string &dynamic,
LowercaseEqualStatic(const std::string& dynamic, const std::string& statik) { const std::string &statik) {
return std::equal(dynamic.begin(), dynamic.end(), statik.begin(), statik.end(), return std::equal(
[] (const char& a, const char& b) { dynamic.begin(), dynamic.end(), statik.begin(), statik.end(),
return std::tolower(a) == b; [](const char &a, const char &b) { return std::tolower(a) == b; });
});
} }
Registry& Registry::instance() { Registry &Registry::instance() {
static Registry instance; static Registry instance;
return instance; return instance;
} }
Registry::Registry() Registry::Registry() {}
{}
Registry::~Registry() Registry::~Registry() {}
{}
void void Registry::registerHeader(const std::string &name,
Registry::registerHeader(const std::string& name, Registry::RegistryFunc func) Registry::RegistryFunc func) {
{ auto it = registry.find(name);
auto it = registry.find(name); if (it != std::end(registry)) {
if (it != std::end(registry)) { throw std::runtime_error("Header already registered");
throw std::runtime_error("Header already registered"); }
}
registry.insert(std::make_pair(name, std::move(func))); registry.insert(std::make_pair(name, std::move(func)));
} }
std::vector<std::string> std::vector<std::string> Registry::headersList() {
Registry::headersList() { std::vector<std::string> names;
std::vector<std::string> names; names.reserve(registry.size());
names.reserve(registry.size());
for (const auto &header: registry) { for (const auto &header : registry) {
names.push_back(header.first); names.push_back(header.first);
} }
return names; return names;
} }
std::unique_ptr<Header> std::unique_ptr<Header> Registry::makeHeader(const std::string &name) {
Registry::makeHeader(const std::string& name) { auto it = registry.find(name);
auto it = registry.find(name); if (it == std::end(registry)) {
if (it == std::end(registry)) { throw std::runtime_error("Unknown header");
throw std::runtime_error("Unknown header"); }
}
return it->second(); return it->second();
} }
bool bool Registry::isRegistered(const std::string &name) {
Registry::isRegistered(const std::string& name) { auto it = registry.find(name);
auto it = registry.find(name); return it != std::end(registry);
return it != std::end(registry);
} }
Collection& Collection &Collection::add(const std::shared_ptr<Header> &header) {
Collection::add(const std::shared_ptr<Header>& header) { headers.insert(std::make_pair(header->name(), header));
headers.insert(std::make_pair(header->name(), header));
return *this; return *this;
} }
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)); return *this;
return *this;
} }
std::shared_ptr<const Header> std::shared_ptr<const Header> Collection::get(const std::string &name) const {
Collection::get(const std::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"); }
}
return header.second; return header.second;
} }
std::shared_ptr<Header> std::shared_ptr<Header> Collection::get(const std::string &name) {
Collection::get(const std::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"); }
}
return header.second; return header.second;
} }
Raw Raw Collection::getRaw(const std::string &name) const {
Collection::getRaw(const std::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"); }
}
return it->second; return it->second;
} }
std::shared_ptr<const Header> std::shared_ptr<const Header>
Collection::tryGet(const std::string& name) const { Collection::tryGet(const std::string &name) const {
auto header = getImpl(name); auto header = getImpl(name);
if (!header.first) return nullptr; if (!header.first)
return nullptr;
return header.second; return header.second;
} }
std::shared_ptr<Header> std::shared_ptr<Header> Collection::tryGet(const std::string &name) {
Collection::tryGet(const std::string& name) { auto header = getImpl(name);
auto header = getImpl(name); if (!header.first)
if (!header.first) return nullptr; return nullptr;
return header.second; return header.second;
} }
Optional<Raw> Optional<Raw> Collection::tryGetRaw(const std::string &name) const {
Collection::tryGetRaw(const std::string& name) const { auto it = rawHeaders.find(name);
auto it = rawHeaders.find(name); if (it == std::end(rawHeaders)) {
if (it == std::end(rawHeaders)) { return Optional<Raw>(None());
return Optional<Raw>(None()); }
}
return Optional<Raw>(Some(it->second)); return Optional<Raw>(Some(it->second));
} }
bool bool Collection::has(const std::string &name) const {
Collection::has(const std::string& name) const { return getImpl(name).first;
return getImpl(name).first;
} }
std::vector<std::shared_ptr<Header>> std::vector<std::shared_ptr<Header>> Collection::list() const {
Collection::list() const { std::vector<std::shared_ptr<Header>> ret;
std::vector<std::shared_ptr<Header>> ret; ret.reserve(headers.size());
ret.reserve(headers.size()); for (const auto &h : headers) {
for (const auto& h: headers) { ret.push_back(h.second);
ret.push_back(h.second); }
}
return ret; return ret;
} }
bool bool Collection::remove(const std::string &name) {
Collection::remove(const std::string& name) { auto tit = headers.find(name);
auto tit = headers.find(name); if (tit == std::end(headers)) {
if (tit == std::end(headers)) { auto rit = rawHeaders.find(name);
auto rit = rawHeaders.find(name); if (rit == std::end(rawHeaders))
if (rit == std::end(rawHeaders)) return false; return false;
rawHeaders.erase(rit); rawHeaders.erase(rit);
return true;
}
headers.erase(tit);
return true; return true;
}
headers.erase(tit);
return true;
} }
void void Collection::clear() {
Collection::clear() { headers.clear();
headers.clear(); rawHeaders.clear();
rawHeaders.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 std::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);
} }
return std::make_pair(true, it->second); return std::make_pair(true, it->second);
} }
} // namespace Header } // namespace Header
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -3,17 +3,16 @@ ...@@ -3,17 +3,16 @@
using namespace Pistache; using namespace Pistache;
TEST(http_uri_test, query_as_string_test) TEST(http_uri_test, query_as_string_test) {
{ Http::Uri::Query query1;
Http::Uri::Query query1; ASSERT_TRUE(query1.as_str().empty());
ASSERT_TRUE(query1.as_str().empty());
Http::Uri::Query query2; Http::Uri::Query query2;
query2.add("value1", "name1"); query2.add("value1", "name1");
ASSERT_STREQ(query2.as_str().c_str(), "?value1=name1"); ASSERT_STREQ(query2.as_str().c_str(), "?value1=name1");
Http::Uri::Query query3; Http::Uri::Query query3;
query3.add("value1", "name1"); query3.add("value1", "name1");
query3.add("value2", "name2"); query3.add("value2", "name2");
ASSERT_STREQ(query3.as_str().c_str(), "?value2=name2&value1=name1"); ASSERT_STREQ(query3.as_str().c_str(), "?value2=name2&value1=name1");
} }
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
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