From 35a707653465e4a7bcb7927e1f1eed281078c2f5 Mon Sep 17 00:00:00 2001 From: Jean-Yves Avenard Date: Mon, 6 Aug 2018 21:31:22 +0200 Subject: [PATCH] Bug 1481176 - P1. Export GetParameterHTTP as static method. r=valentin,r=bzbarsky nsContentTypeParser used internally a nsIMIMEHeaderParam reference, effectively limiting its use on the main thread, and as such restricting any methods handling mime type there too. nsContentTypeParser only made use of a single method nsMIMEHeaderParamImpl::GetParameterHTTP, so we make that method static and export it via nsNetUtil. Differential Revision: https://phabricator.services.mozilla.com/D2788 --- netwerk/base/nsNetUtil.cpp | 10 +++++++ netwerk/base/nsNetUtil.h | 18 +++++++++++++ netwerk/mime/nsMIMEHeaderParamImpl.cpp | 17 ++++++++++++ netwerk/mime/nsMIMEHeaderParamImpl.h | 37 ++++++++++++++++---------- 4 files changed, 68 insertions(+), 14 deletions(-) diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 926a38807a5d..98cbd6b3b412 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -80,6 +80,7 @@ #include "nsICertOverrideService.h" #include "nsQueryObject.h" #include "mozIThirdPartyUtil.h" +#include "../mime/nsMIMEHeaderParamImpl.h" #include @@ -3259,5 +3260,14 @@ InScriptableRange(uint64_t val) return val <= kJS_MAX_SAFE_UINTEGER; } +nsresult +GetParameterHTTP(const nsACString& aHeaderVal, + const char* aParamName, + nsAString& aResult) +{ + return nsMIMEHeaderParamImpl::GetParameterHTTP( + aHeaderVal, aParamName, aResult); +} + } // namespace net } // namespace mozilla diff --git a/netwerk/base/nsNetUtil.h b/netwerk/base/nsNetUtil.h index 2ec15332d3c6..05c0896fd61e 100644 --- a/netwerk/base/nsNetUtil.h +++ b/netwerk/base/nsNetUtil.h @@ -991,6 +991,24 @@ bool InScriptableRange(int64_t val); // Make sure a 64bit value can be captured by JS MAX_SAFE_INTEGER bool InScriptableRange(uint64_t val); +/** + * Given the value of a single header field (such as + * Content-Disposition and Content-Type) and the name of a parameter + * (e.g. filename, name, charset), returns the value of the parameter. + * See nsIMIMEHeaderParam.idl for more information. + * + * @param aHeaderVal a header string to get the value of a parameter + * from. + * @param aParamName the name of a MIME header parameter (e.g. + * filename, name, charset). If empty or nullptr, + * returns the first (possibly) _unnamed_ 'parameter'. + * @return the value of aParamName in Unichar(UTF-16). + */ +nsresult +GetParameterHTTP(const nsACString& aHeaderVal, + const char* aParamName, + nsAString& aResult); + } // namespace net } // namespace mozilla diff --git a/netwerk/mime/nsMIMEHeaderParamImpl.cpp b/netwerk/mime/nsMIMEHeaderParamImpl.cpp index c6f57f8bc9a0..0c9f77c2a0aa 100644 --- a/netwerk/mime/nsMIMEHeaderParamImpl.cpp +++ b/netwerk/mime/nsMIMEHeaderParamImpl.cpp @@ -64,7 +64,23 @@ nsMIMEHeaderParamImpl::GetParameterHTTP(const nsACString& aHeaderVal, aFallbackCharset, aTryLocaleCharset, aLang, aResult); } +/* static */ +nsresult +nsMIMEHeaderParamImpl::GetParameterHTTP(const nsACString& aHeaderVal, + const char *aParamName, + nsAString& aResult) +{ + return DoGetParameter(aHeaderVal, + aParamName, + HTTP_FIELD_ENCODING, + EmptyCString(), + false, + nullptr, + aResult); +} + // XXX : aTryLocaleCharset is not yet effective. +/* static */ nsresult nsMIMEHeaderParamImpl::DoGetParameter(const nsACString& aHeaderVal, const char *aParamName, @@ -355,6 +371,7 @@ nsMIMEHeaderParamImpl::GetParameterInternal(const char *aHeaderValue, } +/* static */ nsresult nsMIMEHeaderParamImpl::DoParameterInternal(const char *aHeaderValue, const char *aParamName, diff --git a/netwerk/mime/nsMIMEHeaderParamImpl.h b/netwerk/mime/nsMIMEHeaderParamImpl.h index 8ec8a70db33b..1789987125e3 100644 --- a/netwerk/mime/nsMIMEHeaderParamImpl.h +++ b/netwerk/mime/nsMIMEHeaderParamImpl.h @@ -14,6 +14,16 @@ public: NS_DECL_NSIMIMEHEADERPARAM nsMIMEHeaderParamImpl() = default; + + /** + * Identical to calling + * GetParameterHTTP(aHeaderVal, aParameterName, EmptyCString(), false, nullptr, aResult) + * See nsIMIMEHeaderParam.idl for more information. + */ + static nsresult GetParameterHTTP(const nsACString& aHeaderVal, + const char *aParamName, + nsAString &aResult); + private: virtual ~nsMIMEHeaderParamImpl() = default; enum ParamDecoding { @@ -21,21 +31,20 @@ private: HTTP_FIELD_ENCODING }; - nsresult DoGetParameter(const nsACString& aHeaderVal, - const char *aParamName, - ParamDecoding aDecoding, - const nsACString& aFallbackCharset, - bool aTryLocaleCharset, - char **aLang, - nsAString& aResult); - - nsresult DoParameterInternal(const char *aHeaderValue, - const char *aParamName, - ParamDecoding aDecoding, - char **aCharset, - char **aLang, - char **aResult); + static nsresult DoGetParameter(const nsACString& aHeaderVal, + const char *aParamName, + ParamDecoding aDecoding, + const nsACString& aFallbackCharset, + bool aTryLocaleCharset, + char **aLang, + nsAString& aResult); + static nsresult DoParameterInternal(const char *aHeaderValue, + const char *aParamName, + ParamDecoding aDecoding, + char **aCharset, + char **aLang, + char **aResult); }; #endif