]> git.sesse.net Git - cubemap/blob - parse.h
Move stopping into ServerPool.
[cubemap] / parse.h
1 #ifndef _PARSE_H
2 #define _PARSE_H
3
4 // Various routines that deal with parsing; both configuration files and HTTP requests.
5
6 #include <map>
7 #include <vector>
8 #include <string>
9
10 struct ConfigLine {
11         std::string keyword;
12         std::vector<std::string> arguments;
13         std::map<std::string, std::string> parameters;
14 };
15
16 // Split a line on whitespace, e.g. "foo  bar baz" -> {"foo", "bar", "baz"}.
17 std::vector<std::string> split_tokens(const std::string &line);
18
19 // Split a string on \n or \r\n, e.g. "foo\nbar\r\n\nbaz\r\n\r\n" -> {"foo", "bar", "baz"}.
20 std::vector<std::string> split_lines(const std::string &str);
21
22 // Parse the configuration file.
23 std::vector<ConfigLine> parse_config(const std::string &filename);
24
25 enum ParameterType {
26         PARAMETER_OPTIONAL,
27         PARAMATER_MANDATORY,
28 };
29
30 std::string fetch_config_string(const std::vector<ConfigLine> &config, const std::string &keyword,
31                                 ParameterType parameter_type, const std::string &default_value = "");
32
33 // Note: Limits are inclusive.
34 int fetch_config_int(const std::vector<ConfigLine> &config, const std::string &keyword,
35                      int min_limit, int max_limit,
36                      ParameterType parameter_type, int default_value = -1);
37
38 // Add the new data to an existing string, looking for \r\n\r\n
39 // (typical of HTTP requests and/or responses). Will return one
40 // of the given statuses.
41 //
42 // Note that if you give too much data in new_data_size, you could
43 // get an RP_OUT_OF_SPACE even if you expected RP_EXTRA_DATA.
44 // Be careful about how large reads you give in.
45 enum RequestParseStatus {
46         RP_OUT_OF_SPACE,       // If larger than 16 kB.
47         RP_NOT_FINISHED_YET,   // Did not get \r\n\r\n yet. 
48         RP_EXTRA_DATA,         // Got \r\n\r\n, but there was extra data behind it.
49         RP_FINISHED,           // Ended exactly in \r\n\r\n.
50 };
51 RequestParseStatus wait_for_double_newline(std::string *existing_data, const char *new_data, size_t new_data_size);
52
53 #endif  // !defined(_PARSE_H)