Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d2110d90f | ||
|
|
b5c47a5ef0 | ||
|
|
fb0d4d24bd |
148
mux.go
148
mux.go
@@ -32,31 +32,15 @@ import (
|
|||||||
// the pattern "/" matches all paths not matched by other registered
|
// the pattern "/" matches all paths not matched by other registered
|
||||||
// patterns, not just the URL with Path == "/".
|
// patterns, not just the URL with Path == "/".
|
||||||
//
|
//
|
||||||
// Patterns may also contain schemes and hostnames.
|
// Patterns may optionally begin with a host name, restricting matches to
|
||||||
// Wildcard patterns can be used to match multiple hostnames (e.g. "*.example.com").
|
// 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/".
|
||||||
//
|
//
|
||||||
// The following are examples of valid patterns, along with the scheme,
|
// Wildcard patterns can be used to match multiple hostnames. For example,
|
||||||
// hostname, and path that they match.
|
// the pattern "*.example.com" will match requests for "blog.example.com"
|
||||||
//
|
// and "gemini.example.com", but not "example.org".
|
||||||
// Pattern │ Scheme │ Hostname │ Path
|
|
||||||
// ─────────────────────────────┼────────┼──────────┼─────────────
|
|
||||||
// /file │ gemini │ * │ /file
|
|
||||||
// /directory/ │ gemini │ * │ /directory/*
|
|
||||||
// hostname/file │ gemini │ hostname │ /file
|
|
||||||
// hostname/directory/ │ gemini │ hostname │ /directory/*
|
|
||||||
// scheme://hostname/file │ scheme │ hostname │ /file
|
|
||||||
// scheme://hostname/directory/ │ scheme │ hostname │ /directory/*
|
|
||||||
// //hostname/file │ * │ hostname │ /file
|
|
||||||
// //hostname/directory/ │ * │ hostname │ /directory/*
|
|
||||||
// scheme:///file │ scheme │ * │ /file
|
|
||||||
// scheme:///directory/ │ scheme │ * │ /directory/*
|
|
||||||
// ///file │ * │ * │ /file
|
|
||||||
// ///directory/ │ * │ * │ /directory/*
|
|
||||||
//
|
|
||||||
// A pattern without a hostname will match any hostname.
|
|
||||||
// If a pattern begins with "//", it will match any scheme.
|
|
||||||
// Otherwise, a pattern with no scheme is treated as though it has a
|
|
||||||
// scheme of "gemini".
|
|
||||||
//
|
//
|
||||||
// If a subtree has been registered and a request is received naming the
|
// If a subtree has been registered and a request is received naming the
|
||||||
// subtree root without its trailing slash, Mux redirects that
|
// subtree root without its trailing slash, Mux redirects that
|
||||||
@@ -71,19 +55,19 @@ import (
|
|||||||
// to an equivalent, cleaner URL.
|
// to an equivalent, cleaner URL.
|
||||||
type Mux struct {
|
type Mux struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
m map[muxKey]Handler
|
m map[hostpath]Handler
|
||||||
es []muxEntry // slice of entries sorted from longest to shortest
|
es []muxEntry // slice of entries sorted from longest to shortest
|
||||||
}
|
}
|
||||||
|
|
||||||
type muxKey struct {
|
type hostpath struct {
|
||||||
scheme string
|
host string
|
||||||
host string
|
path string
|
||||||
path string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type muxEntry struct {
|
type muxEntry struct {
|
||||||
handler Handler
|
handler Handler
|
||||||
key muxKey
|
host string
|
||||||
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
// cleanPath returns the canonical path for p, eliminating . and .. elements.
|
||||||
@@ -110,24 +94,17 @@ 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 *Mux) match(key muxKey) Handler {
|
func (mux *Mux) match(host, path string) Handler {
|
||||||
// Check for exact match first.
|
// Check for exact match first.
|
||||||
if r, ok := mux.m[key]; ok {
|
if h, ok := mux.m[hostpath{host, path}]; ok {
|
||||||
return r
|
return h
|
||||||
} else if r, ok := mux.m[muxKey{"", key.host, key.path}]; ok {
|
|
||||||
return r
|
|
||||||
} else if r, ok := mux.m[muxKey{key.scheme, "", key.path}]; ok {
|
|
||||||
return r
|
|
||||||
} else if r, ok := mux.m[muxKey{"", "", key.path}]; ok {
|
|
||||||
return r
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for longest valid match. mux.es contains all patterns
|
// Check for longest valid match. mux.es contains all patterns
|
||||||
// that end in / sorted from longest to shortest.
|
// that end in / sorted from longest to shortest.
|
||||||
for _, e := range mux.es {
|
for _, e := range mux.es {
|
||||||
if (e.key.scheme == "" || key.scheme == e.key.scheme) &&
|
if len(e.host) == len(host) && e.host == host &&
|
||||||
(e.key.host == "" || key.host == e.key.host) &&
|
strings.HasPrefix(path, e.path) {
|
||||||
strings.HasPrefix(key.path, e.key.path) {
|
|
||||||
return e.handler
|
return e.handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,31 +115,32 @@ func (mux *Mux) match(key muxKey) Handler {
|
|||||||
// This occurs when a handler for path + "/" was already registered, but
|
// 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
|
// 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.
|
// URL, setting the path to u.Path + "/" and returning true to indicate so.
|
||||||
func (mux *Mux) redirectToPathSlash(key muxKey, u *url.URL) (*url.URL, bool) {
|
func (mux *Mux) redirectToPathSlash(host, path string, u *url.URL) (*url.URL, bool) {
|
||||||
mux.mu.RLock()
|
mux.mu.RLock()
|
||||||
shouldRedirect := mux.shouldRedirectRLocked(key)
|
shouldRedirect := mux.shouldRedirectRLocked(host, path)
|
||||||
mux.mu.RUnlock()
|
mux.mu.RUnlock()
|
||||||
if !shouldRedirect {
|
if !shouldRedirect {
|
||||||
return u, false
|
return u, false
|
||||||
}
|
}
|
||||||
return u.ResolveReference(&url.URL{Path: key.path + "/"}), true
|
return u.ResolveReference(&url.URL{Path: path + "/"}), true
|
||||||
}
|
}
|
||||||
|
|
||||||
// shouldRedirectRLocked reports whether the given path and host should be redirected to
|
// shouldRedirectRLocked reports whether the given path and host should be redirected to
|
||||||
// path+"/". This should happen if a handler is registered for path+"/" but
|
// path+"/". This should happen if a handler is registered for path+"/" but
|
||||||
// not path -- see comments at Mux.
|
// not path -- see comments at Mux.
|
||||||
func (mux *Mux) shouldRedirectRLocked(key muxKey) bool {
|
func (mux *Mux) shouldRedirectRLocked(host, path string) bool {
|
||||||
if _, exist := mux.m[key]; exist {
|
if _, exist := mux.m[hostpath{host, path}]; exist {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
n := len(key.path)
|
n := len(path)
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if _, exist := mux.m[muxKey{key.scheme, key.host, key.path + "/"}]; exist {
|
if _, exist := mux.m[hostpath{host, path + "/"}]; exist {
|
||||||
return key.path[n-1] != '/'
|
return path[n-1] != '/'
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,13 +160,17 @@ func getWildcard(hostname string) (string, bool) {
|
|||||||
// internally-generated handler that redirects to the canonical path. If the
|
// internally-generated handler that redirects to the canonical path. If the
|
||||||
// host contains a port, it is ignored when matching handlers.
|
// host contains a port, it is ignored when matching handlers.
|
||||||
func (mux *Mux) Handler(r *Request) Handler {
|
func (mux *Mux) Handler(r *Request) Handler {
|
||||||
scheme := r.URL.Scheme
|
// Disallow non-Gemini schemes
|
||||||
|
if r.URL.Scheme != "gemini" {
|
||||||
|
return NotFoundHandler()
|
||||||
|
}
|
||||||
|
|
||||||
host := r.URL.Hostname()
|
host := r.URL.Hostname()
|
||||||
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,
|
||||||
// redirect for /tree/.
|
// redirect for /tree/.
|
||||||
if u, ok := mux.redirectToPathSlash(muxKey{scheme, host, path}, r.URL); ok {
|
if u, ok := mux.redirectToPathSlash(host, path, r.URL); ok {
|
||||||
return StatusHandler(StatusPermanentRedirect, u.String())
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,16 +183,30 @@ func (mux *Mux) Handler(r *Request) Handler {
|
|||||||
mux.mu.RLock()
|
mux.mu.RLock()
|
||||||
defer mux.mu.RUnlock()
|
defer mux.mu.RUnlock()
|
||||||
|
|
||||||
h := mux.match(muxKey{scheme, host, path})
|
h := mux.match(host, path)
|
||||||
|
|
||||||
if h == nil {
|
if h == nil {
|
||||||
// Try wildcard
|
// Try wildcard
|
||||||
if wildcard, ok := getWildcard(host); ok {
|
if wildcard, ok := getWildcard(host); ok {
|
||||||
h = mux.match(muxKey{scheme, wildcard, path})
|
if u, ok := mux.redirectToPathSlash(wildcard, path, r.URL); ok {
|
||||||
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
||||||
|
}
|
||||||
|
h = mux.match(wildcard, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if h == nil {
|
||||||
|
// Try empty host
|
||||||
|
if u, ok := mux.redirectToPathSlash("", path, r.URL); ok {
|
||||||
|
return StatusHandler(StatusPermanentRedirect, u.String())
|
||||||
|
}
|
||||||
|
h = mux.match("", path)
|
||||||
|
}
|
||||||
|
|
||||||
if h == nil {
|
if h == nil {
|
||||||
h = NotFoundHandler()
|
h = NotFoundHandler()
|
||||||
}
|
}
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,48 +230,32 @@ func (mux *Mux) Handle(pattern string, handler Handler) {
|
|||||||
mux.mu.Lock()
|
mux.mu.Lock()
|
||||||
defer mux.mu.Unlock()
|
defer mux.mu.Unlock()
|
||||||
|
|
||||||
var key muxKey
|
var host, path string
|
||||||
if strings.HasPrefix(pattern, "//") {
|
|
||||||
// match any scheme
|
|
||||||
key.scheme = ""
|
|
||||||
pattern = pattern[2:]
|
|
||||||
} else {
|
|
||||||
// extract scheme
|
|
||||||
cut := strings.Index(pattern, "://")
|
|
||||||
if cut == -1 {
|
|
||||||
// default scheme of gemini
|
|
||||||
key.scheme = "gemini"
|
|
||||||
} else {
|
|
||||||
key.scheme = pattern[:cut]
|
|
||||||
pattern = pattern[cut+3:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// extract hostname and path
|
// extract hostname and path
|
||||||
cut := strings.Index(pattern, "/")
|
cut := strings.Index(pattern, "/")
|
||||||
if cut == -1 {
|
if cut == -1 {
|
||||||
key.host = pattern
|
host = pattern
|
||||||
key.path = "/"
|
path = "/"
|
||||||
} else {
|
} else {
|
||||||
key.host = pattern[:cut]
|
host = pattern[:cut]
|
||||||
key.path = pattern[cut:]
|
path = pattern[cut:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip port from hostname
|
// strip port from hostname
|
||||||
if hostname, _, err := net.SplitHostPort(key.host); err == nil {
|
if hostname, _, err := net.SplitHostPort(host); err == nil {
|
||||||
key.host = hostname
|
host = hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, exist := mux.m[key]; exist {
|
if _, exist := mux.m[hostpath{host, path}]; exist {
|
||||||
panic("gemini: multiple registrations for " + pattern)
|
panic("gemini: multiple registrations for " + pattern)
|
||||||
}
|
}
|
||||||
|
|
||||||
if mux.m == nil {
|
if mux.m == nil {
|
||||||
mux.m = make(map[muxKey]Handler)
|
mux.m = make(map[hostpath]Handler)
|
||||||
}
|
}
|
||||||
mux.m[key] = handler
|
mux.m[hostpath{host, path}] = handler
|
||||||
e := muxEntry{handler, key}
|
e := muxEntry{handler, host, path}
|
||||||
if key.path[len(key.path)-1] == '/' {
|
if path[len(path)-1] == '/' {
|
||||||
mux.es = appendSorted(mux.es, e)
|
mux.es = appendSorted(mux.es, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -283,9 +263,7 @@ func (mux *Mux) Handle(pattern string, handler Handler) {
|
|||||||
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
|
func appendSorted(es []muxEntry, e muxEntry) []muxEntry {
|
||||||
n := len(es)
|
n := len(es)
|
||||||
i := sort.Search(n, func(i int) bool {
|
i := sort.Search(n, func(i int) bool {
|
||||||
return len(es[i].key.scheme) < len(e.key.scheme) ||
|
return len(es[i].path) < len(e.path)
|
||||||
len(es[i].key.host) < len(es[i].key.host) ||
|
|
||||||
len(es[i].key.path) < len(e.key.path)
|
|
||||||
})
|
})
|
||||||
if i == n {
|
if i == n {
|
||||||
return append(es, e)
|
return append(es, e)
|
||||||
|
|||||||
407
mux_test.go
407
mux_test.go
@@ -2,6 +2,7 @@ package gemini
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
"net/url"
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -10,6 +11,221 @@ type nopHandler struct{}
|
|||||||
|
|
||||||
func (*nopHandler) ServeGemini(context.Context, ResponseWriter, *Request) {}
|
func (*nopHandler) ServeGemini(context.Context, ResponseWriter, *Request) {}
|
||||||
|
|
||||||
|
type nopResponseWriter struct {
|
||||||
|
Status Status
|
||||||
|
Meta string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *nopResponseWriter) WriteHeader(status Status, meta string) {
|
||||||
|
w.Status = status
|
||||||
|
w.Meta = meta
|
||||||
|
}
|
||||||
|
|
||||||
|
func (nopResponseWriter) SetMediaType(mediatype string) {}
|
||||||
|
func (nopResponseWriter) Write(b []byte) (int, error) { return 0, io.EOF }
|
||||||
|
func (nopResponseWriter) Flush() error { return nil }
|
||||||
|
|
||||||
|
func TestMux(t *testing.T) {
|
||||||
|
type Test struct {
|
||||||
|
URL string
|
||||||
|
Pattern string
|
||||||
|
Redirect string
|
||||||
|
NotFound bool
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
Patterns []string
|
||||||
|
Tests []Test
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Patterns: []string{"/a", "/b/", "/b/c/d", "/b/c/d/"},
|
||||||
|
Tests: []Test{
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com",
|
||||||
|
Redirect: "gemini://example.com/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/",
|
||||||
|
NotFound: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/c",
|
||||||
|
NotFound: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/a",
|
||||||
|
Pattern: "/a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/a/",
|
||||||
|
NotFound: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b",
|
||||||
|
Redirect: "gemini://example.com/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b/",
|
||||||
|
Pattern: "/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b/c",
|
||||||
|
Pattern: "/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b/c/d",
|
||||||
|
Pattern: "/b/c/d",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b/c/d/e/",
|
||||||
|
Pattern: "/b/c/d/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Patterns: []string{
|
||||||
|
"/", "/a", "/b/",
|
||||||
|
"example.com", "example.com/a", "example.com/b/",
|
||||||
|
"*.example.com", "*.example.com/a", "*.example.com/b/",
|
||||||
|
},
|
||||||
|
Tests: []Test{
|
||||||
|
{
|
||||||
|
URL: "gemini://example.net/",
|
||||||
|
Pattern: "/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.net/a",
|
||||||
|
Pattern: "/a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.net/b",
|
||||||
|
Redirect: "gemini://example.net/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.net/b/",
|
||||||
|
Pattern: "/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/",
|
||||||
|
Pattern: "example.com",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b",
|
||||||
|
Redirect: "gemini://example.com/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/b/",
|
||||||
|
Pattern: "example.com/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://a.example.com/",
|
||||||
|
Pattern: "*.example.com",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://b.example.com/a",
|
||||||
|
Pattern: "*.example.com/a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://c.example.com/b",
|
||||||
|
Redirect: "gemini://c.example.com/b/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://d.example.com/b/",
|
||||||
|
Pattern: "*.example.com/b/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Patterns: []string{"example.net", "*.example.org"},
|
||||||
|
Tests: []Test{
|
||||||
|
{
|
||||||
|
// The following redirect occurs as a result of cleaning
|
||||||
|
// the path provided to the Mux. This happens even if there
|
||||||
|
// are no matching handlers.
|
||||||
|
URL: "gemini://example.com",
|
||||||
|
Redirect: "gemini://example.com/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.com/",
|
||||||
|
NotFound: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.net",
|
||||||
|
Redirect: "gemini://example.net/",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://example.org/",
|
||||||
|
NotFound: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
URL: "gemini://gemini.example.org",
|
||||||
|
Redirect: "gemini://gemini.example.org/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
type handler struct {
|
||||||
|
nopHandler
|
||||||
|
Pattern string
|
||||||
|
}
|
||||||
|
|
||||||
|
mux := &Mux{}
|
||||||
|
for _, pattern := range test.Patterns {
|
||||||
|
mux.Handle(pattern, &handler{
|
||||||
|
Pattern: pattern,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range test.Tests {
|
||||||
|
u, err := url.Parse(test.URL)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &Request{URL: u}
|
||||||
|
|
||||||
|
h := mux.Handler(req)
|
||||||
|
|
||||||
|
if h, ok := h.(*handler); ok {
|
||||||
|
if h.Pattern != test.Pattern {
|
||||||
|
t.Errorf("wrong pattern for %q: expected %q, got %q", test.URL, test.Pattern, h.Pattern)
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check redirects and NotFounds
|
||||||
|
w := &nopResponseWriter{}
|
||||||
|
h.ServeGemini(context.Background(), w, req)
|
||||||
|
|
||||||
|
switch w.Status {
|
||||||
|
case StatusNotFound:
|
||||||
|
if !test.NotFound {
|
||||||
|
t.Errorf("expected pattern for %q, got NotFound", test.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
case StatusPermanentRedirect:
|
||||||
|
if test.Redirect == "" {
|
||||||
|
t.Errorf("expected pattern for %q, got redirect to %q", test.URL, w.Meta)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := url.Parse(test.Redirect)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if w.Meta != res.String() {
|
||||||
|
t.Errorf("bad redirect for %q: expected %q, got %q", test.URL, res.String(), w.Meta)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
t.Errorf("unexpected response for %q: %d %s", test.URL, w.Status, w.Meta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMuxMatch(t *testing.T) {
|
func TestMuxMatch(t *testing.T) {
|
||||||
type Match struct {
|
type Match struct {
|
||||||
URL string
|
URL string
|
||||||
@@ -21,7 +237,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
Matches []Match
|
Matches []Match
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: *, path: /*
|
// hostname: *, path: /*
|
||||||
Pattern: "/",
|
Pattern: "/",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/path", true},
|
{"gemini://example.com/path", true},
|
||||||
@@ -34,7 +250,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: *, path: /path
|
// hostname: *, path: /path
|
||||||
Pattern: "/path",
|
Pattern: "/path",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/path", true},
|
{"gemini://example.com/path", true},
|
||||||
@@ -47,7 +263,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: *, path: /subtree/*
|
// hostname: *, path: /subtree/*
|
||||||
Pattern: "/subtree/",
|
Pattern: "/subtree/",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/subtree/", true},
|
{"gemini://example.com/subtree/", true},
|
||||||
@@ -62,7 +278,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: example.com, path: /*
|
// hostname: example.com, path: /*
|
||||||
Pattern: "example.com",
|
Pattern: "example.com",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/path", true},
|
{"gemini://example.com/path", true},
|
||||||
@@ -75,7 +291,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: example.com, path: /path
|
// hostname: example.com, path: /path
|
||||||
Pattern: "example.com/path",
|
Pattern: "example.com/path",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/path", true},
|
{"gemini://example.com/path", true},
|
||||||
@@ -88,7 +304,7 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: example.com, path: /subtree/*
|
// hostname: example.com, path: /subtree/*
|
||||||
Pattern: "example.com/subtree/",
|
Pattern: "example.com/subtree/",
|
||||||
Matches: []Match{
|
Matches: []Match{
|
||||||
{"gemini://example.com/subtree/", true},
|
{"gemini://example.com/subtree/", true},
|
||||||
@@ -102,170 +318,6 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
{"http://example.com/subtree/", false},
|
{"http://example.com/subtree/", false},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
// scheme: http, hostname: example.com, path: /*
|
|
||||||
Pattern: "http://example.com",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.com/", true},
|
|
||||||
{"http://example.com/path.gmi", true},
|
|
||||||
{"http://example.com/path/", true},
|
|
||||||
{"http://example.org/path", false},
|
|
||||||
{"gemini://example.com/path", false},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: http, hostname: example.com, path: /path
|
|
||||||
Pattern: "http://example.com/path",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.com/", false},
|
|
||||||
{"http://example.com/path.gmi", false},
|
|
||||||
{"http://example.com/path/", false},
|
|
||||||
{"http://example.org/path", false},
|
|
||||||
{"gemini://example.com/path", false},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: http, hostname: example.com, path: /subtree/*
|
|
||||||
Pattern: "http://example.com/subtree/",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/subtree/", true},
|
|
||||||
{"http://example.com/subtree/nested/", true},
|
|
||||||
{"http://example.com/subtree/nested/file", true},
|
|
||||||
{"http://example.org/subtree/", false},
|
|
||||||
{"http://example.org/subtree/nested/", false},
|
|
||||||
{"http://example.org/subtree/nested/file", false},
|
|
||||||
{"http://example.com/subtree", false},
|
|
||||||
{"http://www.example.com/subtree/", false},
|
|
||||||
{"gemini://example.com/subtree/", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: example.com, path: /*
|
|
||||||
Pattern: "//example.com",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/path", true},
|
|
||||||
{"gemini://example.com/", true},
|
|
||||||
{"gemini://example.com/path.gmi", true},
|
|
||||||
{"gemini://example.com/path/", true},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: example.com, path: /path
|
|
||||||
Pattern: "//example.com/path",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/path", true},
|
|
||||||
{"gemini://example.com/", false},
|
|
||||||
{"gemini://example.com/path.gmi", false},
|
|
||||||
{"gemini://example.com/path/", false},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: example.com, path: /subtree/*
|
|
||||||
Pattern: "//example.com/subtree/",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/subtree/", true},
|
|
||||||
{"gemini://example.com/subtree/nested/", true},
|
|
||||||
{"gemini://example.com/subtree/nested/file", true},
|
|
||||||
{"gemini://example.org/subtree/", false},
|
|
||||||
{"gemini://example.org/subtree/nested/", false},
|
|
||||||
{"gemini://example.org/subtree/nested/file", false},
|
|
||||||
{"gemini://example.com/subtree", false},
|
|
||||||
{"gemini://www.example.com/subtree/", false},
|
|
||||||
{"http://example.com/subtree/", true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: http, hostname: *, path: /*
|
|
||||||
Pattern: "http://",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.com/", true},
|
|
||||||
{"http://example.com/path.gmi", true},
|
|
||||||
{"http://example.com/path/", true},
|
|
||||||
{"http://example.org/path", true},
|
|
||||||
{"gemini://example.com/path", false},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: http, hostname: *, path: /path
|
|
||||||
Pattern: "http:///path",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.com/", false},
|
|
||||||
{"http://example.com/path.gmi", false},
|
|
||||||
{"http://example.com/path/", false},
|
|
||||||
{"http://example.org/path", true},
|
|
||||||
{"gemini://example.com/path", false},
|
|
||||||
{"gemini://example.org/path", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: http, hostname: *, path: /subtree/*
|
|
||||||
Pattern: "http:///subtree/",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://example.com/subtree/", true},
|
|
||||||
{"http://example.com/subtree/nested/", true},
|
|
||||||
{"http://example.com/subtree/nested/file", true},
|
|
||||||
{"http://example.org/subtree/", true},
|
|
||||||
{"http://example.org/subtree/nested/", true},
|
|
||||||
{"http://example.org/subtree/nested/file", true},
|
|
||||||
{"http://example.com/subtree", false},
|
|
||||||
{"http://www.example.com/subtree/", true},
|
|
||||||
{"gemini://example.com/subtree/", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: *, path: /*
|
|
||||||
Pattern: "//",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/path", true},
|
|
||||||
{"gemini://example.com/", true},
|
|
||||||
{"gemini://example.com/path.gmi", true},
|
|
||||||
{"gemini://example.com/path/", true},
|
|
||||||
{"gemini://example.org/path", true},
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.org/path", true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: *, path: /path
|
|
||||||
Pattern: "///path",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/path", true},
|
|
||||||
{"gemini://example.com/", false},
|
|
||||||
{"gemini://example.com/path.gmi", false},
|
|
||||||
{"gemini://example.com/path/", false},
|
|
||||||
{"gemini://example.org/path", true},
|
|
||||||
{"http://example.com/path", true},
|
|
||||||
{"http://example.org/path", true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// scheme: *, hostname: *, path: /subtree/*
|
|
||||||
Pattern: "///subtree/",
|
|
||||||
Matches: []Match{
|
|
||||||
{"gemini://example.com/subtree/", true},
|
|
||||||
{"gemini://example.com/subtree/nested/", true},
|
|
||||||
{"gemini://example.com/subtree/nested/file", true},
|
|
||||||
{"gemini://example.org/subtree/", true},
|
|
||||||
{"gemini://example.org/subtree/nested/", true},
|
|
||||||
{"gemini://example.org/subtree/nested/file", true},
|
|
||||||
{"gemini://example.com/subtree", false},
|
|
||||||
{"gemini://www.example.com/subtree/", true},
|
|
||||||
{"http://example.com/subtree/", true},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
// scheme: gemini, hostname: *.example.com, path: /*
|
// scheme: gemini, hostname: *.example.com, path: /*
|
||||||
Pattern: "*.example.com",
|
Pattern: "*.example.com",
|
||||||
@@ -277,25 +329,14 @@ func TestMuxMatch(t *testing.T) {
|
|||||||
{"http://www.example.com/", false},
|
{"http://www.example.com/", false},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
// scheme: http, hostname: *.example.com, path: /*
|
|
||||||
Pattern: "http://*.example.com",
|
|
||||||
Matches: []Match{
|
|
||||||
{"http://mail.example.com/", true},
|
|
||||||
{"http://www.example.com/index.gmi", true},
|
|
||||||
{"http://example.com/", false},
|
|
||||||
{"http://a.b.example.com/", false},
|
|
||||||
{"gemini://www.example.com/", false},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
for _, test := range tests {
|
||||||
h := &nopHandler{}
|
h := &nopHandler{}
|
||||||
var mux Mux
|
var mux Mux
|
||||||
mux.Handle(test.Pattern, h)
|
mux.Handle(test.Pattern, h)
|
||||||
|
|
||||||
for _, match := range tests[i].Matches {
|
for _, match := range test.Matches {
|
||||||
u, err := url.Parse(match.URL)
|
u, err := url.Parse(match.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user