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

Unified Diff: net/http/http_util.cc

Issue 17340: Add q-values to languages in Accept-Language HTTP header to be compatible wit... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 11 months 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 side-by-side diff with in-line comments
Download patch
Index: net/http/http_util.cc
===================================================================
--- net/http/http_util.cc (revision 7989)
+++ net/http/http_util.cc (working copy)
@@ -57,7 +57,7 @@
DCHECK(url.is_valid() && (url.SchemeIs("http") || url.SchemeIs("https")));
if (url.has_query())
return url.path() + "?" + url.query();
- return url.path();
+ return url.path();
}
// static
@@ -430,7 +430,7 @@
while (lines.GetNext()) {
const char* line_begin = lines.token_begin();
const char* line_end = lines.token_end();
-
+
if (prev_line_continuable && IsLWS(*line_begin)) {
// Join continuation; reduce the leading LWS to a single SP.
raw_headers.push_back(' ');
@@ -451,6 +451,50 @@
return raw_headers;
}
+// TODO(jungshik): 1. If the list is 'fr-CA,fr-FR,en,de', we have to add
+// 'fr' after 'fr-CA' with the same q-value as 'fr-CA' because
+// web servers, in general, do not fall back to 'fr' and may end up picking
+// 'en' which has a lower preference than 'fr-CA' and 'fr-FR'.
+// 2. This function assumes that the input is a comma separated list
+// without any whitespace. As long as it comes from the preference and
+// a user does not manually edit the preference file, it's the case. Still,
+// we may have to make it more robust.
+std::string HttpUtil::GenerateAcceptLanguageHeader(
+ const std::string& raw_language_list) {
+ // We use integers for qvalue and qvalue decrement that are 10 times
+ // larger than actual values to avoid a problem with comparing
+ // two floating point numbers.
+ const unsigned int kQvalueDecrement10 = 2;
+ unsigned int qvalue10 = 10;
+ StringTokenizer t(raw_language_list, ",");
+ std::string lang_list_with_q;
+ while (t.GetNext()) {
+ std::string language = t.token();
+ if (qvalue10 == 10) {
+ // q=1.0 is implicit.
+ lang_list_with_q = language;
+ } else {
+ DCHECK(qvalue10 >= 0 && qvalue10 < 10);
+ StringAppendF(&lang_list_with_q, ",%s;q=0.%d", language.c_str(),
+ qvalue10);
+ }
+ // It does not make sense to have 'q=0'.
+ if (qvalue10 > kQvalueDecrement10)
+ qvalue10 -= kQvalueDecrement10;
+ }
+ return lang_list_with_q;
+}
+
+std::string HttpUtil::GenerateAcceptCharsetHeader(const std::string& charset) {
+ std::string charset_with_q = charset;
+ if (LowerCaseEqualsASCII(charset, "utf-8")) {
+ charset_with_q += ",*;q=0.5";
+ } else {
+ charset_with_q += ",utf-8;q=0.7,*;q=0.3";
+ }
+ return charset_with_q;
+}
+
// BNF from section 4.2 of RFC 2616:
//
// message-header = field-name ":" [ field-value ]
« net/http/http_util.h ('K') | « net/http/http_util.h ('k') | net/http/http_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698