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