Support HEAD requests

This commit is contained in:
Trevor Slocum 2020-12-08 18:53:02 -08:00
parent 055dd08886
commit 74dc22338e
4 changed files with 19 additions and 7 deletions

2
go.mod
View File

@ -7,6 +7,6 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/yookoala/gofast v0.4.1-0.20201013050739-975113c54107
gitlab.com/tslocum/gmitohtml v1.0.3-0.20201203184239-2a1abe8efe7c
golang.org/x/tools v0.0.0-20201204222352-654352759326 // indirect
golang.org/x/tools v0.0.0-20201206230334-368bee879bfd // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)

4
go.sum
View File

@ -36,8 +36,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-20201204222352-654352759326 h1:XKLw9EEEfGJFE2K5Ni4nXgtFBIfI+zKPIi2SlRYmIG4=
golang.org/x/tools v0.0.0-20201204222352-654352759326/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20201206230334-368bee879bfd h1:EqFvKLTxjH6gEy2baWxX2AgJwZkBIDIcZFYcoYlI9RA=
golang.org/x/tools v0.0.0-20201206230334-368bee879bfd/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=

View File

@ -35,6 +35,9 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) {
w.Header().Set("Content-Type", cssType)
w.WriteHeader(status)
if r.Method == "HEAD" {
return status, 0, ""
}
w.Write(cssBytes)
return status, int64(len(cssBytes)), ""
}
@ -138,6 +141,9 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) {
w.Header().Set("Content-Type", htmlType)
w.WriteHeader(status)
if r.Method == "HEAD" {
return status, 0, serve.Log
}
w.Write(result)
return status, int64(len(result)), serve.Log
}
@ -181,6 +187,9 @@ func serveHTTPS(w http.ResponseWriter, r *http.Request) (int, int64, string) {
w.Header().Set("Content-Type", contentType)
w.WriteHeader(status)
if r.Method == "HEAD" {
return status, 0, serve.Log
}
w.Write(result)
return status, int64(len(result)), serve.Log
}

View File

@ -300,6 +300,7 @@ func handleConn(c *tls.Conn) {
t := time.Now()
var request *url.URL
var logPath string
method := "GET"
status := 0
size := int64(-1)
protocol := "Gemini"
@ -309,7 +310,7 @@ func handleConn(c *tls.Conn) {
return
}
entry := logEntry(protocol, request, status, size, time.Since(t))
entry := logEntry(method, protocol, request, status, size, time.Since(t))
if !quiet {
log.Println(string(entry))
@ -375,7 +376,7 @@ func handleConn(c *tls.Conn) {
clientCertKeys = append(clientCertKeys, pubKey)
}
if strings.HasPrefix(requestData, "GET ") {
if strings.HasPrefix(requestData, "GET ") || strings.HasPrefix(requestData, "HEAD ") {
w := newResponseWriter(c)
defer w.WriteHeader(http.StatusOK)
@ -399,6 +400,8 @@ func handleConn(c *tls.Conn) {
r.URL.Scheme = "https"
r.URL.Host = strings.ToLower(r.Host)
method = r.Method
request = r.URL
status, size, logPath = serveHTTPS(w, r)
@ -441,7 +444,7 @@ func handleConn(c *tls.Conn) {
status, size, logPath = handleRequest(c, request, requestData)
}
func logEntry(protocol string, request *url.URL, status int, size int64, elapsed time.Duration) []byte {
func logEntry(method string, protocol string, request *url.URL, status int, size int64, elapsed time.Duration) []byte {
hostFormatted := "-"
pathFormatted := "-"
sizeFormatted := "-"
@ -461,7 +464,7 @@ func logEntry(protocol string, request *url.URL, status int, size int64, elapsed
if size >= 0 {
sizeFormatted = strconv.FormatInt(size, 10)
}
return []byte(fmt.Sprintf(`%s - - - [%s] "GET %s %s" %d %s %.4f`, hostFormatted, time.Now().Format(logTimeFormat), pathFormatted, protocol, status, sizeFormatted, elapsed.Seconds()))
return []byte(fmt.Sprintf(`%s - - - [%s] "%s %s %s" %d %s %.4f`, hostFormatted, time.Now().Format(logTimeFormat), method, pathFormatted, protocol, status, sizeFormatted, elapsed.Seconds()))
}
func handleListener(l net.Listener) {