1
0
Fork 0

Defer sending log entry to channel.

This commit is contained in:
Solderpunk 2019-11-06 18:50:44 +02:00
parent dc92fbd228
commit b4cbd47bce

View file

@ -10,12 +10,12 @@ import (
func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) { func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) {
defer conn.Close() defer conn.Close()
var log LogEntry var log LogEntry
log.Time = time.Now() log.Time = time.Now()
log.RemoteAddr = conn.RemoteAddr() log.RemoteAddr = conn.RemoteAddr()
log.RequestURL = "-" log.RequestURL = "-"
log.Status = 0 log.Status = 0
defer func() { logEntries <- log }()
// Read request // Read request
reader := bufio.NewReaderSize(conn, 1024) reader := bufio.NewReaderSize(conn, 1024)
@ -23,12 +23,10 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
if overflow { if overflow {
conn.Write([]byte("59 Request too long!r\n")) conn.Write([]byte("59 Request too long!r\n"))
log.Status = 59 log.Status = 59
logEntries <- log
return return
} else if err != nil { } else if err != nil {
conn.Write([]byte("40 Unknown error reading request!r\n")) conn.Write([]byte("40 Unknown error reading request!r\n"))
log.Status = 40 log.Status = 40
logEntries <- log
return return
} }
@ -37,7 +35,6 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
if err != nil { if err != nil {
conn.Write([]byte("59 Error parsing URL!r\n")) conn.Write([]byte("59 Error parsing URL!r\n"))
log.Status = 59 log.Status = 59
logEntries <- log
return return
} }
log.RequestURL = URL.String() log.RequestURL = URL.String()
@ -51,7 +48,6 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
if URL.Scheme != "gemini" { if URL.Scheme != "gemini" {
conn.Write([]byte("53 No proxying to non-Gemini content!\r\n")) conn.Write([]byte("53 No proxying to non-Gemini content!\r\n"))
log.Status = 53 log.Status = 53
logEntries <- log
return return
} }
@ -60,5 +56,4 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
body := fmt.Sprintf("Molly at %s says \"Hi!\" from %s.\n", URL.Host, URL.Path) body := fmt.Sprintf("Molly at %s says \"Hi!\" from %s.\n", URL.Host, URL.Path)
conn.Write([]byte(body)) conn.Write([]byte(body))
log.Status = 20 log.Status = 20
logEntries <- log
} }