]> git.sesse.net Git - cubemap/blob - parse.h
a2e35802d38d31e804e4b16dcf1f948df6f7cfa7
[cubemap] / parse.h
1 #ifndef _PARSE_H
2 #define _PARSE_H
3
4 // Various routines that deal with parsing; both HTTP requests and more generic text.
5
6 #include <stddef.h>
7 #include <string>
8 #include <algorithm>
9 #include <string>
10 #include <unordered_map>
11 #include <vector>
12
13 // Locale-unaware tolower(); matches RFC 2616 no matter what the locale is set to.
14 static inline char ascii_tolower(const char ch)
15 {
16         if (ch >= 'A' && ch <= 'Z') {
17                 return ch + 'a' - 'A';
18         } else {
19                 return ch;
20         }
21 }
22
23 // Case-insensitive header comparison and hashing.
24 struct HTTPLess {
25         bool operator() (const std::string &a, const std::string &b) const
26         {
27                 return std::lexicographical_compare(
28                         begin(a), end(a), begin(b), end(b),
29                         [](char a, char b) {
30                                 return ascii_tolower(a) < ascii_tolower(b);
31                         });
32         }
33 };
34 struct HTTPHash {
35         size_t operator() (const std::string &s) const
36         {
37                 std::string s_low = s;
38                 for (char &ch : s_low) { ch = ascii_tolower(ch); }
39                 return std::hash<std::string>() (s_low);
40         }
41 };
42 using HTTPHeaderMultimap = std::unordered_multimap<std::string, std::string, HTTPHash, HTTPLess>;
43
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);
46
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);
49
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);
53
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.
57 //
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.
66 };
67 RequestParseStatus wait_for_double_newline(std::string *existing_data, const char *new_data, size_t new_data_size);
68
69 #endif  // !defined(_PARSE_H)