Bug 1488356 - Follow-up: nsSmtpService::NewURI() (for mailto: URLs) can ignore the charset passed in. r=mkmelin
authorJorg K <jorgk@jorgk.com>
Thu, 06 Sep 2018 08:50:32 +0200
changeset 33083 d1bb256c78f7a83615b4750f27c9bc62d9f83e2f
parent 33082 5b9052faa6b4bf08b693ef95eade5c1a60fed618
child 33084 7953345b93f62b54f93d4d8bcbc5464c9faa7c78
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersmkmelin
bugs1488356
Bug 1488356 - Follow-up: nsSmtpService::NewURI() (for mailto: URLs) can ignore the charset passed in. r=mkmelin
mailnews/compose/src/nsSmtpService.cpp
--- a/mailnews/compose/src/nsSmtpService.cpp
+++ b/mailnews/compose/src/nsSmtpService.cpp
@@ -20,17 +20,16 @@
 #include "nsCOMPtr.h"
 #include "nsIMsgIdentity.h"
 #include "nsIPrompt.h"
 #include "nsIWindowWatcher.h"
 #include "nsAutoPtr.h"
 #include "nsComposeStrings.h"
 #include "nsIAsyncInputStream.h"
 #include "nsIPrincipal.h"
-#include "mozilla/Encoding.h"
 
 #define SERVER_DELIMITER ','
 #define APPEND_SERVERS_VERSION_PREF_NAME "append_preconfig_smtpservers.version"
 #define MAIL_ROOT_PREF "mail."
 #define PREF_MAIL_SMTPSERVERS "mail.smtpservers"
 #define PREF_MAIL_SMTPSERVERS_APPEND_SERVERS "mail.smtpservers.appendsmtpservers"
 #define PREF_MAIL_SMTP_DEFAULTSERVER "mail.smtp.defaultserver"
 
@@ -283,112 +282,31 @@ nsSmtpService::AllowPort(int32_t port, c
 NS_IMETHODIMP nsSmtpService::GetProtocolFlags(uint32_t *result)
 {
     *result = URI_NORELATIVE | ALLOWS_PROXY | URI_LOADABLE_BY_ANYONE |
       URI_NON_PERSISTABLE | URI_DOES_NOT_RETURN_DATA |
       URI_FORBIDS_COOKIE_ACCESS;
     return NS_OK;
 }
 
-// Copied from M-C's nsMIMEHeaderParamImpl.cpp
-static nsresult
-ToUTF8(const nsACString& aString,
-       const char* aCharset,
-       bool aAllowSubstitution,
-       nsACString& aResult)
-{
-  if (!aCharset || !*aCharset)
-    return NS_ERROR_INVALID_ARG;
-
-  auto encoding = mozilla::Encoding::ForLabelNoReplacement(
-                    mozilla::MakeStringSpan(aCharset));
-  if (!encoding) {
-    return NS_ERROR_UCONV_NOCONV;
-  }
-  if (aAllowSubstitution) {
-    nsresult rv = encoding->DecodeWithoutBOMHandling(aString, aResult);
-    if (NS_SUCCEEDED(rv)) {
-      return NS_OK;
-    }
-    return rv;
-  }
-  return encoding->DecodeWithoutBOMHandlingAndWithoutReplacement(aString,
-                                                                 aResult);
-}
-
-// Copied from M-C's nsUTF8ConverterService.cpp after its removal in bug 1488115.
-static nsresult
-ConvertURISpecToUTF8(const nsACString& aSpec,
-                     const char* aCharset,
-                     nsACString& aUTF8Spec)
-{
-  // assume UTF-8 if the spec contains unescaped non-ASCII characters.
-  // No valid spec in Mozilla would break this assumption.
-  if (!IsASCII(aSpec)) {
-    aUTF8Spec = aSpec;
-    return NS_OK;
-  }
-
-  aUTF8Spec.Truncate();
-
-  nsAutoCString unescapedSpec;
-  // NS_UnescapeURL does not fill up unescapedSpec unless there's at least
-  // one character to unescape.
-  bool written = NS_UnescapeURL(PromiseFlatCString(aSpec).get(),
-                                aSpec.Length(),
-                                esc_OnlyNonASCII,
-                                unescapedSpec);
-
-  if (!written) {
-    aUTF8Spec = aSpec;
-    return NS_OK;
-  }
-  // return if ASCII only or escaped UTF-8
-  if (IsASCII(unescapedSpec) || IsUTF8(unescapedSpec)) {
-    aUTF8Spec = unescapedSpec;
-    return NS_OK;
-  }
-
-  return ToUTF8(unescapedSpec, aCharset, true, aUTF8Spec);
-}
-
 // the smtp service is also the protocol handler for mailto urls....
 
 NS_IMETHODIMP nsSmtpService::NewURI(const nsACString &aSpec,
-                                    const char *aOriginCharset,
+                                    const char *aOriginCharset,  // ignored, always UTF-8.
                                     nsIURI *aBaseURI,
                                     nsIURI **_retval)
 {
   // get a new smtp url
   nsresult rv;
 
-  // Note bug 1488356 comment #19:
-  // "A non-UTF-8 charset would be passed if the URL object is created because
-  // the URL string exists in a link in a non-UTF-8 HTML page."
-  // In other words: We need this code for mailto: URLs on non-UTF-8 HTML pages.
-  nsAutoCString utf8Spec;
-  if (aOriginCharset)
-  {
-    rv = ConvertURISpecToUTF8(aSpec, aOriginCharset, utf8Spec);
-  }
-
-  // utf8Spec is filled up only when aOriginCharset is specified and
-  // the conversion is successful. Otherwise, fall back to aSpec.
   nsCOMPtr<nsIURI> mailtoUrl;
-  if (aOriginCharset && NS_SUCCEEDED(rv)) {
-    rv = NS_MutateURI(new nsMailtoUrl::Mutator())
-           .SetSpec(utf8Spec)
-           .Finalize(mailtoUrl);
-    NS_ENSURE_SUCCESS(rv, rv);
-  } else {
-    rv = NS_MutateURI(new nsMailtoUrl::Mutator())
-           .SetSpec(aSpec)
-           .Finalize(mailtoUrl);
-    NS_ENSURE_SUCCESS(rv, rv);
-  }
+  rv = NS_MutateURI(new nsMailtoUrl::Mutator())
+         .SetSpec(aSpec)
+         .Finalize(mailtoUrl);
+  NS_ENSURE_SUCCESS(rv, rv);
 
   mailtoUrl.forget(_retval);
   return NS_OK;
 }
 
 NS_IMETHODIMP nsSmtpService::NewChannel(nsIURI *aURI, nsIChannel **_retval)
 {
   return NewChannel2(aURI, nullptr, _retval);