netwerk/protocol/http/nsHttpConnectionInfo.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/http/nsHttpConnectionInfo.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim: set sw=4 ts=8 et tw=80 : */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +// HttpLog.h should generally be included first
    1.11 +#include "HttpLog.h"
    1.12 +
    1.13 +// Log on level :5, instead of default :4.
    1.14 +#undef LOG
    1.15 +#define LOG(args) LOG5(args)
    1.16 +#undef LOG_ENABLED
    1.17 +#define LOG_ENABLED() LOG5_ENABLED()
    1.18 +
    1.19 +#include "nsHttpConnectionInfo.h"
    1.20 +#include "mozilla/net/DNS.h"
    1.21 +#include "prnetdb.h"
    1.22 +
    1.23 +namespace mozilla {
    1.24 +namespace net {
    1.25 +
    1.26 +nsHttpConnectionInfo::nsHttpConnectionInfo(const nsACString &host, int32_t port,
    1.27 +                                           const nsACString &username,
    1.28 +                                           nsProxyInfo* proxyInfo,
    1.29 +                                           bool usingSSL)
    1.30 +    : mUsername(username)
    1.31 +    , mProxyInfo(proxyInfo)
    1.32 +    , mUsingSSL(usingSSL)
    1.33 +    , mUsingConnect(false)
    1.34 +{
    1.35 +    LOG(("Creating nsHttpConnectionInfo @%x\n", this));
    1.36 +
    1.37 +    mUsingHttpProxy = (proxyInfo && proxyInfo->IsHTTP());
    1.38 +
    1.39 +    if (mUsingHttpProxy) {
    1.40 +        mUsingConnect = mUsingSSL;  // SSL always uses CONNECT
    1.41 +        uint32_t resolveFlags = 0;
    1.42 +        if (NS_SUCCEEDED(mProxyInfo->GetResolveFlags(&resolveFlags)) &&
    1.43 +            resolveFlags & nsIProtocolProxyService::RESOLVE_ALWAYS_TUNNEL) {
    1.44 +            mUsingConnect = true;
    1.45 +        }
    1.46 +    }
    1.47 +
    1.48 +    SetOriginServer(host, port);
    1.49 +}
    1.50 +
    1.51 +void
    1.52 +nsHttpConnectionInfo::SetOriginServer(const nsACString &host, int32_t port)
    1.53 +{
    1.54 +    mHost = host;
    1.55 +    mPort = port == -1 ? DefaultPort() : port;
    1.56 +
    1.57 +    //
    1.58 +    // build hash key:
    1.59 +    //
    1.60 +    // the hash key uniquely identifies the connection type.  two connections
    1.61 +    // are "equal" if they end up talking the same protocol to the same server
    1.62 +    // and are both used for anonymous or non-anonymous connection only;
    1.63 +    // anonymity of the connection is setup later from nsHttpChannel::AsyncOpen
    1.64 +    // where we know we use anonymous connection (LOAD_ANONYMOUS load flag)
    1.65 +    //
    1.66 +
    1.67 +    const char *keyHost;
    1.68 +    int32_t keyPort;
    1.69 +
    1.70 +    if (mUsingHttpProxy && !mUsingConnect) {
    1.71 +        keyHost = ProxyHost();
    1.72 +        keyPort = ProxyPort();
    1.73 +    }
    1.74 +    else {
    1.75 +        keyHost = Host();
    1.76 +        keyPort = Port();
    1.77 +    }
    1.78 +
    1.79 +    mHashKey.AssignLiteral("....");
    1.80 +    mHashKey.Append(keyHost);
    1.81 +    mHashKey.Append(':');
    1.82 +    mHashKey.AppendInt(keyPort);
    1.83 +    if (!mUsername.IsEmpty()) {
    1.84 +        mHashKey.Append('[');
    1.85 +        mHashKey.Append(mUsername);
    1.86 +        mHashKey.Append(']');
    1.87 +    }
    1.88 +
    1.89 +    if (mUsingHttpProxy)
    1.90 +        mHashKey.SetCharAt('P', 0);
    1.91 +    if (mUsingSSL)
    1.92 +        mHashKey.SetCharAt('S', 1);
    1.93 +
    1.94 +    // NOTE: for transparent proxies (e.g., SOCKS) we need to encode the proxy
    1.95 +    // info in the hash key (this ensures that we will continue to speak the
    1.96 +    // right protocol even if our proxy preferences change).
    1.97 +    //
    1.98 +    // NOTE: for SSL tunnels add the proxy information to the cache key.
    1.99 +    // We cannot use the proxy as the host parameter (as we do for non SSL)
   1.100 +    // because this is a single host tunnel, but we need to include the proxy
   1.101 +    // information so that a change in proxy config will mean this connection
   1.102 +    // is not reused
   1.103 +
   1.104 +    if ((!mUsingHttpProxy && ProxyHost()) ||
   1.105 +        (mUsingHttpProxy && mUsingConnect)) {
   1.106 +        mHashKey.AppendLiteral(" (");
   1.107 +        mHashKey.Append(ProxyType());
   1.108 +        mHashKey.Append(':');
   1.109 +        mHashKey.Append(ProxyHost());
   1.110 +        mHashKey.Append(':');
   1.111 +        mHashKey.AppendInt(ProxyPort());
   1.112 +        mHashKey.Append(')');
   1.113 +    }
   1.114 +}
   1.115 +
   1.116 +nsHttpConnectionInfo*
   1.117 +nsHttpConnectionInfo::Clone() const
   1.118 +{
   1.119 +    nsHttpConnectionInfo* clone = new nsHttpConnectionInfo(mHost, mPort, mUsername, mProxyInfo, mUsingSSL);
   1.120 +
   1.121 +    // Make sure the anonymous and private flags are transferred!
   1.122 +    clone->SetAnonymous(GetAnonymous());
   1.123 +    clone->SetPrivate(GetPrivate());
   1.124 +
   1.125 +    return clone;
   1.126 +}
   1.127 +
   1.128 +bool
   1.129 +nsHttpConnectionInfo::UsingProxy()
   1.130 +{
   1.131 +    if (!mProxyInfo)
   1.132 +        return false;
   1.133 +    return !mProxyInfo->IsDirect();
   1.134 +}
   1.135 +
   1.136 +bool
   1.137 +nsHttpConnectionInfo::HostIsLocalIPLiteral() const
   1.138 +{
   1.139 +    PRNetAddr prAddr;
   1.140 +    // If the host/proxy host is not an IP address literal, return false.
   1.141 +    if (ProxyHost()) {
   1.142 +        if (PR_StringToNetAddr(ProxyHost(), &prAddr) != PR_SUCCESS) {
   1.143 +          return false;
   1.144 +        }
   1.145 +    } else if (PR_StringToNetAddr(Host(), &prAddr) != PR_SUCCESS) {
   1.146 +        return false;
   1.147 +    }
   1.148 +    NetAddr netAddr;
   1.149 +    PRNetAddrToNetAddr(&prAddr, &netAddr);
   1.150 +    return IsIPAddrLocal(&netAddr);
   1.151 +}
   1.152 +
   1.153 +} // namespace mozilla::net
   1.154 +} // namespace mozilla

mercurial