2019-10-29 18:24:48 +01:00
|
|
|
#!/usr/bin/env retro
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
# Atua-WWW: Serving Gopher to Web Browsers
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
Atua is a Gopher server written in Retro. I've used it for a few
|
|
|
|
years, but most people don't use Gopher clients these days. This
|
|
|
|
is a variation of Atua that translates the Gopher directories to
|
|
|
|
HTML and speaks enough HTTP to serve files to the bloated
|
|
|
|
browsers in use today.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
This will get run as an inetd service, which keeps things simple
|
|
|
|
as it prevents needing to handle socket I/O directly.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
Atua-WWW uses Retro's *retro-unix* interface layer. Designed to
|
|
|
|
run a single program then exit, this makes using Retro very
|
|
|
|
useful on Unix (or Linux, Haiku, or other POSIX-ish systems).
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
# Configuration
|
|
|
|
|
|
|
|
Atua needs to know:
|
|
|
|
|
|
|
|
- the path to the files to serve
|
|
|
|
- the name of the index file
|
|
|
|
- The maximum length of a selector
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-20 15:37:36 +02:00
|
|
|
'/home/crc/atua 'PATH s:const
|
|
|
|
'/gophermap 'DEFAULT-INDEX s:const
|
2020-11-25 20:55:16 +01:00
|
|
|
#1024 'MAX-SELECTOR-LENGTH const
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
# I/O Words
|
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
Retro only supports basic output by default. The RRE interface
|
|
|
|
that Atua uses adds support for files and stdin, so we map these
|
|
|
|
to words and provide some other helpers.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
## Console Output
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
The Gopher protocol uses tabs and cr/lf for signficant things.
|
|
|
|
To aid in this, I define output words for tabs and end of line.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2018-05-07 18:24:36 +02:00
|
|
|
:eol (-) ASCII:CR c:put ASCII:LF c:put ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
## Console Input
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
Input lines end with a cr, lf, or tab. The `eol?` checks for
|
|
|
|
this. The `s:get` word could easily be made more generic in
|
|
|
|
terms of what it checks for. This suffices for a Gopher server
|
|
|
|
though.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:eol? (c-f)
|
2020-11-25 20:55:16 +01:00
|
|
|
[ [ ASCII:CR eq? ]
|
|
|
|
[ ASCII:LF eq? ]
|
|
|
|
[ ASCII:SPACE eq? ] tri or or ]
|
|
|
|
[ $? eq? ] bi or ;
|
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
:s:get (a-)
|
2017-10-17 03:32:30 +02:00
|
|
|
buffer:set
|
2018-05-07 18:24:36 +02:00
|
|
|
[ c:get dup buffer:add eol? not ] while ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
{{
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2020-11-25 20:55:16 +01:00
|
|
|
First up are buffers for the selector string and the file buffer.
|
|
|
|
The variables and buffers are kept private.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Selector d:create
|
|
|
|
MAX-SELECTOR-LENGTH n:inc allot
|
|
|
|
:buffer here ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
Next up, variables to track information related to the requested
|
|
|
|
selector. Atua will construct filenames based on these.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Requested-File var
|
|
|
|
'Requested-Index var
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
`FID`, the file id, tracks the open file handle that Atua uses
|
|
|
|
when reading in a file. The `Size` variable will hold the size of
|
|
|
|
the file (in bytes).
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'FID var
|
|
|
|
'Size var
|
|
|
|
'Mode var
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
I use a `Server-Info` variable to decide whether or not to display
|
|
|
|
the index footer. This will become a configurable option in the
|
|
|
|
future.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Server-Info var
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
These are just simple accessor words to aid in overall readability.
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:requested-file (-s) @Requested-File ;
|
|
|
|
:requested-index (-s) @Requested-Index ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:get-mime-type (s-s)
|
2021-06-04 20:34:59 +02:00
|
|
|
[ $. s:index/char ] sip +
|
2017-10-17 03:32:30 +02:00
|
|
|
(textual_files)
|
|
|
|
'.md [ 'text/markdown ] s:case
|
|
|
|
'.htm [ 'text/html ] s:case
|
|
|
|
'.html [ 'text/html ] s:case
|
2019-12-10 22:52:40 +01:00
|
|
|
'.retro.html [ 'text/html ] s:case
|
|
|
|
'.muri.html [ 'text/html ] s:case
|
|
|
|
'.forth.html [ 'text/html ] s:case
|
2017-10-17 03:32:30 +02:00
|
|
|
'.css [ 'text/css ] s:case
|
|
|
|
(image_files)
|
|
|
|
'.png [ 'image/png ] s:case
|
|
|
|
'.jpg [ 'image/jpeg ] s:case
|
|
|
|
'.jpeg [ 'image/jpeg ] s:case
|
|
|
|
'.gif [ 'image/gif ] s:case
|
|
|
|
'.bmp [ 'image/bmp ] s:case
|
|
|
|
(application_files)
|
|
|
|
'.pdf [ 'application/pdf ] s:case
|
|
|
|
'.gz [ 'application/gzip ] s:case
|
|
|
|
'.zip [ 'application/zip ] s:case
|
|
|
|
'.json [ 'application/json ] s:case
|
|
|
|
'.js [ 'application/x-javascript ] s:case
|
|
|
|
'.xml [ 'application/xml ] s:case
|
|
|
|
drop 'text/plain ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:with-path (-s)
|
|
|
|
PATH &Selector s:chop s:append ;
|
2020-11-25 20:55:16 +01:00
|
|
|
|
2017-10-17 03:32:30 +02:00
|
|
|
:construct-filenames (-)
|
|
|
|
with-path s:keep !Requested-File
|
|
|
|
with-path '/gophermap s:append s:keep !Requested-Index
|
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
A *gophermap* is a file that makes it easier to handle Gopher menus.
|
|
|
|
Atua's gophermap support covers:
|
|
|
|
|
|
|
|
- comment lines
|
|
|
|
|
|
|
|
Comment lines are static text without any tabs. They will be
|
|
|
|
reformatted according to protocol and sent.
|
|
|
|
|
|
|
|
- selector lines
|
|
|
|
|
|
|
|
Any line with a tab is treated as a selector line and is transferred
|
|
|
|
without changing.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
'Tab var
|
|
|
|
:eol? [ ASCII:LF eq? ] [ ASCII:CR eq? ] bi or ;
|
|
|
|
:tab? @Tab ;
|
|
|
|
:check-tab
|
|
|
|
dup ASCII:HT eq? [ &Tab v:on ] if ;
|
2018-05-07 18:24:36 +02:00
|
|
|
:gopher:s:get (a-)
|
2017-10-17 03:32:30 +02:00
|
|
|
&Tab v:off
|
|
|
|
buffer:set
|
|
|
|
[ @FID file:read dup buffer:add check-tab eol? not ] while
|
|
|
|
buffer:get drop
|
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
The internal helpers are now defined, so switch to the part of the
|
|
|
|
namespace that'll be left exposed to the world.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
---reveal---
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
An information line s:get a format like:
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
i...text...<tab><tab>null.host<tab>port<cr,lf>
|
|
|
|
|
|
|
|
The `gopher:i` displays a string in this format. It's used later for
|
|
|
|
the index footer.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:i (s-)
|
2020-11-25 20:55:16 +01:00
|
|
|
s:put eol ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
The `gopher:icon` displays an indicator for menu items.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:icon
|
2020-11-25 20:55:16 +01:00
|
|
|
$0 [ '_TXT__ s:put ] case
|
2018-05-07 18:24:36 +02:00
|
|
|
$1 [ '[DIR]_ s:put ] case
|
2020-11-25 20:55:16 +01:00
|
|
|
$2 [ '_CSO__ s:put ] case
|
|
|
|
$3 [ '_ERR__ s:put ] case
|
|
|
|
$4 [ '_BIN__ s:put ] case
|
|
|
|
$5 [ '_BIN__ s:put ] case
|
|
|
|
$6 [ '_UUE__ s:put ] case
|
2018-05-07 18:24:36 +02:00
|
|
|
$7 [ '[FND]_ s:put ] case
|
2020-11-25 20:55:16 +01:00
|
|
|
$8 [ '_TEL__ s:put ] case
|
|
|
|
$9 [ '_BIN__ s:put ] case
|
|
|
|
$I [ '_IMG__ s:put ] case
|
|
|
|
$S [ '_AUD__ s:put ] case
|
|
|
|
$g [ '_GIF__ s:put ] case
|
|
|
|
$h [ '_HTM__ s:put ] case
|
|
|
|
drop '_???__ s:put
|
2017-10-17 03:32:30 +02:00
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:get-selector (-)
|
2018-05-07 18:24:36 +02:00
|
|
|
&Selector s:get &Selector s:get ;
|
2020-11-09 22:11:24 +01:00
|
|
|
|
2017-10-17 03:32:30 +02:00
|
|
|
(Rewrite_This:_It's_too_big)
|
|
|
|
:gopher:file-for-request (-s)
|
|
|
|
&Mode v:off
|
|
|
|
construct-filenames
|
|
|
|
&Selector s:chop s:length n:-zero?
|
|
|
|
[ requested-file file:exists?
|
|
|
|
[ requested-file file:R file:open file:size n:strictly-positive? ] [ FALSE ] choose
|
|
|
|
[ requested-file ]
|
|
|
|
[ requested-index file:exists?
|
|
|
|
[ requested-index &Server-Info v:on ]
|
|
|
|
[ PATH '/empty.index.html s:append ] choose
|
|
|
|
] choose
|
|
|
|
]
|
|
|
|
[ PATH DEFAULT-INDEX s:append &Server-Info v:on ] choose
|
|
|
|
;
|
2018-03-24 14:44:15 +01:00
|
|
|
|
2018-12-19 02:05:13 +01:00
|
|
|
:gopher:read-file (f-)
|
2017-10-17 03:32:30 +02:00
|
|
|
file:R file:open !FID
|
|
|
|
@FID file:size !Size
|
2018-12-19 02:05:13 +01:00
|
|
|
@Size [ @FID file:read c:put ] times
|
2017-10-17 03:32:30 +02:00
|
|
|
@FID file:close
|
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:link
|
|
|
|
dup fetch $h eq? push
|
|
|
|
dup fetch gopher:icon n:inc
|
|
|
|
[ ASCII:HT [ #0 ] case ] s:map
|
|
|
|
dup s:length over + n:inc
|
|
|
|
pop [ #4 + ] if
|
2020-11-25 20:55:16 +01:00
|
|
|
'<a_href="%s">%s</a> s:format s:put ;
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
:gopher:generate-index (f-)
|
2018-05-07 18:24:36 +02:00
|
|
|
'Content-type:_text/html s:put eol eol
|
|
|
|
'<!DOCTYPE_HTML_PUBLIC_"-//W3C//DTD_HTML_4.01//EN" s:put sp
|
|
|
|
'"http://www.w3.org/TR/html4/strict.dtd"> s:put eol
|
2020-11-25 20:55:16 +01:00
|
|
|
{ '<html><head>
|
|
|
|
'<meta_http-equiv="Content-Type"_content="text/html;_charset=utf-8">
|
|
|
|
'<link_href=data:,_rel=icon>
|
|
|
|
'<style_type="text/css">p_{_font-family:_monospace;_white-space:_pre;_}
|
|
|
|
'_*_{_color:_#bbb;_background:_#090909;_font-size:_large;_}
|
|
|
|
'a_{_color:_#FF6600;_}
|
|
|
|
'</style><title>forthworks.com</title></head>
|
|
|
|
} [ s:put eol ] a:for-each
|
|
|
|
'<body><p> s:put
|
2017-10-17 03:32:30 +02:00
|
|
|
file:R file:open !FID
|
|
|
|
@FID file:size !Size
|
2018-05-07 18:24:36 +02:00
|
|
|
[ buffer gopher:s:get
|
2020-11-25 20:55:16 +01:00
|
|
|
buffer tab? [ link eol ] [ gopher:i ] choose
|
2017-10-17 03:32:30 +02:00
|
|
|
@FID file:tell @Size lt? ] while
|
|
|
|
@FID file:close
|
2018-05-07 18:24:36 +02:00
|
|
|
'</p></body></html> s:put
|
2017-10-17 03:32:30 +02:00
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
2018-05-07 18:24:36 +02:00
|
|
|
In a prior version of this I used `s:put` to send the content. That
|
2017-10-17 03:32:30 +02:00
|
|
|
stopped at the first zero value, which kept it from working with
|
|
|
|
binary data. I added `gopher:send` to send the `Size` number of
|
|
|
|
bytes to stdout, fixing this issue.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:send (p-)
|
2018-12-19 02:05:13 +01:00
|
|
|
requested-file get-mime-type 'Content-type:_ s:put s:put eol eol ;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
The only thing left is the top level server.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
:gopher:server
|
|
|
|
gopher:get-selector
|
2018-05-07 18:24:36 +02:00
|
|
|
'HTTP/1.0_200_OK s:put eol
|
2017-10-17 03:32:30 +02:00
|
|
|
gopher:file-for-request
|
|
|
|
@Server-Info
|
2018-03-24 14:58:13 +01:00
|
|
|
[ gopher:generate-index ]
|
2018-12-19 02:05:13 +01:00
|
|
|
[ gopher:send gopher:read-file ] choose
|
2017-10-17 03:32:30 +02:00
|
|
|
;
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
Close off the helper portion of the namespace.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
}}
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
|
|
|
|
And run the `gopher:server`.
|
|
|
|
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|
2017-10-17 03:32:30 +02:00
|
|
|
gopher:server
|
|
|
|
reset
|
2018-03-24 14:58:13 +01:00
|
|
|
~~~
|