MimeType.h (3428B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_MimeType_h 8 #define mozilla_dom_MimeType_h 9 10 #include "nsTArray.h" 11 #include "nsTHashMap.h" 12 13 template <typename char_type> 14 struct HashKeyType; 15 template <> 16 struct HashKeyType<char16_t> { 17 using HashType = nsStringHashKey; 18 }; 19 template <> 20 struct HashKeyType<char> { 21 using HashType = nsCStringHashKey; 22 }; 23 24 template <typename char_type> 25 class TMimeType final { 26 private: 27 ~TMimeType() = default; 28 29 class ParameterValue : public nsTString<char_type> { 30 public: 31 bool mRequiresQuoting; 32 33 ParameterValue() : mRequiresQuoting(false) {} 34 }; 35 36 bool mIsBase64{false}; 37 nsTString<char_type> mType; 38 nsTString<char_type> mSubtype; 39 nsTHashMap<typename HashKeyType<char_type>::HashType, ParameterValue> 40 mParameters; 41 nsTArray<nsTString<char_type>> mParameterNames; 42 43 public: 44 TMimeType(const nsTSubstring<char_type>& aType, 45 const nsTSubstring<char_type>& aSubtype) 46 : mType(aType), mSubtype(aSubtype) {} 47 48 static nsTArray<nsTDependentSubstring<char_type>> SplitMimetype( 49 const nsTSubstring<char_type>& aMimeType); 50 51 static RefPtr<TMimeType<char_type>> Parse( 52 const nsTSubstring<char_type>& aMimeType); 53 54 // @param aMimeType - the mimetype string 55 // @param aOutEssence - will hold the value of the content-type 56 // @param aOutCharset - will hold the value of the charset 57 // @return true if the mimetype was parsed, false otherwise. 58 static bool Parse(const nsTSubstring<char_type>& aMimeType, 59 nsTSubstring<char_type>& aOutEssence, 60 nsTSubstring<char_type>& aOutCharset); 61 62 void Serialize(nsTSubstring<char_type>& aStr) const; 63 64 // Returns the `<mType>/<mSubtype>` 65 void GetEssence(nsTSubstring<char_type>& aOutput) const; 66 67 // Returns the `<mSubtype>` 68 void GetSubtype(nsTSubstring<char_type>& aOutput) const; 69 70 bool IsBase64() const { return mIsBase64; } 71 72 // @param aName - the name of the parameter 73 // @return true if the parameter name is found, false otherwise. 74 bool HasParameter(const nsTSubstring<char_type>& aName) const; 75 76 // @param aName - the name of the parameter 77 // @param aOutput - will hold the value of the parameter (quoted if necessary) 78 // @param aAppend - if true, the method will append to the string; 79 // otherwise the string is truncated before appending. 80 // @param aWithQuotes - if true, output can contain quoted string 81 // @return true if the parameter name is found, false otherwise. 82 bool GetParameterValue(const nsTSubstring<char_type>& aName, 83 nsTSubstring<char_type>& aOutput, bool aAppend = false, 84 bool aWithQuotes = true) const; 85 86 // @param aName - the name of the parameter 87 // @param aValue - the value of the parameter 88 void SetParameterValue(const nsTSubstring<char_type>& aName, 89 const nsTSubstring<char_type>& aValue); 90 91 size_t GetParameterCount() const { return mParameterNames.Length(); } 92 93 NS_INLINE_DECL_REFCOUNTING(TMimeType) 94 }; 95 96 using MimeType = TMimeType<char16_t>; 97 using CMimeType = TMimeType<char>; 98 99 #endif // mozilla_dom_MimeType_h