File: parse.cpp

package info (click to toggle)
cubemap 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 380 kB
  • ctags: 398
  • sloc: cpp: 3,895; sh: 114; perl: 86; makefile: 61
file content (83 lines) | stat: -rw-r--r-- 2,047 bytes parent folder | download | duplicates (2)
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
#include <ctype.h>
#include <string.h>
#include <string>
#include <vector>

#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;
}

#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;
}