c14b47fdad06fd6ce76cea3d8409aaf36400a6e1
[cubemap] / parse.cpp
1 #include <ctype.h>
2 #include <string.h>
3 #include <string>
4 #include <vector>
5
6 #include "parse.h"
7
8 using namespace std;
9
10 vector<string> split_tokens(const string &line)
11 {
12         vector<string> ret;
13         string current_token;
14
15         for (size_t i = 0; i < line.size(); ++i) {
16                 if (isspace(line[i])) {
17                         if (!current_token.empty()) {
18                                 ret.push_back(current_token);
19                         }
20                         current_token.clear();
21                 } else {
22                         current_token.push_back(line[i]);
23                 }
24         }
25         if (!current_token.empty()) {
26                 ret.push_back(current_token);
27         }
28         return ret;
29 }
30
31 vector<string> split_lines(const string &str)
32 {
33         vector<string> ret;
34         string current_line;
35
36         for (size_t i = 0; i < str.size(); ++i) {
37                 // Skip \r if followed by an \n.
38                 if (str[i] == '\r' && i < str.size() - 1 && str[i + 1] == '\n') {
39                         continue;
40                 }
41
42                 // End of the current line?
43                 if (str[i] == '\n') {
44                         if (!current_line.empty()) {
45                                 ret.push_back(current_line);
46                         }
47                         current_line.clear();
48                 } else {
49                         current_line.push_back(str[i]);
50                 }
51         }
52         if (!current_line.empty()) {
53                 ret.push_back(current_line);
54         }
55         return ret;
56 }
57
58 #define MAX_REQUEST_SIZE 16384  /* 16 kB. */
59
60 RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
61 {
62         // Guard against overlong requests gobbling up all of our space.
63         if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
64                 return RP_OUT_OF_SPACE;
65         }       
66
67         // See if we have \r\n\r\n anywhere in the request. We start three bytes
68         // before what we just appended, in case we just got the final character.
69         size_t existing_data_bytes = existing_data->size();
70         existing_data->append(string(new_data, new_data + new_data_size));
71         
72         const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
73         const char *ptr = reinterpret_cast<char *>(
74                 memmem(existing_data->data() + start_at, existing_data->size() - start_at,
75                        "\r\n\r\n", 4));
76         if (ptr == NULL) {
77                 return RP_NOT_FINISHED_YET;
78         }
79         if (ptr != existing_data->data() + existing_data->size() - 4) {
80                 return RP_EXTRA_DATA;
81         }
82         return RP_FINISHED;
83 }