Merge mozilla-central to autoland. CLOSED TREE
[gecko.git] / netwerk / base / nsProxyInfo.cpp
blob6859845739615ee080f868d49f75b374a22d3efc
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"
10 #include "nsCOMPtr.h"
12 namespace mozilla {
13 namespace net {
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)
34 : mHost(aHost),
35 mUsername(aUsername),
36 mPassword(aPassword),
37 mProxyAuthorizationHeader(aProxyAuthorizationHeader),
38 mConnectionIsolationKey(aConnectionIsolationKey),
39 mPort(aPort),
40 mFlags(aFlags),
41 mResolveFlags(aResolveFlags),
42 mTimeout(aTimeout),
43 mNext(nullptr) {
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;
56 } else {
57 mType = kProxyType_DIRECT;
61 NS_IMETHODIMP
62 nsProxyInfo::GetHost(nsACString& result) {
63 result = mHost;
64 return NS_OK;
67 NS_IMETHODIMP
68 nsProxyInfo::GetPort(int32_t* result) {
69 *result = mPort;
70 return NS_OK;
73 NS_IMETHODIMP
74 nsProxyInfo::GetType(nsACString& result) {
75 result = mType;
76 return NS_OK;
79 NS_IMETHODIMP
80 nsProxyInfo::GetFlags(uint32_t* result) {
81 *result = mFlags;
82 return NS_OK;
85 NS_IMETHODIMP
86 nsProxyInfo::GetResolveFlags(uint32_t* result) {
87 *result = mResolveFlags;
88 return NS_OK;
91 NS_IMETHODIMP
92 nsProxyInfo::GetUsername(nsACString& result) {
93 result = mUsername;
94 return NS_OK;
97 NS_IMETHODIMP
98 nsProxyInfo::GetPassword(nsACString& result) {
99 result = mPassword;
100 return NS_OK;
103 NS_IMETHODIMP
104 nsProxyInfo::GetProxyAuthorizationHeader(nsACString& result) {
105 result = mProxyAuthorizationHeader;
106 return NS_OK;
109 NS_IMETHODIMP
110 nsProxyInfo::GetConnectionIsolationKey(nsACString& result) {
111 result = mConnectionIsolationKey;
112 return NS_OK;
115 NS_IMETHODIMP
116 nsProxyInfo::GetFailoverTimeout(uint32_t* result) {
117 *result = mTimeout;
118 return NS_OK;
121 NS_IMETHODIMP
122 nsProxyInfo::GetFailoverProxy(nsIProxyInfo** result) {
123 NS_IF_ADDREF(*result = mNext);
124 return NS_OK;
127 NS_IMETHODIMP
128 nsProxyInfo::SetFailoverProxy(nsIProxyInfo* proxy) {
129 nsCOMPtr<nsProxyInfo> pi = do_QueryInterface(proxy);
130 NS_ENSURE_ARG(pi);
132 pi.swap(mNext);
133 return NS_OK;
136 NS_IMETHODIMP
137 nsProxyInfo::GetSourceId(nsACString& result) {
138 result = mSourceId;
139 return NS_OK;
142 NS_IMETHODIMP
143 nsProxyInfo::SetSourceId(const nsACString& sourceId) {
144 mSourceId = sourceId;
145 return NS_OK;
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;
162 /* static */
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();
180 /* static */
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());
189 if (last) {
190 last->mNext = pi;
191 // |mNext| will be released in |last|'s destructor.
192 NS_IF_ADDREF(last->mNext);
193 } else {
194 first = pi;
196 last = pi;
199 return first;
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();
215 } // namespace net
216 } // namespace mozilla