Add a way to extract the charset and the position of the charset parameter from a content-type header. Use this in XMLHttpRequest to not clobber the non-charset parameters and not clobber the charset if it's already what we'll set, up to case. Apparently some broken servers are case-sensitive when looking at charsets! Bug 393968 (wherein the patch lies) and bug 397234, r+sr=biesi
authorbzbarsky@mit.edu
Mon, 03 Dec 2007 13:34:44 -0800
changeset 8603 06e20efee31f98dc9a7b2cea0959fa1fbdf54660
parent 8602 368618a98a1454990bc44ac7cc7e1fbacb2dab37
child 8604 9580784554c0108261a6efe6b961314bd8b91747
push idunknown
push userunknown
push dateunknown
bugs393968, 397234
milestone1.9b2pre
Add a way to extract the charset and the position of the charset parameter from a content-type header. Use this in XMLHttpRequest to not clobber the non-charset parameters and not clobber the charset if it's already what we'll set, up to case. Apparently some broken servers are case-sensitive when looking at charsets! Bug 393968 (wherein the patch lies) and bug 397234, r+sr=biesi
content/base/src/nsXMLHttpRequest.cpp
content/base/test/Makefile.in
content/base/test/test_bug393968.html
content/base/test/test_bug397234.html
netwerk/base/public/nsINetUtil.idl
netwerk/base/public/nsNetUtil.h
netwerk/base/src/nsIOService.cpp
netwerk/base/src/nsURLHelper.cpp
netwerk/base/src/nsURLHelper.h
netwerk/test/unit/test_extract_charset_from_content_type.js
--- a/content/base/src/nsXMLHttpRequest.cpp
+++ b/content/base/src/nsXMLHttpRequest.cpp
@@ -1961,25 +1961,46 @@ nsXMLHttpRequest::Send(nsIVariant *aBody
                       GetRequestHeader(NS_LITERAL_CSTRING("Content-Type"),
                                        contentType)) ||
           contentType.IsEmpty()) {
         contentType = NS_LITERAL_CSTRING("application/xml");
       }
 
       // We don't want to set a charset for streams.
       if (!charset.IsEmpty()) {
-        nsCAutoString actualType, dummy;
-        rv = NS_ParseContentType(contentType, actualType, dummy);
+        nsCAutoString specifiedCharset;
+        PRBool haveCharset;
+        PRInt32 charsetStart, charsetEnd;
+        rv = NS_ExtractCharsetFromContentType(contentType, specifiedCharset,
+                                              &haveCharset, &charsetStart,
+                                              &charsetEnd);
         if (NS_FAILED(rv)) {
-          actualType.AssignLiteral("application/xml");
+          contentType.AssignLiteral("application/xml");
+          specifiedCharset.Truncate();
+          haveCharset = PR_FALSE;
         }
 
-        contentType.Assign(actualType);
-        contentType.AppendLiteral(";charset=");
-        contentType.Append(charset);
+        if (!haveCharset) {
+          charsetStart = charsetEnd = contentType.Length();
+        } 
+
+        // If the content-type the page set already has a charset parameter,
+        // and it's the same charset, up to case, as |charset|, just send the
+        // page-set content-type header.  Apparently at least
+        // google-web-toolkit is broken and relies on the exact case of its
+        // charset parameter, which makes things break if we use |charset|
+        // (which is always a fully resolved charset per our charset alias
+        // table, hence might be differently cased).
+        if (!specifiedCharset.Equals(charset,
+                                     nsCaseInsensitiveCStringComparator())) {
+          nsCAutoString newCharset("; charset=");
+          newCharset.Append(charset);
+          contentType.Replace(charsetStart, charsetEnd - charsetStart,
+                              newCharset);
+        }
       }
 
       rv = uploadChannel->SetUploadStream(postDataStream, contentType, -1);
       // Reset the method to its original value
       if (httpChannel) {
         httpChannel->SetRequestMethod(method);
       }
     }
--- a/content/base/test/Makefile.in
+++ b/content/base/test/Makefile.in
@@ -86,17 +86,19 @@ include $(topsrcdir)/config/rules.mk
 		test_bug372086.html \
 		test_bug373181.xhtml \
 		test_bug375314.html \
 		test_bug382113.html \
 		test_bug390219.html \
 		test_bug390735.html \
 		test_bug392318.html \
 		test_bug392511.html \
