flounder

A simple gemini site builder
git clone git://git.alexwennerberg.com/flounder
Log | Files | Refs | README | LICENSE

commit 49cd54f0be605b3371b0d7ab1557f2805df276aa
parent 7e163707f2dd7f197bea26d8980af7d010f24b89
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 23 Oct 2022 15:40:53 -0700

remove application logging etc

Diffstat:
Mgo.mod | 1-
Mgo.sum | 4----
Mhttp.go | 13+++----------
Dlog.go | 148-------------------------------------------------------------------------------
4 files changed, 3 insertions(+), 163 deletions(-)

diff --git a/go.mod b/go.mod @@ -6,7 +6,6 @@ require ( git.sr.ht/~adnano/go-gemini v0.2.2 git.sr.ht/~aw/gorilla-feeds v1.1.3 github.com/BurntSushi/toml v0.3.1 - github.com/gorilla/handlers v1.5.1 github.com/gorilla/sessions v1.2.1 github.com/kr/pretty v0.2.1 // indirect github.com/mattn/go-sqlite3 v1.14.6 diff --git a/go.sum b/go.sum @@ -6,10 +6,6 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/felixge/httpsnoop v1.0.1 h1:lvB5Jl89CsZtGIWuTcDM1E/vkVs49/Ml7JJe07l8SPQ= -github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= -github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4= -github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7FsgI= diff --git a/http.go b/http.go @@ -17,7 +17,6 @@ import ( gmi "git.sr.ht/~adnano/go-gemini" feeds "git.sr.ht/~aw/gorilla-feeds" - "github.com/gorilla/handlers" "github.com/gorilla/sessions" _ "github.com/mattn/go-sqlite3" "golang.org/x/crypto/bcrypt" @@ -579,11 +578,7 @@ func adminHandler(w http.ResponseWriter, r *http.Request) { func userFile(w http.ResponseWriter, r *http.Request) { var userName string // stop annoying bots - count := strings.Count(r.Host, ".") - if count > 2 { - http.Error(w, "", 000) - return - } + custom := domains[r.Host] if custom != "" { userName = custom @@ -596,7 +591,6 @@ func userFile(w http.ResponseWriter, r *http.Request) { return } p := cleanPath(unescaped) - fmt.Println(p) var isDir bool fullPath := path.Join(c.FilesDirectory, userName, p) // TODO rename filepath stat, err := os.Stat(fullPath) @@ -846,6 +840,7 @@ func checkDomainHandler(w http.ResponseWriter, r *http.Request) { } if domain == c.Host || strings.HasSuffix(domain, "."+c.Host) { w.Write([]byte(domain)) + return } http.Error(w, "Not Found", 404) } @@ -898,8 +893,6 @@ func runHTTPServer() { // admin commands serveMux.HandleFunc(hostname+"/admin/user/", adminUserHandler) - wrapped := handlers.CustomLoggingHandler(log.Writer(), serveMux, logFormatter) - // Deprecated serveMux.HandleFunc("proxy."+hostname+"/", proxyGemini) @@ -910,7 +903,7 @@ func runHTTPServer() { WriteTimeout: 10 * time.Second, IdleTimeout: 120 * time.Second, Addr: fmt.Sprintf(":%d", c.HttpPort), - Handler: wrapped, + Handler: serveMux, } log.Fatal(srv.ListenAndServe()) } diff --git a/log.go b/log.go @@ -1,148 +0,0 @@ -package main - -import ( - "io" - "net" - "net/http" - "net/url" - "strconv" - "time" - "unicode/utf8" - - "github.com/gorilla/handlers" -) - -// Copy pasted from gorilla handler library, modified slightly - -const lowerhex = "0123456789abcdef" - -const apacheTS = "02/Jan/2006:15:04:05 -0700" - -func logFormatter(writer io.Writer, params handlers.LogFormatterParams) { - buf := buildCommonLogLine(params.Request, params.URL, params.TimeStamp, params.StatusCode, params.Size) - buf = append(buf, '\n') - writer.Write(buf) -} - -// buildCommonLogLine builds a log entry for req in Apache Common Log Format. -// ts is the timestamp with which the entry should be logged. -// status and size are used to provide the response HTTP status and size. -func buildCommonLogLine(req *http.Request, url url.URL, ts time.Time, status int, size int) []byte { - user := getAuthUser(req) - username := "-" - if user.Username != "" { - username = user.Username - } - - // Get forwarded IP address - ipAddr := req.Header.Get("X-Forwarded-For") // TODO make so it works with many servers - if ipAddr == "" { - ipAddr = req.RemoteAddr - } - referer := req.Header.Get("Referer") - - host, _, err := net.SplitHostPort(ipAddr) - if err != nil { - host = ipAddr - } - - uri := req.RequestURI - - // Requests using the CONNECT method over HTTP/2.0 must use - // the authority field (aka r.Host) to identify the target. - // Refer: https://httpwg.github.io/specs/rfc7540.html#CONNECT - if req.ProtoMajor == 2 && req.Method == "CONNECT" { - uri = req.Host - } - if uri == "" { - uri = url.RequestURI() - } - - desthost := req.Host - - buf := make([]byte, 0, 3*(len(host)+len(desthost)+len(username)+len(req.Method)+len(uri)+len(req.Proto)+len(referer)+50)/2) - buf = append(buf, host...) - buf = append(buf, " - "...) - buf = append(buf, username...) - buf = append(buf, " ["...) - buf = append(buf, ts.Format(apacheTS)...) - buf = append(buf, `] `...) - buf = append(buf, desthost...) - buf = append(buf, ` "`...) - buf = append(buf, req.Method...) - buf = append(buf, " "...) - buf = appendQuoted(buf, uri) - buf = append(buf, " "...) - buf = append(buf, req.Proto...) - buf = append(buf, `" - `...) - buf = append(buf, referer...) - buf = append(buf, " - "...) - buf = append(buf, strconv.Itoa(status)...) - buf = append(buf, " "...) - buf = append(buf, strconv.Itoa(size)...) - return buf -} - -func appendQuoted(buf []byte, s string) []byte { - var runeTmp [utf8.UTFMax]byte - for width := 0; len(s) > 0; s = s[width:] { - r := rune(s[0]) - width = 1 - if r >= utf8.RuneSelf { - r, width = utf8.DecodeRuneInString(s) - } - if width == 1 && r == utf8.RuneError { - buf = append(buf, `\x`...) - buf = append(buf, lowerhex[s[0]>>4]) - buf = append(buf, lowerhex[s[0]&0xF]) - continue - } - if r == rune('"') || r == '\\' { // always backslashed - buf = append(buf, '\\') - buf = append(buf, byte(r)) - continue - } - if strconv.IsPrint(r) { - n := utf8.EncodeRune(runeTmp[:], r) - buf = append(buf, runeTmp[:n]...) - continue - } - switch r { - case '\a': - buf = append(buf, `\a`...) - case '\b': - buf = append(buf, `\b`...) - case '\f': - buf = append(buf, `\f`...) - case '\n': - buf = append(buf, `\n`...) - case '\r': - buf = append(buf, `\r`...) - case '\t': - buf = append(buf, `\t`...) - case '\v': - buf = append(buf, `\v`...) - default: - switch { - case r < ' ': - buf = append(buf, `\x`...) - buf = append(buf, lowerhex[s[0]>>4]) - buf = append(buf, lowerhex[s[0]&0xF]) - case r > utf8.MaxRune: - r = 0xFFFD - fallthrough - case r < 0x10000: - buf = append(buf, `\u`...) - for s := 12; s >= 0; s -= 4 { - buf = append(buf, lowerhex[r>>uint(s)&0xF]) - } - default: - buf = append(buf, `\U`...) - for s := 28; s >= 0; s -= 4 { - buf = append(buf, lowerhex[r>>uint(s)&0xF]) - } - } - } - } - return buf -}