forked from solderpunk/molly-brown
Factor out request reading code.
This commit is contained in:
parent
7ffbb6c6ef
commit
b384105d86
56
handler.go
56
handler.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -33,15 +34,8 @@ func handleGeminiRequest(conn net.Conn, config Config, logEntries chan LogEntry)
|
||||||
defer func() { logEntries <- log }()
|
defer func() { logEntries <- log }()
|
||||||
|
|
||||||
// Read request
|
// Read request
|
||||||
reader := bufio.NewReaderSize(conn, 1024)
|
URL, err := readRequest(conn, log)
|
||||||
request, overflow, err := reader.ReadLine()
|
if err != nil {
|
||||||
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
|
|
||||||
return
|
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
|
// Reject non-gemini schemes
|
||||||
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"))
|
||||||
|
@ -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) {
|
func resolvePath(path string, config Config) (string, os.FileInfo, error) {
|
||||||
// Handle tildes
|
// Handle tildes
|
||||||
if strings.HasPrefix(path, "/~") {
|
if strings.HasPrefix(path, "/~") {
|
||||||
|
|
Loading…
Reference in New Issue