summaryrefslogtreecommitdiffstats
path: root/netwerk/base/nsProxyInfo.cpp
blob: 6859845739615ee080f868d49f75b374a22d3efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsProxyInfo.h"

#include "mozilla/net/NeckoChannelParams.h"
#include "nsCOMPtr.h"

namespace mozilla {
namespace net {

// Yes, we support QI to nsProxyInfo
NS_IMPL_ISUPPORTS(nsProxyInfo, nsProxyInfo, nsIProxyInfo)

// These pointers are declared in nsProtocolProxyService.cpp and
// comparison of mType by string pointer is valid within necko
extern const char kProxyType_HTTP[];
extern const char kProxyType_HTTPS[];
extern const char kProxyType_SOCKS[];
extern const char kProxyType_SOCKS4[];
extern const char kProxyType_SOCKS5[];
extern const char kProxyType_DIRECT[];
extern const char kProxyType_PROXY[];

nsProxyInfo::nsProxyInfo(const nsACString& aType, const nsACString& aHost,
                         int32_t aPort, const nsACString& aUsername,
                         const nsACString& aPassword, uint32_t aFlags,
                         uint32_t aTimeout, uint32_t aResolveFlags,
                         const nsACString& aProxyAuthorizationHeader,
                         const nsACString& aConnectionIsolationKey)
    : mHost(aHost),
      mUsername(aUsername),
      mPassword(aPassword),
      mProxyAuthorizationHeader(aProxyAuthorizationHeader),
      mConnectionIsolationKey(aConnectionIsolationKey),
      mPort(aPort),
      mFlags(aFlags),
      mResolveFlags(aResolveFlags),
      mTimeout(aTimeout),
      mNext(nullptr) {
  if (aType.EqualsASCII(kProxyType_HTTP)) {
    mType = kProxyType_HTTP;
  } else if (aType.EqualsASCII(kProxyType_HTTPS)) {
    mType = kProxyType_HTTPS;
  } else if (aType.EqualsASCII(kProxyType_SOCKS)) {
    mType = kProxyType_SOCKS;
  } else if (aType.EqualsASCII(kProxyType_SOCKS4)) {
    mType = kProxyType_SOCKS4;
  } else if (aType.EqualsASCII(kProxyType_SOCKS5)) {
    mType = kProxyType_SOCKS5;
  } else if (aType.EqualsASCII(kProxyType_PROXY)) {
    mType = kProxyType_PROXY;
  } else {
    mType = kProxyType_DIRECT;
  }
}

NS_IMETHODIMP
nsProxyInfo::GetHost(nsACString& result) {
  result = mHost;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetPort(int32_t* result) {
  *result = mPort;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetType(nsACString& result) {
  result = mType;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetFlags(uint32_t* result) {
  *result = mFlags;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetResolveFlags(uint32_t* result) {
  *result = mResolveFlags;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetUsername(nsACString& result) {
  result = mUsername;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetPassword(nsACString& result) {
  result = mPassword;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetProxyAuthorizationHeader(nsACString& result) {
  result = mProxyAuthorizationHeader;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetConnectionIsolationKey(nsACString& result) {
  result = mConnectionIsolationKey;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetFailoverTimeout(uint32_t* result) {
  *result = mTimeout;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetFailoverProxy(nsIProxyInfo** result) {
  NS_IF_ADDREF(*result = mNext);
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::SetFailoverProxy(nsIProxyInfo* proxy) {
  nsCOMPtr<nsProxyInfo> pi = do_QueryInterface(proxy);
  NS_ENSURE_ARG(pi);

  pi.swap(mNext);
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::GetSourceId(nsACString& result) {
  result = mSourceId;
  return NS_OK;
}

NS_IMETHODIMP
nsProxyInfo::SetSourceId(const nsACString& sourceId) {
  mSourceId = sourceId;
  return NS_OK;
}

bool nsProxyInfo::IsDirect() {
  if (!mType) return true;
  return mType == kProxyType_DIRECT;
}

bool nsProxyInfo::IsHTTP() { return mType == kProxyType_HTTP; }

bool nsProxyInfo::IsHTTPS() { return mType == kProxyType_HTTPS; }

bool nsProxyInfo::IsSOCKS() {
  return mType == kProxyType_SOCKS || mType == kProxyType_SOCKS4 ||
         mType == kProxyType_SOCKS5;
}

/* static */
void nsProxyInfo::SerializeProxyInfo(nsProxyInfo* aProxyInfo,
                                     nsTArray<ProxyInfoCloneArgs>& aResult) {
  for (nsProxyInfo* iter = aProxyInfo; iter; iter = iter->mNext) {
    ProxyInfoCloneArgs* arg = aResult.AppendElement();
    arg->type() = nsCString(iter->Type());
    arg->host() = iter->Host();
    arg->port() = iter->Port();
    arg->username() = iter->Username();
    arg->password() = iter->Password();
    arg->proxyAuthorizationHeader() = iter->ProxyAuthorizationHeader();
    arg->connectionIsolationKey() = iter->ConnectionIsolationKey();
    arg->flags() = iter->Flags();
    arg->timeout() = iter->Timeout();
    arg->resolveFlags() = iter->ResolveFlags();
  }
}

/* static */
nsProxyInfo* nsProxyInfo::DeserializeProxyInfo(
    const nsTArray<ProxyInfoCloneArgs>& aArgs) {
  nsProxyInfo *pi = nullptr, *first = nullptr, *last = nullptr;
  for (const ProxyInfoCloneArgs& info : aArgs) {
    pi = new nsProxyInfo(info.type(), info.host(), info.port(), info.username(),
                         info.password(), info.flags(), info.timeout(),
                         info.resolveFlags(), info.proxyAuthorizationHeader(),
                         info.connectionIsolationKey());
    if (last) {
      last->mNext = pi;
      // |mNext| will be released in |last|'s destructor.
      NS_IF_ADDREF(last->mNext);
    } else {
      first = pi;
    }
    last = pi;
  }

  return first;
}

already_AddRefed<nsProxyInfo> nsProxyInfo::CloneProxyInfoWithNewResolveFlags(
    uint32_t aResolveFlags) {
  nsTArray<ProxyInfoCloneArgs> args;
  SerializeProxyInfo(this, args);

  for (auto& arg : args) {
    arg.resolveFlags() = aResolveFlags;
  }

  RefPtr<nsProxyInfo> result = DeserializeProxyInfo(args);
  return result.forget();
}

}  // namespace net
}  // namespace mozilla