aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--resp.go4
-rw-r--r--resp_test.go11
2 files changed, 15 insertions, 0 deletions
diff --git a/resp.go b/resp.go
index b213f58..deae50a 100644
--- a/resp.go
+++ b/resp.go
@@ -35,6 +35,10 @@ func (h *redirector) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
pkg := path.Join(req.Host, req.URL.Path)
+ if !h.re.MatchString(pkg) {
+ w.WriteHeader(http.StatusNotFound)
+ return
+ }
if req.URL.Query().Get("go-get") != "1" {
w.Header().Set("Location", "https://pkg.go.dev/"+pkg)
w.WriteHeader(http.StatusFound)
diff --git a/resp_test.go b/resp_test.go
index 1f8c458..7e01946 100644
--- a/resp_test.go
+++ b/resp_test.go
@@ -14,6 +14,17 @@ import (
func TestRedirector_ServeHTTP(t *testing.T) {
r := &redirector{regexp.MustCompile(`src\.example\.com/x`), "https://example.com/git", "git"}
+ t.Run("NotFound", func(t *testing.T) {
+ req := httptest.NewRequest(http.MethodGet, "https://example.com/foo", nil)
+ w := httptest.NewRecorder()
+ r.ServeHTTP(w, req)
+
+ resp := w.Result()
+ if http.StatusNotFound != resp.StatusCode {
+ t.Errorf("expected %d, got %d", http.StatusNotFound, resp.StatusCode)
+ }
+ })
+
t.Run("GoVisit", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://src.example.com/x/foo?go-get=1", nil)
w := httptest.NewRecorder()