summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
-rw-r--r--contrib/sharedhost/replacement.go47
-rw-r--r--contrib/sharedhost/replacement_test.go57
2 files changed, 0 insertions, 104 deletions
diff --git a/contrib/sharedhost/replacement.go b/contrib/sharedhost/replacement.go
deleted file mode 100644
index cabc6b1..0000000
--- a/contrib/sharedhost/replacement.go
+++ /dev/null
@@ -1,47 +0,0 @@
-package sharedhost
-
-import (
- "context"
- "crypto/tls"
- "net/url"
- "path"
-
- sr "tildegit.org/tjp/sliderule"
-)
-
-// ReplaceTilde builds a middleware which substitutes a leading '~' in the request path.
-//
-// It makes the alteration to a copy of the request which is then passed into the
-// wrapped Handler. This way middlewares outside of this one inspecting the request
-// afterwards will see the original URL.
-//
-// Typically the replacement should end with a "/", so that the ~ ends up mapping to a
-// particular directory on the filesystem. For instance with a replacement string of
-// "users/", "domain.com/~jim/index.gmi" maps to "domain.com/users/jim/index.gmi".
-func ReplaceTilde(replacement string) sr.Middleware {
- return func(inner sr.Handler) sr.Handler {
- return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
- if len(request.Path) > 1 && request.Path[0:2] == "/~" {
- request = cloneRequest(request)
- request.Path = path.Clean("/" + replacement + request.Path[2:])
- }
-
- return inner.Handle(ctx, request)
- })
- }
-}
-
-func cloneRequest(start *sr.Request) *sr.Request {
- next := &sr.Request{}
- *next = *start
-
- next.URL = &url.URL{}
- *next.URL = *start.URL
-
- if start.TLSState != nil {
- next.TLSState = &tls.ConnectionState{}
- *next.TLSState = *start.TLSState
- }
-
- return next
-}
diff --git a/contrib/sharedhost/replacement_test.go b/contrib/sharedhost/replacement_test.go
deleted file mode 100644
index 9fa9161..0000000
--- a/contrib/sharedhost/replacement_test.go
+++ /dev/null
@@ -1,57 +0,0 @@
-package sharedhost_test
-
-import (
- "context"
- "net/url"
- "testing"
-
- "github.com/stretchr/testify/assert"
-
- sr "tildegit.org/tjp/sliderule"
- "tildegit.org/tjp/sliderule/contrib/sharedhost"
-)
-
-func TestReplaceTilde(t *testing.T) {
- tests := []struct {
- replacement string
- inputURL string
- replacedPath string
- }{
- {
- replacement: "users/",
- inputURL: "gemini://domain.com/~username/foo/bar",
- replacedPath: "/users/username/foo/bar",
- },
- {
- replacement: "people-",
- inputURL: "gemini://domain.com/non/match",
- replacedPath: "/non/match",
- },
- {
- replacement: "people-",
- inputURL: "gemini://domain.com/~someone/dir/file",
- replacedPath: "/people-someone/dir/file",
- },
- }
-
- for _, test := range tests {
- t.Run(test.inputURL, func(t *testing.T) {
- u, err := url.Parse(test.inputURL)
- assert.Nil(t, err)
-
- originalPath := u.Path
-
- replacer := sharedhost.ReplaceTilde(test.replacement)
- request := &sr.Request{URL: u}
- handler := replacer(sr.HandlerFunc(func(_ context.Context, request *sr.Request) *sr.Response {
- assert.Equal(t, test.replacedPath, request.Path)
- return &sr.Response{}
- }))
-
- handler.Handle(context.Background(), request)
-
- // original request was unmodified
- assert.Equal(t, originalPath, request.Path)
- })
- }
-}