From e6a9d7058640c2453e042a2fd9089c1272db2b59 Mon Sep 17 00:00:00 2001 From: Christian Biesinger Date: Thu, 18 Feb 2010 15:42:32 +0100 Subject: [PATCH] bug 445168 format IPv6 address literals correctly when using a proxy r=bz --- netwerk/protocol/http/src/nsHttpChannel.cpp | 22 +++------------- .../protocol/http/src/nsHttpConnection.cpp | 7 +++--- netwerk/protocol/http/src/nsHttpHandler.cpp | 25 +++++++++++++++++++ netwerk/protocol/http/src/nsHttpHandler.h | 6 +++++ 4 files changed, 38 insertions(+), 22 deletions(-) diff --git a/netwerk/protocol/http/src/nsHttpChannel.cpp b/netwerk/protocol/http/src/nsHttpChannel.cpp index 666265147b5..b339d43c5eb 100644 --- a/netwerk/protocol/http/src/nsHttpChannel.cpp +++ b/netwerk/protocol/http/src/nsHttpChannel.cpp @@ -225,25 +225,9 @@ nsHttpChannel::Init(nsIURI *uri, // Set request headers // nsCAutoString hostLine; - if (strchr(host.get(), ':')) { - // host is an IPv6 address literal and must be encapsulated in []'s - hostLine.Assign('['); - // scope id is not needed for Host header. - int scopeIdPos = host.FindChar('%'); - if (scopeIdPos == kNotFound) - hostLine.Append(host); - else if (scopeIdPos > 0) - hostLine.Append(Substring(host, 0, scopeIdPos)); - else - return NS_ERROR_MALFORMED_URI; - hostLine.Append(']'); - } - else - hostLine.Assign(host); - if (port != -1) { - hostLine.Append(':'); - hostLine.AppendInt(port); - } + rv = nsHttpHandler::GenerateHostPort(host, port, hostLine); + if (NS_FAILED(rv)) + return rv; rv = mRequestHead.SetHeader(nsHttp::Host, hostLine); if (NS_FAILED(rv)) return rv; diff --git a/netwerk/protocol/http/src/nsHttpConnection.cpp b/netwerk/protocol/http/src/nsHttpConnection.cpp index fbee674a9c5..37fc3f9d3d5 100644 --- a/netwerk/protocol/http/src/nsHttpConnection.cpp +++ b/netwerk/protocol/http/src/nsHttpConnection.cpp @@ -693,9 +693,10 @@ nsHttpConnection::SetupSSLProxyConnect() NS_ENSURE_TRUE(!mSSLProxyConnectStream, NS_ERROR_ALREADY_INITIALIZED); nsCAutoString buf; - buf.Assign(mConnInfo->Host()); - buf.Append(':'); - buf.AppendInt(mConnInfo->Port()); + nsresult rv = nsHttpHandler::GenerateHostPort( + nsDependentCString(mConnInfo->Host()), mConnInfo->Port(), buf); + if (NS_FAILED(rv)) + return rv; // CONNECT host:port HTTP/1.1 nsHttpRequestHead request; diff --git a/netwerk/protocol/http/src/nsHttpHandler.cpp b/netwerk/protocol/http/src/nsHttpHandler.cpp index 8dadafd136c..408a011a90f 100644 --- a/netwerk/protocol/http/src/nsHttpHandler.cpp +++ b/netwerk/protocol/http/src/nsHttpHandler.cpp @@ -517,6 +517,31 @@ nsHttpHandler::OnChannelRedirect(nsIChannel* oldChan, nsIChannel* newChan, return rv; } +/* static */ nsresult +nsHttpHandler::GenerateHostPort(const nsCString& host, PRInt32 port, + nsCString& hostLine) +{ + if (strchr(host.get(), ':')) { + // host is an IPv6 address literal and must be encapsulated in []'s + hostLine.Assign('['); + // scope id is not needed for Host header. + int scopeIdPos = host.FindChar('%'); + if (scopeIdPos == kNotFound) + hostLine.Append(host); + else if (scopeIdPos > 0) + hostLine.Append(Substring(host, 0, scopeIdPos)); + else + return NS_ERROR_MALFORMED_URI; + hostLine.Append(']'); + } + else + hostLine.Assign(host); + if (port != -1) { + hostLine.Append(':'); + hostLine.AppendInt(port); + } +} + //----------------------------------------------------------------------------- // nsHttpHandler //----------------------------------------------------------------------------- diff --git a/netwerk/protocol/http/src/nsHttpHandler.h b/netwerk/protocol/http/src/nsHttpHandler.h index 026b40a727a..13dbca48116 100644 --- a/netwerk/protocol/http/src/nsHttpHandler.h +++ b/netwerk/protocol/http/src/nsHttpHandler.h @@ -203,6 +203,12 @@ public: { NotifyObservers(chan, NS_HTTP_ON_EXAMINE_CACHED_RESPONSE_TOPIC); } + + // Generates the host:port string for use in the Host: header as well as the + // CONNECT line for proxies. This handles IPv6 literals correctly. + static nsresult GenerateHostPort(const nsCString& host, PRInt32 port, + nsCString& hostLine); + private: //