4 // Various routines that deal with parsing; both HTTP requests and more generic text.
10 #include <unordered_map>
13 // Locale-unaware tolower(); matches RFC 2616 no matter what the locale is set to.
14 static inline char ascii_tolower(const char ch)
16 if (ch >= 'A' && ch <= 'Z') {
17 return ch + 'a' - 'A';
23 // Case-insensitive header comparison and hashing.
25 bool operator() (const std::string &a, const std::string &b) const
27 return std::lexicographical_compare(
28 begin(a), end(a), begin(b), end(b),
30 return ascii_tolower(a) < ascii_tolower(b);
35 size_t operator() (const std::string &s) const
37 std::string s_low = s;
38 for (char &ch : s_low) { ch = ascii_tolower(ch); }
39 return std::hash<std::string>() (s_low);
42 using HTTPHeaderMultimap = std::unordered_multimap<std::string, std::string, HTTPHash, HTTPLess>;
44 // Split a line on whitespace, e.g. "foo bar baz" -> {"foo", "bar", "baz"}.
45 std::vector<std::string> split_tokens(const std::string &line);
47 // Split a string on \n or \r\n, e.g. "foo\nbar\r\n\nbaz\r\n\r\n" -> {"foo", "bar", "baz"}.
48 std::vector<std::string> split_lines(const std::string &str);
50 // Extract HTTP headers from a request or response. Ignores the first line,
51 // where the verb or the return code is.
52 HTTPHeaderMultimap extract_headers(const std::vector<std::string> &lines, const std::string &log_context);
54 // Add the new data to an existing string, looking for \r\n\r\n
55 // (typical of HTTP requests and/or responses). Will return one
56 // of the given statuses.
58 // Note that if you give too much data in new_data_size, you could
59 // get an RP_OUT_OF_SPACE even if you expected RP_EXTRA_DATA.
60 // Be careful about how large reads you give in.
61 enum RequestParseStatus {
62 RP_OUT_OF_SPACE, // If larger than 16 kB.
63 RP_NOT_FINISHED_YET, // Did not get \r\n\r\n yet.
64 RP_EXTRA_DATA, // Got \r\n\r\n, but there was extra data behind it.
65 RP_FINISHED, // Ended exactly in \r\n\r\n.
67 RequestParseStatus wait_for_double_newline(std::string *existing_data, const char *new_data, size_t new_data_size);
69 #endif // !defined(_PARSE_H)