changeset 1127:71ba4a66ec95

Return 404 if a proxied service is not found.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 07 Nov 2018 16:08:25 +0100
parents dbc663b74724
children dc9111a60926
files pkg/controllers/proxy.go pkg/controllers/routes.go pkg/middleware/notfound.go
diffstat 3 files changed, 43 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/proxy.go	Tue Nov 06 13:54:10 2018 +0100
+++ b/pkg/controllers/proxy.go	Wed Nov 07 16:08:25 2018 +0100
@@ -33,6 +33,7 @@
 	"golang.org/x/net/html/charset"
 
 	"gemma.intevation.de/gemma/pkg/config"
+	"gemma.intevation.de/gemma/pkg/middleware"
 )
 
 // proxyBlackList is a set of URLs that should not be rewritten by the proxy.
@@ -66,7 +67,8 @@
 
 		if entry, found := vars["entry"]; found {
 			if s, found = lookup(entry); !found {
-				abort("warn: cannot find entry '%s'\n", entry)
+				log.Printf("warn: cannot find eintry '%s'\n", entry)
+				panic(middleware.ErrNotFound)
 			}
 		} else {
 			expectedMAC, err := base64.URLEncoding.DecodeString(vars["hash"])
--- a/pkg/controllers/routes.go	Tue Nov 06 13:54:10 2018 +0100
+++ b/pkg/controllers/routes.go	Wed Nov 07 16:08:25 2018 +0100
@@ -92,10 +92,10 @@
 	}).Methods(http.MethodGet)
 
 	// External proxies.
-	external := &httputil.ReverseProxy{
+	external := middleware.NotFound(&httputil.ReverseProxy{
 		Director:       proxyDirector(models.ExternalServices.Find),
 		ModifyResponse: proxyModifyResponse("/api/external/"),
-	}
+	})
 
 	externalAuth := any(external)
 
@@ -110,10 +110,10 @@
 			http.MethodPut, http.MethodDelete)
 
 	// Internal proxies.
-	internal := &httputil.ReverseProxy{
+	internal := middleware.NotFound(&httputil.ReverseProxy{
 		Director:       proxyDirector(models.InternalServices.Find),
 		ModifyResponse: proxyModifyResponse("/api/internal/"),
-	}
+	})
 
 	internalAuth := any(
 		middleware.ModifyQuery(internal, middleware.InjectUser))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/middleware/notfound.go	Wed Nov 07 16:08:25 2018 +0100
@@ -0,0 +1,36 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2018 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package middleware
+
+import (
+	"errors"
+	"net/http"
+)
+
+var ErrNotFound = errors.New("Not found")
+
+func NotFound(next http.Handler) http.Handler {
+	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+		defer func() {
+			if p := recover(); p != nil {
+				if p == ErrNotFound {
+					http.NotFound(rw, req)
+				} else {
+					panic(p)
+				}
+			}
+		}()
+		next.ServeHTTP(rw, req)
+	})
+}