nsProxyInfo.cpp (7014B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */ 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 #include "nsProxyInfo.h" 8 9 #include "mozilla/net/NeckoChannelParams.h" 10 #include "nsCOMPtr.h" 11 12 namespace mozilla { 13 namespace net { 14 15 // Yes, we support QI to nsProxyInfo 16 NS_IMPL_ISUPPORTS(nsProxyInfo, nsProxyInfo, nsIProxyInfo) 17 18 // These pointers are declared in nsProtocolProxyService.cpp and 19 // comparison of mType by string pointer is valid within necko 20 extern const char kProxyType_HTTP[]; 21 extern const char kProxyType_HTTPS[]; 22 extern const char kProxyType_SOCKS[]; 23 extern const char kProxyType_SOCKS4[]; 24 extern const char kProxyType_SOCKS5[]; 25 extern const char kProxyType_DIRECT[]; 26 extern const char kProxyType_PROXY[]; 27 extern const char kProxyType_MASQUE[]; 28 29 nsProxyInfo::nsProxyInfo(const nsACString& aType, const nsACString& aHost, 30 int32_t aPort, const nsACString& aUsername, 31 const nsACString& aPassword, uint32_t aFlags, 32 uint32_t aTimeout, uint32_t aResolveFlags, 33 const nsACString& aProxyAuthorizationHeader, 34 const nsACString& aConnectionIsolationKey, 35 const nsACString& aMasqueTemplate) 36 : mHost(aHost), 37 mUsername(aUsername), 38 mPassword(aPassword), 39 mProxyAuthorizationHeader(aProxyAuthorizationHeader), 40 mConnectionIsolationKey(aConnectionIsolationKey), 41 mMasqueTemplate(aMasqueTemplate), 42 mPort(aPort), 43 mFlags(aFlags), 44 mResolveFlags(aResolveFlags), 45 mTimeout(aTimeout), 46 mNext(nullptr) { 47 if (aType.EqualsASCII(kProxyType_HTTP)) { 48 mType = kProxyType_HTTP; 49 } else if (aType.EqualsASCII(kProxyType_HTTPS)) { 50 mType = kProxyType_HTTPS; 51 } else if (aType.EqualsASCII(kProxyType_SOCKS)) { 52 mType = kProxyType_SOCKS; 53 } else if (aType.EqualsASCII(kProxyType_SOCKS4)) { 54 mType = kProxyType_SOCKS4; 55 } else if (aType.EqualsASCII(kProxyType_SOCKS5)) { 56 mType = kProxyType_SOCKS5; 57 } else if (aType.EqualsASCII(kProxyType_PROXY)) { 58 mType = kProxyType_PROXY; 59 } else if (aType.EqualsASCII(kProxyType_MASQUE)) { 60 mType = kProxyType_MASQUE; 61 } else { 62 mType = kProxyType_DIRECT; 63 } 64 } 65 66 NS_IMETHODIMP 67 nsProxyInfo::GetHost(nsACString& result) { 68 result = mHost; 69 return NS_OK; 70 } 71 72 NS_IMETHODIMP 73 nsProxyInfo::GetPort(int32_t* result) { 74 *result = mPort; 75 return NS_OK; 76 } 77 78 NS_IMETHODIMP 79 nsProxyInfo::GetType(nsACString& result) { 80 result = mType; 81 return NS_OK; 82 } 83 84 NS_IMETHODIMP 85 nsProxyInfo::GetFlags(uint32_t* result) { 86 *result = mFlags; 87 return NS_OK; 88 } 89 90 NS_IMETHODIMP 91 nsProxyInfo::GetResolveFlags(uint32_t* result) { 92 *result = mResolveFlags; 93 return NS_OK; 94 } 95 96 NS_IMETHODIMP 97 nsProxyInfo::GetUsername(nsACString& result) { 98 result = mUsername; 99 return NS_OK; 100 } 101 102 NS_IMETHODIMP 103 nsProxyInfo::GetPassword(nsACString& result) { 104 result = mPassword; 105 return NS_OK; 106 } 107 108 NS_IMETHODIMP 109 nsProxyInfo::GetProxyAuthorizationHeader(nsACString& result) { 110 result = mProxyAuthorizationHeader; 111 return NS_OK; 112 } 113 114 NS_IMETHODIMP 115 nsProxyInfo::GetConnectionIsolationKey(nsACString& result) { 116 result = mConnectionIsolationKey; 117 return NS_OK; 118 } 119 120 NS_IMETHODIMP 121 nsProxyInfo::GetFailoverTimeout(uint32_t* result) { 122 *result = mTimeout; 123 return NS_OK; 124 } 125 126 NS_IMETHODIMP 127 nsProxyInfo::GetFailoverProxy(nsIProxyInfo** result) { 128 NS_IF_ADDREF(*result = mNext); 129 return NS_OK; 130 } 131 132 NS_IMETHODIMP 133 nsProxyInfo::SetFailoverProxy(nsIProxyInfo* proxy) { 134 nsCOMPtr<nsProxyInfo> pi = do_QueryInterface(proxy); 135 NS_ENSURE_ARG(pi); 136 137 pi.swap(mNext); 138 return NS_OK; 139 } 140 141 NS_IMETHODIMP 142 nsProxyInfo::GetSourceId(nsACString& result) { 143 result = mSourceId; 144 return NS_OK; 145 } 146 147 NS_IMETHODIMP 148 nsProxyInfo::SetSourceId(const nsACString& sourceId) { 149 mSourceId = sourceId; 150 return NS_OK; 151 } 152 153 NS_IMETHODIMP 154 nsProxyInfo::SetMasqueTemplate(const nsACString& aMasqueTemplate) { 155 mMasqueTemplate = aMasqueTemplate; 156 return NS_OK; 157 } 158 159 NS_IMETHODIMP 160 nsProxyInfo::GetMasqueTemplate(nsACString& aMasqueTemplate) { 161 aMasqueTemplate = mMasqueTemplate; 162 return NS_OK; 163 } 164 165 bool nsProxyInfo::IsDirect() { 166 if (!mType) return true; 167 return mType == kProxyType_DIRECT; 168 } 169 170 bool nsProxyInfo::IsHTTP() { return mType == kProxyType_HTTP; } 171 172 bool nsProxyInfo::IsHTTPS() { 173 return mType == kProxyType_HTTPS || mType == kProxyType_MASQUE; 174 } 175 176 bool nsProxyInfo::IsSOCKS() { 177 return mType == kProxyType_SOCKS || mType == kProxyType_SOCKS4 || 178 mType == kProxyType_SOCKS5; 179 } 180 181 bool nsProxyInfo::IsHttp3Proxy() { return mType == kProxyType_MASQUE; } 182 183 /* static */ 184 void nsProxyInfo::SerializeProxyInfo(nsProxyInfo* aProxyInfo, 185 nsTArray<ProxyInfoCloneArgs>& aResult) { 186 for (nsProxyInfo* iter = aProxyInfo; iter; iter = iter->mNext) { 187 ProxyInfoCloneArgs* arg = aResult.AppendElement(); 188 arg->type() = nsCString(iter->Type()); 189 arg->host() = iter->Host(); 190 arg->port() = iter->Port(); 191 arg->masqueTemplate() = iter->MasqueTemplate(); 192 arg->username() = iter->Username(); 193 arg->password() = iter->Password(); 194 arg->proxyAuthorizationHeader() = iter->ProxyAuthorizationHeader(); 195 arg->connectionIsolationKey() = iter->ConnectionIsolationKey(); 196 arg->flags() = iter->Flags(); 197 arg->timeout() = iter->Timeout(); 198 arg->resolveFlags() = iter->ResolveFlags(); 199 } 200 } 201 202 /* static */ 203 nsProxyInfo* nsProxyInfo::DeserializeProxyInfo( 204 const nsTArray<ProxyInfoCloneArgs>& aArgs) { 205 nsProxyInfo *pi = nullptr, *first = nullptr, *last = nullptr; 206 for (const ProxyInfoCloneArgs& info : aArgs) { 207 pi = new nsProxyInfo(info.type(), info.host(), info.port(), info.username(), 208 info.password(), info.flags(), info.timeout(), 209 info.resolveFlags(), info.proxyAuthorizationHeader(), 210 info.connectionIsolationKey(), info.masqueTemplate()); 211 if (last) { 212 last->mNext = pi; 213 // |mNext| will be released in |last|'s destructor. 214 NS_IF_ADDREF(last->mNext); 215 } else { 216 first = pi; 217 } 218 last = pi; 219 } 220 221 return first; 222 } 223 224 already_AddRefed<nsProxyInfo> nsProxyInfo::CloneProxyInfoWithNewResolveFlags( 225 uint32_t aResolveFlags) { 226 nsTArray<ProxyInfoCloneArgs> args; 227 SerializeProxyInfo(this, args); 228 229 for (auto& arg : args) { 230 arg.resolveFlags() = aResolveFlags; 231 } 232 233 RefPtr<nsProxyInfo> result = DeserializeProxyInfo(args); 234 return result.forget(); 235 } 236 237 already_AddRefed<nsProxyInfo> nsProxyInfo::CreateFallbackProxyInfo() { 238 nsTArray<ProxyInfoCloneArgs> args; 239 SerializeProxyInfo(this, args); 240 241 for (auto& arg : args) { 242 if (arg.type().Equals("masque"_ns)) { 243 arg.type() = "https"; 244 } 245 } 246 247 RefPtr<nsProxyInfo> result = DeserializeProxyInfo(args); 248 return result.forget(); 249 } 250 251 } // namespace net 252 } // namespace mozilla