+		test_bug393968.html \
 		test_bug395915.html \
+		test_bug397234.html \
 		test_bug398243.html \
 		formReset.html \
 		bug382113_object.html \
 		test_CrossSiteXHR.html \
 		file_CrossSiteXHR_fail1.xml \
 		file_CrossSiteXHR_fail2.xml \
 		file_CrossSiteXHR_fail2.xml^headers^ \
 		file_CrossSiteXHR_fail3.xml \
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug393968.html
@@ -0,0 +1,38 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=393968
+-->
+<head>
+  <title>Test for Bug 393968</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=393968">Mozilla Bug 393968</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 393968 **/
+var req = new XMLHttpRequest();
+req.open("POST", window.location.href);
+req.setRequestHeader("Content-Type", "text/plain; charset=us-ascii; boundary=01234567890");
+req.send("Some text");
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+is(req.channel
+      .QueryInterface(Components.interfaces.nsIHttpChannel)
+      .getRequestHeader("Content-Type"),
+   "text/plain; charset=UTF-8; boundary=01234567890",
+   "Headers should match");
+
+</script>
+</pre>
+</body>
+</html>
+
new file mode 100644
--- /dev/null
+++ b/content/base/test/test_bug397234.html
@@ -0,0 +1,39 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=397234
+-->
+<head>
+  <title>Test for Bug 397234</title>
+  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=397234">Mozilla Bug 397234</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/** Test for Bug 397234 **/
+var req = new XMLHttpRequest();
+req.open("POST", window.location.href);
+// Capitalization of charet param is on purpose!
+req.setRequestHeader("Content-Type", "text/plain; charset='uTf-8'");
+req.send("Some text");
+
+netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+is(req.channel
+      .QueryInterface(Components.interfaces.nsIHttpChannel)
+      .getRequestHeader("Content-Type"),
+   "text/plain; charset='uTf-8'",
+   "Headers should match");
+
+</script>
+</pre>
+</body>
+</html>
+
--- a/netwerk/base/public/nsINetUtil.idl
+++ b/netwerk/base/public/nsINetUtil.idl
@@ -40,17 +40,17 @@
 
 #include "nsISupports.idl"
 
 interface nsIURI;
 
 /**
  * nsINetUtil provides various network-related utility methods.
  */
-[scriptable, uuid(fbbd8771-8059-4269-8352-a6f2ca6b3c7b)]
+[scriptable, uuid(57322c6f-f4ec-4e46-8253-b74be220de16)]
 interface nsINetUtil : nsISupports
 {
   /**
    * Parse a content-type header and return the content type and
    * charset (if any).
    *
    * @param aTypeHeader the header string to parse
    * @param [out] aCharset the charset parameter specified in the
@@ -188,9 +188,34 @@ interface nsINetUtil : nsISupports
    * @param aStr the URL to be unescaped
    * @param aFlags only ESCAPE_URL_ONLY_NONASCII and ESCAPE_URL_SKIP_CONTROL
    *               are recognized.  If |aFlags| is 0 all escape sequences are 
    *               unescaped
    * @return unescaped string
    */
   ACString unescapeString(in ACString aStr, in unsigned long aFlags);
 
+  /**
+   * Extract the charset parameter location and value from a content-type
+   * header.
+   *
+   * @param aTypeHeader the header string to parse
+   * @param [out] aCharset the charset parameter specified in the
+   *              header, if any.
+   * @param [out] aCharsetStart index of the start of the charset parameter
+   *              (the ';' separating it from what came before) in aTypeHeader.
+   *              This is only set if this function returns true.
+   * @param [out] aCharsetEnd index of the end of the charset parameter (the
+   *              ';' separating it from what comes after, or inded of the end
+   *              of the string) in aTypeHeader.  This is only set if this
+   *              function returns true.
+   *
+   * @return whether a charset parameter was found.  This can be false even in
+   * cases when parseContentType would claim to have a charset, if the type
+   * that won out does not have a charset parameter specified, because in this
+   * case setting the charset does in fact correspond to appending to the
+   * string.
+   */
+  boolean extractCharsetFromContentType(in AUTF8String aTypeHeader,
+                                        out AUTF8String aCharset,
+                                        out long aCharsetStart,
+                                        out long aCharsetEnd);
 };
