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..d10347138bc2cfd51bf5729c74d70ead45b99456 |
| --- /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) { |
|
dmichael (off chromium)
2013/06/14 16:21:43
Might be worth a TODO to use Tokenize from base/st
jvoung (off chromium)
2013/06/14 20:38:03
Done.
|
| + std::stringstream ss(str); |
| + std::string item; |
| + int num = 0; |
| + while (std::getline(ss, item, delim)) { |
| + ++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); |
|
dmichael (off chromium)
2013/06/14 16:21:43
nit: you may tack on the delimiter even if it wasn
jvoung (off chromium)
2013/06/14 20:38:03
I'm not sure if that's the case?
(1) item should
dmichael (off chromium)
2013/06/14 21:09:03
My bad, I was misunderstanding it. But I like it b
|
| + item += more; |
| + } |
| + elems->push_back(item); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +namespace plugin { |
| + |
| +NaClHttpResponseHeaders::NaClHttpResponseHeaders() {} |
| + |
| +NaClHttpResponseHeaders::~NaClHttpResponseHeaders() {} |
| + |
| +void NaClHttpResponseHeaders::Parse(std::string headers_str) { |
| + // 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) |
|
dmichael (off chromium)
2013/06/14 16:21:43
It actually seems to me that rather than make Spli
jvoung (off chromium)
2013/06/14 20:38:03
Sounds good, that will also make it easier to use
|
| + // 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")); |
|
dmichael (off chromium)
2013/06/14 16:21:43
The above could be replaced with TrimWhitespaceASC
jvoung (off chromium)
2013/06/14 20:38:03
Added a TODO. I think http headers are only allow
|
| + 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()) |
|
dmichael (off chromium)
2013/06/14 16:21:43
(could become LowerCaseEqualsASCII later)
jvoung (off chromium)
2013/06/14 20:38:03
Done.
|
| + 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 |