Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set sw=4 ts=8 et tw=80 : */
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 // HttpLog.h should generally be included first
8 #include "HttpLog.h"
10 // Log on level :5, instead of default :4.
11 #undef LOG
12 #define LOG(args) LOG5(args)
13 #undef LOG_ENABLED
14 #define LOG_ENABLED() LOG5_ENABLED()
16 #include "nsHttpConnectionInfo.h"
17 #include "mozilla/net/DNS.h"
18 #include "prnetdb.h"
20 namespace mozilla {
21 namespace net {
23 nsHttpConnectionInfo::nsHttpConnectionInfo(const nsACString &host, int32_t port,
24 const nsACString &username,
25 nsProxyInfo* proxyInfo,
26 bool usingSSL)
27 : mUsername(username)
28 , mProxyInfo(proxyInfo)
29 , mUsingSSL(usingSSL)
30 , mUsingConnect(false)
31 {
32 LOG(("Creating nsHttpConnectionInfo @%x\n", this));
34 mUsingHttpProxy = (proxyInfo && proxyInfo->IsHTTP());
36 if (mUsingHttpProxy) {
37 mUsingConnect = mUsingSSL; // SSL always uses CONNECT
38 uint32_t resolveFlags = 0;
39 if (NS_SUCCEEDED(mProxyInfo->GetResolveFlags(&resolveFlags)) &&
40 resolveFlags & nsIProtocolProxyService::RESOLVE_ALWAYS_TUNNEL) {
41 mUsingConnect = true;
42 }
43 }
45 SetOriginServer(host, port);
46 }
48 void
49 nsHttpConnectionInfo::SetOriginServer(const nsACString &host, int32_t port)
50 {
51 mHost = host;
52 mPort = port == -1 ? DefaultPort() : port;
54 //
55 // build hash key:
56 //
57 // the hash key uniquely identifies the connection type. two connections
58 // are "equal" if they end up talking the same protocol to the same server
59 // and are both used for anonymous or non-anonymous connection only;
60 // anonymity of the connection is setup later from nsHttpChannel::AsyncOpen
61 // where we know we use anonymous connection (LOAD_ANONYMOUS load flag)
62 //
64 const char *keyHost;
65 int32_t keyPort;
67 if (mUsingHttpProxy && !mUsingConnect) {
68 keyHost = ProxyHost();
69 keyPort = ProxyPort();
70 }
71 else {
72 keyHost = Host();
73 keyPort = Port();
74 }
76 mHashKey.AssignLiteral("....");
77 mHashKey.Append(keyHost);
78 mHashKey.Append(':');
79 mHashKey.AppendInt(keyPort);
80 if (!mUsername.IsEmpty()) {
81 mHashKey.Append('[');
82 mHashKey.Append(mUsername);
83 mHashKey.Append(']');
84 }
86 if (mUsingHttpProxy)
87 mHashKey.SetCharAt('P', 0);
88 if (mUsingSSL)
89 mHashKey.SetCharAt('S', 1);
91 // NOTE: for transparent proxies (e.g., SOCKS) we need to encode the proxy
92 // info in the hash key (this ensures that we will continue to speak the
93 // right protocol even if our proxy preferences change).
94 //
95 // NOTE: for SSL tunnels add the proxy information to the cache key.
96 // We cannot use the proxy as the host parameter (as we do for non SSL)
97 // because this is a single host tunnel, but we need to include the proxy
98 // information so that a change in proxy config will mean this connection
99 // is not reused
101 if ((!mUsingHttpProxy && ProxyHost()) ||
102 (mUsingHttpProxy && mUsingConnect)) {
103 mHashKey.AppendLiteral(" (");
104 mHashKey.Append(ProxyType());
105 mHashKey.Append(':');
106 mHashKey.Append(ProxyHost());
107 mHashKey.Append(':');
108 mHashKey.AppendInt(ProxyPort());
109 mHashKey.Append(')');
110 }
111 }
113 nsHttpConnectionInfo*
114 nsHttpConnectionInfo::Clone() const
115 {
116 nsHttpConnectionInfo* clone = new nsHttpConnectionInfo(mHost, mPort, mUsername, mProxyInfo, mUsingSSL);
118 // Make sure the anonymous and private flags are transferred!
119 clone->SetAnonymous(GetAnonymous());
120 clone->SetPrivate(GetPrivate());
122 return clone;
123 }
125 bool
126 nsHttpConnectionInfo::UsingProxy()
127 {
128 if (!mProxyInfo)
129 return false;
130 return !mProxyInfo->IsDirect();
131 }
133 bool
134 nsHttpConnectionInfo::HostIsLocalIPLiteral() const
135 {
136 PRNetAddr prAddr;
137 // If the host/proxy host is not an IP address literal, return false.
138 if (ProxyHost()) {
139 if (PR_StringToNetAddr(ProxyHost(), &prAddr) != PR_SUCCESS) {
140 return false;
141 }
142 } else if (PR_StringToNetAddr(Host(), &prAddr) != PR_SUCCESS) {
143 return false;
144 }
145 NetAddr netAddr;
146 PRNetAddrToNetAddr(&prAddr, &netAddr);
147 return IsIPAddrLocal(&netAddr);
148 }
150 } // namespace mozilla::net
151 } // namespace mozilla