39
26
Your challenge is to write a code-golf HTTP server that accepts GET requests. It obviously doesn't have to be fully featured, but it must serve files from a directory.
Rules:
- The HTTP server must listen on TCP port 36895 (0x901F)
- It must serve files from
/var/www
on *NIX systems (e.g. Linux), orC:\hgolf
on Windows. - You may ignore all incoming HTTP headers except the
GET
itself. - If the HTTP method is not GET, you must send back a status code of "405 Not Supported" and a body of "405 Not Supported".
- If the file does not exist, you must send back a status code of "404 File Not Found" and a body of "404 File Not Found".
- If the file exists but could not be read for some reason, you must send back a status code of "500 Server Error" and a body of "500 Server Error".
- If the user requests
/
or any other existing directory root (e.g./foo/
where a directoryfoo
exists in/var/www/
), respond with a blank page. - Your response must contain at least the minimum headers to allow the content to be displayed on Firefox 8.0 and Internet Explorer 8.0
- You must respond with the
Content-Type
header set, but you only have to support extensionshtml => text/html
andtxt => text/plain
. For any other file extension, Sendapplication/octet-stream
as the content type. - Your code must be able to transfer both ASCII and binary data, though you do not explicitly have to distinguish between the two.
- You may not use 3rd party libraries.
- You may not use in-built classes or features designed to process HTTP requests (e.g.
HttpListener
in C#) - If your code will only work on a specific OS due to the socket APIs you've used, please state this.
Solutions must include an image showing it serving a HTML page to a browser.
If you've got any questions, please feel free to ask! :)
3This should include a big caveat: do not expose any of these solutions to a public network interface! Since the aim is code golf rather than security, they will be dangerously insecure. (E.g. I expect that all of them would allow
..
in the path as a way of breaking out of the defined document root). – Peter Taylor – 2013-03-02T12:02:22.4408Someone always invents a better fool. – Peter Taylor – 2013-03-02T15:11:48.043
21@PeterTaylor So I should stop using my solution to serve my blog? :-O – Gareth – 2013-03-02T18:33:53.067
is the
http
module in Node.js ok? – markasoftware – 2014-01-02T22:26:03.433@Markasoftware You may not use in-built classes or features designed to process HTTP requests (e.g. HttpListener in C#) – nyuszika7h – 2014-06-22T10:41:07.860