1

There is an answer that serves a file over HTTP using nc

{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat some.file; } | nc -l -p 8080
{ echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <some.file)\r\n\r\n"; cat some.file; } | nc -l -p 8080

A comment says:

Chrome won't stop throbbing unless you use the second nc command.

Also, not both of the nc commands show will quit after responding. You can keep restarting it with: while :; do { echo -ne "HTTP/1.0 200 OK\r\nContent-Length: $(wc -c <some.file)\r\n\r\n"; cat some.file; } | nc -l -p 8080; done. Use ctrl+z to but it to sleep.

Does throbbing mean that a web browser keeps automatically reload the URL?

Is the purpose of the second nc command that adds Content-Length: $(wc -c <some.file) to the first nc command to stop Chrome from throbbing?

URL http://IP:8080/ followed by nothing or anything will work in a web browser to get the file. Is this flexibility provided by HTTP protocol, nc, or the web browser?

Thanks.

Tim
  • 101,790

2 Answers2

1

Chrome keeps going with the first version of the command because it doesn't know how large the file being sent is, so it didn't know when it has received it all, the throbbing means it's still waiting to see if there is any more.

The second version adds the HTTP header with the size of the payload before the files content, so chrome now knows how much more data it is expecting and marks the transaction as complete when the end of the file is reached.

nc combined with the HTTP/1.0 200 OK\r\n is implementing the ABSOLUTE bare minimum (it's not really, but come is being very generous in what it accepts) of the HTTP protocol requires to send something, adding the Content-Length moves a little closer to the spec. The HTTP protocol is well documented, I suggest you read the rfc

EDIT:

  1. Throbbing means that the browser doesn't think it's finished loading the page. So until chrome can be sure it has all the file it will keep throbbing.
  2. You need to go read about what makes up a URL, but the part after the port number points to a resource you want the server to provide. In this case the server just doesn't care what you ask for it is always going to return some.file
  3. I can't really answer that, I expect it's a typo and should be note instead of not
hardillb
  • 408
  • 2
  • 10
  • Thanks. (1) Could you be specific about "Chrome keeps going with the first version of the command"? What does throbbing mean? (2) URL http://IP:8080/ followed by nothing or anything will work in a web browser to get the file. Is this flexibility provided by HTTP protocol, nc, or the web browser? (3) in the quoted comment, does "not both of the nc commands show will quit after responding" mean without "not" instead? – Tim Dec 04 '19 at 13:24
  • See the edit for those answers. Basically this is a nasty hack that takes advantage of Chrome being very lenient with what it accepts back from the "server". If everything followed the spec to the absolute letter it wouldn't work. – hardillb Dec 04 '19 at 13:36
  • (1) I was wondering "Throbbing means that the browser doesn't think it's finished loading the page. So until chrome can be sure it has all the file it will keep" what? (2) "In this case the server just doesn't care what you ask for it is always going to return some.file". So is the flexibility provided by the web server (the nc command), not by the HTTP protocol? – Tim Dec 04 '19 at 13:36
  • (3) "Basically this is a nasty hack that takes advantage of Chrome being very lenient with what it accepts back from the server. If everything followed the spec to the absolute letter it wouldn't work." Because of what, would it not work? – Tim Dec 04 '19 at 13:37
  • The first sentence in that comment do seem a bit odd, I don't think it's correct English, maybe there IS typo – 炸鱼薯条德里克 Dec 06 '19 at 02:05
1

In HTTP/1.0, if the Content-Length header is missing in the response, the end of the response is instead indicated by closing the socket. nc doesn't do this by default, and so chrome (or any other browser) waits ("throbs") "forever" for this to happen.

Add the -N parameter to nc in the first command and it should work fine:

{ echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat some.file; } | nc -N -l -p 8080

Please note that a Content-Length header is not required in HTTP/1.0 (rfc1945 section 7.2.2) unless a body is expected and it is empty (section 7.2).

The URL http://IP:8080/ makes the browser send anything after the IP and port to the server, in this case just the last / as the indication of what it wants from the server. In the example nc commands, the / (or anything else really) sent by the browser is totally ignored and the response is always the same. So you could use http://IP:8080/hello/world.png with the same result.

Jonas Berlin
  • 1,124