Rename Handler to Responder
This commit is contained in:
parent
53326e59a0
commit
aeff8a051c
@ -45,7 +45,7 @@ func main() {
|
|||||||
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
|
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.Handle("localhost", handler)
|
server.Register("localhost", handler)
|
||||||
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -49,7 +49,7 @@ func main() {
|
|||||||
var mux gmi.ServeMux
|
var mux gmi.ServeMux
|
||||||
mux.Handle("/", gmi.FileServer(gmi.Dir("/var/www")))
|
mux.Handle("/", gmi.FileServer(gmi.Dir("/var/www")))
|
||||||
|
|
||||||
server.Handle("localhost", &mux)
|
server.Register("localhost", &mux)
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
4
fs.go
4
fs.go
@ -16,7 +16,7 @@ func init() {
|
|||||||
|
|
||||||
// FileServer takes a filesystem and returns a Handler which uses that filesystem.
|
// FileServer takes a filesystem and returns a Handler which uses that filesystem.
|
||||||
// The returned Handler sanitizes paths before handling them.
|
// The returned Handler sanitizes paths before handling them.
|
||||||
func FileServer(fsys FS) Handler {
|
func FileServer(fsys FS) Responder {
|
||||||
return fsHandler{fsys}
|
return fsHandler{fsys}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ type fsHandler struct {
|
|||||||
FS
|
FS
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fsh fsHandler) Serve(w *ResponseWriter, r *Request) {
|
func (fsh fsHandler) Respond(w *ResponseWriter, r *Request) {
|
||||||
path := path.Clean(r.URL.Path)
|
path := path.Clean(r.URL.Path)
|
||||||
f, err := fsh.Open(path)
|
f, err := fsh.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
200
server.go
200
server.go
@ -29,33 +29,33 @@ type Server struct {
|
|||||||
// If the certificate is nil, the connection will be aborted.
|
// If the certificate is nil, the connection will be aborted.
|
||||||
GetCertificate func(hostname string, store *CertificateStore) *tls.Certificate
|
GetCertificate func(hostname string, store *CertificateStore) *tls.Certificate
|
||||||
|
|
||||||
// registered handlers
|
// registered responders
|
||||||
handlers map[handlerKey]Handler
|
responders map[responderKey]Responder
|
||||||
}
|
}
|
||||||
|
|
||||||
type handlerKey struct {
|
type responderKey struct {
|
||||||
scheme string
|
scheme string
|
||||||
hostname string
|
hostname string
|
||||||
wildcard bool
|
wildcard bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers a handler for the given pattern.
|
// Register registers a responder for the given pattern.
|
||||||
// Patterns must be in the form of scheme://hostname (e.g. gemini://example.com).
|
// Patterns must be in the form of scheme://hostname (e.g. gemini://example.com).
|
||||||
// If no scheme is specified, a default scheme of gemini:// is assumed.
|
// If no scheme is specified, a default scheme of gemini:// is assumed.
|
||||||
// Wildcard patterns are supported (e.g. *.example.com).
|
// Wildcard patterns are supported (e.g. *.example.com).
|
||||||
func (s *Server) Handle(pattern string, handler Handler) {
|
func (s *Server) Register(pattern string, responder Responder) {
|
||||||
if pattern == "" {
|
if pattern == "" {
|
||||||
panic("gmi: invalid pattern")
|
panic("gmi: invalid pattern")
|
||||||
}
|
}
|
||||||
if handler == nil {
|
if responder == nil {
|
||||||
panic("gmi: nil handler")
|
panic("gmi: nil responder")
|
||||||
}
|
}
|
||||||
if s.handlers == nil {
|
if s.responders == nil {
|
||||||
s.handlers = map[handlerKey]Handler{}
|
s.responders = map[responderKey]Responder{}
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.SplitN(pattern, "://", 2)
|
split := strings.SplitN(pattern, "://", 2)
|
||||||
var key handlerKey
|
var key responderKey
|
||||||
if len(split) == 2 {
|
if len(split) == 2 {
|
||||||
key.scheme = split[0]
|
key.scheme = split[0]
|
||||||
key.hostname = split[1]
|
key.hostname = split[1]
|
||||||
@ -69,18 +69,12 @@ func (s *Server) Handle(pattern string, handler Handler) {
|
|||||||
key.wildcard = true
|
key.wildcard = true
|
||||||
}
|
}
|
||||||
|
|
||||||
s.handlers[key] = handler
|
s.responders[key] = responder
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleFunc registers a handler function for the given pattern.
|
// RegisterFunc registers a responder function for the given pattern.
|
||||||
func (s *Server) HandleFunc(pattern string, handler func(*ResponseWriter, *Request)) {
|
func (s *Server) RegisterFunc(pattern string, responder func(*ResponseWriter, *Request)) {
|
||||||
s.Handle(pattern, HandlerFunc(handler))
|
s.Register(pattern, ResponderFunc(responder))
|
||||||
}
|
|
||||||
|
|
||||||
type handlerEntry struct {
|
|
||||||
scheme string
|
|
||||||
host string
|
|
||||||
h Handler
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenAndServe listens for requests at the server's configured address.
|
// ListenAndServe listens for requests at the server's configured address.
|
||||||
@ -127,7 +121,7 @@ func (s *Server) Serve(l net.Listener) error {
|
|||||||
if max := 1 * time.Second; tempDelay > max {
|
if max := 1 * time.Second; tempDelay > max {
|
||||||
tempDelay = max
|
tempDelay = max
|
||||||
}
|
}
|
||||||
log.Printf("gemini: Accept error: %v; retrying in %v", err, tempDelay)
|
log.Printf("gmi: Accept error: %v; retrying in %v", err, tempDelay)
|
||||||
time.Sleep(tempDelay)
|
time.Sleep(tempDelay)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -141,6 +135,58 @@ func (s *Server) Serve(l net.Listener) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// respond responds to a connection.
|
||||||
|
func (s *Server) respond(conn net.Conn) {
|
||||||
|
r := bufio.NewReader(conn)
|
||||||
|
w := newResponseWriter(conn)
|
||||||
|
// Read requested URL
|
||||||
|
rawurl, err := r.ReadString('\r')
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Read terminating line feed
|
||||||
|
if b, err := r.ReadByte(); err != nil {
|
||||||
|
return
|
||||||
|
} else if b != '\n' {
|
||||||
|
w.WriteHeader(StatusBadRequest, "Bad request")
|
||||||
|
}
|
||||||
|
// Trim carriage return
|
||||||
|
rawurl = rawurl[:len(rawurl)-1]
|
||||||
|
// Ensure URL is valid
|
||||||
|
if len(rawurl) > 1024 {
|
||||||
|
w.WriteHeader(StatusBadRequest, "Bad request")
|
||||||
|
} else if url, err := url.Parse(rawurl); err != nil || url.User != nil {
|
||||||
|
// Note that we return an error status if User is specified in the URL
|
||||||
|
w.WriteHeader(StatusBadRequest, "Bad request")
|
||||||
|
} else {
|
||||||
|
// If no scheme is specified, assume a default scheme of gemini://
|
||||||
|
if url.Scheme == "" {
|
||||||
|
url.Scheme = "gemini"
|
||||||
|
}
|
||||||
|
req := &Request{
|
||||||
|
URL: url,
|
||||||
|
RemoteAddr: conn.RemoteAddr(),
|
||||||
|
TLS: conn.(*tls.Conn).ConnectionState(),
|
||||||
|
}
|
||||||
|
s.responder(req).Respond(w, req)
|
||||||
|
}
|
||||||
|
w.b.Flush()
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Server) responder(r *Request) Responder {
|
||||||
|
if h, ok := s.responders[responderKey{r.URL.Scheme, r.URL.Hostname(), false}]; ok {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
wildcard := strings.SplitN(r.URL.Hostname(), ".", 2)
|
||||||
|
if len(wildcard) == 2 {
|
||||||
|
if h, ok := s.responders[responderKey{r.URL.Scheme, wildcard[1], true}]; ok {
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NotFoundHandler()
|
||||||
|
}
|
||||||
|
|
||||||
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
||||||
type ResponseWriter struct {
|
type ResponseWriter struct {
|
||||||
b *bufio.Writer
|
b *bufio.Writer
|
||||||
@ -207,62 +253,10 @@ func (w *ResponseWriter) Write(b []byte) (int, error) {
|
|||||||
return w.b.Write(b)
|
return w.b.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// respond responds to a connection.
|
// A Responder responds to a Gemini request.
|
||||||
func (s *Server) respond(conn net.Conn) {
|
type Responder interface {
|
||||||
r := bufio.NewReader(conn)
|
// Respond accepts a Request and constructs a Response.
|
||||||
w := newResponseWriter(conn)
|
Respond(*ResponseWriter, *Request)
|
||||||
// Read requested URL
|
|
||||||
rawurl, err := r.ReadString('\r')
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Read terminating line feed
|
|
||||||
if b, err := r.ReadByte(); err != nil {
|
|
||||||
return
|
|
||||||
} else if b != '\n' {
|
|
||||||
w.WriteHeader(StatusBadRequest, "Bad request")
|
|
||||||
}
|
|
||||||
// Trim carriage return
|
|
||||||
rawurl = rawurl[:len(rawurl)-1]
|
|
||||||
// Ensure URL is valid
|
|
||||||
if len(rawurl) > 1024 {
|
|
||||||
w.WriteHeader(StatusBadRequest, "Bad request")
|
|
||||||
} else if url, err := url.Parse(rawurl); err != nil || url.User != nil {
|
|
||||||
// Note that we return an error status if User is specified in the URL
|
|
||||||
w.WriteHeader(StatusBadRequest, "Bad request")
|
|
||||||
} else {
|
|
||||||
// If no scheme is specified, assume a default scheme of gemini://
|
|
||||||
if url.Scheme == "" {
|
|
||||||
url.Scheme = "gemini"
|
|
||||||
}
|
|
||||||
req := &Request{
|
|
||||||
URL: url,
|
|
||||||
RemoteAddr: conn.RemoteAddr(),
|
|
||||||
TLS: conn.(*tls.Conn).ConnectionState(),
|
|
||||||
}
|
|
||||||
s.handler(req).Serve(w, req)
|
|
||||||
}
|
|
||||||
w.b.Flush()
|
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) handler(r *Request) Handler {
|
|
||||||
if h, ok := s.handlers[handlerKey{r.URL.Scheme, r.URL.Hostname(), false}]; ok {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
wildcard := strings.SplitN(r.URL.Hostname(), ".", 2)
|
|
||||||
if len(wildcard) == 2 {
|
|
||||||
if h, ok := s.handlers[handlerKey{r.URL.Scheme, wildcard[1], true}]; ok {
|
|
||||||
return h
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NotFoundHandler()
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Handler responds to a Gemini request.
|
|
||||||
type Handler interface {
|
|
||||||
// Serve accepts a Request and constructs a Response.
|
|
||||||
Serve(*ResponseWriter, *Request)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Input responds to the request with a request for input using the given prompt.
|
// Input responds to the request with a request for input using the given prompt.
|
||||||
@ -272,8 +266,8 @@ func Input(w *ResponseWriter, r *Request, prompt string) {
|
|||||||
|
|
||||||
// InputHandler returns a simple handler that responds to each request with
|
// InputHandler returns a simple handler that responds to each request with
|
||||||
// a request for input.
|
// a request for input.
|
||||||
func InputHandler(prompt string) Handler {
|
func InputHandler(prompt string) Responder {
|
||||||
return HandlerFunc(func(w *ResponseWriter, r *Request) {
|
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||||
Input(w, r, prompt)
|
Input(w, r, prompt)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -297,8 +291,8 @@ func SensitiveInput(w *ResponseWriter, r *Request, prompt string) {
|
|||||||
|
|
||||||
// SensitiveInputHandler returns a simpler handler that responds to each request
|
// SensitiveInputHandler returns a simpler handler that responds to each request
|
||||||
// with a request for sensitive input.
|
// with a request for sensitive input.
|
||||||
func SensitiveInputHandler(prompt string) Handler {
|
func SensitiveInputHandler(prompt string) Responder {
|
||||||
return HandlerFunc(func(w *ResponseWriter, r *Request) {
|
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||||
SensitiveInput(w, r, prompt)
|
SensitiveInput(w, r, prompt)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -321,8 +315,8 @@ func Redirect(w *ResponseWriter, r *Request, url string) {
|
|||||||
|
|
||||||
// RedirectHandler returns a simple handler that responds to each request with
|
// RedirectHandler returns a simple handler that responds to each request with
|
||||||
// a redirect to the given URL.
|
// a redirect to the given URL.
|
||||||
func RedirectHandler(url string) Handler {
|
func RedirectHandler(url string) Responder {
|
||||||
return HandlerFunc(func(w *ResponseWriter, r *Request) {
|
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||||
Redirect(w, r, url)
|
Redirect(w, r, url)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -334,8 +328,8 @@ func PermanentRedirect(w *ResponseWriter, r *Request, url string) {
|
|||||||
|
|
||||||
// PermanentRedirectHandler returns a simple handler that responds to each request with
|
// PermanentRedirectHandler returns a simple handler that responds to each request with
|
||||||
// a redirect to the given URL.
|
// a redirect to the given URL.
|
||||||
func PermanentRedirectHandler(url string) Handler {
|
func PermanentRedirectHandler(url string) Responder {
|
||||||
return HandlerFunc(func(w *ResponseWriter, r *Request) {
|
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||||
PermanentRedirect(w, r, url)
|
PermanentRedirect(w, r, url)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -347,8 +341,8 @@ func NotFound(w *ResponseWriter, r *Request) {
|
|||||||
|
|
||||||
// NotFoundHandler returns a simple handler that responds to each request with
|
// NotFoundHandler returns a simple handler that responds to each request with
|
||||||
// the status code NotFound.
|
// the status code NotFound.
|
||||||
func NotFoundHandler() Handler {
|
func NotFoundHandler() Responder {
|
||||||
return HandlerFunc(NotFound)
|
return ResponderFunc(NotFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gone replies to the request with the Gone status code.
|
// Gone replies to the request with the Gone status code.
|
||||||
@ -358,8 +352,8 @@ func Gone(w *ResponseWriter, r *Request) {
|
|||||||
|
|
||||||
// GoneHandler returns a simple handler that responds to each request with
|
// GoneHandler returns a simple handler that responds to each request with
|
||||||
// the status code Gone.
|
// the status code Gone.
|
||||||
func GoneHandler() Handler {
|
func GoneHandler() Responder {
|
||||||
return HandlerFunc(Gone)
|
return ResponderFunc(Gone)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CertificateRequired responds to the request with the CertificateRequired
|
// CertificateRequired responds to the request with the CertificateRequired
|
||||||
@ -388,16 +382,16 @@ func WithCertificate(w *ResponseWriter, r *Request, f func(*x509.Certificate)) {
|
|||||||
// CertificateHandler returns a simple handler that requests a certificate from
|
// CertificateHandler returns a simple handler that requests a certificate from
|
||||||
// clients if they did not provide one, and calls f with the first certificate
|
// clients if they did not provide one, and calls f with the first certificate
|
||||||
// if they did.
|
// if they did.
|
||||||
func CertificateHandler(f func(*x509.Certificate)) Handler {
|
func CertificateHandler(f func(*x509.Certificate)) Responder {
|
||||||
return HandlerFunc(func(w *ResponseWriter, r *Request) {
|
return ResponderFunc(func(w *ResponseWriter, r *Request) {
|
||||||
WithCertificate(w, r, f)
|
WithCertificate(w, r, f)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandlerFunc is a wrapper around a bare function that implements Handler.
|
// ResponderFunc is a wrapper around a bare function that implements Handler.
|
||||||
type HandlerFunc func(*ResponseWriter, *Request)
|
type ResponderFunc func(*ResponseWriter, *Request)
|
||||||
|
|
||||||
func (f HandlerFunc) Serve(w *ResponseWriter, r *Request) {
|
func (f ResponderFunc) Respond(w *ResponseWriter, r *Request) {
|
||||||
f(w, r)
|
f(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -443,7 +437,7 @@ type ServeMux struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type muxEntry struct {
|
type muxEntry struct {
|
||||||
h Handler
|
h Responder
|
||||||
pattern string
|
pattern string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -471,7 +465,7 @@ func cleanPath(p string) string {
|
|||||||
|
|
||||||
// Find a handler on a handler map given a path string.
|
// Find a handler on a handler map given a path string.
|
||||||
// Most-specific (longest) pattern wins.
|
// Most-specific (longest) pattern wins.
|
||||||
func (mux *ServeMux) match(path string) (h Handler, pattern string) {
|
func (mux *ServeMux) match(path string) (h Responder, pattern string) {
|
||||||
// Check for exact match first.
|
// Check for exact match first.
|
||||||
v, ok := mux.m[path]
|
v, ok := mux.m[path]
|
||||||
if ok {
|
if ok {
|
||||||
@ -536,7 +530,7 @@ func (mux *ServeMux) shouldRedirectRLocked(path string) bool {
|
|||||||
//
|
//
|
||||||
// If there is no registered handler that applies to the request,
|
// If there is no registered handler that applies to the request,
|
||||||
// Handler returns a "not found" handler and an empty pattern.
|
// Handler returns a "not found" handler and an empty pattern.
|
||||||
func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
|
func (mux *ServeMux) Handler(r *Request) (h Responder, pattern string) {
|
||||||
path := cleanPath(r.URL.Path)
|
path := cleanPath(r.URL.Path)
|
||||||
|
|
||||||
// If the given path is /tree and its handler is not registered,
|
// If the given path is /tree and its handler is not registered,
|
||||||
@ -557,7 +551,7 @@ func (mux *ServeMux) Handler(r *Request) (h Handler, pattern string) {
|
|||||||
|
|
||||||
// handler is the main implementation of Handler.
|
// handler is the main implementation of Handler.
|
||||||
// The path is known to be in canonical form.
|
// The path is known to be in canonical form.
|
||||||
func (mux *ServeMux) handler(path string) (h Handler, pattern string) {
|
func (mux *ServeMux) handler(path string) (h Responder, pattern string) {
|
||||||
mux.mu.RLock()
|
mux.mu.RLock()
|
||||||
defer mux.mu.RUnlock()
|
defer mux.mu.RUnlock()
|
||||||
|
|
||||||
@ -568,16 +562,16 @@ func (mux *ServeMux) handler(path string) (h Handler, pattern string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve dispatches the request to the handler whose
|
// Respond dispatches the request to the handler whose
|
||||||
// pattern most closely matches the request URL.
|
// pattern most closely matches the request URL.
|
||||||
func (mux *ServeMux) Serve(w *ResponseWriter, r *Request) {
|
func (mux *ServeMux) Respond(w *ResponseWriter, r *Request) {
|
||||||
h, _ := mux.Handler(r)
|
h, _ := mux.Handler(r)
|
||||||
h.Serve(w, r)
|
h.Respond(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers the handler for the given pattern.
|
// Handle registers the handler for the given pattern.
|
||||||
// If a handler already exists for pattern, Handle panics.
|
// If a handler already exists for pattern, Handle panics.
|
||||||
func (mux *ServeMux) Handle(pattern string, handler Handler) {
|
func (mux *ServeMux) Handle(pattern string, handler Responder) {
|
||||||
mux.mu.Lock()
|
mux.mu.Lock()
|
||||||
defer mux.mu.Unlock()
|
defer mux.mu.Unlock()
|
||||||
|
|
||||||
@ -621,5 +615,5 @@ func (mux *ServeMux) HandleFunc(pattern string, handler func(*ResponseWriter, *R
|
|||||||
if handler == nil {
|
if handler == nil {
|
||||||
panic("gmi: nil handler")
|
panic("gmi: nil handler")
|
||||||
}
|
}
|
||||||
mux.Handle(pattern, HandlerFunc(handler))
|
mux.Handle(pattern, ResponderFunc(handler))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user