From be89b61596cb01272ac09d0e8de2ea292949474f Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Mon, 16 Nov 2020 09:17:42 -0800 Subject: [PATCH] Rename ListDirectory as List --- CONFIGURATION.md | 20 ++++++++++++++++---- config.go | 8 ++++---- go.mod | 2 +- go.sum | 4 ++-- main.go | 2 +- server.go | 35 +++++++++++++++++------------------ 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 0de8624..c264fad 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -120,9 +120,9 @@ When enabled, symbolic links may be accessed. This attribute is disabled by defa When enabled, hidden files and directories may be accessed. This attribute is disabled by default. -##### ListDirectory +##### List -When enabled, directories without an index file will serve a list their +When enabled, directories without an index file will serve a list of their contents. This attribute is disabled by default. ##### Input @@ -143,7 +143,19 @@ specific content type for a path. Forward requests to [FastCGI](https://en.wikipedia.org/wiki/FastCGI) server at specified address or path. -A `Root` attribute must also be specified to use `FastCGI`. +A `Root` attribute must also be specified. + +Connect via Unix socket: + +``` +unix:///var/run/php.sock +``` + +Connect via TCP: + +``` +tcp://127.0.0.1:9000 +``` ## End-of-line indicator @@ -188,7 +200,7 @@ hosts: path: /sites root: /home/geminirocks/data cache: 604800 # Cache for 1 week - listdirectory: true + list: true # Enable directory listing - path: ^/(help|info)$ root: /home/geminirocks/data/help diff --git a/config.go b/config.go index f5e2b0d..018ddc4 100644 --- a/config.go +++ b/config.go @@ -37,8 +37,8 @@ type pathConfig struct { // Serve hidden files and directories HiddenFiles bool - // List directory entries - ListDirectory bool + // List directory + List bool // Content type Type string @@ -156,8 +156,8 @@ func readconfig(configPath string) error { if defaultPath.HiddenFiles { serve.HiddenFiles = defaultPath.HiddenFiles } - if defaultPath.ListDirectory { - serve.ListDirectory = defaultPath.ListDirectory + if defaultPath.List { + serve.List = defaultPath.List } if defaultPath.Cache != "" && serve.Cache == "" { serve.Cache = defaultPath.Cache diff --git a/go.mod b/go.mod index ac0d967..7641bca 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,6 @@ require ( github.com/h2non/filetype v1.1.0 github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/yookoala/gofast v0.4.1-0.20201013050739-975113c54107 - golang.org/x/tools v0.0.0-20201112185108-eeaa07dd7696 // indirect + golang.org/x/tools v0.0.0-20201116165547-1a2739ce1bfb // indirect gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 ) diff --git a/go.sum b/go.sum index fd7dd72..4e276ac 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200908211811-12e1bf57a112/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= -golang.org/x/tools v0.0.0-20201112185108-eeaa07dd7696 h1:Bfazo+enXJET5SbHeh95NtxabJF6fJ9r/jpfRJgd3j4= -golang.org/x/tools v0.0.0-20201112185108-eeaa07dd7696/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20201116165547-1a2739ce1bfb h1:QEy3Xa/NrmW1ReQDWZq9JhNZaS2eWWumNV6RAEKnQzQ= +golang.org/x/tools v0.0.0-20201116165547-1a2739ce1bfb/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 7cf7fcc..0bacb04 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ func main() { log.SetFlags(0) configFile := flag.String("config", "", "path to configuration file") - flag.BoolVar(&verbose, "verbose", false, "print request and response information") + flag.BoolVar(&verbose, "verbose", false, "print request log") flag.Parse() if *configFile == "" { diff --git a/server.go b/server.go index 7b491f7..aff5a86 100644 --- a/server.go +++ b/server.go @@ -25,6 +25,8 @@ const ( urlMaxLength = 1024 geminiType = "text/gemini; charset=utf-8" + + logTimeFormat = "2006-01-02 15:04:05" ) const ( @@ -214,7 +216,7 @@ func servePath(c *tls.Conn, request *url.URL, serve *pathConfig) (int, int64) { if err != nil { _, err := os.Stat(path.Join(filePath, "index.gemini")) if err != nil { - if serve.ListDirectory { + if serve.List { return serveDirList(c, serve, request, filePath), -1 } return writeStatus(c, statusNotFound), -1 @@ -387,28 +389,25 @@ func handleConn(c *tls.Conn) { func logEntry(request *url.URL, status int, size int64, elapsed time.Duration) []byte { hostFormatted := "-" - if request.Hostname() != "" { - hostFormatted = request.Hostname() - if request.Port() != "" { - hostFormatted += ":" + request.Port() - } else { - hostFormatted += ":1965" + pathFormatted := "-" + sizeFormatted := "-" + if request != nil { + if request.Path != "" { + pathFormatted = request.Path + } + if request.Hostname() != "" { + hostFormatted = request.Hostname() + if request.Port() != "" { + hostFormatted += ":" + request.Port() + } else { + hostFormatted += ":1965" + } } } - - timeFormatted := time.Now().Format("2006-01-02 15:04:05") - - pathFormatted := request.Path - if pathFormatted == "" { - pathFormatted = "-" - } - - sizeFormatted := "-" if size >= 0 { sizeFormatted = strconv.FormatInt(size, 10) } - - return []byte(fmt.Sprintf(`%s - - - [%s] "GET %s Gemini" %d %s %.4f`, hostFormatted, timeFormatted, pathFormatted, status, sizeFormatted, elapsed.Seconds())) + return []byte(fmt.Sprintf(`%s - - - [%s] "GET %s Gemini" %d %s %.4f`, hostFormatted, time.Now().Format(logTimeFormat), pathFormatted, status, sizeFormatted, elapsed.Seconds())) } func handleListener(l net.Listener) {