This is a small HTTP/1.1 server written in Retro Forth.
First, some configuration options. Since this will run under inetd there's no need to specify the port. But the path to the files to serve is rather useful, so define it here.
Next, I need to handle the incoming requests. In v0 these were just discarded, but here we actually want to store the request
So an incoming request will look like:
GET / HTTP/1.1 Host: retroforth.org
With the lines ending in a CR,LF sequence.
I need to allocate space for the data I care about. There are three items:
• The Requested file
• The desired virtual Host
• The query string (if any)
The header processor will read each item and store the Host and Requested file. Everything else is ignored.
I implement eot? to decide if a line (or field) indicator has been reached. This is used by s:get to decide when the input should stop. s:get records the characters read into the specified buffer. And finally, read-request reads the input.
Next is reading in the desired file. An initial request may be just a /. In this case, Casket will replace the Requested filename with /index.html. In the odd case that a file is requested without a leading /, I have a word to add this. And then a word that constructs a filename.
This also has a word check-for-params that is used to separate the requested file from any query string that may be present.
Next, I need to determine the file type. I'll do this by taking a look at the extension, and mapping this to a MIME type.
Using these, I can construct a word to read in the file and send it to the client.
Reading files is now a bit more involved, since images and other formats have binary data.
If the mime type is application/fsp, this will run the code in the file. The code should output the necessary headers and content.
In the above, eol will send an end of line sequence.
The last support word is a handler for 404 errors. This will send the 404 status code and a human readable error message.
And now for the top level server.
Receive a request:
See if the file exists:
Send an "200 OK" response and the file (or a 404 if the file wasn't found):
And the code for Casket is done.
Casket requires [Retro](http://forthworks.com/retro) and a Unix system with inetd.
Install Retro and put the casket.forth somewhere. Then add a configuration line to your /etc/inetd.conf. I use:
http stream tcp nowait/6/30/2 casket /home/crc/servers/casket.forth
Edit the WEBROOT in casket.forth to point to your web directory. Then go to the web directory and create a directory for each domain. E.g.,
Put your index.html and other files here and try accessing your website.
• [retro forth](http://forthworks.com/r/latest.tar.gz)
Casket has been in use since the second half of 2018 serving a number of small websites. It's also used to host the Casket project page you are looking at.
Copyright (c) 2018 - 2019, Charles Childers
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.