25 Dec 2017

Gophermoon 0.2 - a Gopher-Server in Lua – Part 3

Gophermoon on Github

the sourcecode is now on Github @

With every blog post, I will post the link to the commit associated with the blog post.

Serving static text files

Today Gophermoon learned how to serve static text files. The program first looks at the selector send by the client. If the selector is empty, and uses the root directory.

--- read the selector from the client
selector =

--- remove CR (13/$0D) from input
selector=selector:sub(1, -2)

--- if an empty selector has been send, use the root
if selector == '' then
   selector = "/"

--- make selector relative to the gopher root
filepath = gopherroot .. selector

Next, if the selector points to a directory, it looks if there is a file names gophermap in the direcory. A gophermap file is a Gopher-Document describing the resources available in this directory (as well as links outside the directory). Gophermoon uses the same gophermap format as Gophernicus (see

Welcome to GopherMoon @
0Gophermoon 0.1 - a Gopher-Server in Lua -- Part 1      /gophermoon01.txt   70
0Gophermoon 0.1 - a Gopher-Server in Lua -- Part 2      /gophermoon02.txt   70
0Gophermoon 0.2 - a Gopher-Server in Lua -- Part 3      /gophermoon03.txt   70
0Gophermoon Sourcecode  /gophermoon   70

Lines that do not contain a tabulator are formatted as "i" (Information) file type.

--- if the selector points to a directory,
--- do we have a "gophermap" file? If "yes",
--- send that file. Else, if the selector
--- points to a (text-) file, send it
--- if both are false, send an error message
if isDir(filepath) then
   gophermap = filepath .. "/gophermap"
   if isReadable(gophermap) then
   if isReadable(filepath) then
      send_info("Welcome to GopherMoon @")
      send_info("Error: Selector not found")

If the selector isn't a directory, the program checks the selector is a readable file, and if it is, it sends the file gopher-style (with CR/LF and terminating the file with ".").

--- send a Textfile Gopher-style
function send_textfile(filepath)
   if f~=nil then
      for line in io.lines(filepath) do
	 io.write(line .. "\r\n")

The commit from today is

Other posts
Creative Commons License by Carsten Strotmann is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License .