Skip to content

net/http: ProxyFromEnvironment should return nil for ip.IsUnspecified #24737

Closed
@glasser

Description

@glasser

What version of Go are you using (go version)?

go1.10.1

Does this issue reproduce with the latest release?

Yes, from code inspection the issue exists on master.

What operating system and processor architecture are you using (go env)?

darwin amd64 (shouldn't be specific)

What did you do?

  • Set a broken HTTP_PROXY environment variable
  • Run a web server listening on the unspecified interface (note that I specify tcp4 but this happens with tcp4 or tcp6)
  • Attempt to use the http client to talk to the web server
  • Get an error because it tried to use $HTTP_PROXY

https://play.golang.org/p/zVHDyxwZREM

What did you expect to see?

I expected to see the same result as if there was no $HTTP_PROXY, because $HTTP_PROXY is ignored for loopback IPs:

2009/11/10 23:00:00 0.0.0.0:2
2009/11/10 23:00:00 200 OK

What did you see instead?

2009/11/10 23:00:00 0.0.0.0:2
2009/11/10 23:00:00 Get http://0.0.0.0:2/bar: proxyconnect tcp: dial tcp: Protocol not available

Discussion

This is either a bug report for http.ProxyFromEnvironment, or a request for a new helper function in net/http. I am happy to submit a CL for either one, but I'm not sure which is right.

It seems to me that what I'm trying to do is a pretty common operation, especially for testing and local development: listen on the unspecified port/interface, get the listen address, and connect to it over HTTP. This seems to mostly work, but it doesn't work if you have a corporate proxy set up with $HTTP_PROXY.

So either I have a bug report: http.ProxyFromEnvironment should return nil for IPs that are "unspecified" just like it does for IPs that are "loopback"

Or I have a feature request: There should be an easy way to map from the Addr you get from net.Listener.Addr() to a fully-functional HTTP URL (or host/port pair suitable for use in an HTTP URL). Specifically this method would change the unspecified IPv4 address to the loopback IPv4 address, and the unspecified IPv6 address to the loopback IPv6 address. Certainly I can write this code manually but it seems like something that others using net.Listen will run into.

My suspicion is that I'm looking for the feature request here, because even though URLs like http://[::]:4000/ and http://0.0.0.0:4000/ work with Go's net/http client (in the absence of proxy env vars), they might not work as well with other ecosystems' implementations, so being able to generate a more broadly usable URL might be a good idea. On the other hand, this might just be a fool's errand; for example, I've learned that CircleCI's default environment returns :: from net.Listen but doesn't support connecting to ::1 (connecting to :: does work, modulo the proxy issue).

But I think the bug may still be a bug, since it's unclear to me when you would ever want to send the unspecified IP to a corporate HTTP proxy.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions