mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 672448 - Allow the Accept-Language header to have quality ('q') values up to 2 decimal places. r=biesi
This commit is contained in:
parent
98660593a8
commit
0b0f9f7fde
@ -203,8 +203,8 @@ PRTimeToSeconds(PRTime t_usec)
|
|||||||
|
|
||||||
#define NowInSeconds() PRTimeToSeconds(PR_Now())
|
#define NowInSeconds() PRTimeToSeconds(PR_Now())
|
||||||
|
|
||||||
// round q-value to one decimal place; return most significant digit as uint.
|
// Round q-value to 2 decimal places; return 2 most significant digits as uint.
|
||||||
#define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.05) * 10.0))
|
#define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.005) * 100.0))
|
||||||
|
|
||||||
#define HTTP_LWS " \t"
|
#define HTTP_LWS " \t"
|
||||||
#define HTTP_HEADER_VALUE_SEPS HTTP_LWS ","
|
#define HTTP_HEADER_VALUE_SEPS HTTP_LWS ","
|
||||||
|
@ -1221,7 +1221,7 @@ PrepareAcceptLanguages(const char *i_AcceptLanguages, nsACString &o_AcceptLangua
|
|||||||
if (!i_AcceptLanguages)
|
if (!i_AcceptLanguages)
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
|
|
||||||
uint32_t n, size, wrote;
|
uint32_t n, count_n, size, wrote;
|
||||||
double q, dec;
|
double q, dec;
|
||||||
char *p, *p2, *token, *q_Accept, *o_Accept;
|
char *p, *p2, *token, *q_Accept, *o_Accept;
|
||||||
const char *comma;
|
const char *comma;
|
||||||
@ -1244,7 +1244,7 @@ PrepareAcceptLanguages(const char *i_AcceptLanguages, nsACString &o_AcceptLangua
|
|||||||
*q_Accept = '\0';
|
*q_Accept = '\0';
|
||||||
q = 1.0;
|
q = 1.0;
|
||||||
dec = q / (double) n;
|
dec = q / (double) n;
|
||||||
n = 0;
|
count_n = 0;
|
||||||
p2 = q_Accept;
|
p2 = q_Accept;
|
||||||
for (token = nsCRT::strtok(o_Accept, ",", &p);
|
for (token = nsCRT::strtok(o_Accept, ",", &p);
|
||||||
token != (char *) 0;
|
token != (char *) 0;
|
||||||
@ -1257,12 +1257,28 @@ PrepareAcceptLanguages(const char *i_AcceptLanguages, nsACString &o_AcceptLangua
|
|||||||
*trim = '\0';
|
*trim = '\0';
|
||||||
|
|
||||||
if (*token != '\0') {
|
if (*token != '\0') {
|
||||||
comma = n++ != 0 ? "," : ""; // delimiter if not first item
|
comma = count_n++ != 0 ? "," : ""; // delimiter if not first item
|
||||||
uint32_t u = QVAL_TO_UINT(q);
|
uint32_t u = QVAL_TO_UINT(q);
|
||||||
if (u < 10)
|
|
||||||
wrote = PR_snprintf(p2, available, "%s%s;q=0.%u", comma, token, u);
|
// Only display q-value if less than 1.00.
|
||||||
else
|
if (u < 100) {
|
||||||
|
const char *qval_str;
|
||||||
|
|
||||||
|
// With a small number of languages, one decimal place is enough to prevent duplicate q-values.
|
||||||
|
// Also, trailing zeroes do not add any information, so they can be removed.
|
||||||
|
if ((n < 10) || ((u % 10) == 0)) {
|
||||||
|
u = (u + 5) / 10;
|
||||||
|
qval_str = "%s%s;q=0.%u";
|
||||||
|
} else {
|
||||||
|
// Values below 10 require zero padding.
|
||||||
|
qval_str = "%s%s;q=0.%02u";
|
||||||
|
}
|
||||||
|
|
||||||
|
wrote = PR_snprintf(p2, available, qval_str, comma, token, u);
|
||||||
|
} else {
|
||||||
wrote = PR_snprintf(p2, available, "%s%s", comma, token);
|
wrote = PR_snprintf(p2, available, "%s%s", comma, token);
|
||||||
|
}
|
||||||
|
|
||||||
q -= dec;
|
q -= dec;
|
||||||
p2 += wrote;
|
p2 += wrote;
|
||||||
available -= wrote;
|
available -= wrote;
|
||||||
|
88
netwerk/test/unit/test_header_Accept-Language.js
Normal file
88
netwerk/test/unit/test_header_Accept-Language.js
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
//
|
||||||
|
// HTTP Accept-Language header test
|
||||||
|
//
|
||||||
|
|
||||||
|
const Cc = Components.classes;
|
||||||
|
const Ci = Components.interfaces;
|
||||||
|
|
||||||
|
var testpath = "/bug672448";
|
||||||
|
|
||||||
|
function run_test() {
|
||||||
|
let intlPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).getBranch("intl.");
|
||||||
|
|
||||||
|
// Save old value of preference for later.
|
||||||
|
let oldPref = intlPrefs.getCharPref("accept_languages");
|
||||||
|
|
||||||
|
// Test different numbers of languages, to test different fractions.
|
||||||
|
let acceptLangTests = [
|
||||||
|
"qaa", // 1
|
||||||
|
"qaa,qab", // 2
|
||||||
|
"qaa,qab,qac,qad", // 4
|
||||||
|
"qaa,qab,qac,qad,qae,qaf,qag,qah", // 8
|
||||||
|
"qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj", // 10
|
||||||
|
"qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak", // 11
|
||||||
|
"qaa,qab,qac,qad,qae,qaf,qag,qah,qai,qaj,qak,qal,qam,qan,qao,qap,qaq,qar,qas,qat,qau", // 21
|
||||||
|
oldPref, // Restore old value of preference (and test it).
|
||||||
|
];
|
||||||
|
|
||||||
|
let acceptLangTestsNum = acceptLangTests.length;
|
||||||
|
|
||||||
|
for (let i = 0; i < acceptLangTestsNum; i++) {
|
||||||
|
// Set preference to test value.
|
||||||
|
intlPrefs.setCharPref("accept_languages", acceptLangTests[i]);
|
||||||
|
|
||||||
|
// Test value.
|
||||||
|
test_accepted_languages();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_accepted_languages() {
|
||||||
|
let channel = setupChannel(testpath);
|
||||||
|
|
||||||
|
let AcceptLanguage = channel.getRequestHeader("Accept-Language");
|
||||||
|
|
||||||
|
let acceptedLanguages = AcceptLanguage.split(",");
|
||||||
|
|
||||||
|
let acceptedLanguagesLength = acceptedLanguages.length;
|
||||||
|
|
||||||
|
for (let i = 0; i < acceptedLanguagesLength; i++) {
|
||||||
|
let acceptedLanguage, qualityValue;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// The q-value must conform to the definition in HTTP/1.1 Section 3.9.
|
||||||
|
[_, acceptedLanguage, qualityValue] = acceptedLanguages[i].trim().match(/^([a-z0-9_-]*?)(?:;q=(1(?:\.0{0,3})?|0(?:\.[0-9]{0,3})))?$/i);
|
||||||
|
} catch(e) {
|
||||||
|
do_throw("Invalid language tag or quality value: " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
// The first language shouldn't have a quality value.
|
||||||
|
do_check_eq(qualityValue, undefined);
|
||||||
|
} else {
|
||||||
|
let decimalPlaces;
|
||||||
|
|
||||||
|
// When the number of languages is small, we keep the quality value to only one decimal place.
|
||||||
|
// Otherwise, it can be up to two decimal places.
|
||||||
|
if (acceptedLanguagesLength < 10) {
|
||||||
|
do_check_true(qualityValue.length == 3);
|
||||||
|
|
||||||
|
decimalPlaces = 1;
|
||||||
|
} else {
|
||||||
|
do_check_true(qualityValue.length >= 3);
|
||||||
|
do_check_true(qualityValue.length <= 4);
|
||||||
|
|
||||||
|
decimalPlaces = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// All the other languages should have an evenly-spaced quality value.
|
||||||
|
do_check_eq(parseFloat(qualityValue).toFixed(decimalPlaces), (1.0 - ((1 / acceptedLanguagesLength) * i)).toFixed(decimalPlaces));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupChannel(path) {
|
||||||
|
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
||||||
|
let chan = ios.newChannel("http://localhost:4444" + path, "", null);
|
||||||
|
chan.QueryInterface(Ci.nsIHttpChannel);
|
||||||
|
return chan;
|
||||||
|
}
|
@ -132,6 +132,7 @@ skip-if = os == "android"
|
|||||||
[test_gre_resources.js]
|
[test_gre_resources.js]
|
||||||
[test_gzipped_206.js]
|
[test_gzipped_206.js]
|
||||||
[test_head.js]
|
[test_head.js]
|
||||||
|
[test_header_Accept-Language.js]
|
||||||
[test_headers.js]
|
[test_headers.js]
|
||||||
[test_http_headers.js]
|
[test_http_headers.js]
|
||||||
[test_httpcancel.js]
|
[test_httpcancel.js]
|
||||||
|
Loading…
Reference in New Issue
Block a user