Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(371)

Side by Side Diff: net/http/http_util.cc

Issue 2559243003: Extend with a language code in http accept languages
Patch Set: Rebased, fixed nits and handed over this CL :( Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/http/http_util.h ('k') | net/http/http_util_icu.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 803 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 base::StringTokenizer tokenizer(str, std::string(1, '\0')); 814 base::StringTokenizer tokenizer(str, std::string(1, '\0'));
815 while (tokenizer.GetNext()) { 815 while (tokenizer.GetNext()) {
816 disassembled_headers.append(tokenizer.token_begin(), tokenizer.token_end()); 816 disassembled_headers.append(tokenizer.token_begin(), tokenizer.token_end());
817 disassembled_headers.append("\r\n"); 817 disassembled_headers.append("\r\n");
818 } 818 }
819 disassembled_headers.append("\r\n"); 819 disassembled_headers.append("\r\n");
820 820
821 return disassembled_headers; 821 return disassembled_headers;
822 } 822 }
823 823
824 // TODO(jungshik): 1. If the list is 'fr-CA,fr-FR,en,de', we have to add
825 // 'fr' after 'fr-CA' with the same q-value as 'fr-CA' because
826 // web servers, in general, do not fall back to 'fr' and may end up picking
827 // 'en' which has a lower preference than 'fr-CA' and 'fr-FR'.
828 // 2. This function assumes that the input is a comma separated list
829 // without any whitespace. As long as it comes from the preference and
830 // a user does not manually edit the preference file, it's the case. Still,
831 // we may have to make it more robust.
832 std::string HttpUtil::GenerateAcceptLanguageHeader(
833 const std::string& raw_language_list) {
834 // We use integers for qvalue and qvalue decrement that are 10 times
835 // larger than actual values to avoid a problem with comparing
836 // two floating point numbers.
837 const unsigned int kQvalueDecrement10 = 2;
838 unsigned int qvalue10 = 10;
839 base::StringTokenizer t(raw_language_list, ",");
840 std::string lang_list_with_q;
841 while (t.GetNext()) {
842 std::string language = t.token();
843 if (qvalue10 == 10) {
844 // q=1.0 is implicit.
845 lang_list_with_q = language;
846 } else {
847 DCHECK_LT(qvalue10, 10U);
848 base::StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(),
849 qvalue10);
850 }
851 // It does not make sense to have 'q=0'.
852 if (qvalue10 > kQvalueDecrement10)
853 qvalue10 -= kQvalueDecrement10;
854 }
855 return lang_list_with_q;
856 }
857
858 void HttpUtil::AppendHeaderIfMissing(const char* header_name, 824 void HttpUtil::AppendHeaderIfMissing(const char* header_name,
859 const std::string& header_value, 825 const std::string& header_value,
860 std::string* headers) { 826 std::string* headers) {
861 if (header_value.empty()) 827 if (header_value.empty())
862 return; 828 return;
863 if (HttpUtil::HasHeader(*headers, header_name)) 829 if (HttpUtil::HasHeader(*headers, header_name))
864 return; 830 return;
865 *headers += std::string(header_name) + ": " + header_value + "\r\n"; 831 *headers += std::string(header_name) + ": " + header_value + "\r\n";
866 } 832 }
867 833
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 return true; 1113 return true;
1148 } 1114 }
1149 1115
1150 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const { 1116 bool HttpUtil::NameValuePairsIterator::IsQuote(char c) const {
1151 if (strict_quotes_) 1117 if (strict_quotes_)
1152 return c == '"'; 1118 return c == '"';
1153 return HttpUtil::IsQuote(c); 1119 return HttpUtil::IsQuote(c);
1154 } 1120 }
1155 1121
1156 } // namespace net 1122 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_util.h ('k') | net/http/http_util_icu.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698