1

I have a very simple netcat webserver set up to display the /etc/motd file for some machines, which works fine in Firefox, but intermittently fails in Chrome. I suspect it's an HTTP header / caching issue, but I'm not sure what I need to do to make Chrome behave.

My script:

#!/bin/bash

while true
do { 
  echo -e "HTTP/1.1 200 OK\r\nContent-Length: $(cat -s /etc/motd | wc -c)\r\nContent-Type: text/plain\r\n\r\n"
  cat -s /etc/motd;
} | nc -l 2020
done

Which serves up:

HTTP/1.1 200 OK
Content-Length: 252
Content-Type: text/plain

.../etc/motd file contents....

Firefox seems to handle this fine, as does Chrome sometimes, but other times it returns:

This webpage is not available

Google Chrome's connection attempt to machine.example.com was rejected. The website may be down, or your network may not be properly configured.

I haven't seen any particular pattern causing the connection to fail, but my current theory is there's some sort of additional header Chrome expects to see, or perhaps the connection is being closed abruptly which Chrome assumes means the machine is down and doesn't actually re-try it.

1 Answer 1

4

netcat was not designed for scalability, merely a debugging tool. If the connection is closed, then nc stops listening. Since you have an infinite loop that starts nc again, you often can open the page. Keep in mind that most browsers initially request a resource (for example, /) but also /favicon.ico. That is a case that this construct may not handle well due to the short period where nc does not run.

If you need a simple webserver, you can use Python. For example, to serve the current directory:

python -m SimpleHTTPServer 2020

If you are using Python 3, you need to run:

python -m http.server 2020

Documentation for BaseHTTPServer, you only need to add a request handler.

1
  • Yes sorry, they were supposed to be double quotes. The header output in my question is exactly what my browser is seeing.
    – dimo414
    Sep 5, 2012 at 17:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .