2021-03-24 11:09:53 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE-GO file.
|
|
|
|
|
2020-10-28 12:59:45 -06:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
2021-02-20 13:49:07 -07:00
|
|
|
"context"
|
2021-02-17 18:09:37 -07:00
|
|
|
"net"
|
2020-10-28 12:59:45 -06:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-03-15 13:44:35 -06:00
|
|
|
// Mux is a Gemini request multiplexer.
|
2020-10-28 12:59:45 -06:00
|
|
|
// It matches the URL of each incoming request against a list of registered
|
|
|
|
// patterns and calls the handler for the pattern that
|
|
|
|
// most closely matches the URL.
|
|
|
|
//
|
|
|
|
// Patterns name fixed, rooted paths, like "/favicon.ico",
|
|
|
|
// or rooted subtrees, like "/images/" (note the trailing slash).
|
|
|
|
// Longer patterns take precedence over shorter ones, so that
|
|
|
|
// if there are handlers registered for both "/images/"
|
|
|
|
// and "/images/thumbnails/", the latter handler will be
|
|
|
|
// called for paths beginning "/images/thumbnails/" and the
|
|
|
|
// former will receive requests for any other paths in the
|
|
|
|
// "/images/" subtree.
|
|
|
|
//
|
|
|
|
// Note that since a pattern ending in a slash names a rooted subtree,
|
|
|
|
// the pattern "/" matches all paths not matched by other registered
|
|
|
|
// patterns, not just the URL with Path == "/".
|
|
|
|
//
|
2021-06-26 16:50:05 -06:00
|
|
|
// Patterns may optionally begin with a host name, restricting matches to
|
|
|
|
// URLs on that host only. Host-specific patterns take precedence over
|
|
|
|
// general patterns, so that a handler might register for the two patterns
|
|
|
|
// "/search" and "search.example.com/" without also taking over requests
|
|
|
|
// for "gemini://example.com/".
|
2021-02-17 18:09:37 -07:00
|
|
|
//
|
2021-06-26 18:26:30 -06:00
|
|
|
// Wildcard patterns can be used to match multiple hostnames. For example,
|
|
|
|
// the pattern "*.example.com" will match requests for "blog.example.com"
|
|
|
|
// and "gemini.example.com", but not "example.org".
|
2021-02-17 18:09:37 -07:00
|
|
|
//
|
2020-10-28 12:59:45 -06:00
|
|
|
// If a subtree has been registered and a request is received naming the
|
2021-03-15 13:44:35 -06:00
|
|
|
// subtree root without its trailing slash, Mux redirects that
|
2020-10-28 12:59:45 -06:00
|
|
|
// request to the subtree root (adding the trailing slash). This behavior can
|
|
|
|
// be overridden with a separate registration for the path without
|
2021-03-15 13:44:35 -06:00
|
|
|
// the trailing slash. For example, registering "/images/" causes Mux
|
2020-10-28 12:59:45 -06:00
|
|
|
// to redirect a request for "/images" to "/images/", unless "/images" has
|
|
|
|
// been registered separately.
|
|
|
|
//
|
2021-03-15 13:44:35 -06:00
|
|
|
// Mux also takes care of sanitizing the URL request path and
|
2020-10-28 12:59:45 -06:00
|
|
|
// redirecting any request containing . or .. elements or repeated slashes
|
|
|
|
// to an equivalent, cleaner URL.
|
2021-03-15 13:44:35 -06:00
|
|
|
type Mux struct {
|
2020-10-28 12:59:45 -06:00
|
|
|
mu sync.RWMutex
|
2021-06-26 16:50:05 -06:00
|
|
|
m map[hostpath]Handler
|
2021-02-17 18:09:37 -07:00
|
|
|
es []muxEntry // slice of entries sorted from longest to shortest
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:50:05 -06:00
|
|
|
type hostpath struct {
|
|
|
|
host string
|
|
|
|
path string
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type muxEntry struct {
|
2021-02-17 18:09:37 -07:00
|
|
|
handler Handler
|
2021-06-26 16:50:05 -06:00
|
|
|
host string
|
|
|
|
path string
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
|
|
|
func cleanPath(p string) string {
|
|
|
|
if p == "" {
|
|
|
|
return "/"
|
|
|
|
}
|
|
|
|
if p[0] != '/' {
|
|
|
|
p = "/" + p
|
|
|
|
}
|
|
|
|
np := path.Clean(p)
|
|
|
|
// path.Clean removes trailing slash except for root;
|
|
|
|
// put the trailing slash back if necessary.
|
|
|
|
if p[len(p)-1] == '/' && np != "/" {
|
|
|
|
// Fast path for common case of p being the string we want:
|
|
|
|
if len(p) == len(np)+1 && strings.HasPrefix(p, np) {
|
|
|
|
np = p
|
|
|
|
} else {
|
|
|
|
np += "/"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return np
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a handler on a handler map given a path string.
|
|
|
|
// Most-specific (longest) pattern wins.
|
2021-06-26 16:50:05 -06:00
|
|
|
func (mux *Mux) match(host, path string) Handler {
|
2020-10-28 12:59:45 -06:00
|
|
|
// Check for exact match first.
|
2021-06-26 16:50:05 -06:00
|
|
|
if h, ok := mux.m[hostpath{host, path}]; ok {
|
|
|
|
return h
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for longest valid match. mux.es contains all patterns
|
|
|
|
// that end in / sorted from longest to shortest.
|
|
|
|
for _, e := range mux.es {
|
2021-06-26 16:50:05 -06:00
|
|
|
if len(e.host) == len(host) && e.host == host &&
|
|
|
|
strings.HasPrefix(path, e.path) {
|
2021-02-17 18:09:37 -07:00
|
|
|
return e.handler
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// redirectToPathSlash determines if the given path needs appending "/" to it.
|
|
|
|
// This occurs when a handler for path + "/" was already registered, but
|
|
|
|
// not for path itself. If the path needs appending to, it creates a new
|
|
|
|
// URL, setting the path to u.Path + "/" and returning true to indicate so.
|
2021-06-26 16:50:05 -06:00
|
|
|
func (mux *Mux) redirectToPathSlash(host, path string, u *url.URL) (*url.URL, bool) {
|
2020-10-28 12:59:45 -06:00
|
|
|
mux.mu.RLock()
|
2021-06-26 16:50:05 -06:00
|
|
|
shouldRedirect := mux.shouldRedirectRLocked(host, path)
|
2020-10-28 12:59:45 -06:00
|
|
|
mux.mu.RUnlock()
|
|
|
|
if !shouldRedirect {
|
|
|
|
return u, false
|
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
return u.ResolveReference(&url.URL{Path: path + "/"}), true
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// shouldRedirectRLocked reports whether the given path and host should be redirected to
|
|
|
|
// path+"/". This should happen if a handler is registered for path+"/" but
|
2021-03-15 13:44:35 -06:00
|
|
|
// not path -- see comments at Mux.
|
2021-06-26 16:50:05 -06:00
|
|
|
func (mux *Mux) shouldRedirectRLocked(host, path string) bool {
|
|
|
|
if _, exist := mux.m[hostpath{host, path}]; exist {
|
2020-10-28 12:59:45 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:50:05 -06:00
|
|
|
n := len(path)
|
2020-10-28 12:59:45 -06:00
|
|
|
if n == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
if _, exist := mux.m[hostpath{host, path + "/"}]; exist {
|
|
|
|
return path[n-1] != '/'
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
|
2020-10-28 12:59:45 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:09:37 -07:00
|
|
|
func getWildcard(hostname string) (string, bool) {
|
|
|
|
if net.ParseIP(hostname) == nil {
|
|
|
|
split := strings.SplitN(hostname, ".", 2)
|
|
|
|
if len(split) == 2 {
|
|
|
|
return "*." + split[1], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handler returns the handler to use for the given request, consulting
|
|
|
|
// r.URL.Scheme, r.URL.Host, and r.URL.Path. It always returns a non-nil handler. If
|
|
|
|
// the path is not in its canonical form, the handler will be an
|
|
|
|
// internally-generated handler that redirects to the canonical path. If the
|
|
|
|
// host contains a port, it is ignored when matching handlers.
|
2021-03-15 13:44:35 -06:00
|
|
|
func (mux *Mux) Handler(r *Request) Handler {
|
2021-06-26 16:50:05 -06:00
|
|
|
// Disallow non-Gemini schemes
|
|
|
|
if r.URL.Scheme != "gemini" {
|
|
|
|
return NotFoundHandler()
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:09:37 -07:00
|
|
|
host := r.URL.Hostname()
|
2020-10-28 12:59:45 -06:00
|
|
|
path := cleanPath(r.URL.Path)
|
|
|
|
|
|
|
|
// If the given path is /tree and its handler is not registered,
|
|
|
|
// redirect for /tree/.
|
2021-06-26 16:50:05 -06:00
|
|
|
if u, ok := mux.redirectToPathSlash(host, path, r.URL); ok {
|
2021-02-20 14:45:37 -07:00
|
|
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if path != r.URL.Path {
|
|
|
|
u := *r.URL
|
|
|
|
u.Path = path
|
2021-02-20 14:45:37 -07:00
|
|
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
mux.mu.RLock()
|
|
|
|
defer mux.mu.RUnlock()
|
|
|
|
|
2021-06-26 16:50:05 -06:00
|
|
|
h := mux.match(host, path)
|
|
|
|
|
2021-02-17 18:09:37 -07:00
|
|
|
if h == nil {
|
|
|
|
// Try wildcard
|
|
|
|
if wildcard, ok := getWildcard(host); ok {
|
2021-06-26 16:50:05 -06:00
|
|
|
if u, ok := mux.redirectToPathSlash(wildcard, path, r.URL); ok {
|
|
|
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
|
|
|
}
|
|
|
|
h = mux.match(wildcard, path)
|
2021-02-17 18:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
|
|
|
|
if h == nil {
|
|
|
|
// Try empty host
|
|
|
|
if u, ok := mux.redirectToPathSlash("", path, r.URL); ok {
|
|
|
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
|
|
|
}
|
|
|
|
h = mux.match("", path)
|
|
|
|
}
|
|
|
|
|
2021-02-17 18:09:37 -07:00
|
|
|
if h == nil {
|
|
|
|
h = NotFoundHandler()
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
|
2021-02-17 18:09:37 -07:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeGemini dispatches the request to the handler whose
|
|
|
|
// pattern most closely matches the request URL.
|
2021-03-15 13:44:35 -06:00
|
|
|
func (mux *Mux) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
2021-02-17 18:09:37 -07:00
|
|
|
h := mux.Handler(r)
|
2021-02-20 13:49:07 -07:00
|
|
|
h.ServeGemini(ctx, w, r)
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:50:50 -07:00
|
|
|
// Handle registers the handler for the given pattern.
|
|
|
|
// If a handler already exists for pattern, Handle panics.
|
2021-03-15 13:44:35 -06:00
|
|
|
func (mux *Mux) Handle(pattern string, handler Handler) {
|
2021-02-19 16:06:54 -07:00
|
|
|
if pattern == "" {
|
|
|
|
panic("gemini: invalid pattern")
|
|
|
|
}
|
2021-02-17 18:09:37 -07:00
|
|
|
if handler == nil {
|
|
|
|
panic("gemini: nil handler")
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:59:45 -06:00
|
|
|
mux.mu.Lock()
|
|
|
|
defer mux.mu.Unlock()
|
|
|
|
|
2021-06-26 16:50:05 -06:00
|
|
|
var host, path string
|
2021-02-17 18:09:37 -07:00
|
|
|
// extract hostname and path
|
|
|
|
cut := strings.Index(pattern, "/")
|
|
|
|
if cut == -1 {
|
2021-06-26 16:50:05 -06:00
|
|
|
host = pattern
|
|
|
|
path = "/"
|
2021-02-17 18:09:37 -07:00
|
|
|
} else {
|
2021-06-26 16:50:05 -06:00
|
|
|
host = pattern[:cut]
|
|
|
|
path = pattern[cut:]
|
2021-02-17 18:09:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// strip port from hostname
|
2021-06-26 16:50:05 -06:00
|
|
|
if hostname, _, err := net.SplitHostPort(host); err == nil {
|
|
|
|
host = hostname
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
2021-02-17 18:09:37 -07:00
|
|
|
|
2021-06-26 16:50:05 -06:00
|
|
|
if _, exist := mux.m[hostpath{host, path}]; exist {
|
2020-10-28 12:59:45 -06:00
|
|
|
panic("gemini: multiple registrations for " + pattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mux.m == nil {
|
2021-06-26 16:50:05 -06:00
|
|
|
mux.m = make(map[hostpath]Handler)
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|
2021-06-26 16:50:05 -06:00
|
|
|
mux.m[hostpath{host, path}] = handler
|
|
|
|
e := muxEntry{handler, host, path}
|
|
|
|
if path[len(path)-1] == '/' {
|
2020-10-28 12:59:45 -06:00
|
|
|
mux.es = appendSorted(mux.es, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
|
|
|
|
n := len(es)
|
|
|
|
i := sort.Search(n, func(i int) bool {
|
2021-06-26 16:50:05 -06:00
|
|
|
return len(es[i].path) < len(e.path)
|
2020-10-28 12:59:45 -06:00
|
|
|
})
|
|
|
|
if i == n {
|
|
|
|
return append(es, e)
|
|
|
|
}
|
|
|
|
// we now know that i points at where we want to insert
|
|
|
|
es = append(es, muxEntry{}) // try to grow the slice in place, any entry works.
|
|
|
|
copy(es[i+1:], es[i:]) // move shorter entries down
|
|
|
|
es[i] = e
|
|
|
|
return es
|
|
|
|
}
|
|
|
|
|
2021-02-08 10:50:50 -07:00
|
|
|
// HandleFunc registers the handler function for the given pattern.
|
2021-03-15 13:44:35 -06:00
|
|
|
func (mux *Mux) HandleFunc(pattern string, handler HandlerFunc) {
|
2021-02-23 07:48:58 -07:00
|
|
|
mux.Handle(pattern, handler)
|
2020-10-28 12:59:45 -06:00
|
|
|
}
|