By default, simple-httpd starts listening on localhost. If (system-name) is not a name that resolves to 127.0.0.1 or ::1, which is the case here on my macOS system, the connection will fail since simple-httpd is only listening on the loopback address. For example, if (system-name) returns "foo.example.com", the URL http://foo.example.com:8080/imp/ will fail because foo.example.com is actually resolving to my LAN address, not the loopback address, whereas simple-httpd is only listening on loopback.
To avoid this problem, we interrogate the server socket set up by simple-httpd to see what :host it was created with (see make-network-process), identify when that is one of the values that means "localhost", and just use the name "localhost" in those cases. We'll also use "localhost" when you bind to 0.0.0.0 or ::.
Since I had already pulled back the process-contact info for simple-httpd's socket, I went ahead and pulled the port out of that as well.
I decided to use process-contact instead of httpd-host and httpd-port because, if you have somehow managed to change httpd-host and/or httpd-port since starting simple-httpd, we'll still pick up the actual, correct values in use by the server that's running right now.
By default, simple-httpd starts listening on localhost. If
(system-name)
is not a name that resolves to 127.0.0.1 or ::1, which is the case here on my macOS system, the connection will fail since simple-httpd is only listening on the loopback address. For example, if(system-name)
returns "foo.example.com", the URLhttp://foo.example.com:8080/imp/
will fail because foo.example.com is actually resolving to my LAN address, not the loopback address, whereas simple-httpd is only listening on loopback.To avoid this problem, we interrogate the server socket set up by simple-httpd to see what
:host
it was created with (seemake-network-process
), identify when that is one of the values that means "localhost", and just use the name "localhost" in those cases. We'll also use "localhost" when you bind to 0.0.0.0 or ::.Since I had already pulled back the
process-contact
info for simple-httpd's socket, I went ahead and pulled the port out of that as well.I decided to use
process-contact
instead ofhttpd-host
andhttpd-port
because, if you have somehow managed to changehttpd-host
and/orhttpd-port
since starting simple-httpd, we'll still pick up the actual, correct values in use by the server that's running right now.