Skip to content

Commit

Permalink
agent: Support for raw key lookup. Fixes #150.
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed May 20, 2014
1 parent fa4cb49 commit 88d91c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions command/agent/kvs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ func (s *HTTPServer) KVSGet(resp http.ResponseWriter, req *http.Request, args *s
resp.WriteHeader(404)
return nil, nil
}

// Check if we are in raw mode with a normal get, write out
// the raw body
if _, ok := params["raw"]; ok && method == "KVS.Get" {
body := out.Entries[0].Value
resp.Header().Set("Content-Length", strconv.FormatInt(int64(len(body)), 10))
resp.Write(body)
return nil, nil
}

return out.Entries, nil
}

Expand Down
34 changes: 34 additions & 0 deletions command/agent/kvs_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,37 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) {
}
})
}

func TestKVSEndpoint_GET_Raw(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
buf := bytes.NewBuffer([]byte("test"))
req, err := http.NewRequest("PUT", "/v1/kv/test", buf)
if err != nil {
t.Fatalf("err: %v", err)
}
resp := httptest.NewRecorder()
obj, err := srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
if res := obj.(bool); !res {
t.Fatalf("should work")
}

req, err = http.NewRequest("GET", "/v1/kv/test?raw", nil)
if err != nil {
t.Fatalf("err: %v", err)
}
resp = httptest.NewRecorder()
obj, err = srv.KVSEndpoint(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
}
assertIndex(t, resp)

// Check the body
if !bytes.Equal(resp.Body.Bytes(), []byte("test")) {
t.Fatalf("bad: %s", resp.Body.Bytes())
}
})
}
4 changes: 4 additions & 0 deletions website/source/docs/agent/http.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ For example, listing "/web/" with a "/" seperator may return:
Using the key listing method may be suitable when you do not need
the values or flags, or want to implement a key-space explorer.

If the "?raw" query parameter is used with a non-recursive GET,
then the response is just the raw value of the key, without any
encoding.

If no entries are found, a 404 code is returned.

This endpoint supports blocking queries and all consistency modes.
Expand Down

2 comments on commit 88d91c6

@mitchellh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What Content-Type does this send down?

@armon
Copy link
Member Author

@armon armon commented on 88d91c6 May 21, 2014 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.