From 50cd4f2cfe6b354245e038dac19ce02a28c2ab89 Mon Sep 17 00:00:00 2001 From: "kim (grufwub)" Date: Mon, 13 Jul 2020 13:08:30 +0100 Subject: [PATCH] undo separate variable declarations, CGI stderr = nil, formatting changes Signed-off-by: kim (grufwub) --- core/cache.go | 2 +- core/cgi.go | 3 +++ core/conn.go | 23 +++++++++++------------ core/filecontents.go | 4 ++-- core/filesystem.go | 27 ++++++++------------------- gopher/filecontents.go | 7 ++----- gopher/gophermap.go | 6 +++--- gopher/server.go | 6 +++--- 8 files changed, 33 insertions(+), 45 deletions(-) diff --git a/core/cache.go b/core/cache.go index 31de43d..d9dd485 100644 --- a/core/cache.go +++ b/core/cache.go @@ -71,7 +71,7 @@ func (lru *lruCacheMap) Remove(key string) { // Iterate performs an iteration over all key:value pairs in LRUCacheMap with supplied function func (lru *lruCacheMap) Iterate(iterator func(key string, value *file)) { for key := range lru.hashMap { - element, _ := lru.hashMap[key].Value.(*element) + element := lru.hashMap[key].Value.(*element) iterator(element.key, element.value) } } diff --git a/core/cgi.go b/core/cgi.go index 3fb963f..2dd5211 100644 --- a/core/cgi.go +++ b/core/cgi.go @@ -90,6 +90,9 @@ func execute(writer io.Writer, p *Path, env []string) Error { // Setup cmd out writer cmd.Stdout = writer + // Not interested in err + cmd.Stderr = nil + // Start executing err := cmd.Start() if err != nil { diff --git a/core/conn.go b/core/conn.go index 12f4c43..349bf11 100644 --- a/core/conn.go +++ b/core/conn.go @@ -48,8 +48,8 @@ func (c *deadlineConn) Close() error { // Conn wraps a DeadlineConn with a buffer type conn struct { - buf *bufio.ReadWriter - closer io.Closer + buf *bufio.ReadWriter + cl io.Closer } // wrapConn wraps a net.Conn in DeadlineConn, then within Conn and returns the result @@ -67,15 +67,10 @@ func (c *conn) ReadLine() ([]byte, Error) { // return slice b := make([]byte, 0) - // Declare variables - var line []byte - var isPrefix bool - var err error - // Read! for len(b) < connReadMax { // read the line - line, isPrefix, err = c.buf.ReadLine() + line, isPrefix, err := c.buf.ReadLine() if err != nil { return nil, WrapError(ConnReadErr, err) } @@ -93,7 +88,7 @@ func (c *conn) ReadLine() ([]byte, Error) { } // WriteBytes writes a byte slice to the buffer and returns error status -func (c *conn) WriteBytes(b []byte) Error { +func (c *conn) Write(b []byte) Error { _, err := c.buf.Write(b) if err != nil { return WrapError(ConnWriteErr, err) @@ -101,8 +96,12 @@ func (c *conn) WriteBytes(b []byte) Error { return nil } -// WriteFrom writes to the buffer from a reader and returns error status -func (c *conn) WriteFrom(r io.Reader) Error { +// ReadFrom writes to the buffer from a reader and returns error status +func (c *conn) ReadFrom(r io.Reader) Error { + // Since this buffer wraps deadlineConn, which DOES NOT have + // a ReadFrom method implemented, it will force the buffer to + // use it's own internal byte buffer along with the deadlineConn's + // Write implementation (forcing the deadline to be regularly updated) _, err := c.buf.ReadFrom(r) if err != nil { return WrapError(ConnWriteErr, err) @@ -118,7 +117,7 @@ func (c *conn) Writer() io.Writer { // Close flushes the underlying buffer then closes the conn func (c *conn) Close() Error { err := c.buf.Flush() - err = c.closer.Close() + err = c.cl.Close() if err != nil { return WrapError(ConnCloseErr, err) } diff --git a/core/filecontents.go b/core/filecontents.go index 1ba6973..3426508 100644 --- a/core/filecontents.go +++ b/core/filecontents.go @@ -16,7 +16,7 @@ type generatedFileContents struct { // WriteToClient writes the generated file contents to the client func (fc *generatedFileContents) WriteToClient(client *Client, path *Path) Error { - return client.Conn().WriteBytes(fc.content) + return client.Conn().Write(fc.content) } // Load does nothing @@ -32,7 +32,7 @@ type RegularFileContents struct { // WriteToClient writes the current contents of FileContents to the client func (fc *RegularFileContents) WriteToClient(client *Client, path *Path) Error { - return client.Conn().WriteBytes(fc.contents) + return client.Conn().Write(fc.contents) } // Load takes an open FD and loads the file contents into FileContents memory diff --git a/core/filesystem.go b/core/filesystem.go index b011533..5678e59 100644 --- a/core/filesystem.go +++ b/core/filesystem.go @@ -106,14 +106,11 @@ func (fs *FileSystemObject) ReadFile(fd *os.File) ([]byte, Error) { // Read buffer buf := make([]byte, fileReadBufSize) - - // Declare variables - var count int - var err error + rd := bufio.NewReaderSize(fd, fileReadBufSize) // Read through file until null bytes / error for { - count, err = fd.Read(buf) + count, err := rd.Read(buf) if err != nil { if err == io.EOF { break @@ -134,23 +131,18 @@ func (fs *FileSystemObject) ReadFile(fd *os.File) ([]byte, Error) { // ScanFile scans a supplied file at file descriptor, using iterator function func (fs *FileSystemObject) ScanFile(fd *os.File, iterator func(string) bool) Error { // Buffered reader - rdr := bufio.NewReaderSize(fd, fileReadBufSize) - - // Declare variables - var b, line []byte - var err error - var isPrefix, done bool + rd := bufio.NewReaderSize(fd, fileReadBufSize) // Iterate through file! for { // Line buffer - b = make([]byte, 0) + b := make([]byte, 0) // Read until line-end, or file end! - done = false + done := false for { // Read a line - line, isPrefix, err = rdr.ReadLine() + line, isPrefix, err := rd.ReadLine() if err != nil { if err == io.EOF { done = true @@ -190,13 +182,10 @@ func (fs *FileSystemObject) ScanDirectory(fd *os.File, p *Path, iterator func(os // Sort by name sort.Sort(byName(dirList)) - // Declare variables - var fp *Path - // Walk through the directory list using supplied iterator function for _, info := range dirList { // Make new Path object - fp = p.JoinPath(info.Name()) + fp := p.JoinPath(info.Name()) // Skip restricted files if IsRestrictedPath(fp) || WithinCGIDir(fp) { @@ -296,7 +285,7 @@ func (fs *FileSystemObject) HandleClient(client *Client, request *Request, newFi func (fs *FileSystemObject) FetchFile(client *Client, fd *os.File, stat os.FileInfo, p *Path, newFileContents func(*Path) FileContents) Error { // If file too big, write direct to client if stat.Size() > fileSizeMax { - return client.Conn().WriteFrom(fd) + return client.Conn().ReadFrom(fd) } // Get cache read lock, defer unlock diff --git a/gopher/filecontents.go b/gopher/filecontents.go index 74f90aa..5e0ad2b 100644 --- a/gopher/filecontents.go +++ b/gopher/filecontents.go @@ -12,19 +12,16 @@ type gophermapContents struct { // WriteToClient renders each cached section of the gophermap, and writes them to the client func (gc *gophermapContents) WriteToClient(client *core.Client, path *core.Path) core.Error { - // Declare variables - var err core.Error - // Render + write the sections! for _, section := range gc.sections { - err = section.RenderAndWrite(client) + err := section.RenderAndWrite(client) if err != nil { return err } } // Finally, write the footer (including last-line) - return client.Conn().WriteBytes(footer) + return client.Conn().Write(footer) } // Load takes an open FD and loads the gophermap contents into memory as different renderable sections diff --git a/gopher/gophermap.go b/gopher/gophermap.go index 69c1e08..e7fb506 100644 --- a/gopher/gophermap.go +++ b/gopher/gophermap.go @@ -144,7 +144,7 @@ type TextSection struct { // RenderAndWrite simply writes the byte slice to the client func (s *TextSection) RenderAndWrite(client *core.Client) core.Error { - return client.Conn().WriteBytes(s.contents) + return client.Conn().Write(s.contents) } // DirectorySection is an implementation that holds a dir path, and map of hidden files, to later list a dir contents @@ -173,7 +173,7 @@ func (s *DirectorySection) RenderAndWrite(client *core.Client) core.Error { } // Write dirContents to client - return client.Conn().WriteBytes(dirContents) + return client.Conn().Write(dirContents) } // FileSection is an implementation that holds a file path, and writes the file contents to client @@ -196,7 +196,7 @@ func (s *FileSection) RenderAndWrite(client *core.Client) core.Error { } // Write the file contents to the client - return client.Conn().WriteBytes(b) + return client.Conn().Write(b) } // SubgophermapSection is an implementation to hold onto a gophermap path, then read, render and write contents to a client diff --git a/gopher/server.go b/gopher/server.go index 676e260..0ebed85 100644 --- a/gopher/server.go +++ b/gopher/server.go @@ -23,7 +23,7 @@ func serve(client *core.Client) { lenBefore := len(line) line = strings.TrimPrefix(line, "URL:") if len(line) < lenBefore { - client.Conn().WriteBytes(generateHTMLRedirect(line)) + client.Conn().Write(generateHTMLRedirect(line)) client.LogInfo(clientRedirectFmtStr, line) return } @@ -80,7 +80,7 @@ func serve(client *core.Client) { // Add footer, write contents dirContents = append(dirContents, footer...) - return client.Conn().WriteBytes(dirContents) + return client.Conn().Write(dirContents) }, ) @@ -97,7 +97,7 @@ func serve(client *core.Client) { func handleError(client *core.Client, err core.Error) { response, ok := generateErrorResponse(err.Code()) if ok { - client.Conn().WriteBytes(response) + client.Conn().Write(response) } core.SystemLog.Error(err.Error()) }