nsHttpRequestHead.h (5959B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef nsHttpRequestHead_h__ 7 #define nsHttpRequestHead_h__ 8 9 #include "nsHttp.h" 10 #include "nsHttpHeaderArray.h" 11 #include "nsString.h" 12 #include "mozilla/RecursiveMutex.h" 13 14 class nsIHttpHeaderVisitor; 15 16 // This needs to be forward declared here so we can include only this header 17 // without also including PHttpChannelParams.h 18 namespace IPC { 19 template <typename> 20 struct ParamTraits; 21 } // namespace IPC 22 23 namespace mozilla { 24 namespace net { 25 26 class DictionaryCacheEntry; 27 28 //----------------------------------------------------------------------------- 29 // nsHttpRequestHead represents the request line and headers from an HTTP 30 // request. 31 //----------------------------------------------------------------------------- 32 33 class nsHttpRequestHead { 34 public: 35 nsHttpRequestHead(); 36 explicit nsHttpRequestHead(const nsHttpRequestHead& aRequestHead); 37 nsHttpRequestHead(nsHttpRequestHead&& aRequestHead); 38 ~nsHttpRequestHead(); 39 40 nsHttpRequestHead& operator=(const nsHttpRequestHead& aRequestHead); 41 42 // The following function is only used in HttpChannelParent to avoid 43 // copying headers. If you use it be careful to do it only under 44 // nsHttpRequestHead lock!!! 45 const nsHttpHeaderArray& Headers() const MOZ_REQUIRES(mRecursiveMutex); 46 void Enter() const MOZ_CAPABILITY_ACQUIRE(mRecursiveMutex) { 47 mRecursiveMutex.Lock(); 48 } 49 void Exit() const MOZ_CAPABILITY_RELEASE(mRecursiveMutex) { 50 mRecursiveMutex.Unlock(); 51 } 52 53 void SetHeaders(const nsHttpHeaderArray& aHeaders); 54 55 void SetMethod(const nsACString& method); 56 void SetVersion(HttpVersion version); 57 void SetRequestURI(const nsACString& s); 58 void SetPath(const nsACString& s); 59 // keep a ref to the dictionary we offered, if any 60 void SetDictionary(DictionaryCacheEntry* aDict); 61 uint32_t HeaderCount(); 62 63 // Using this function it is possible to itereate through all headers 64 // automatically under one lock. 65 [[nodiscard]] nsresult VisitHeaders( 66 nsIHttpHeaderVisitor* visitor, 67 nsHttpHeaderArray::VisitorFilter filter = nsHttpHeaderArray::eFilterAll); 68 void Method(nsACString& aMethod); 69 HttpVersion Version(); 70 void RequestURI(nsACString& RequestURI); 71 void Path(nsACString& aPath); 72 void SetHTTPS(bool val); 73 bool IsHTTPS(); 74 75 void SetOrigin(const nsACString& scheme, const nsACString& host, 76 int32_t port); 77 void Origin(nsACString& aOrigin); 78 79 [[nodiscard]] nsresult SetHeader(const nsACString& h, const nsACString& v, 80 bool m = false); 81 [[nodiscard]] nsresult SetHeader(const nsHttpAtom& h, const nsACString& v, 82 bool m = false); 83 [[nodiscard]] nsresult SetHeader(const nsHttpAtom& h, const nsACString& v, 84 bool m, 85 nsHttpHeaderArray::HeaderVariety variety); 86 [[nodiscard]] nsresult SetEmptyHeader(const nsACString& h); 87 [[nodiscard]] nsresult GetHeader(const nsHttpAtom& h, nsACString& v); 88 89 [[nodiscard]] nsresult ClearHeader(const nsHttpAtom& h); 90 void ClearHeaders(); 91 92 bool HasHeaderValue(const nsHttpAtom& h, const char* v); 93 // This function returns true if header is set even if it is an empty 94 // header. 95 bool HasHeader(const nsHttpAtom& h); 96 void Flatten(nsACString&, bool pruneProxyHeaders = false); 97 98 // Don't allow duplicate values 99 [[nodiscard]] nsresult SetHeaderOnce(const nsHttpAtom& h, const char* v, 100 bool merge = false); 101 102 bool IsSafeMethod(); 103 104 enum ParsedMethodType { 105 kMethod_Custom, 106 kMethod_Get, 107 kMethod_Post, 108 kMethod_Patch, 109 kMethod_Options, 110 kMethod_Connect, 111 kMethod_Head, 112 kMethod_Put, 113 kMethod_Trace 114 }; 115 116 static void ParseMethod(const nsCString& aRawMethod, 117 ParsedMethodType& aParsedMethod); 118 119 ParsedMethodType ParsedMethod(); 120 bool EqualsMethod(ParsedMethodType aType); 121 bool IsGet() { return EqualsMethod(kMethod_Get); } 122 bool IsPost() { return EqualsMethod(kMethod_Post); } 123 bool IsPatch() { return EqualsMethod(kMethod_Patch); } 124 bool IsOptions() { return EqualsMethod(kMethod_Options); } 125 bool IsConnect() { return EqualsMethod(kMethod_Connect); } 126 bool IsHead() { return EqualsMethod(kMethod_Head); } 127 bool IsPut() { return EqualsMethod(kMethod_Put); } 128 bool IsTrace() { return EqualsMethod(kMethod_Trace); } 129 void ParseHeaderSet(const char* buffer); 130 131 private: 132 // All members must be copy-constructable and assignable 133 nsHttpHeaderArray mHeaders MOZ_GUARDED_BY(mRecursiveMutex); 134 nsCString mMethod MOZ_GUARDED_BY(mRecursiveMutex){"GET"_ns}; 135 HttpVersion mVersion MOZ_GUARDED_BY(mRecursiveMutex){HttpVersion::v1_1}; 136 137 // mRequestURI and mPath are strings instead of an nsIURI 138 // because this is used off the main thread 139 // TODO: nsIURI is thread-safe now, should be fixable. 140 nsCString mRequestURI MOZ_GUARDED_BY(mRecursiveMutex); 141 nsCString mPath MOZ_GUARDED_BY(mRecursiveMutex); 142 143 RefPtr<DictionaryCacheEntry> mDict MOZ_GUARDED_BY(mRecursiveMutex); 144 145 nsCString mOrigin MOZ_GUARDED_BY(mRecursiveMutex); 146 ParsedMethodType mParsedMethod MOZ_GUARDED_BY(mRecursiveMutex){kMethod_Get}; 147 bool mHTTPS MOZ_GUARDED_BY(mRecursiveMutex){false}; 148 149 // We are using RecursiveMutex instead of a Mutex because VisitHeader 150 // function calls nsIHttpHeaderVisitor::VisitHeader while under lock. 151 mutable RecursiveMutex mRecursiveMutex MOZ_UNANNOTATED{ 152 "nsHttpRequestHead.mRecursiveMutex"}; 153 154 // During VisitHeader we sould not allow call to SetHeader. 155 bool mInVisitHeaders MOZ_GUARDED_BY(mRecursiveMutex){false}; 156 157 friend struct IPC::ParamTraits<nsHttpRequestHead>; 158 }; 159 160 } // namespace net 161 } // namespace mozilla 162 163 #endif // nsHttpRequestHead_h__