File: parse.cpp

package info (click to toggle)
cubemap 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 428 kB
  • sloc: cpp: 4,431; sh: 114; perl: 102; makefile: 60
file content (110 lines) | stat: -rw-r--r-- 2,789 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <ctype.h>
#include <string.h>
#include <string>
#include <vector>

#include "log.h"
#include "parse.h"

using namespace std;

vector<string> split_tokens(const string &line)
{
	vector<string> ret;
	string current_token;

	for (size_t i = 0; i < line.size(); ++i) {
		if (isspace(line[i])) {
			if (!current_token.empty()) {
				ret.push_back(current_token);
			}
			current_token.clear();
		} else {
			current_token.push_back(line[i]);
		}
	}
	if (!current_token.empty()) {
		ret.push_back(current_token);
	}
	return ret;
}

vector<string> split_lines(const string &str)
{
	vector<string> ret;
	string current_line;

	for (size_t i = 0; i < str.size(); ++i) {
		// Skip \r if followed by an \n.
		if (str[i] == '\r' && i < str.size() - 1 && str[i + 1] == '\n') {
			continue;
		}

		// End of the current line?
		if (str[i] == '\n') {
			if (!current_line.empty()) {
				ret.push_back(current_line);
			}
			current_line.clear();
		} else {
			current_line.push_back(str[i]);
		}
	}
	if (!current_line.empty()) {
		ret.push_back(current_line);
	}
	return ret;
}

multimap<string, string> extract_headers(const vector<string> &lines, const string &log_context)
{
	multimap<string, string> parameters;
	for (size_t i = 1; i < lines.size(); ++i) {
		size_t split = lines[i].find(":");
		if (split == string::npos) {
			log(WARNING, "[%s] Ignoring malformed HTTP response line '%s'",
			        log_context.c_str(), lines[i].c_str());
			continue;
		}

		string key(lines[i].begin(), lines[i].begin() + split);

		// Skip any spaces after the colon.
		do {
			++split;
		} while (split < lines[i].size() && (lines[i][split] == ' ' || lines[i][split] == '\t'));

		string value(lines[i].begin() + split, lines[i].end());

		parameters.insert(make_pair(key, value));
	}

	return parameters;
}

#define MAX_REQUEST_SIZE 16384  /* 16 kB. */

RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
{
	// Guard against overlong requests gobbling up all of our space.
	if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
		return RP_OUT_OF_SPACE;
	}	

	// See if we have \r\n\r\n anywhere in the request. We start three bytes
	// before what we just appended, in case we just got the final character.
	size_t existing_data_bytes = existing_data->size();
	existing_data->append(string(new_data, new_data + new_data_size));
	
	const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
	const char *ptr = reinterpret_cast<char *>(
		memmem(existing_data->data() + start_at, existing_data->size() - start_at,
		       "\r\n\r\n", 4));
	if (ptr == NULL) {
		return RP_NOT_FINISHED_YET;
	}
	if (ptr != existing_data->data() + existing_data->size() - 4) {
		return RP_EXTRA_DATA;
	}
	return RP_FINISHED;
}