git » gofer » commit e7866d3

http: Add "status" action

author Alberto Bertogli
2020-06-12 10:20:16 UTC
committer Alberto Bertogli
2020-06-12 10:20:16 UTC
parent 7c766f2b7bcf748bf699934617aeea9c98627add

http: Add "status" action

This patch adds a "status" action which will make the server return that
particular status code for the path.

config/config.go +1 -0
etc/gofer.schema.cue +2 -0
etc/gofer.yaml +5 -0
server/http.go +13 -0
test/01-be.yaml +3 -0
test/01-fe.yaml +1 -0
test/test.sh +2 -0

diff --git a/config/config.go b/config/config.go
index a2f7997..dcb1d5b 100644
--- a/config/config.go
+++ b/config/config.go
@@ -26,6 +26,7 @@ type HTTP struct {
 	File     map[string]string
 	Redirect map[string]string
 	CGI      map[string]string
+	Status   map[string]int
 
 	Auth map[string]string
 
diff --git a/etc/gofer.schema.cue b/etc/gofer.schema.cue
index a26a582..bf878d3 100644
--- a/etc/gofer.schema.cue
+++ b/etc/gofer.schema.cue
@@ -33,6 +33,8 @@ _http: {
 
 	cgi?: [string]: string
 
+	status?: [string]: int
+
 	auth?: [string]: string
 
 	setheader?: [string]: [string]: string
diff --git a/etc/gofer.yaml b/etc/gofer.yaml
index 3363965..6170c82 100644
--- a/etc/gofer.yaml
+++ b/etc/gofer.yaml
@@ -57,6 +57,11 @@ http:
     #cgi:
     #  "/gitweb": "/usr/share/gitweb/gitweb.cgi"
 
+    # Return a specific status. Can be useful to return 404 on specific
+    # sub-paths.
+    #status:
+    #  "/notfound": 404
+
     # Enforce authentication on these paths. The target is the file containing
     # the user and passwords.
     #auth:
diff --git a/server/http.go b/server/http.go
index f104d9e..b513328 100644
--- a/server/http.go
+++ b/server/http.go
@@ -62,6 +62,11 @@ func httpServer(addr string, conf config.HTTP) *http.Server {
 		}
 	}
 
+	for from, status := range conf.Status {
+		log.Infof("%s route %s -> status %d", srv.Addr, from, status)
+		mux.Handle(from, makeStatus(from, status))
+	}
+
 	// Wrap the authentication handlers.
 	if len(conf.Auth) > 0 {
 		authMux := http.NewServeMux()
@@ -309,6 +314,14 @@ func makeRedirect(from string, to url.URL, conf *config.HTTP) http.Handler {
 	})
 }
 
+func makeStatus(from string, status int) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		tr, _ := trace.FromContext(r.Context())
+		tr.Printf("status %d", status)
+		w.WriteHeader(status)
+	})
+}
+
 func makeProxy(from string, to url.URL, conf *config.HTTP) http.Handler {
 	proxy := &httputil.ReverseProxy{}
 	proxy.Transport = &proxyTransport{}
diff --git a/test/01-be.yaml b/test/01-be.yaml
index 64b813e..8feebc3 100644
--- a/test/01-be.yaml
+++ b/test/01-be.yaml
@@ -17,6 +17,9 @@ http:
     cgi:
       "/cgi/": "testdata/cgi.sh?param 1;param 2"
 
+    status:
+      "/status/543": 543
+
     diropts:
       "/dir/":
         listing:
diff --git a/test/01-fe.yaml b/test/01-fe.yaml
index 7838e98..48281de 100644
--- a/test/01-fe.yaml
+++ b/test/01-fe.yaml
@@ -6,6 +6,7 @@ _proxy: &proxyroutes
   "/authdir/": "http://localhost:8450/authdir/"
   "/file": "http://localhost:8450/file"
   "/cgi/": "http://localhost:8450/cgi/"
+  "/status/": "http://localhost:8450/status/"
   "/bad/unreacheable": "http://localhost:1/"
   "/bad/empty": "http:"
   "/dar/": "http://localhost:8450/dir/"
diff --git a/test/test.sh b/test/test.sh
index d45704d..22659f5 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -155,6 +155,8 @@ do
 	exp $base/bad/unreacheable -status 502
 	exp $base/bad/empty -status 502
 
+	exp $base/status/543 -status 543
+
 	# Test that the FE doesn't forward this - it exists on the BE, but the
 	# route doesn't end in a / so it shouldn't be forwarded.
 	exp $base/file/second -status 404