This source file includes following definitions.
- SplitString
- SplitOnce
- Parse
- GetHeader
- GetCacheValidators
- CacheControlNoStore
#include "ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.h"
#include <algorithm>
#include <sstream>
namespace {
void SplitString(const std::string& str,
char delim,
std::vector<std::string>* elems) {
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, delim)) {
elems->push_back(item);
}
}
bool SplitOnce(const std::string& str,
char delim,
std::vector<std::string>* elems) {
size_t pos = str.find(delim);
if (pos != std::string::npos) {
elems->push_back(str.substr(0, pos));
elems->push_back(str.substr(pos + 1));
return true;
}
return false;
}
}
namespace plugin {
NaClHttpResponseHeaders::NaClHttpResponseHeaders() {}
NaClHttpResponseHeaders::~NaClHttpResponseHeaders() {}
void NaClHttpResponseHeaders::Parse(const std::string& headers_str) {
std::vector<std::string> lines;
SplitString(headers_str, '\n', &lines);
for (size_t i = 0; i < lines.size(); ++i) {
std::vector<std::string> tokens;
if (!SplitOnce(lines[i], ':', &tokens)) {
continue;
}
std::string key = tokens[0];
if (key.length() == 0 || key[0] == ' ' || key[0] == '\t')
continue;
size_t pos = key.find_last_not_of(" \t");
if (pos != std::string::npos)
key.erase(pos + 1);
std::string value = tokens[1];
value.erase(0, value.find_first_not_of(" \t"));
header_entries_.push_back(Entry(key, value));
}
}
std::string NaClHttpResponseHeaders::GetHeader(const std::string& name) {
for (size_t i = 0; i < header_entries_.size(); ++i) {
const Entry& entry = header_entries_[i];
std::string key = entry.first;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
if (key.compare(name) == 0)
return entry.second;
}
return std::string();
}
std::string NaClHttpResponseHeaders::GetCacheValidators() {
std::string result = GetHeader("etag");
if (!result.empty())
result = "etag:" + result;
std::string lm = GetHeader("last-modified");
if (!lm.empty()) {
if (!result.empty())
result += "&";
result += "last-modified:" + lm;
}
return result;
}
bool NaClHttpResponseHeaders::CacheControlNoStore() {
for (size_t i = 0; i < header_entries_.size(); ++i) {
const Entry& entry = header_entries_[i];
std::string key = entry.first;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
if (key.compare("cache-control") == 0) {
std::string cc = entry.second;
std::transform(cc.begin(), cc.end(), cc.begin(), ::tolower);
if (entry.second.find("no-store") != std::string::npos)
return true;
}
}
return false;
}
}