netwerk/protocol/http/nsHttpConnectionInfo.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial