Who is wrong about the http://0:port?
Asked Answered
E

2

7

The Plack suite commonly uses the http://0:port. E.g. the following

plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");'

prints

HTTP::Server::PSGI: Accepting connections at http://0:5000/

However, the LWP::UserAgent (or some deeper called modules) didn't accepts it, e.g. the:

perl -MLWP::UserAgent -E '$u=LWP::UserAgent->new;$res=$u->get("http://0:5000/valid/path");print $res->status_line'

prints:

500 No Host option provided

but the

perl -MLWP::UserAgent -E '$u=LWP::UserAgent->new;$res=$u->get("http://localhost:5000/valid/path");print $res->status_line'

prints

200 OK

The question is: who is wrong?

  • Is the http://0:port valid, e.g. the LWP is "wrong"
  • or it isn't valid and the PSGI uses it as only "randomly valid" shortcut?
Embarkment answered 6/7, 2015 at 9:51 Comment(4)
Localhost is not address 0, it's (typically) 127.0.0.1. Try pinging localhost to see the actual address.Exhortation
This is relevant: lists.scsys.co.uk/pipermail/catalyst/2012-April/028434.htmlSchelling
@JoachimPileborg ok, edited the question title. :) The question's merit is remain: the url http://0:5000 is valid or no? Could/should be used or no?Embarkment
Address 0 is the wildcard address used by servers when they want to listen to connections from all interfaces. It makes no sense trying to connect to it.Exhortation
G
6

The output of the Plack suite is the output of a server. A server typically binds a socket to a certain port and address in order to serve content there. The notation http://0:port means in this case: listen on port port on all addresses of this machine. This is handy if you don't know or don't want to specify all addresses of the machine where the server should be reachable.

The output of the LWP::UserAgent ist the output of a client. In order to open a connection to a server, you must explicitly specify the address and the port to connect to. 0 is no valid IP address, therefore the connection fails when you connect to http://0:port.

Gothart answered 6/7, 2015 at 10:21 Comment(4)
The http://0:port is an URL. So, the http://0:5000 isn't valid URL and it is "only" handy. Right?Embarkment
it's no valid URL. It's just the result of a printf somewhere in the code of Plack. A server doesn't bind to a URL but to the combination of IP address and port.Gothart
okay, understand the bind - but the PSGI prints an URL, e.g. http://0:5000 . ok - now know - it prints an invalid URL, but it is handy in many situations - many browsers just translates it to 127.0.0.1. Thanx. :)Embarkment
While the answer is correct it should contain: the printed URL isn't valid. (and imho, using the http://*:5000 would be more clear - however it isn't as handy as 0:5000. :) +1 anyway :)Daric
I
0

Safari 11, curl and wget resolve http://0:5000 to http://0.0.0.0:5000 and connect to the localhost.

I just tested it, after seeing the URL and finding the answer to the question dissatisfying.

Impetrate answered 14/11, 2017 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.