2020-09-21 19:48:42 +00:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2020-09-21 23:17:10 +00:00
|
|
|
"crypto/x509"
|
2020-09-21 19:48:42 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Status codes.
|
|
|
|
const (
|
|
|
|
StatusInput = 10
|
|
|
|
StatusSensitiveInput = 11
|
|
|
|
StatusSuccess = 20
|
|
|
|
StatusRedirectTemporary = 30
|
|
|
|
StatusRedirectPermanent = 31
|
|
|
|
StatusTemporaryFailure = 40
|
|
|
|
StatusServerUnavailable = 41
|
|
|
|
StatusCGIError = 42
|
|
|
|
StatusProxyError = 43
|
|
|
|
StatusSlowDown = 44
|
|
|
|
StatusPermanentFailure = 50
|
|
|
|
StatusNotFound = 51
|
|
|
|
StatusGone = 52
|
|
|
|
StatusProxyRequestRefused = 53
|
|
|
|
StatusBadRequest = 59
|
|
|
|
StatusClientCertificateRequired = 60
|
|
|
|
StatusCertificateNotAuthorised = 61
|
|
|
|
StatusCertificateNotValid = 62
|
|
|
|
)
|
|
|
|
|
|
|
|
// Status code categories.
|
|
|
|
const (
|
|
|
|
StatusClassInput = 1
|
|
|
|
StatusClassSuccess = 2
|
|
|
|
StatusClassRedirect = 3
|
|
|
|
StatusClassTemporaryFailure = 4
|
|
|
|
StatusClassPermanentFailure = 5
|
|
|
|
StatusClassClientCertificateRequired = 6
|
|
|
|
)
|
|
|
|
|
|
|
|
// Response is a Gemini response.
|
|
|
|
type Response struct {
|
|
|
|
Status int
|
|
|
|
Meta string
|
|
|
|
Body []byte
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// Write writes the Gemini response header and body to the provided io.Writer.
|
2020-09-21 19:48:42 +00:00
|
|
|
func (r *Response) Write(w io.Writer) {
|
|
|
|
header := strconv.Itoa(r.Status) + " " + r.Meta + "\r\n"
|
|
|
|
w.Write([]byte(header))
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// Only write the response body on success
|
2020-09-21 19:48:42 +00:00
|
|
|
if r.Status/10 == StatusClassSuccess {
|
|
|
|
w.Write(r.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Server is a Gemini server.
|
|
|
|
type Server struct {
|
|
|
|
Addr string
|
2020-09-21 22:21:42 +00:00
|
|
|
TLSConfig tls.Config
|
2020-09-21 19:48:42 +00:00
|
|
|
Handler Handler
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// ListenAndServe listens for requests at the server's configured address.
|
2020-09-21 19:48:42 +00:00
|
|
|
func (s *Server) ListenAndServe() error {
|
|
|
|
addr := s.Addr
|
|
|
|
if addr == "" {
|
|
|
|
addr = ":1965"
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
|
2020-09-21 22:21:42 +00:00
|
|
|
tlsListener := tls.NewListener(ln, &s.TLSConfig)
|
2020-09-21 19:48:42 +00:00
|
|
|
return s.Serve(tlsListener)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve listens for requests on the provided listener.
|
|
|
|
func (s *Server) Serve(ln net.Listener) error {
|
|
|
|
for {
|
|
|
|
rw, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-22 01:31:09 +00:00
|
|
|
var resp *Response
|
|
|
|
|
|
|
|
if rawurl, err := readLine(rw); err != nil {
|
|
|
|
resp = &Response{
|
|
|
|
Status: StatusBadRequest,
|
|
|
|
Meta: "Bad request",
|
|
|
|
}
|
|
|
|
} else if len(rawurl) > 1024 {
|
|
|
|
resp = &Response{
|
|
|
|
Status: StatusBadRequest,
|
|
|
|
Meta: "URL exceeds 1024 bytes",
|
|
|
|
}
|
|
|
|
} else if url, err := url.Parse(rawurl); err != nil {
|
|
|
|
resp = &Response{
|
|
|
|
Status: StatusBadRequest,
|
|
|
|
Meta: "Invalid URL",
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Gather information about the request
|
|
|
|
reqInfo := &RequestInfo{
|
|
|
|
URL: url,
|
|
|
|
Certificates: rw.(*tls.Conn).ConnectionState().PeerCertificates,
|
|
|
|
RemoteAddr: rw.RemoteAddr(),
|
|
|
|
}
|
|
|
|
resp = s.Handler.Serve(reqInfo)
|
2020-09-21 19:48:42 +00:00
|
|
|
}
|
2020-09-21 23:17:10 +00:00
|
|
|
|
2020-09-21 19:48:42 +00:00
|
|
|
resp.Write(rw)
|
|
|
|
rw.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-21 23:17:10 +00:00
|
|
|
// RequestInfo contains information about a request.
|
|
|
|
type RequestInfo struct {
|
2020-09-22 00:44:10 +00:00
|
|
|
URL *url.URL // the requested URL
|
|
|
|
Certificates []*x509.Certificate // client certificates
|
|
|
|
RemoteAddr net.Addr
|
2020-09-21 23:17:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:36:09 +00:00
|
|
|
// A Handler responds to a Gemini request.
|
2020-09-21 19:48:42 +00:00
|
|
|
type Handler interface {
|
2020-09-21 23:17:10 +00:00
|
|
|
// Serve accepts a Request and returns a Response.
|
|
|
|
Serve(*RequestInfo) *Response
|
2020-09-21 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mux is a Gemini request multiplexer.
|
|
|
|
// It matches the URL of each incoming request against a list of registered
|
|
|
|
// patterns and calls the handler for the pattern that most closesly matches
|
|
|
|
// the URL.
|
|
|
|
type Mux struct {
|
|
|
|
entries []muxEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
type muxEntry struct {
|
|
|
|
scheme string
|
|
|
|
host string
|
|
|
|
path string
|
|
|
|
handler Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mux) match(url *url.URL) Handler {
|
|
|
|
for _, e := range m.entries {
|
|
|
|
if (e.scheme == "" || url.Scheme == e.scheme) &&
|
|
|
|
(e.host == "" || url.Host == e.host) &&
|
|
|
|
strings.HasPrefix(url.Path, e.path) {
|
|
|
|
return e.handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// Handle registers a handler for the given pattern.
|
2020-09-21 19:48:42 +00:00
|
|
|
func (m *Mux) Handle(pattern string, handler Handler) {
|
|
|
|
url, err := url.Parse(pattern)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
m.entries = append(m.entries, muxEntry{
|
|
|
|
url.Scheme,
|
|
|
|
url.Host,
|
|
|
|
url.Path,
|
|
|
|
handler,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// HandleFunc registers a HandlerFunc for the given pattern.
|
2020-09-21 23:17:10 +00:00
|
|
|
func (m *Mux) HandleFunc(pattern string, handlerFunc func(req *RequestInfo) *Response) {
|
2020-09-21 19:48:42 +00:00
|
|
|
handler := HandlerFunc(handlerFunc)
|
|
|
|
m.Handle(pattern, handler)
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// Serve responds to the request with the appropriate handler.
|
2020-09-21 23:17:10 +00:00
|
|
|
func (m *Mux) Serve(req *RequestInfo) *Response {
|
|
|
|
h := m.match(req.URL)
|
2020-09-21 19:48:42 +00:00
|
|
|
if h == nil {
|
|
|
|
return &Response{
|
|
|
|
Status: StatusNotFound,
|
|
|
|
Meta: "Not found",
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 23:17:10 +00:00
|
|
|
return h.Serve(req)
|
2020-09-21 19:48:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 21:23:51 +00:00
|
|
|
// A wrapper around a bare function that implements Handler.
|
2020-09-21 23:17:10 +00:00
|
|
|
type HandlerFunc func(req *RequestInfo) *Response
|
2020-09-21 19:48:42 +00:00
|
|
|
|
2020-09-21 23:17:10 +00:00
|
|
|
func (f HandlerFunc) Serve(req *RequestInfo) *Response {
|
|
|
|
return f(req)
|
2020-09-21 19:48:42 +00:00
|
|
|
}
|