1
0
Fork 0
molly-brown/handler.go

51 lines
1 KiB
Go
Raw Normal View History

2019-11-06 10:08:44 -05:00
package main
import (
"bufio"
"fmt"
"net"
"net/url"
2019-11-06 11:38:41 -05:00
"time"
2019-11-06 10:08:44 -05:00
)
2019-11-06 11:38:41 -05:00
func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry) {
2019-11-06 10:08:44 -05:00
defer conn.Close()
2019-11-06 11:38:41 -05:00
var log LogEntry
log.Time = time.Now()
log.RemoteAddr = conn.RemoteAddr()
log.RequestURL = "-"
log.Status = 0
2019-11-06 10:08:44 -05:00
// Read request
reader := bufio.NewReaderSize(conn, 1024)
request, overflow, err := reader.ReadLine()
if overflow {
conn.Write([]byte("59 Request too long!r\n"))
2019-11-06 11:38:41 -05:00
log.Status = 59
logEntries <- log
2019-11-06 10:08:44 -05:00
return
} else if err != nil {
conn.Write([]byte("40 Unknown error reading request!r\n"))
2019-11-06 11:38:41 -05:00
log.Status = 40
logEntries <- log
2019-11-06 10:08:44 -05:00
return
}
// Parse request as URL
URL, err := url.Parse(string(request))
if err != nil {
conn.Write([]byte("59 Error parsing URL!r\n"))
2019-11-06 11:38:41 -05:00
log.Status = 59
logEntries <- log
2019-11-06 10:08:44 -05:00
return
}
2019-11-06 11:38:41 -05:00
log.RequestURL = URL.String()
2019-11-06 10:08:44 -05:00
// Generic response
conn.Write([]byte("20 text/gemini\r\n"))
body := fmt.Sprintf("Molly at %s says \"Hi!\" from %s.\n", URL.Host, URL.Path)
conn.Write([]byte(body))
2019-11-06 11:38:41 -05:00
log.Status = 20
logEntries <- log
2019-11-06 10:08:44 -05:00
}