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/. */
7 #include "nsProxyInfo.h"
9 #include "mozilla/net/NeckoChannelParams.h"
15 // Yes, we support QI to nsProxyInfo
16 NS_IMPL_ISUPPORTS(nsProxyInfo
, nsProxyInfo
, nsIProxyInfo
)
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
[];
28 nsProxyInfo::nsProxyInfo(const nsACString
& aType
, const nsACString
& aHost
,
29 int32_t aPort
, const nsACString
& aUsername
,
30 const nsACString
& aPassword
, uint32_t aFlags
,
31 uint32_t aTimeout
, uint32_t aResolveFlags
,
32 const nsACString
& aProxyAuthorizationHeader
,
33 const nsACString
& aConnectionIsolationKey
)
37 mProxyAuthorizationHeader(aProxyAuthorizationHeader
),
38 mConnectionIsolationKey(aConnectionIsolationKey
),
41 mResolveFlags(aResolveFlags
),
44 if (aType
.EqualsASCII(kProxyType_HTTP
)) {
45 mType
= kProxyType_HTTP
;
46 } else if (aType
.EqualsASCII(kProxyType_HTTPS
)) {
47 mType
= kProxyType_HTTPS
;
48 } else if (aType
.EqualsASCII(kProxyType_SOCKS
)) {
49 mType
= kProxyType_SOCKS
;
50 } else if (aType
.EqualsASCII(kProxyType_SOCKS4
)) {
51 mType
= kProxyType_SOCKS4
;
52 } else if (aType
.EqualsASCII(kProxyType_SOCKS5
)) {
53 mType
= kProxyType_SOCKS5
;
54 } else if (aType
.EqualsASCII(kProxyType_PROXY
)) {
55 mType
= kProxyType_PROXY
;
57 mType
= kProxyType_DIRECT
;
62 nsProxyInfo::GetHost(nsACString
& result
) {
68 nsProxyInfo::GetPort(int32_t* result
) {
74 nsProxyInfo::GetType(nsACString
& result
) {
80 nsProxyInfo::GetFlags(uint32_t* result
) {
86 nsProxyInfo::GetResolveFlags(uint32_t* result
) {
87 *result
= mResolveFlags
;
92 nsProxyInfo::GetUsername(nsACString
& result
) {
98 nsProxyInfo::GetPassword(nsACString
& result
) {
104 nsProxyInfo::GetProxyAuthorizationHeader(nsACString
& result
) {
105 result
= mProxyAuthorizationHeader
;
110 nsProxyInfo::GetConnectionIsolationKey(nsACString
& result
) {
111 result
= mConnectionIsolationKey
;
116 nsProxyInfo::GetFailoverTimeout(uint32_t* result
) {
122 nsProxyInfo::GetFailoverProxy(nsIProxyInfo
** result
) {
123 NS_IF_ADDREF(*result
= mNext
);
128 nsProxyInfo::SetFailoverProxy(nsIProxyInfo
* proxy
) {
129 nsCOMPtr
<nsProxyInfo
> pi
= do_QueryInterface(proxy
);
137 nsProxyInfo::GetSourceId(nsACString
& result
) {
143 nsProxyInfo::SetSourceId(const nsACString
& sourceId
) {
144 mSourceId
= sourceId
;
148 bool nsProxyInfo::IsDirect() {
149 if (!mType
) return true;
150 return mType
== kProxyType_DIRECT
;
153 bool nsProxyInfo::IsHTTP() { return mType
== kProxyType_HTTP
; }
155 bool nsProxyInfo::IsHTTPS() { return mType
== kProxyType_HTTPS
; }
157 bool nsProxyInfo::IsSOCKS() {
158 return mType
== kProxyType_SOCKS
|| mType
== kProxyType_SOCKS4
||
159 mType
== kProxyType_SOCKS5
;
163 void nsProxyInfo::SerializeProxyInfo(nsProxyInfo
* aProxyInfo
,
164 nsTArray
<ProxyInfoCloneArgs
>& aResult
) {
165 for (nsProxyInfo
* iter
= aProxyInfo
; iter
; iter
= iter
->mNext
) {
166 ProxyInfoCloneArgs
* arg
= aResult
.AppendElement();
167 arg
->type() = nsCString(iter
->Type());
168 arg
->host() = iter
->Host();
169 arg
->port() = iter
->Port();
170 arg
->username() = iter
->Username();
171 arg
->password() = iter
->Password();
172 arg
->proxyAuthorizationHeader() = iter
->ProxyAuthorizationHeader();
173 arg
->connectionIsolationKey() = iter
->ConnectionIsolationKey();
174 arg
->flags() = iter
->Flags();
175 arg
->timeout() = iter
->Timeout();
176 arg
->resolveFlags() = iter
->ResolveFlags();
181 nsProxyInfo
* nsProxyInfo::DeserializeProxyInfo(
182 const nsTArray
<ProxyInfoCloneArgs
>& aArgs
) {
183 nsProxyInfo
*pi
= nullptr, *first
= nullptr, *last
= nullptr;
184 for (const ProxyInfoCloneArgs
& info
: aArgs
) {
185 pi
= new nsProxyInfo(info
.type(), info
.host(), info
.port(), info
.username(),
186 info
.password(), info
.flags(), info
.timeout(),
187 info
.resolveFlags(), info
.proxyAuthorizationHeader(),
188 info
.connectionIsolationKey());
191 // |mNext| will be released in |last|'s destructor.
192 NS_IF_ADDREF(last
->mNext
);
202 already_AddRefed
<nsProxyInfo
> nsProxyInfo::CloneProxyInfoWithNewResolveFlags(
203 uint32_t aResolveFlags
) {
204 nsTArray
<ProxyInfoCloneArgs
> args
;
205 SerializeProxyInfo(this, args
);
207 for (auto& arg
: args
) {
208 arg
.resolveFlags() = aResolveFlags
;
211 RefPtr
<nsProxyInfo
> result
= DeserializeProxyInfo(args
);
212 return result
.forget();
216 } // namespace mozilla