--- a/netwerk/base/public/nsNetUtil.h
+++ b/netwerk/base/public/nsNetUtil.h
@@ -787,16 +787,35 @@ NS_ParseContentType(const nsACString &ra
     rv = util->ParseContentType(rawContentType, charset, &hadCharset,
                                 contentType);
     if (NS_SUCCEEDED(rv) && hadCharset)
         contentCharset = charset;
     return rv;
 }
 
 inline nsresult
+NS_ExtractCharsetFromContentType(const nsACString &rawContentType,
+                                 nsCString        &contentCharset,
+                                 PRBool           *hadCharset,
+                                 PRInt32          *charsetStart,
+                                 PRInt32          *charsetEnd)
+{
+    // contentCharset is left untouched if not present in rawContentType
+    nsresult rv;
+    nsCOMPtr<nsINetUtil> util = do_GetIOService(&rv);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    return util->ExtractCharsetFromContentType(rawContentType,
+                                               contentCharset,
+                                               charsetStart,
+                                               charsetEnd,
+                                               hadCharset);
+}
+
+inline nsresult
 NS_NewLocalFileInputStream(nsIInputStream **result,
                            nsIFile         *file,
                            PRInt32          ioFlags       = -1,
                            PRInt32          perm          = -1,
                            PRInt32          behaviorFlags = 0)
 {
     nsresult rv;
     nsCOMPtr<nsIFileInputStream> in =
--- a/netwerk/base/src/nsIOService.cpp
+++ b/netwerk/base/src/nsIOService.cpp
@@ -962,8 +962,24 @@ nsIOService::UnescapeString(const nsACSt
                             PRUint32 aFlags, nsACString &aResult)
 {
   aResult.Truncate();
   PRBool unescaped = NS_UnescapeURL(aStr.BeginReading(), aStr.Length(), 
                                     aFlags | esc_AlwaysCopy, aResult);
   return NS_OK;
 }
 
+NS_IMETHODIMP
+nsIOService::ExtractCharsetFromContentType(const nsACString &aTypeHeader,
+                                           nsACString &aCharset,
+                                           PRInt32 *aCharsetStart,
+                                           PRInt32 *aCharsetEnd,
+                                           PRBool *aHadCharset)
+{
+    nsCAutoString ignored;
+    net_ParseContentType(aTypeHeader, ignored, aCharset, aHadCharset,
+                         aCharsetStart, aCharsetEnd);
+    if (*aHadCharset && *aCharsetStart == -1) {
+        *aHadCharset = PR_FALSE;
+    }
+    return NS_OK;
+}
+
--- a/netwerk/base/src/nsURLHelper.cpp
+++ b/netwerk/base/src/nsURLHelper.cpp
@@ -739,34 +739,41 @@ net_FindMediaDelimiter(const nsCString& 
         // string, so just go back to the top of the loop and look for
         // |delimiter| again.
     } while (PR_TRUE);
 
     NS_NOTREACHED("How did we get here?");
     return flatStr.Length();
 }
 
