]> git.sesse.net Git - cubemap/blobdiff - parse.h
fix potential ambiguity between ::bind() and std::bind()
[cubemap] / parse.h
diff --git a/parse.h b/parse.h
index 6912dd9a5c314ca41494aed2a6be41ba5094ab44..70b3b5ac20869850ada68d71518cd10ee08dbcb7 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -3,8 +3,44 @@
 
 // Various routines that deal with parsing; both HTTP requests and more generic text.
 
-#include <vector>
+#include <stddef.h>
+#include <string>
+#include <algorithm>
 #include <string>
+#include <unordered_map>
+#include <vector>
+
+// 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 HTTPEqual {
+       bool operator() (const std::string &a, const std::string &b) const
+       {
+               return a.size() == b.size() &&
+                       std::equal(
+                               begin(a), end(a), begin(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<std::string>() (s_low);
+       }
+};
+using HTTPHeaderMultimap = std::unordered_multimap<std::string, std::string, HTTPHash, HTTPEqual>;
 
 // Split a line on whitespace, e.g. "foo  bar baz" -> {"foo", "bar", "baz"}.
 std::vector<std::string> split_tokens(const std::string &line);
@@ -12,6 +48,10 @@ std::vector<std::string> split_tokens(const std::string &line);
 // Split a string on \n or \r\n, e.g. "foo\nbar\r\n\nbaz\r\n\r\n" -> {"foo", "bar", "baz"}.
 std::vector<std::string> 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.
+HTTPHeaderMultimap extract_headers(const std::vector<std::string> &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
 // of the given statuses.