Chromium Code Reviews| Index: ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.cc |
| diff --git a/ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.cc b/ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bfd5bd41bd05ed0f387881ff0e2ce4aeca0959cf |
| --- /dev/null |
| +++ b/ppapi/native_client/src/trusted/plugin/nacl_http_response_headers.cc |
| @@ -0,0 +1,105 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "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, |
| + int limit) { |
| + std::stringstream ss(str); |
| + std::string item; |
| + int num = 0; |
| + while(std::getline(ss, item, delim)) { |
|
Derek Schuff
2013/06/07 21:38:04
while (
actually, why not just run this whole file
jvoung (off chromium)
2013/06/07 23:46:25
Done.
|
| + ++num; |
| + if (limit == 0 || num < limit) { |
| + elems->push_back(item); |
| + } else { |
| + // Loop through the rest, slap back on the delim in case |
| + // we ate it earlier. We can't just use "ss >> rest" |
| + // because the operator>> also tokenizes. |
| + std::string more; |
| + while(std::getline(ss, more, delim)) { |
| + item.push_back(delim); |
| + item += more; |
| + } |
| + elems->push_back(item); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +namespace plugin { |
| + |
| +NaClHttpResponseHeaders::NaClHttpResponseHeaders() { } |
| + |
| +NaClHttpResponseHeaders::~NaClHttpResponseHeaders() { } |
| + |
| +void NaClHttpResponseHeaders::Parse(std::string headers_str) { |
|
Derek Schuff
2013/06/07 21:38:04
how much munging is done on this data before it ge
jvoung (off chromium)
2013/06/07 23:46:25
Hmm, it looks like ppb_url_response gets data from
|
| + // PPAPI response headers are \n delimited. Separate out the lines. |
| + std::vector<std::string> lines; |
| + SplitString(headers_str, '\n', &lines, 0); |
| + |
| + for (size_t i = 0; i < lines.size(); ++i) { |
| + std::vector<std::string> tokens; |
| + // Split along the key-value pair separator char. |
| + SplitString(lines[i], ':', &tokens, 2); |
| + if (tokens.size() != 2) |
| + // Ignore funny header lines that don't have the key-value separator. |
| + continue; |
| + std::string key = tokens[0]; |
| + // Also ignore keys that start with white-space (they are invalid). |
| + // See: HttpResponseHeadersTest.NormalizeHeadersLeadingWhitespace. |
| + if (key[0] == ' ' || key[0] == '\t') |
| + continue; |
| + // Strip trailing whitespace from the key to normalize. |
| + size_t pos = key.find_last_not_of(" \t"); |
| + if (pos != std::string::npos) |
| + key.erase(pos + 1); |
| + // Strip leading whitespace from the value to normalize. |
| + std::string value = tokens[1]; |
| + value.erase(0, value.find_first_not_of(" \t")); |
| + header_entries_.push_back(Entry(key, value)); |
| + } |
| +} |
| + |
| +std::string NaClHttpResponseHeaders::GetCacheValidators() { |
| + std::string result; |
| + 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("etag") == 0 || key.compare("last-modified") == 0) { |
| + if (!result.empty()) |
| + result += "&"; |
| + // Return the original headers, not the tolower ones. |
| + result += entry.first + ":" + entry.second; |
| + } |
| + } |
| + 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; |
| +} |
| + |
| +} // namespace plugin |