Browse Source

Factor out request reading code.

master
Solderpunk 2 years ago
parent
commit
b384105d86
  1. 56
      handler.go

56
handler.go

@ -6,6 +6,7 @@ import (
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"errors"
"encoding/hex"
"fmt"
"io/ioutil"
@ -33,15 +34,8 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
defer func() { logEntries <- log }()
// Read request
reader := bufio.NewReaderSize(conn, 1024)
request, overflow, err := reader.ReadLine()
if overflow {
conn.Write([]byte("59 Request too long!\r\n"))
log.Status = 59
return
} else if err != nil {
conn.Write([]byte("40 Unknown error reading request!\r\n"))
log.Status = 40
URL, err := readRequest(conn, log)
if err != nil {
return
}
@ -62,20 +56,6 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
}
}
// Parse request as URL
URL, err := url.Parse(string(request))
if err != nil {
conn.Write([]byte("59 Error parsing URL!\r\n"))
log.Status = 59
return
}
log.RequestURL = URL.String()
// Set implicit scheme
if URL.Scheme == "" {
URL.Scheme = "gemini"
}
// Reject non-gemini schemes
if URL.Scheme != "gemini" {
conn.Write([]byte("53 No proxying to non-Gemini content!\r\n"))
@ -171,6 +151,36 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
}
func readRequest(conn net.Conn, log LogEntry) (*url.URL, error) {
reader := bufio.NewReaderSize(conn, 1024)
request, overflow, err := reader.ReadLine()
if overflow {
conn.Write([]byte("59 Request too long!\r\n"))
log.Status = 59
return nil, errors.New("Request too long")
} else if err != nil {
conn.Write([]byte("40 Unknown error reading request!\r\n"))
log.Status = 40
return nil, errors.New("Error reading request")
}
// Parse request as URL
URL, err := url.Parse(string(request))
if err != nil {
conn.Write([]byte("59 Error parsing URL!\r\n"))
log.Status = 59
return nil, errors.New("Bad URL in request")
}
log.RequestURL = URL.String()
// Set implicit scheme
if URL.Scheme == "" {
URL.Scheme = "gemini"
}
return URL, nil
}
func resolvePath(path string, config Config) (string, os.FileInfo, error) {
// Handle tildes
if strings.HasPrefix(path, "/~") {

Loading…
Cancel
Save