Skip to content

Commit

Permalink
Add support for case insensitive headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmoraes authored and oktal committed Jul 30, 2017
1 parent 11c248e commit 54a3731
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
24 changes: 23 additions & 1 deletion include/pistache/http_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#pragma once

#include <functional>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <memory>
Expand All @@ -16,6 +18,21 @@ namespace Pistache {
namespace Http {
namespace Header {

std::string
toLowercase(std::string str);

struct LowercaseHash {
size_t operator()(const std::string& key) const {
return std::hash<std::string>{}(toLowercase(key));
}
};

struct LowercaseEqual {
bool operator()(const std::string& left, const std::string& right) const {
return toLowercase(left) == toLowercase(right);
}
};

class Collection {
public:

Expand Down Expand Up @@ -94,7 +111,12 @@ class Collection {
private:
std::pair<bool, std::shared_ptr<Header>> getImpl(const std::string& name) const;

std::unordered_map<std::string, std::shared_ptr<Header>> headers;
std::unordered_map<
std::string,
std::shared_ptr<Header>,
LowercaseHash,
LowercaseEqual
> headers;
std::unordered_map<std::string, Raw> rawHeaders;
};

Expand Down
13 changes: 12 additions & 1 deletion src/common/http_headers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ namespace Http {
namespace Header {

namespace {
std::unordered_map<std::string, Registry::RegistryFunc> registry;
std::unordered_map<
std::string,
Registry::RegistryFunc,
LowercaseHash,
LowercaseEqual
> registry;
}

RegisterHeader(Accept);
Expand All @@ -35,6 +40,12 @@ RegisterHeader(Location);
RegisterHeader(Server);
RegisterHeader(UserAgent);

std::string
toLowercase(std::string str) {
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
return str;
}

void
Registry::registerHeader(std::string name, Registry::RegistryFunc func)
{
Expand Down

0 comments on commit 54a3731

Please sign in to comment.