X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=parse.h;h=a2e35802d38d31e804e4b16dcf1f948df6f7cfa7;hp=1924cc08c9f3aab49c54bff4917ab38a6fb0a88d;hb=fa431bfab848624c2490a1134e084e6c1dd0dbfa;hpb=1d285bcbfd1aa3f7911cfb98a947a37f68154428;ds=sidebyside diff --git a/parse.h b/parse.h index 1924cc0..a2e3580 100644 --- a/parse.h +++ b/parse.h @@ -5,9 +5,42 @@ #include #include +#include +#include #include #include +// Locale-unaware tolower(); matches RFC 2616 no matter what the locale is set to. +static inline char ascii_tolower(const char ch) +{ + if (ch >= 'A' && ch <= 'Z') { + return ch + 'a' - 'A'; + } else { + return ch; + } +} + +// Case-insensitive header comparison and hashing. +struct HTTPLess { + bool operator() (const std::string &a, const std::string &b) const + { + return std::lexicographical_compare( + begin(a), end(a), begin(b), end(b), + [](char a, char b) { + return ascii_tolower(a) < ascii_tolower(b); + }); + } +}; +struct HTTPHash { + size_t operator() (const std::string &s) const + { + std::string s_low = s; + for (char &ch : s_low) { ch = ascii_tolower(ch); } + return std::hash() (s_low); + } +}; +using HTTPHeaderMultimap = std::unordered_multimap; + // Split a line on whitespace, e.g. "foo bar baz" -> {"foo", "bar", "baz"}. std::vector split_tokens(const std::string &line); @@ -16,8 +49,7 @@ std::vector split_lines(const std::string &str); // Extract HTTP headers from a request or response. Ignores the first line, // where the verb or the return code is. -std::unordered_multimap extract_headers( - const std::vector &lines, const std::string &log_context); +HTTPHeaderMultimap extract_headers(const std::vector &lines, const std::string &log_context); // Add the new data to an existing string, looking for \r\n\r\n // (typical of HTTP requests and/or responses). Will return one