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> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/stringprintf.h" | |
15 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
16 #include "base/string_piece.h" | 15 #include "base/string_piece.h" |
17 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/stringprintf.h" |
| 18 #include "base/strings/string_tokenizer.h" |
18 #include "base/time.h" | 19 #include "base/time.h" |
19 | 20 |
20 using std::string; | 21 using std::string; |
21 | 22 |
22 namespace net { | 23 namespace net { |
23 | 24 |
24 //----------------------------------------------------------------------------- | 25 //----------------------------------------------------------------------------- |
25 | 26 |
26 // Return the index of the closing quote of the string, if any. | 27 // Return the index of the closing quote of the string, if any. |
27 static size_t FindStringEnd(const string& line, size_t start, char delim) { | 28 static size_t FindStringEnd(const string& line, size_t start, char delim) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if (string::npos == type_end) | 107 if (string::npos == type_end) |
107 type_end = content_type_str.length(); | 108 type_end = content_type_str.length(); |
108 | 109 |
109 size_t charset_val = 0; | 110 size_t charset_val = 0; |
110 size_t charset_end = 0; | 111 size_t charset_end = 0; |
111 bool type_has_charset = false; | 112 bool type_has_charset = false; |
112 | 113 |
113 // Iterate over parameters | 114 // Iterate over parameters |
114 size_t param_start = content_type_str.find_first_of(';', type_end); | 115 size_t param_start = content_type_str.find_first_of(';', type_end); |
115 if (param_start != string::npos) { | 116 if (param_start != string::npos) { |
116 StringTokenizer tokenizer(begin + param_start, content_type_str.end(), | 117 base::StringTokenizer tokenizer(begin + param_start, content_type_str.end(), |
117 ";"); | 118 ";"); |
118 tokenizer.set_quote_chars("\""); | 119 tokenizer.set_quote_chars("\""); |
119 while (tokenizer.GetNext()) { | 120 while (tokenizer.GetNext()) { |
120 string::const_iterator equals_sign = | 121 string::const_iterator equals_sign = |
121 std::find(tokenizer.token_begin(), tokenizer.token_end(), '='); | 122 std::find(tokenizer.token_begin(), tokenizer.token_end(), '='); |
122 if (equals_sign == tokenizer.token_end()) | 123 if (equals_sign == tokenizer.token_end()) |
123 continue; | 124 continue; |
124 | 125 |
125 string::const_iterator param_name_begin = tokenizer.token_begin(); | 126 string::const_iterator param_name_begin = tokenizer.token_begin(); |
126 string::const_iterator param_name_end = equals_sign; | 127 string::const_iterator param_name_end = equals_sign; |
127 TrimLWS(¶m_name_begin, ¶m_name_end); | 128 TrimLWS(¶m_name_begin, ¶m_name_end); |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 | 594 |
594 // Copy the status line. | 595 // Copy the status line. |
595 const char* status_line_end = FindStatusLineEnd(input_begin, input_end); | 596 const char* status_line_end = FindStatusLineEnd(input_begin, input_end); |
596 raw_headers.append(input_begin, status_line_end); | 597 raw_headers.append(input_begin, status_line_end); |
597 | 598 |
598 // After the status line, every subsequent line is a header line segment. | 599 // After the status line, every subsequent line is a header line segment. |
599 // Should a segment start with LWS, it is a continuation of the previous | 600 // Should a segment start with LWS, it is a continuation of the previous |
600 // line's field-value. | 601 // line's field-value. |
601 | 602 |
602 // TODO(ericroman): is this too permissive? (delimits on [\r\n]+) | 603 // TODO(ericroman): is this too permissive? (delimits on [\r\n]+) |
603 CStringTokenizer lines(status_line_end, input_end, "\r\n"); | 604 base::CStringTokenizer lines(status_line_end, input_end, "\r\n"); |
604 | 605 |
605 // This variable is true when the previous line was continuable. | 606 // This variable is true when the previous line was continuable. |
606 bool prev_line_continuable = false; | 607 bool prev_line_continuable = false; |
607 | 608 |
608 while (lines.GetNext()) { | 609 while (lines.GetNext()) { |
609 const char* line_begin = lines.token_begin(); | 610 const char* line_begin = lines.token_begin(); |
610 const char* line_end = lines.token_end(); | 611 const char* line_end = lines.token_end(); |
611 | 612 |
612 if (prev_line_continuable && IsLWS(*line_begin)) { | 613 if (prev_line_continuable && IsLWS(*line_begin)) { |
613 // Join continuation; reduce the leading LWS to a single SP. | 614 // Join continuation; reduce the leading LWS to a single SP. |
(...skipping 18 matching lines...) Expand all Loading... |
632 // them as line breaks. | 633 // them as line breaks. |
633 raw_headers.erase(std::remove(raw_headers.begin(), raw_headers.end(), '\0'), | 634 raw_headers.erase(std::remove(raw_headers.begin(), raw_headers.end(), '\0'), |
634 raw_headers.end()); | 635 raw_headers.end()); |
635 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0'); | 636 std::replace(raw_headers.begin(), raw_headers.end(), '\n', '\0'); |
636 | 637 |
637 return raw_headers; | 638 return raw_headers; |
638 } | 639 } |
639 | 640 |
640 std::string HttpUtil::ConvertHeadersBackToHTTPResponse(const std::string& str) { | 641 std::string HttpUtil::ConvertHeadersBackToHTTPResponse(const std::string& str) { |
641 std::string disassembled_headers; | 642 std::string disassembled_headers; |
642 StringTokenizer tokenizer(str, std::string(1, '\0')); | 643 base::StringTokenizer tokenizer(str, std::string(1, '\0')); |
643 while (tokenizer.GetNext()) { | 644 while (tokenizer.GetNext()) { |
644 disassembled_headers.append(tokenizer.token_begin(), tokenizer.token_end()); | 645 disassembled_headers.append(tokenizer.token_begin(), tokenizer.token_end()); |
645 disassembled_headers.append("\r\n"); | 646 disassembled_headers.append("\r\n"); |
646 } | 647 } |
647 disassembled_headers.append("\r\n"); | 648 disassembled_headers.append("\r\n"); |
648 | 649 |
649 return disassembled_headers; | 650 return disassembled_headers; |
650 } | 651 } |
651 | 652 |
652 // TODO(jungshik): 1. If the list is 'fr-CA,fr-FR,en,de', we have to add | 653 // TODO(jungshik): 1. If the list is 'fr-CA,fr-FR,en,de', we have to add |
653 // 'fr' after 'fr-CA' with the same q-value as 'fr-CA' because | 654 // 'fr' after 'fr-CA' with the same q-value as 'fr-CA' because |
654 // web servers, in general, do not fall back to 'fr' and may end up picking | 655 // web servers, in general, do not fall back to 'fr' and may end up picking |
655 // 'en' which has a lower preference than 'fr-CA' and 'fr-FR'. | 656 // 'en' which has a lower preference than 'fr-CA' and 'fr-FR'. |
656 // 2. This function assumes that the input is a comma separated list | 657 // 2. This function assumes that the input is a comma separated list |
657 // without any whitespace. As long as it comes from the preference and | 658 // without any whitespace. As long as it comes from the preference and |
658 // a user does not manually edit the preference file, it's the case. Still, | 659 // a user does not manually edit the preference file, it's the case. Still, |
659 // we may have to make it more robust. | 660 // we may have to make it more robust. |
660 std::string HttpUtil::GenerateAcceptLanguageHeader( | 661 std::string HttpUtil::GenerateAcceptLanguageHeader( |
661 const std::string& raw_language_list) { | 662 const std::string& raw_language_list) { |
662 // We use integers for qvalue and qvalue decrement that are 10 times | 663 // We use integers for qvalue and qvalue decrement that are 10 times |
663 // larger than actual values to avoid a problem with comparing | 664 // larger than actual values to avoid a problem with comparing |
664 // two floating point numbers. | 665 // two floating point numbers. |
665 const unsigned int kQvalueDecrement10 = 2; | 666 const unsigned int kQvalueDecrement10 = 2; |
666 unsigned int qvalue10 = 10; | 667 unsigned int qvalue10 = 10; |
667 StringTokenizer t(raw_language_list, ","); | 668 base::StringTokenizer t(raw_language_list, ","); |
668 std::string lang_list_with_q; | 669 std::string lang_list_with_q; |
669 while (t.GetNext()) { | 670 while (t.GetNext()) { |
670 std::string language = t.token(); | 671 std::string language = t.token(); |
671 if (qvalue10 == 10) { | 672 if (qvalue10 == 10) { |
672 // q=1.0 is implicit. | 673 // q=1.0 is implicit. |
673 lang_list_with_q = language; | 674 lang_list_with_q = language; |
674 } else { | 675 } else { |
675 DCHECK_LT(qvalue10, 10U); | 676 DCHECK_LT(qvalue10, 10U); |
676 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(), | 677 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(), |
677 qvalue10); | 678 qvalue10); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 value_is_quoted_ = true; | 921 value_is_quoted_ = true; |
921 // Do not store iterators into this. See declaration of unquoted_value_. | 922 // Do not store iterators into this. See declaration of unquoted_value_. |
922 unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_); | 923 unquoted_value_ = HttpUtil::Unquote(value_begin_, value_end_); |
923 } | 924 } |
924 } | 925 } |
925 | 926 |
926 return true; | 927 return true; |
927 } | 928 } |
928 | 929 |
929 } // namespace net | 930 } // namespace net |
OLD | NEW |