Search code examples
perllwppsgi

Who is wrong about the http://0:port?


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?

Solution

  • 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.