small formatting changes, remove extra goroutine spawned in Worker.Serve()

Signed-off-by: kim (grufwub) <grufwub@gmail.com>
This commit is contained in:
kim (grufwub) 2020-04-25 18:38:26 +01:00
parent 0ad5a890bf
commit 3de53f5f25
6 changed files with 85 additions and 94 deletions

View File

@ -70,18 +70,20 @@ func (fc *FileCache) Init(size int, fileSizeMax float64) {
func (fc *FileCache) FetchRegular(request *FileSystemRequest) ([]byte, *GophorError) {
/* Calls fc.Fetch() but with the filecontents init function for a regular file */
return fc.Fetch(request, func(path string) FileContents {
contents := new(RegularFileContents)
contents.path = path
return contents
return &RegularFileContents{
path,
nil,
}
})
}
func (fc *FileCache) FetchGophermap(request *FileSystemRequest) ([]byte, *GophorError) {
/* Calls fc.Fetch() but with the filecontents init function for a gophermap */
return fc.Fetch(request, func(path string) FileContents {
contents := new(GophermapContents)
contents.path = path
return contents
return &RegularFileContents{
path,
nil,
}
})
}
@ -132,9 +134,7 @@ func (fc *FileCache) Fetch(request *FileSystemRequest, newFileContents func(stri
/* Create new file wrapper around contents */
file = NewFile(contents)
/* NOTE: file isn't in cache yet so no need to lock file write mutex
* before loading contents from disk
*/
/* File isn't in cache yet so no need to get file lock mutex */
gophorErr := file.LoadContents()
if gophorErr != nil {
/* Error loading contents, unlock read mutex then return error */

View File

@ -93,17 +93,15 @@ type GophermapSection interface {
* onto a static section of text as a slice of bytes.
*/
type GophermapText struct {
contents []byte
Contents []byte
}
func NewGophermapText(contents []byte) *GophermapText {
s := new(GophermapText)
s.contents = contents
return s
return &GophermapText{ contents }
}
func (s *GophermapText) Render(request *FileSystemRequest) ([]byte, *GophorError) {
return replaceStrings(string(s.contents), request.Host), nil
return replaceStrings(string(s.Contents), request.Host), nil
}
/* GophermapDirListing:
@ -113,21 +111,19 @@ func (s *GophermapText) Render(request *FileSystemRequest) ([]byte, *GophorError
* Render() call is received.
*/
type GophermapDirListing struct {
path string
Path string
Hidden map[string]bool
}
func NewGophermapDirListing(path string) *GophermapDirListing {
s := new(GophermapDirListing)
s.path = path
return s
return &GophermapDirListing{ path, nil }
}
func (s *GophermapDirListing) Render(request *FileSystemRequest) ([]byte, *GophorError) {
/* We could just pass the request directly, but in case the request
* path happens to differ for whatever reason we create a new one
*/
return listDir(&FileSystemRequest{ s.path, request.Host }, s.Hidden)
return listDir(&FileSystemRequest{ s.Path, request.Host }, s.Hidden)
}
func readGophermap(path string) ([]GophermapSection, *GophorError) {

View File

@ -27,11 +27,11 @@ type MapElement struct {
}
func NewFixedMap(size int) *FixedMap {
fm := new(FixedMap)
fm.Map = make(map[string]*MapElement)
fm.List = list.New()
fm.Size = size
return fm
return &FixedMap{
make(map[string]*MapElement),
list.New(),
size,
}
}
/* Get file in map for key, or nil */

12
fs.go
View File

@ -37,12 +37,12 @@ type File struct {
}
func NewFile(contents FileContents) *File {
f := new(File)
f.contents = contents
f.mutex = sync.RWMutex{}
f.isFresh = true
f.lastRefresh = 0
return f
return &File{
contents,
sync.RWMutex{},
true,
0,
}
}
func (f *File) Contents(request *FileSystemRequest) []byte {

View File

@ -52,8 +52,7 @@ func main() {
/* Run this in it's own goroutine so we can go straight back to accepting */
go func() {
w := NewWorker(newConn)
w.Serve()
NewWorker(newConn).Serve()
}()
}
}()

120
worker.go
View File

@ -23,70 +23,66 @@ type Worker struct {
}
func NewWorker(conn *GophorConn) *Worker {
worker := new(Worker)
worker.Conn = conn
return worker
return &Worker{ conn }
}
func (worker *Worker) Serve() {
go func() {
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
}
/* Keep count :) */
iter += 1
}
/* Handle request */
gophorErr := worker.RespondGopher(received)
/* Handle any error */
if gophorErr != nil {
Config.LogSystemError("%s\n", gophorErr.Error())
/* 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)
}
}
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
}
/* Keep count :) */
iter += 1
}
/* Handle request */
gophorErr := worker.RespondGopher(received)
/* Handle any error */
if gophorErr != nil {
Config.LogSystemError("%s\n", gophorErr.Error())
/* 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)
}
}
}
func (worker *Worker) SendRaw(b []byte) *GophorError {
@ -111,12 +107,12 @@ func (worker *Worker) RespondGopher(data []byte) *GophorError {
/* According to Gopher spec, only read up to first Tab or Crlf */
dataStr := readUpToFirstTabOrCrlf(data)
/* Handle URL request if so. TODO: this is so unelegant... */
/* Handle URL request if presented */
lenBefore := len(dataStr)
dataStr = strings.TrimPrefix(dataStr, "URL:")
switch len(dataStr) {
case lenBefore-4:
/* Handle URL prefix */
/* Send an HTML redirect to supplied URL */
worker.Log("Redirecting to URL: %s\n", data)
return worker.SendRaw(generateHtmlRedirect(dataStr))
default: