diff options
author | Pale Moon <git-repo@palemoon.org> | 2018-04-10 21:16:06 +0200 |
---|---|---|
committer | Pale Moon <git-repo@palemoon.org> | 2018-04-10 21:16:06 +0200 |
commit | d968d8787b4f9ddea86e82c1770f8b45193c3d99 (patch) | |
tree | 3909248788475429d009901d5c769031374bfaf2 | |
parent | b3fb8ab8c471736bf64c10b9b33b1716992559f6 (diff) | |
download | palemoon-gre-d968d8787b4f9ddea86e82c1770f8b45193c3d99.tar.gz |
Use correct handling of author-supplied charsets in XMLHttpRequest
This resolves #1436 (and potentially other websites with inexplicable "it doesn't work" bugs).
-rw-r--r-- | dom/base/nsXMLHttpRequest.cpp | 32 |
1 files changed, 27 insertions, 5 deletions
diff --git a/dom/base/nsXMLHttpRequest.cpp b/dom/base/nsXMLHttpRequest.cpp index 4d2f892b0..2cd4760db 100644 --- a/dom/base/nsXMLHttpRequest.cpp +++ b/dom/base/nsXMLHttpRequest.cpp @@ -2845,6 +2845,13 @@ nsXMLHttpRequest::Send(nsIVariant* aVariant, const Nullable<RequestBody>& aBody) contentType)) || contentType.IsEmpty()) { contentType = defaultContentType; + + if (!charset.IsEmpty()) { + // If we are providing the default content type, then we also need to + // provide a charset declaration. + contentType.Append(NS_LITERAL_CSTRING(";charset=")); + contentType.Append(charset); + } } // We don't want to set a charset for streams. @@ -2855,7 +2862,7 @@ nsXMLHttpRequest::Send(nsIVariant* aVariant, const Nullable<RequestBody>& aBody) rv = NS_ExtractCharsetFromContentType(contentType, specifiedCharset, &haveCharset, &charsetStart, &charsetEnd); - if (NS_SUCCEEDED(rv)) { + while (NS_SUCCEEDED(rv) && haveCharset) { // special case: the extracted charset is quoted with single quotes // -- for the purpose of preserving what was set we want to handle // them as delimiters (although they aren't really) @@ -2875,11 +2882,26 @@ nsXMLHttpRequest::Send(nsIVariant* aVariant, const Nullable<RequestBody>& aBody) // table, hence might be differently cased). if (!specifiedCharset.Equals(charset, nsCaseInsensitiveCStringComparator())) { - nsAutoCString newCharset("; charset="); - newCharset.Append(charset); - contentType.Replace(charsetStart, charsetEnd - charsetStart, - newCharset); + // Find the start of the actual charset declaration, skipping the + // "; charset=" to avoid modifying whitespace. + int32_t charIdx = + Substring(contentType, charsetStart, + charsetEnd - charsetStart).FindChar('=') + 1; + MOZ_ASSERT(charIdx != -1); + + contentType.Replace(charsetStart + charIdx, + charsetEnd - charsetStart - charIdx, + charset); } + + // Look for another charset declaration in the string, limiting the + // search to only look for charsets before the current charset, to + // prevent finding the same charset twice. + nsDependentCSubstring interestingSection = + Substring(contentType, 0, charsetStart); + rv = NS_ExtractCharsetFromContentType(interestingSection, + specifiedCharset, &haveCharset, + &charsetStart, &charsetEnd); } } |