mirror of
https://github.com/gravitational/teleport
synced 2024-10-22 02:03:24 +00:00
more work on rewrites
This commit is contained in:
parent
9cba8efd32
commit
09398da494
|
@ -23,6 +23,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gravitational/roundtrip"
|
"github.com/gravitational/roundtrip"
|
||||||
|
@ -113,3 +114,30 @@ func ParseBool(q url.Values, name string) (bool, bool, error) {
|
||||||
}
|
}
|
||||||
return val, true, nil
|
return val, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RewritePair is a rewrite expression
|
||||||
|
type RewritePair struct {
|
||||||
|
// Expr is matching expression
|
||||||
|
Expr *regexp.Regexp
|
||||||
|
// Replacement is replacement
|
||||||
|
Replacement string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rewrite creates a rewrite pair, panics if in epxression
|
||||||
|
// is not a valid regular expressoin
|
||||||
|
func Rewrite(in, out string) *RewritePair {
|
||||||
|
return &RewritePair{
|
||||||
|
Expr: regexp.MustCompile(in),
|
||||||
|
Replacement: out,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RewritePaths creates a middleware that rewrites paths in incoming request
|
||||||
|
func RewritePaths(next http.Handler, rewrites ...RewritePair) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
for _, rewrite := range rewrites {
|
||||||
|
req.URL.Path = rewrite.Expr.ReplaceAllString(req.URL.Path, rewrite.Replacement)
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, req)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
60
lib/httplib/httplib_test.go
Normal file
60
lib/httplib/httplib_test.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
/*
|
||||||
|
Copyright 2016 Gravitational, Inc.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package httplib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
. "gopkg.in/check.v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTP(t *testing.T) { TestingT(t) }
|
||||||
|
|
||||||
|
type HTTPSuite struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = Suite(&HTTPSuite{})
|
||||||
|
|
||||||
|
func (s *HTTPSuite) TestRewritePaths(c *C) {
|
||||||
|
RewritePaths(newTestHandler(), Rewrite("/v1/sessions/([^/]+)/stream", "/v1/namespaces/default/sessions/1/stream"))
|
||||||
|
}
|
||||||
|
|
||||||
|
type testHandler struct {
|
||||||
|
httprouter.Router
|
||||||
|
capturedNamespace string
|
||||||
|
capturedID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestHandler() http.Handler {
|
||||||
|
h := &testHandler{}
|
||||||
|
h.Router = *httprouter.New()
|
||||||
|
h.POST("/v1/sessions/:id/stream", MakeHandler(h.postSessionChunkOriginal))
|
||||||
|
h.POST("/v1/namespaces/:namespace/sessions/:id/stream", MakeHandler(h.postSessionChunkNamespace))
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *testHandler) postSessionChunkOriginal(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
|
||||||
|
return "ok", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *testHandler) postSessionChunkNamespace(w http.ResponseWriter, r *http.Request, p httprouter.Params) (interface{}, error) {
|
||||||
|
h.capturedNamespace = p.ByName("namespace")
|
||||||
|
h.capturedID = p.ByName("id")
|
||||||
|
return "ok", nil
|
||||||
|
}
|
Loading…
Reference in a new issue