Added Const for use of CtxKeys (#1511)

* Added Const for CtxKeys

* Move CtxKey Const declarations

* Fixed tests

* fix test
This commit is contained in:
Toby Allen
2017-03-13 15:22:46 +00:00
committed by Matt Holt
parent 96bfb9f347
commit d5cc10f7aa
14 changed files with 39 additions and 43 deletions

View File

@@ -14,7 +14,6 @@ import (
"os"
"github.com/mholt/caddy"
"github.com/russross/blackfriday"
)
@@ -349,7 +348,7 @@ func (c Context) Files(name string) ([]string, error) {
// IsMITM returns true if it seems likely that the TLS connection
// is being intercepted.
func (c Context) IsMITM() bool {
if val, ok := c.Req.Context().Value(caddy.CtxKey("mitm")).(bool); ok {
if val, ok := c.Req.Context().Value(MitmCtxKey).(bool); ok {
return val
}
return false

View File

@@ -197,3 +197,16 @@ var EmptyNext = HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, e
func SameNext(next1, next2 Handler) bool {
return fmt.Sprintf("%v", next1) == fmt.Sprintf("%v", next2)
}
// Context key constants
const (
// URIxRewriteCtxKey is a context key used to store original unrewritten
// URI in context.WithValue
URIxRewriteCtxKey caddy.CtxKey = "caddy_rewrite_original_uri"
// RemoteUserCtxKey is a context key used to store remote user for request
RemoteUserCtxKey caddy.CtxKey = "remote_user"
// MitmCtxKey stores Mitm result
MitmCtxKey caddy.CtxKey = "mitm"
)

View File

@@ -9,8 +9,6 @@ import (
"net/http"
"strings"
"sync"
"github.com/mholt/caddy"
)
// tlsHandler is a http.Handler that will inject a value
@@ -74,7 +72,7 @@ func (h *tlsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if checked {
r = r.WithContext(context.WithValue(r.Context(), caddy.CtxKey("mitm"), mitm))
r = r.WithContext(context.WithValue(r.Context(), MitmCtxKey, mitm))
}
if mitm && h.closeOnMITM {

View File

@@ -7,8 +7,6 @@ import (
"net/http/httptest"
"reflect"
"testing"
"github.com/mholt/caddy"
)
func TestParseClientHello(t *testing.T) {
@@ -287,7 +285,7 @@ func TestHeuristicFunctionsAndHandler(t *testing.T) {
want := ch.interception
handler := &tlsHandler{
next: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got, checked = r.Context().Value(caddy.CtxKey("mitm")).(bool)
got, checked = r.Context().Value(MitmCtxKey).(bool)
}),
listener: newTLSListener(nil, nil),
}

View File

@@ -241,7 +241,7 @@ func (r *replacer) getSubstitution(key string) string {
// if a rewrite has happened, the original URI should be used as the path
// rather than the rewritten URI
var path string
origpath, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
origpath, _ := r.request.Context().Value(URIxRewriteCtxKey).(string)
if origpath == "" {
path = r.request.URL.Path
} else {
@@ -251,7 +251,7 @@ func (r *replacer) getSubstitution(key string) string {
return path
case "{path_escaped}":
var path string
origpath, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
origpath, _ := r.request.Context().Value(URIxRewriteCtxKey).(string)
if origpath == "" {
path = r.request.URL.Path
} else {
@@ -284,13 +284,13 @@ func (r *replacer) getSubstitution(key string) string {
}
return port
case "{uri}":
uri, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
uri, _ := r.request.Context().Value(URIxRewriteCtxKey).(string)
if uri == "" {
uri = r.request.URL.RequestURI()
}
return uri
case "{uri_escaped}":
uri, _ := r.request.Context().Value(caddy.URIxRewriteCtxKey).(string)
uri, _ := r.request.Context().Value(URIxRewriteCtxKey).(string)
if uri == "" {
uri = r.request.URL.RequestURI()
}

View File

@@ -8,8 +8,6 @@ import (
"strings"
"testing"
"time"
"github.com/mholt/caddy"
)
func TestNewReplacer(t *testing.T) {
@@ -164,7 +162,7 @@ func TestPathRewrite(t *testing.T) {
t.Fatalf("Request Formation Failed: %s\n", err.Error())
}
ctx := context.WithValue(request.Context(), caddy.URIxRewriteCtxKey, "a/custom/path.php?key=value")
ctx := context.WithValue(request.Context(), URIxRewriteCtxKey, "a/custom/path.php?key=value")
request = request.WithContext(ctx)
repl := NewReplacer(request, recordRequest, "")

View File

@@ -292,7 +292,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}()
w.Header().Set("Server", "Caddy")
c := context.WithValue(r.Context(), caddy.URLPathCtxKey, r.URL.Path)
c := context.WithValue(r.Context(), staticfiles.URLPathCtxKey, r.URL.Path)
r = r.WithContext(c)
sanitizePath(r)