+// aOffset should be added to aCharsetStart and aCharsetEnd if this
+// function sets them.
 static void
 net_ParseMediaType(const nsACString &aMediaTypeStr,
                    nsACString       &aContentType,
                    nsACString       &aContentCharset,
-                   PRBool           *aHadCharset)
+                   PRInt32          aOffset,
+                   PRBool           *aHadCharset,
+                   PRInt32          *aCharsetStart,
+                   PRInt32          *aCharsetEnd)
 {
     const nsCString& flatStr = PromiseFlatCString(aMediaTypeStr);
     const char* start = flatStr.get();
     const char* end = start + flatStr.Length();
 
     // Trim LWS leading and trailing whitespace from type.  We include '(' in
     // the trailing trim set to catch media-type comments, which are not at all
     // standard, but may occur in rare cases.
     const char* type = net_FindCharNotInSet(start, end, HTTP_LWS);
     const char* typeEnd = net_FindCharInSet(type, end, HTTP_LWS ";(");
 
     const char* charset = "";
     const char* charsetEnd = charset;
+    PRInt32 charsetParamStart;
+    PRInt32 charsetParamEnd;
 
     // Iterate over parameters
     PRBool typeHasCharset = PR_FALSE;
     PRUint32 paramStart = flatStr.FindChar(';', typeEnd - start);
     if (paramStart != PRUint32(kNotFound)) {
         // We have parameters.  Iterate over them.
         PRUint32 curParamStart = paramStart + 1;
         do {
@@ -777,16 +784,18 @@ net_ParseMediaType(const nsACString &aMe
                                                          start + curParamEnd,
                                                          HTTP_LWS);
             static const char charsetStr[] = "charset=";
             if (PL_strncasecmp(paramName, charsetStr,
                                sizeof(charsetStr) - 1) == 0) {
                 charset = paramName + sizeof(charsetStr) - 1;
                 charsetEnd = start + curParamEnd;
                 typeHasCharset = PR_TRUE;
+                charsetParamStart = curParamStart - 1;
+                charsetParamEnd = curParamEnd;
             }
 
             curParamStart = curParamEnd + 1;
         } while (curParamStart < flatStr.Length());
     }
 
     if (typeHasCharset) {
         // Trim LWS leading and trailing whitespace from charset.  We include
@@ -819,28 +828,48 @@ net_ParseMediaType(const nsACString &aMe
                                 nsCaseInsensitiveCStringComparator());
         if (!eq) {
             aContentType.Assign(type, typeEnd - type);
             ToLowerCase(aContentType);
         }
         if ((!eq && *aHadCharset) || typeHasCharset) {
             *aHadCharset = PR_TRUE;
             aContentCharset.Assign(charset, charsetEnd - charset);
+            if (typeHasCharset) {
+                *aCharsetStart = charsetParamStart + aOffset;
+                *aCharsetEnd = charsetParamEnd + aOffset;
+            } else {
+                *aCharsetStart = -1;
+                *aCharsetEnd = -1;
+            }
         }
     }
 }
 
 #undef HTTP_LWS
 
 void
 net_ParseContentType(const nsACString &aHeaderStr,
                      nsACString       &aContentType,
                      nsACString       &aContentCharset,
                      PRBool           *aHadCharset)
 {
+    PRInt32 dummy1, dummy2;
+    net_ParseContentType(aHeaderStr, aContentType, aContentCharset,
+                         aHadCharset, &dummy1, &dummy2);
+}
+
+void
+net_ParseContentType(const nsACString &aHeaderStr,
+                     nsACString       &aContentType,
+                     nsACString       &aContentCharset,
+                     PRBool           *aHadCharset,
+                     PRInt32          *aCharsetStart,
+                     PRInt32          *aCharsetEnd)
+{
     //
     // Augmented BNF (from RFC 2616 section 3.7):
     //
     //   header-value = media-type *( LWS "," LWS media-type )
     //   media-type   = type "/" subtype *( LWS ";" LWS parameter )
     //   type         = token
     //   subtype      = token
     //   parameter    = attribute "=" value
@@ -870,17 +899,18 @@ net_ParseContentType(const nsACString &a
         // to look for its end.
         PRUint32 curTypeEnd =
             net_FindMediaDelimiter(flatStr, curTypeStart, ',');
         
         // At this point curTypeEnd points to the spot where the media-type
         // starting at curTypeEnd ends.  Time to parse that!
         net_ParseMediaType(Substring(flatStr, curTypeStart,
                                      curTypeEnd - curTypeStart),
-                           aContentType, aContentCharset, aHadCharset);
+                           aContentType, aContentCharset, curTypeStart,
+                           aHadCharset, aCharsetStart, aCharsetEnd);
 
         // And let's move on to the next media-type
         curTypeStart = curTypeEnd + 1;
     } while (curTypeStart < flatStr.Length());
 }
 
 PRBool
 net_IsValidHostName(const nsCSubstring &host)
--- a/netwerk/base/src/nsURLHelper.h
+++ b/netwerk/base/src/nsURLHelper.h
@@ -198,16 +198,31 @@ NS_HIDDEN_(char *) net_RFindCharNotInSet
  * specified), aHadCharset is set to false.  Otherwise, it's set to
  * true.  Note that aContentCharset can be empty even if aHadCharset
  * is true.
  */
 NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
                                       nsACString       &aContentType,
                                       nsACString       &aContentCharset,
                                       PRBool*          aHadCharset);
