2020-04-17 15:39:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bufio"
|
2020-05-03 21:42:51 +00:00
|
|
|
"os"
|
2020-04-17 15:39:25 +00:00
|
|
|
)
|
|
|
|
|
2020-05-04 17:19:56 +00:00
|
|
|
type FileContents interface {
|
|
|
|
/* Interface that provides an adaptable implementation
|
|
|
|
* for holding onto some level of information about the
|
|
|
|
* contents of a file.
|
|
|
|
*/
|
2020-05-07 07:59:53 +00:00
|
|
|
Render(*Responder) *GophorError
|
|
|
|
Load() *GophorError
|
2020-05-04 17:19:56 +00:00
|
|
|
Clear()
|
|
|
|
}
|
|
|
|
|
2020-04-25 18:56:03 +00:00
|
|
|
type GeneratedFileContents struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Super simple, holds onto a slice of bytes */
|
|
|
|
|
|
|
|
Contents []byte
|
2020-04-25 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 07:59:53 +00:00
|
|
|
func (fc *GeneratedFileContents) Render(responder *Responder) *GophorError {
|
2020-05-15 12:29:04 +00:00
|
|
|
return responder.WriteData(fc.Contents)
|
2020-04-25 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *GeneratedFileContents) Load() *GophorError {
|
|
|
|
/* do nothing */
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *GeneratedFileContents) Clear() {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:40:34 +00:00
|
|
|
type RegularFileContents struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Simple implemention that holds onto a RequestPath
|
|
|
|
* and slice containing cache'd content
|
|
|
|
*/
|
|
|
|
|
|
|
|
Path *RequestPath
|
|
|
|
Contents []byte
|
2020-04-22 12:40:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 07:59:53 +00:00
|
|
|
func (fc *RegularFileContents) Render(responder *Responder) *GophorError {
|
2020-05-15 12:29:04 +00:00
|
|
|
return responder.WriteData(fc.Contents)
|
2020-04-22 12:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *RegularFileContents) Load() *GophorError {
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Load the file into memory */
|
2020-04-22 12:40:34 +00:00
|
|
|
var gophorErr *GophorError
|
2020-05-08 15:52:17 +00:00
|
|
|
fc.Contents, gophorErr = bufferedRead(fc.Path.Absolute())
|
2020-04-22 12:40:34 +00:00
|
|
|
return gophorErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fc *RegularFileContents) Clear() {
|
2020-05-04 17:19:56 +00:00
|
|
|
fc.Contents = nil
|
2020-04-22 12:40:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
type GophermapContents struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a RequestPath and slice containing individually
|
|
|
|
* renderable sections of the gophermap.
|
|
|
|
*/
|
|
|
|
|
2020-05-14 16:54:06 +00:00
|
|
|
Request *Request
|
2020-05-08 15:52:17 +00:00
|
|
|
Sections []GophermapSection
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 07:59:53 +00:00
|
|
|
func (gc *GophermapContents) Render(responder *Responder) *GophorError {
|
2020-05-05 22:46:50 +00:00
|
|
|
/* Render and send each of the gophermap sections */
|
|
|
|
var gophorErr *GophorError
|
2020-05-04 17:19:56 +00:00
|
|
|
for _, line := range gc.Sections {
|
2020-05-07 07:59:53 +00:00
|
|
|
gophorErr = line.Render(responder)
|
2020-04-17 15:39:25 +00:00
|
|
|
if gophorErr != nil {
|
2020-05-05 22:46:50 +00:00
|
|
|
Config.SysLog.Error("", "Error executing gophermap contents: %s\n", gophorErr.Error())
|
2020-05-14 18:15:51 +00:00
|
|
|
return &GophorError{ InvalidGophermapErr, gophorErr }
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 17:21:21 +00:00
|
|
|
|
2020-05-05 22:46:50 +00:00
|
|
|
/* End on footer text (including lastline) */
|
2020-05-15 12:29:04 +00:00
|
|
|
return responder.WriteData(Config.FooterText)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *GophermapContents) Load() *GophorError {
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Load the gophermap into memory as gophermap sections */
|
2020-04-17 15:39:25 +00:00
|
|
|
var gophorErr *GophorError
|
2020-05-14 16:54:06 +00:00
|
|
|
gc.Sections, gophorErr = readGophermap(gc.Request)
|
2020-05-14 18:15:51 +00:00
|
|
|
if gophorErr != nil {
|
|
|
|
return &GophorError{ InvalidGophermapErr, gophorErr }
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gc *GophermapContents) Clear() {
|
2020-05-04 17:19:56 +00:00
|
|
|
gc.Sections = nil
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GophermapSection interface {
|
2020-05-04 17:19:56 +00:00
|
|
|
/* Interface for storing differring types of gophermap
|
2020-05-08 15:52:17 +00:00
|
|
|
* sections to render when necessary
|
2020-05-04 17:19:56 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-07 07:59:53 +00:00
|
|
|
Render(*Responder) *GophorError
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
type GophermapTextSection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
Contents []byte
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
func (s *GophermapTextSection) Render(responder *Responder) *GophorError {
|
2020-05-15 12:29:04 +00:00
|
|
|
return responder.WriteData(replaceStrings(string(s.Contents), responder.Host))
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
type GophermapDirectorySection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a directory path, and a list of files
|
|
|
|
* to hide from the client when rendering.
|
|
|
|
*/
|
|
|
|
|
2020-05-14 16:54:06 +00:00
|
|
|
Request *Request
|
2020-05-08 15:52:17 +00:00
|
|
|
Hidden map[string]bool
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
func (g *GophermapDirectorySection) Render(responder *Responder) *GophorError {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Create new responder from supplied and using stored path */
|
2020-05-14 16:54:06 +00:00
|
|
|
return listDir(responder.CloneWithRequest(g.Request), g.Hidden)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
type GophermapFileSection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a file path to be read and rendered when requested */
|
2020-05-14 16:54:06 +00:00
|
|
|
Request *Request
|
2020-05-07 07:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
func (g *GophermapFileSection) Render(responder *Responder) *GophorError {
|
2020-05-14 16:54:06 +00:00
|
|
|
fileContents, gophorErr := readIntoGophermap(g.Request.Path.Absolute())
|
2020-05-07 07:59:53 +00:00
|
|
|
if gophorErr != nil {
|
|
|
|
return gophorErr
|
|
|
|
}
|
2020-05-15 12:29:04 +00:00
|
|
|
return responder.WriteData(fileContents)
|
2020-05-07 07:59:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
type GophermapSubmapSection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a gophermap path to be read and rendered when requested */
|
2020-05-14 16:54:06 +00:00
|
|
|
Request *Request
|
2020-05-07 08:16:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GophermapSubmapSection) Render(responder *Responder) *GophorError {
|
|
|
|
/* Load the gophermap into memory as gophermap sections */
|
2020-05-14 16:54:06 +00:00
|
|
|
sections, gophorErr := readGophermap(g.Request)
|
2020-05-07 08:16:37 +00:00
|
|
|
if gophorErr != nil {
|
|
|
|
return gophorErr
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Render and send each of the gophermap sections */
|
|
|
|
for _, line := range sections {
|
|
|
|
gophorErr = line.Render(responder)
|
|
|
|
if gophorErr != nil {
|
|
|
|
Config.SysLog.Error("", "Error executing gophermap contents: %s\n", gophorErr.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type GophermapExecCgiSection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a request with CGI script path and supplied parameters */
|
|
|
|
Request *Request
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
func (g *GophermapExecCgiSection) Render(responder *Responder) *GophorError {
|
2020-05-04 17:19:56 +00:00
|
|
|
/* Create new filesystem request from mixture of stored + supplied */
|
2020-05-08 15:52:17 +00:00
|
|
|
return executeCgi(responder.CloneWithRequest(g.Request))
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
type GophermapExecFileSection struct {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Holds onto a request with executable file path and supplied arguments */
|
|
|
|
Request *Request
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 08:16:37 +00:00
|
|
|
func (g *GophermapExecFileSection) Render(responder *Responder) *GophorError {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Create new responder from supplied and using stored path */
|
|
|
|
return executeFile(responder.CloneWithRequest(g.Request))
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Read and parse a gophermap into separately cacheable and renderable GophermapSection */
|
2020-05-14 16:54:06 +00:00
|
|
|
func readGophermap(request *Request) ([]GophermapSection, *GophorError) {
|
2020-04-18 22:05:25 +00:00
|
|
|
/* Create return slice */
|
2020-04-17 15:39:25 +00:00
|
|
|
sections := make([]GophermapSection, 0)
|
2020-04-18 22:05:25 +00:00
|
|
|
|
2020-05-07 20:15:47 +00:00
|
|
|
/* Create hidden files map now in case dir listing requested */
|
2020-05-08 15:52:17 +00:00
|
|
|
hidden := map[string]bool{
|
2020-05-14 16:54:06 +00:00
|
|
|
request.Path.Relative(): true, /* Ignore current gophermap */
|
|
|
|
CgiBinDirStr: true, /* Ignore cgi-bin if found */
|
2020-05-08 15:52:17 +00:00
|
|
|
}
|
2020-04-18 22:05:25 +00:00
|
|
|
|
|
|
|
/* Keep track of whether we've already come across a title line (only 1 allowed!) */
|
|
|
|
titleAlready := false
|
|
|
|
|
2020-05-14 16:54:06 +00:00
|
|
|
/* Error setting within nested function below */
|
|
|
|
var returnErr *GophorError
|
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
/* Perform buffered scan with our supplied splitter and iterators */
|
2020-05-14 16:54:06 +00:00
|
|
|
gophorErr := bufferedScan(request.Path.Absolute(),
|
2020-04-17 15:39:25 +00:00
|
|
|
func(scanner *bufio.Scanner) bool {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
/* Parse the line item type and handle */
|
|
|
|
lineType := parseLineType(line)
|
|
|
|
switch lineType {
|
|
|
|
case TypeInfoNotStated:
|
|
|
|
/* Append TypeInfo to the beginning of line */
|
2020-05-07 08:16:37 +00:00
|
|
|
sections = append(sections, &GophermapTextSection{ buildInfoLine(line) })
|
2020-04-17 15:39:25 +00:00
|
|
|
|
2020-04-18 22:05:25 +00:00
|
|
|
case TypeTitle:
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Reformat title line to send as info line with appropriate selector */
|
2020-04-18 22:05:25 +00:00
|
|
|
if !titleAlready {
|
2020-05-07 08:16:37 +00:00
|
|
|
sections = append(sections, &GophermapTextSection{ buildLine(TypeInfo, line[1:], "TITLE", NullHost, NullPort) })
|
2020-04-18 22:05:25 +00:00
|
|
|
titleAlready = true
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
case TypeComment:
|
|
|
|
/* We ignore this line */
|
|
|
|
break
|
|
|
|
|
|
|
|
case TypeHiddenFile:
|
|
|
|
/* Add to hidden files map */
|
2020-05-14 16:54:06 +00:00
|
|
|
hidden[request.Path.JoinRel(line[1:])] = true
|
2020-04-17 15:39:25 +00:00
|
|
|
|
|
|
|
case TypeSubGophermap:
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Parse new RequestPath and parameters */
|
2020-05-14 16:54:06 +00:00
|
|
|
subRequest, gophorErr := parseLineRequestString(request.Path, line[1:])
|
2020-05-14 18:15:51 +00:00
|
|
|
if gophorErr != nil {
|
|
|
|
/* Failed parsing line request string, set returnErr and request finish */
|
|
|
|
returnErr = gophorErr
|
|
|
|
return true
|
|
|
|
} else if subRequest.Path.Relative() == "" || subRequest.Path.Relative() == request.Path.Relative() {
|
|
|
|
/* Failed parsing line request string, or we've been supplied same gophermap, and recursion is
|
2020-05-14 16:54:06 +00:00
|
|
|
* recursion is recursion is bad kids! Set return error and request finish.
|
2020-05-08 15:52:17 +00:00
|
|
|
*/
|
2020-05-14 18:15:51 +00:00
|
|
|
returnErr = &GophorError{ InvalidRequestErr, nil }
|
2020-05-14 16:54:06 +00:00
|
|
|
return true
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform file stat */
|
2020-05-14 16:54:06 +00:00
|
|
|
stat, err := os.Stat(subRequest.Path.Absolute())
|
2020-05-03 21:42:51 +00:00
|
|
|
if (err != nil) || (stat.Mode() & os.ModeDir != 0) {
|
|
|
|
/* File read error or is directory */
|
2020-05-14 18:15:51 +00:00
|
|
|
returnErr = &GophorError{ FileStatErr, err }
|
|
|
|
return true
|
2020-05-03 21:42:51 +00:00
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
|
2020-05-03 21:42:51 +00:00
|
|
|
/* Check if we've been supplied subgophermap or regular file */
|
2020-05-14 16:54:06 +00:00
|
|
|
if isGophermap(subRequest.Path.Relative()) {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* If executable, store as GophermapExecFileSection, else GophermapSubmapSection */
|
2020-05-07 20:48:16 +00:00
|
|
|
if stat.Mode().Perm() & 0100 != 0 {
|
2020-05-14 16:54:06 +00:00
|
|
|
sections = append(sections, &GophermapExecFileSection { subRequest })
|
2020-04-17 15:39:25 +00:00
|
|
|
} else {
|
2020-05-14 16:54:06 +00:00
|
|
|
sections = append(sections, &GophermapSubmapSection{ subRequest })
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-05-08 15:52:17 +00:00
|
|
|
/* If stored in cgi-bin store as GophermapExecCgiSection, else GophermapFileSection */
|
2020-05-14 16:54:06 +00:00
|
|
|
if withinCgiBin(subRequest.Path.Relative()) {
|
|
|
|
sections = append(sections, &GophermapExecCgiSection{ subRequest })
|
2020-04-17 15:39:25 +00:00
|
|
|
} else {
|
2020-05-14 16:54:06 +00:00
|
|
|
sections = append(sections, &GophermapFileSection{ subRequest })
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case TypeEnd:
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Lastline, break out at end of loop. GophermapContents.Render() will
|
|
|
|
* append a LastLine string so we don't have to worry about that here.
|
2020-04-17 15:39:25 +00:00
|
|
|
*/
|
|
|
|
return false
|
|
|
|
|
|
|
|
case TypeEndBeginList:
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Append GophermapDirectorySection object then break, as with TypeEnd. */
|
2020-05-14 16:54:06 +00:00
|
|
|
dirRequest := &Request{ NewRequestPath(request.Path.RootDir(), request.Path.TrimRelSuffix(GophermapFileStr)), "" }
|
2020-05-07 20:15:47 +00:00
|
|
|
sections = append(sections, &GophermapDirectorySection{ dirRequest, hidden })
|
2020-04-17 15:39:25 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
default:
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Default is appending to sections slice as GopherMapTextSection */
|
2020-05-07 08:16:37 +00:00
|
|
|
sections = append(sections, &GophermapTextSection{ []byte(line+DOSLineEnd) })
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Check the bufferedScan didn't exit with error */
|
|
|
|
if gophorErr != nil {
|
|
|
|
return nil, gophorErr
|
2020-05-14 16:54:06 +00:00
|
|
|
} else if returnErr != nil {
|
|
|
|
return nil, returnErr
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sections, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Read a text file into a gophermap as text sections */
|
2020-04-17 15:39:25 +00:00
|
|
|
func readIntoGophermap(path string) ([]byte, *GophorError) {
|
|
|
|
/* Create return slice */
|
|
|
|
fileContents := make([]byte, 0)
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Perform buffered scan with our supplied iterator */
|
2020-04-17 15:39:25 +00:00
|
|
|
gophorErr := bufferedScan(path,
|
|
|
|
func(scanner *bufio.Scanner) bool {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
2020-04-17 17:51:30 +00:00
|
|
|
if line == "" {
|
2020-04-17 15:39:25 +00:00
|
|
|
fileContents = append(fileContents, buildInfoLine("")...)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Replace the newline characters */
|
2020-05-02 21:46:58 +00:00
|
|
|
line = replaceNewLines(line)
|
2020-04-17 15:39:25 +00:00
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Iterate through line string, reflowing to new line
|
2020-04-17 15:39:25 +00:00
|
|
|
* until all lines < PageWidth
|
|
|
|
*/
|
|
|
|
for len(line) > 0 {
|
|
|
|
length := minWidth(len(line))
|
|
|
|
fileContents = append(fileContents, buildInfoLine(line[:length])...)
|
|
|
|
line = line[length:]
|
|
|
|
}
|
2020-05-03 21:42:51 +00:00
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
return true
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Check the bufferedScan didn't exit with error */
|
|
|
|
if gophorErr != nil {
|
|
|
|
return nil, gophorErr
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check final output ends on a newline */
|
2020-04-17 17:51:30 +00:00
|
|
|
if !bytes.HasSuffix(fileContents, []byte(DOSLineEnd)) {
|
|
|
|
fileContents = append(fileContents, []byte(DOSLineEnd)...)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fileContents, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:52:17 +00:00
|
|
|
/* Return minimum width out of PageWidth and W */
|
2020-04-17 15:39:25 +00:00
|
|
|
func minWidth(w int) int {
|
2020-04-22 18:42:54 +00:00
|
|
|
if w <= Config.PageWidth {
|
2020-04-17 15:39:25 +00:00
|
|
|
return w
|
|
|
|
} else {
|
2020-04-22 18:42:54 +00:00
|
|
|
return Config.PageWidth
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|