37b5436bd6d7bc82cd7890e0082ad6a26543f727
[cubemap] / parse.cpp
1 #include <ctype.h>
2 #include <string.h>
3 #include <string>
4 #include <vector>
5
6 #include "log.h"
7 #include "parse.h"
8
9 using namespace std;
10
11 vector<string> split_tokens(const string &line)
12 {
13         vector<string> ret;
14         string current_token;
15
16         for (size_t i = 0; i < line.size(); ++i) {
17                 if (isspace(line[i])) {
18                         if (!current_token.empty()) {
19                                 ret.push_back(current_token);
20                         }
21                         current_token.clear();
22                 } else {
23                         current_token.push_back(line[i]);
24                 }
25         }
26         if (!current_token.empty()) {
27                 ret.push_back(current_token);
28         }
29         return ret;
30 }
31
32 vector<string> split_lines(const string &str)
33 {
34         vector<string> ret;
35         string current_line;
36
37         for (size_t i = 0; i < str.size(); ++i) {
38                 // Skip \r if followed by an \n.
39                 if (str[i] == '\r' && i < str.size() - 1 && str[i + 1] == '\n') {
40                         continue;
41                 }
42
43                 // End of the current line?
44                 if (str[i] == '\n') {
45                         if (!current_line.empty()) {
46                                 ret.push_back(current_line);
47                         }
48                         current_line.clear();
49                 } else {
50                         current_line.push_back(str[i]);
51                 }
52         }
53         if (!current_line.empty()) {
54                 ret.push_back(current_line);
55         }
56         return ret;
57 }
58
59 multimap<string, string> extract_headers(const vector<string> &lines, const string &log_context)
60 {
61         multimap<string, string> parameters;
62         for (size_t i = 1; i < lines.size(); ++i) {
63                 size_t split = lines[i].find(":");
64                 if (split == string::npos) {
65                         log(WARNING, "[%s] Ignoring malformed HTTP response line '%s'",
66                                 log_context.c_str(), lines[i].c_str());
67                         continue;
68                 }
69
70                 string key(lines[i].begin(), lines[i].begin() + split);
71
72                 // Skip any spaces after the colon.
73                 do {
74                         ++split;
75                 } while (split < lines[i].size() && (lines[i][split] == ' ' || lines[i][split] == '\t'));
76
77                 string value(lines[i].begin() + split, lines[i].end());
78
79                 parameters.insert(make_pair(key, value));
80         }
81
82         return parameters;
83 }
84
85 #define MAX_REQUEST_SIZE 16384  /* 16 kB. */
86
87 RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
88 {
89         // Guard against overlong requests gobbling up all of our space.
90         if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
91                 return RP_OUT_OF_SPACE;
92         }       
93
94         // See if we have \r\n\r\n anywhere in the request. We start three bytes
95         // before what we just appended, in case we just got the final character.
96         size_t existing_data_bytes = existing_data->size();
97         existing_data->append(string(new_data, new_data + new_data_size));
98         
99         const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
100         const char *ptr = reinterpret_cast<char *>(
101                 memmem(existing_data->data() + start_at, existing_data->size() - start_at,
102                        "\r\n\r\n", 4));
103         if (ptr == NULL) {
104                 return RP_NOT_FINISHED_YET;
105         }
106         if (ptr != existing_data->data() + existing_data->size() - 4) {
107                 return RP_EXTRA_DATA;
108         }
109         return RP_FINISHED;
110 }