skeeto / impatient-mode

Impatient html mode. See your changes in the browser as you type
215 stars 19 forks source link

Use localhost when appropriate in imp-visit-buffer #28

Closed dsedivec closed 4 years ago

dsedivec commented 4 years ago

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.

skeeto commented 4 years ago

Clever solution, thanks!