]> git.sesse.net Git - cubemap/blob - parse.cpp
Move Server:add_data() into Stream, where it more logically belongs.
[cubemap] / parse.cpp
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <string>
5 #include <vector>
6
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 #define MAX_REQUEST_SIZE 16384  /* 16 kB. */
60
61 RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
62 {
63         // Guard against overlong requests gobbling up all of our space.
64         if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
65                 return RP_OUT_OF_SPACE;
66         }       
67
68         // See if we have \r\n\r\n anywhere in the request. We start three bytes
69         // before what we just appended, in case we just got the final character.
70         size_t existing_data_bytes = existing_data->size();
71         existing_data->append(string(new_data, new_data + new_data_size));
72         
73         const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
74         const char *ptr = reinterpret_cast<char *>(
75                 memmem(existing_data->data() + start_at, existing_data->size() - start_at,
76                        "\r\n\r\n", 4));
77         if (ptr == NULL) {
78                 return RP_NOT_FINISHED_YET;
79         }
80         if (ptr != existing_data->data() + existing_data->size() - 4) {
81                 return RP_EXTRA_DATA;
82         }
83         return RP_FINISHED;
84 }