11 vector<string> split_tokens(const string &line)
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);
21 current_token.clear();
23 current_token.push_back(line[i]);
26 if (!current_token.empty()) {
27 ret.push_back(current_token);
32 vector<string> split_lines(const string &str)
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') {
43 // End of the current line?
45 if (!current_line.empty()) {
46 ret.push_back(current_line);
50 current_line.push_back(str[i]);
53 if (!current_line.empty()) {
54 ret.push_back(current_line);
59 HTTPHeaderMultimap extract_headers(const vector<string> &lines, const string &log_context)
61 HTTPHeaderMultimap parameters;
62 for (size_t i = 1; i < lines.size(); ++i) {
63 size_t split = lines[i].find(":");
64 if (split == string::npos) {
65 log(WARNING, "[%s] Ignoring malformed HTTP response line '%s'",
66 log_context.c_str(), lines[i].c_str());
70 string key(lines[i].begin(), lines[i].begin() + split);
72 // Skip any spaces after the colon.
75 } while (split < lines[i].size() && (lines[i][split] == ' ' || lines[i][split] == '\t'));
77 string value(lines[i].begin() + split, lines[i].end());
79 parameters.insert(make_pair(key, value));
85 #define MAX_REQUEST_SIZE 16384 /* 16 kB. */
87 RequestParseStatus wait_for_double_newline(string *existing_data, const char *new_data, size_t new_data_size)
89 // Guard against overlong requests gobbling up all of our space.
90 if (existing_data->size() + new_data_size > MAX_REQUEST_SIZE) {
91 return RP_OUT_OF_SPACE;
94 // See if we have \r\n\r\n anywhere in the request. We start three bytes
95 // before what we just appended, in case we just got the final character.
96 size_t existing_data_bytes = existing_data->size();
97 existing_data->append(string(new_data, new_data + new_data_size));
99 const size_t start_at = (existing_data_bytes >= 3 ? existing_data_bytes - 3 : 0);
100 const char *ptr = reinterpret_cast<char *>(
101 memmem(existing_data->data() + start_at, existing_data->size() - start_at,
103 if (ptr == nullptr) {
104 return RP_NOT_FINISHED_YET;
106 if (ptr != existing_data->data() + existing_data->size() - 4) {
107 return RP_EXTRA_DATA;