2020-04-09 16:33:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2020-04-09 18:29:32 +00:00
|
|
|
"strings"
|
2020-04-09 16:33:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-20 14:18:35 +00:00
|
|
|
type FileType int
|
2020-04-10 19:31:21 +00:00
|
|
|
const (
|
2020-04-15 18:41:00 +00:00
|
|
|
SocketReadBufSize = 256 /* Supplied selector shouldn't be longer than this anyways */
|
2020-04-11 18:32:35 +00:00
|
|
|
MaxSocketReadChunks = 4
|
2020-04-15 18:41:00 +00:00
|
|
|
FileReadBufSize = 1024
|
2020-04-20 14:18:35 +00:00
|
|
|
|
|
|
|
/* Leads to some more concise code below */
|
|
|
|
FileTypeRegular FileType = iota
|
|
|
|
FileTypeDir FileType = iota
|
|
|
|
FileTypeBad FileType = iota
|
2020-04-10 19:31:21 +00:00
|
|
|
)
|
|
|
|
|
2020-04-11 18:32:35 +00:00
|
|
|
type Worker struct {
|
2020-04-23 08:29:58 +00:00
|
|
|
Conn *GophorConn
|
2020-04-11 18:32:35 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 08:29:58 +00:00
|
|
|
func NewWorker(conn *GophorConn) *Worker {
|
2020-04-25 17:38:26 +00:00
|
|
|
return &Worker{ conn }
|
2020-04-09 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 18:32:35 +00:00
|
|
|
func (worker *Worker) Serve() {
|
2020-04-25 17:38:26 +00:00
|
|
|
defer func() {
|
|
|
|
/* Close-up shop */
|
|
|
|
worker.Conn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
var count int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
/* Read buffer + final result */
|
|
|
|
buf := make([]byte, SocketReadBufSize)
|
|
|
|
received := make([]byte, 0)
|
|
|
|
|
|
|
|
iter := 0
|
|
|
|
for {
|
|
|
|
/* Buffered read from listener */
|
|
|
|
count, err = worker.Conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
Config.LogSystemError("Error reading from socket on port %s: %s\n", worker.Conn.Host.Port, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only copy non-null bytes */
|
|
|
|
received = append(received, buf[:count]...)
|
|
|
|
|
|
|
|
/* If count is less than expected read size, we've hit EOF */
|
|
|
|
if count < SocketReadBufSize {
|
|
|
|
/* EOF */
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hit max read chunk size, send error + close connection */
|
|
|
|
if iter == MaxSocketReadChunks {
|
|
|
|
Config.LogSystemError("Reached max socket read size %d. Closing connection...\n", MaxSocketReadChunks*SocketReadBufSize)
|
|
|
|
return
|
2020-04-09 16:33:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Keep count :) */
|
|
|
|
iter += 1
|
|
|
|
}
|
2020-04-20 14:18:35 +00:00
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Handle request */
|
|
|
|
gophorErr := worker.RespondGopher(received)
|
2020-04-09 16:33:17 +00:00
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Handle any error */
|
|
|
|
if gophorErr != nil {
|
|
|
|
Config.LogSystemError("%s\n", gophorErr.Error())
|
2020-04-10 15:01:07 +00:00
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Generate response bytes from error code */
|
|
|
|
response := generateGopherErrorResponseFromCode(gophorErr.Code)
|
|
|
|
|
|
|
|
/* If we got response bytes to send? SEND 'EM! */
|
|
|
|
if response != nil {
|
|
|
|
/* No gods. No masters. We don't care about error checking here */
|
|
|
|
worker.SendRaw(response)
|
2020-04-20 14:18:35 +00:00
|
|
|
}
|
2020-04-25 17:38:26 +00:00
|
|
|
}
|
2020-04-11 18:32:35 +00:00
|
|
|
}
|
2020-04-10 15:01:07 +00:00
|
|
|
|
2020-04-11 20:42:41 +00:00
|
|
|
func (worker *Worker) SendRaw(b []byte) *GophorError {
|
2020-04-22 18:42:54 +00:00
|
|
|
count, err := worker.Conn.Write(b)
|
2020-04-11 20:42:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return &GophorError{ SocketWriteErr, err }
|
|
|
|
} else if count != len(b) {
|
|
|
|
return &GophorError{ SocketWriteCountErr, nil }
|
|
|
|
}
|
|
|
|
return nil
|
2020-04-10 15:01:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:32:09 +00:00
|
|
|
func (worker *Worker) Log(format string, args ...interface{}) {
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.LogAccess(worker.Conn.RemoteAddr().String(), format, args...)
|
2020-04-12 20:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (worker *Worker) LogError(format string, args ...interface{}) {
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.LogAccessError(worker.Conn.RemoteAddr().String(), format, args...)
|
2020-04-12 20:32:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:18:35 +00:00
|
|
|
func (worker *Worker) RespondGopher(data []byte) *GophorError {
|
|
|
|
/* According to Gopher spec, only read up to first Tab or Crlf */
|
|
|
|
dataStr := readUpToFirstTabOrCrlf(data)
|
|
|
|
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Handle URL request if presented */
|
2020-04-20 14:18:35 +00:00
|
|
|
lenBefore := len(dataStr)
|
|
|
|
dataStr = strings.TrimPrefix(dataStr, "URL:")
|
2020-04-20 16:30:01 +00:00
|
|
|
switch len(dataStr) {
|
2020-04-20 14:18:35 +00:00
|
|
|
case lenBefore-4:
|
2020-04-25 17:38:26 +00:00
|
|
|
/* Send an HTML redirect to supplied URL */
|
2020-04-20 14:18:35 +00:00
|
|
|
worker.Log("Redirecting to URL: %s\n", data)
|
|
|
|
return worker.SendRaw(generateHtmlRedirect(dataStr))
|
|
|
|
default:
|
|
|
|
/* Do nothing */
|
2020-04-11 20:42:41 +00:00
|
|
|
}
|
2020-04-10 15:01:07 +00:00
|
|
|
|
2020-04-12 20:32:09 +00:00
|
|
|
/* Sanitize supplied path */
|
2020-04-20 14:18:35 +00:00
|
|
|
requestPath := sanitizePath(dataStr)
|
2020-04-10 15:01:07 +00:00
|
|
|
|
2020-04-13 13:46:13 +00:00
|
|
|
/* Append lastline */
|
2020-04-29 17:28:51 +00:00
|
|
|
response, gophorErr := Config.FileSystem.HandleRequest(requestPath, worker.Conn.Host)
|
|
|
|
if gophorErr != nil {
|
|
|
|
return gophorErr
|
|
|
|
}
|
|
|
|
Config.LogSystem("Served: %s\n", requestPath)
|
2020-04-13 13:46:13 +00:00
|
|
|
|
2020-04-09 18:29:32 +00:00
|
|
|
/* Serve response */
|
2020-04-11 20:42:41 +00:00
|
|
|
return worker.SendRaw(response)
|
2020-04-09 16:33:17 +00:00
|
|
|
}
|
2020-04-20 14:18:35 +00:00
|
|
|
|
|
|
|
func readUpToFirstTabOrCrlf(data []byte) string {
|
|
|
|
/* Only read up to first tab or cr-lf */
|
|
|
|
dataStr := ""
|
|
|
|
dataLen := len(data)
|
|
|
|
for i := 0; i < dataLen; i += 1 {
|
|
|
|
if data[i] == '\t' {
|
|
|
|
break
|
|
|
|
} else if data[i] == DOSLineEnd[0] {
|
2020-04-20 16:30:01 +00:00
|
|
|
if i == dataLen-1 || data[i+1] == DOSLineEnd[1] {
|
2020-04-20 14:18:35 +00:00
|
|
|
/* Finished on Unix line end, NOT DOS */
|
2020-04-20 16:30:01 +00:00
|
|
|
break
|
2020-04-20 14:18:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataStr += string(data[i])
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:30:01 +00:00
|
|
|
return dataStr
|
2020-04-20 14:18:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sanitizePath(dataStr string) string {
|
|
|
|
/* Clean path and trim '/' prefix if still exists */
|
|
|
|
requestPath := strings.TrimPrefix(path.Clean(dataStr), "/")
|
|
|
|
|
2020-04-22 12:41:27 +00:00
|
|
|
if requestPath == "." {
|
|
|
|
requestPath = "/"
|
|
|
|
} else if !strings.HasPrefix(requestPath, "/") {
|
2020-04-20 14:18:35 +00:00
|
|
|
requestPath = "/" + requestPath
|
|
|
|
}
|
|
|
|
|
|
|
|
return requestPath
|
|
|
|
}
|