+/**
+ * As above, but also returns the start and end indexes for the charset
+ * parameter in aHeaderStr.  These are indices for the entire parameter, NOT
+ * just the value.  If there is "effectively" no charset parameter (e.g. if an
+ * earlier type with one is overridden by a later type without one),
+ * *aHadCharset will be true but *aCharsetStart will be set to -1.  Note that
+ * it's possible to have aContentCharset empty and *aHadCharset true when
+ * *aCharsetStart is nonnegative; this corresponds to charset="".
+ */
+NS_HIDDEN_(void) net_ParseContentType(const nsACString &aHeaderStr,
+                                      nsACString       &aContentType,
+                                      nsACString       &aContentCharset,
+                                      PRBool           *aHadCharset,
+                                      PRInt32          *aCharsetStart,
+                                      PRInt32          *aCharsetEnd);
 
 /* inline versions */
 
 /* remember the 64-bit platforms ;-) */
 #define NET_MAX_ADDRESS (((char*)0)-1)
 
 inline char *net_FindCharInSet(const char *str, const char *set)
 {
new file mode 100644
--- /dev/null
+++ b/netwerk/test/unit/test_extract_charset_from_content_type.js
@@ -0,0 +1,196 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is mozilla.org code.
+ *
+ * The Initial Developer of the Original Code is the Mozilla Corporation.
+ * Portions created by the Initial Developer are Copyright (C) 2007
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ *  Boris Zbarsky <bzbarsky@mit.edu>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * use your version of this file under the terms of the MPL, indicate your
+ * decision by deleting the provisions above and replace them with the notice
+ * and other provisions required by the GPL or the LGPL. If you do not delete
+ * the provisions above, a recipient may use your version of this file under
+ * the terms of any one of the MPL, the GPL or the LGPL.
+ *
+ * ***** END LICENSE BLOCK ***** */
+
+var charset = {};
+var charsetStart = {};
+var charsetEnd = {};
+var hadCharset;
+
+function reset() {
+  delete charset.value;
+  delete charsetStart.value;
+  delete charsetEnd.value;
+  hadCharset = undefined;
+}
+
+function check(aHadCharset, aCharset, aCharsetStart, aCharsetEnd) {
+  do_check_eq(aHadCharset, hadCharset);
+  do_check_eq(aCharset, charset.value);
+  if (hadCharset) {
+    do_check_eq(aCharsetStart, charsetStart.value);
+    do_check_eq(aCharsetEnd, charsetEnd.value);
+  }
+}
+
+function run_test() {
+  var netutil = Components.classes["@mozilla.org/network/util;1"]
+                          .getService(Components.interfaces.nsINetUtil);
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html", charset, charsetStart,
+					  charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("TEXT/HTML", charset, charsetStart,
+					  charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html, text/html", charset,
+					  charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html, text/plain",
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/html, ', charset, charsetStart,
+					  charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/html, */*', charset,
+					  charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/html, foo', charset,
+					  charsetStart, charsetEnd);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset=ISO-8859-1",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 9, 29);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html  ;    charset=ISO-8859-1",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 11, 34);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html  ;    charset=ISO-8859-1  ",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 11, 36);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html  ;    charset=ISO-8859-1 ; ",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 11, 35);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/html; charset="ISO-8859-1"',
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 9, 31);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset='ISO-8859-1'",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 9, 31);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset='ISO-8859-1', text/html",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 9, 31);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset='ISO-8859-1', text/html; charset=UTF8",
+					  charset, charsetStart, charsetEnd);
+  check(true, "UTF8", 42, 56);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset=ISO-8859-1, TEXT/HTML",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 9, 29);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/html; charset=ISO-8859-1, TEXT/plain",
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/plain, TEXT/HTML; charset='ISO-8859-1', text/html, TEXT/HTML",
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 21, 43);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain, TEXT/HTML; param="charset=UTF8"; charset=\'ISO-8859-1\'; param2="charset=UTF16", text/html, TEXT/HTML',
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 43, 65);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain, TEXT/HTML; param=charset=UTF8; charset=\'ISO-8859-1\'; param2=charset=UTF16, text/html, TEXT/HTML',
+					  charset, charsetStart, charsetEnd);
+  check(true, "ISO-8859-1", 41, 63);
+
+  hadCharset =
+    netutil.extractCharsetFromContentType("text/plain; param= , text/html",
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain; param=", text/html"',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain; param=", \\" , text/html"',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain; param=", \\" , text/html , "',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain param=", \\" , text/html , "',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain charset=UTF8',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+
+  hadCharset =
+    netutil.extractCharsetFromContentType('text/plain, TEXT/HTML; param="charset=UTF8"; ; param2="charset=UTF16", text/html, TEXT/HTML',
+					  charset, charsetStart, charsetEnd);
+  check(false, "");
+}