OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // The rules for parsing content-types were borrowed from Firefox: | 5 // The rules for parsing content-types were borrowed from Firefox: |
6 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 | 6 // http://lxr.mozilla.org/mozilla/source/netwerk/base/src/nsURLHelper.cpp#834 |
7 | 7 |
8 #include "net/http/http_util.h" | 8 #include "net/http/http_util.h" |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 return false; | 305 return false; |
306 } | 306 } |
307 | 307 |
308 if (interval < base::TimeDelta::FromSeconds(0)) | 308 if (interval < base::TimeDelta::FromSeconds(0)) |
309 return false; | 309 return false; |
310 | 310 |
311 *retry_after = interval; | 311 *retry_after = interval; |
312 return true; | 312 return true; |
313 } | 313 } |
314 | 314 |
315 // static | |
316 bool HttpUtil::HasHeader(const std::string& headers, const char* name) { | |
317 size_t name_len = strlen(name); | |
318 std::string::const_iterator it = | |
319 std::search(headers.begin(), | |
320 headers.end(), | |
321 name, | |
322 name + name_len, | |
323 base::CaseInsensitiveCompareASCII<char>()); | |
324 if (it == headers.end()) | |
325 return false; | |
326 | |
327 // ensure match is prefixed by newline | |
328 if (it != headers.begin() && it[-1] != '\n') | |
329 return false; | |
330 | |
331 // ensure match is suffixed by colon | |
332 if (it + name_len >= headers.end() || it[name_len] != ':') | |
333 return false; | |
334 | |
335 return true; | |
336 } | |
337 | |
338 namespace { | 315 namespace { |
| 316 |
339 // A header string containing any of the following fields will cause | 317 // A header string containing any of the following fields will cause |
340 // an error. The list comes from the XMLHttpRequest standard. | 318 // an error. The list comes from the XMLHttpRequest standard. |
341 // http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method | 319 // http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader-method |
342 const char* const kForbiddenHeaderFields[] = { | 320 const char* const kForbiddenHeaderFields[] = { |
343 "accept-charset", | 321 "accept-charset", |
344 "accept-encoding", | 322 "accept-encoding", |
345 "access-control-request-headers", | 323 "access-control-request-headers", |
346 "access-control-request-method", | 324 "access-control-request-method", |
347 "connection", | 325 "connection", |
348 "content-length", | 326 "content-length", |
349 "cookie", | 327 "cookie", |
350 "cookie2", | 328 "cookie2", |
351 "content-transfer-encoding", | 329 "content-transfer-encoding", |
352 "date", | 330 "date", |
353 "expect", | 331 "expect", |
354 "host", | 332 "host", |
355 "keep-alive", | 333 "keep-alive", |
356 "origin", | 334 "origin", |
357 "referer", | 335 "referer", |
358 "te", | 336 "te", |
359 "trailer", | 337 "trailer", |
360 "transfer-encoding", | 338 "transfer-encoding", |
361 "upgrade", | 339 "upgrade", |
362 "user-agent", | 340 "user-agent", |
363 "via", | 341 "via", |
364 }; | 342 }; |
365 } // anonymous namespace | 343 |
| 344 } // namespace |
366 | 345 |
367 // static | 346 // static |
368 bool HttpUtil::IsSafeHeader(const std::string& name) { | 347 bool HttpUtil::IsSafeHeader(const std::string& name) { |
369 std::string lower_name(base::ToLowerASCII(name)); | 348 std::string lower_name(base::ToLowerASCII(name)); |
370 if (base::StartsWith(lower_name, "proxy-", base::CompareCase::SENSITIVE) || | 349 if (base::StartsWith(lower_name, "proxy-", base::CompareCase::SENSITIVE) || |
371 base::StartsWith(lower_name, "sec-", base::CompareCase::SENSITIVE)) | 350 base::StartsWith(lower_name, "sec-", base::CompareCase::SENSITIVE)) |
372 return false; | 351 return false; |
373 | 352 |
374 for (const char* field : kForbiddenHeaderFields) { | 353 for (const char* field : kForbiddenHeaderFields) { |
375 if (lower_name == field) | 354 if (lower_name == field) |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(), | 753 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(), |
775 qvalue10); | 754 qvalue10); |
776 } | 755 } |
777 // It does not make sense to have 'q=0'. | 756 // It does not make sense to have 'q=0'. |
778 if (qvalue10 > kQvalueDecrement10) | 757 if (qvalue10 > kQvalueDecrement10) |
779 qvalue10 -= kQvalueDecrement10; | 758 qvalue10 -= kQvalueDecrement10; |
780 } | 759 } |
781 return lang_list_with_q; | 760 return lang_list_with_q; |
782 } | 761 } |
783 | 762 |
784 void HttpUtil::AppendHeaderIfMissing(const char* header_name, | |
785 const std::string& header_value, | |
786 std::string* headers) { | |
787 if (header_value.empty()) | |
788 return; | |
789 if (HttpUtil::HasHeader(*headers, header_name)) | |
790 return; | |
791 *headers += std::string(header_name) + ": " + header_value + "\r\n"; | |
792 } | |
793 | |
794 bool HttpUtil::HasStrongValidators(HttpVersion version, | 763 bool HttpUtil::HasStrongValidators(HttpVersion version, |
795 const std::string& etag_header, | 764 const std::string& etag_header, |
796 const std::string& last_modified_header, | 765 const std::string& last_modified_header, |
797 const std::string& date_header) { | 766 const std::string& date_header) { |
798 if (!HasValidators(version, etag_header, last_modified_header)) | 767 if (!HasValidators(version, etag_header, last_modified_header)) |
799 return false; | 768 return false; |
800 | 769 |
801 if (version < HttpVersion(1, 1)) | 770 if (version < HttpVersion(1, 1)) |
802 return false; | 771 return false; |
803 | 772 |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1073 return true; | 1042 return true; |
1074 } | 1043 } |
1075 | 1044 |
1076 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const { | 1045 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const { |
1077 if (strict_quotes_) | 1046 if (strict_quotes_) |
1078 return c == '"'; | 1047 return c == '"'; |
1079 return HttpUtil::IsQuote(c); | 1048 return HttpUtil::IsQuote(c); |
1080 } | 1049 } |
1081 | 1050 |
1082 } // namespace net | 1051 } // namespace net |
OLD | NEW |