OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "native_client/src/trusted/plugin/nacl_http_response_headers.h" | |
6 | |
7 #include <algorithm> | |
8 #include <sstream> | |
9 | |
10 namespace { | |
11 | |
12 void SplitString(const std::string& str, | |
13 char delim, | |
14 std::vector<std::string>* elems, | |
15 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.
| |
16 std::stringstream ss(str); | |
17 std::string item; | |
18 int num = 0; | |
19 while (std::getline(ss, item, delim)) { | |
20 ++num; | |
21 if (limit == 0 || num < limit) { | |
22 elems->push_back(item); | |
23 } else { | |
24 // Loop through the rest, slap back on the delim in case | |
25 // we ate it earlier. We can't just use "ss >> rest" | |
26 // because the operator>> also tokenizes. | |
27 std::string more; | |
28 while (std::getline(ss, more, delim)) { | |
29 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
| |
30 item += more; | |
31 } | |
32 elems->push_back(item); | |
33 return; | |
34 } | |
35 } | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 namespace plugin { | |
41 | |
42 NaClHttpResponseHeaders::NaClHttpResponseHeaders() {} | |
43 | |
44 NaClHttpResponseHeaders::~NaClHttpResponseHeaders() {} | |
45 | |
46 void NaClHttpResponseHeaders::Parse(std::string headers_str) { | |
47 // PPAPI response headers are \n delimited. Separate out the lines. | |
48 std::vector<std::string> lines; | |
49 SplitString(headers_str, '\n', &lines, 0); | |
50 | |
51 for (size_t i = 0; i < lines.size(); ++i) { | |
52 std::vector<std::string> tokens; | |
53 // Split along the key-value pair separator char. | |
54 SplitString(lines[i], ':', &tokens, 2); | |
55 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
| |
56 // Ignore funny header lines that don't have the key-value separator. | |
57 continue; | |
58 std::string key = tokens[0]; | |
59 // Also ignore keys that start with white-space (they are invalid). | |
60 // See: HttpResponseHeadersTest.NormalizeHeadersLeadingWhitespace. | |
61 if (key[0] == ' ' || key[0] == '\t') | |
62 continue; | |
63 // Strip trailing whitespace from the key to normalize. | |
64 size_t pos = key.find_last_not_of(" \t"); | |
65 if (pos != std::string::npos) | |
66 key.erase(pos + 1); | |
67 // Strip leading whitespace from the value to normalize. | |
68 std::string value = tokens[1]; | |
69 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
| |
70 header_entries_.push_back(Entry(key, value)); | |
71 } | |
72 } | |
73 | |
74 std::string NaClHttpResponseHeaders::GetCacheValidators() { | |
75 std::string result; | |
76 for (size_t i = 0; i < header_entries_.size(); ++i) { | |
77 const Entry& entry = header_entries_[i]; | |
78 std::string key = entry.first; | |
79 std::transform(key.begin(), key.end(), key.begin(), ::tolower); | |
80 if (key.compare("etag") == 0 || key.compare("last-modified") == 0) { | |
81 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.
| |
82 result += "&"; | |
83 // Return the original headers, not the tolower ones. | |
84 result += entry.first + ":" + entry.second; | |
85 } | |
86 } | |
87 return result; | |
88 } | |
89 | |
90 bool NaClHttpResponseHeaders::CacheControlNoStore() { | |
91 for (size_t i = 0; i < header_entries_.size(); ++i) { | |
92 const Entry& entry = header_entries_[i]; | |
93 std::string key = entry.first; | |
94 std::transform(key.begin(), key.end(), key.begin(), ::tolower); | |
95 if (key.compare("cache-control") == 0) { | |
96 std::string cc = entry.second; | |
97 std::transform(cc.begin(), cc.end(), cc.begin(), ::tolower); | |
98 if (entry.second.find("no-store") != std::string::npos) | |
99 return true; | |
100 } | |
101 } | |
102 return false; | |
103 } | |
104 | |
105 } // namespace plugin | |
OLD | NEW |