Skip to content

Commit

Permalink
Update runtime with request changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsandall committed Dec 19, 2016
1 parent 4a0dfec commit 333bf4f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
13 changes: 7 additions & 6 deletions runtime/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/golang/glog"
"github.com/open-policy-agent/opa/server"
)

// LoggingHandler returns an http.Handler that will print log messages to glog
Expand All @@ -36,12 +37,12 @@ func (h *LoggingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
glog.Infof("%v %v %v %v %v %vms",
r.RemoteAddr,
r.Method,
dropGlobals(r.URL),
dropRequestParam(r.URL),
statusCode,
recorder.bytesWritten,
float64(dt.Nanoseconds())/1e6)
if glog.V(3) {
for _, g := range getGlobals(r.URL) {
for _, g := range getRequestParam(r.URL) {
glog.Infoln(g)
}
}
Expand Down Expand Up @@ -74,10 +75,10 @@ func (r *recorder) WriteHeader(s int) {
r.inner.WriteHeader(s)
}

func dropGlobals(u *url.URL) string {
func dropRequestParam(u *url.URL) string {
cpy := url.Values{}
for k, v := range u.Query() {
if k != "global" {
if k != server.ParamRequestV1 {
cpy[k] = v
}
}
Expand All @@ -87,8 +88,8 @@ func dropGlobals(u *url.URL) string {
return u.Path + "?" + cpy.Encode()
}

func getGlobals(u *url.URL) (r []string) {
for _, g := range u.Query()["global"] {
func getRequestParam(u *url.URL) (r []string) {
for _, g := range u.Query()[server.ParamRequestV1] {
s, err := url.QueryUnescape(g)
if err == nil {
r = append(r, s)
Expand Down
16 changes: 8 additions & 8 deletions runtime/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ import (
"testing"
)

func TestDropGlobals(t *testing.T) {
func TestDropRequestParam(t *testing.T) {

// Without other params.
abc := `a.b.c:{"foo":[1,2,3,4]}`
abcEncoded := url.QueryEscape(abc)

uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?global=%v`, abcEncoded))
uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?request=%v`, abcEncoded))
if err != nil {
panic(err)
}

result := dropGlobals(uri)
result := dropRequestParam(uri)
expected := "/v1/data/foo/bar"

if result != expected {
Expand All @@ -33,12 +33,12 @@ func TestDropGlobals(t *testing.T) {
def := `d.e.f:{"bar":{"baz":null}}`
defEncoded := url.QueryEscape(def)

uri, err = url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?global=%v&pretty=true&depth=1&global=%v`, abcEncoded, defEncoded))
uri, err = url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?request=%v&pretty=true&depth=1&request=%v`, abcEncoded, defEncoded))
if err != nil {
panic(err)
}

result = dropGlobals(uri)
result = dropRequestParam(uri)
expected = "/v1/data/foo/bar?depth=1&pretty=true"

if result != expected {
Expand All @@ -47,19 +47,19 @@ func TestDropGlobals(t *testing.T) {

}

func TestGetGlobals(t *testing.T) {
func TestGetRequestParam(t *testing.T) {

abc := `a.b.c:{"foo":[1,2,3,4]}`
def := `d.e.f:{"bar":{"baz":null}}`
abcEncoded := url.QueryEscape(abc)
defEncoded := url.QueryEscape(def)

uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?global=%v&pretty=true&global=%v`, abcEncoded, defEncoded))
uri, err := url.ParseRequestURI(fmt.Sprintf(`http://localhost:8181/v1/data/foo/bar?request=%v&pretty=true&request=%v`, abcEncoded, defEncoded))
if err != nil {
panic(err)
}

result := getGlobals(uri)
result := getRequestParam(uri)
expected := []string{abc, def}

if !reflect.DeepEqual(result, expected) {
Expand Down
8 changes: 7 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ type patchImpl struct {
value interface{}
}

const (
// ParamRequestV1 defines the name of the HTTP URL parameter that specifies
// values for the "request" document.
ParamRequestV1 = "request"
)

// Server represents an instance of OPA running in server mode.
type Server struct {
Handler http.Handler
Expand Down Expand Up @@ -462,7 +468,7 @@ func (s *Server) v1DataGet(w http.ResponseWriter, r *http.Request) {
path := stringPathToDataRef(vars["path"])
pretty := getPretty(r.URL.Query()["pretty"])
explainMode := getExplain(r.URL.Query()["explain"])
request, nonGround, err := parseRequest(r.URL.Query()["request"])
request, nonGround, err := parseRequest(r.URL.Query()[ParamRequestV1])

if err != nil {
handleError(w, 400, err)
Expand Down

0 comments on commit 333bf4f

Please sign in to comment.