etcdhttp: parseRequest -> parseKeyRequest

This commit is contained in:
Yicheng Qin 2014-10-21 15:04:00 -07:00
parent e2b6a4fc4c
commit ca73f25615
2 changed files with 23 additions and 24 deletions

View File

@ -108,13 +108,12 @@ func (h serverHandler) serveKeys(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(context.Background(), h.timeout)
defer cancel()
rr, err := parseRequest(r, etcdserver.GenID(), clockwork.NewRealClock())
rr, err := parseKeyRequest(r, etcdserver.GenID(), clockwork.NewRealClock())
if err != nil {
writeError(w, err)
return
}
rr.Path = path.Join(etcdserver.StoreKeysPrefix, rr.Path)
resp, err := h.server.Do(ctx, rr)
if err != nil {
writeError(w, err)
@ -251,10 +250,10 @@ func (h serverHandler) serveRaft(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}
// parseRequest converts a received http.Request to a server Request,
// performing validation of supplied fields as appropriate.
// parseKeyRequest converts a received http.Request on keysPrefix to
// a server Request, performing validation of supplied fields as appropriate.
// If any validation fails, an empty Request and non-nil error is returned.
func parseRequest(r *http.Request, id uint64, clock clockwork.Clock) (etcdserverpb.Request, error) {
func parseKeyRequest(r *http.Request, id uint64, clock clockwork.Clock) (etcdserverpb.Request, error) {
emptyReq := etcdserverpb.Request{}
err := r.ParseForm()
@ -271,7 +270,7 @@ func parseRequest(r *http.Request, id uint64, clock clockwork.Clock) (etcdserver
"incorrect key prefix",
)
}
p := r.URL.Path[len(keysPrefix):]
p := path.Join(etcdserver.StoreKeysPrefix, r.URL.Path[len(keysPrefix):])
var pIdx, wIdx uint64
if pIdx, err = getUint64(r.Form, "prevIndex"); err != nil {

View File

@ -211,7 +211,7 @@ func TestBadParseRequest(t *testing.T) {
},
}
for i, tt := range tests {
got, err := parseRequest(tt.in, 1234, clockwork.NewFakeClock())
got, err := parseKeyRequest(tt.in, 1234, clockwork.NewFakeClock())
if err == nil {
t.Errorf("#%d: unexpected nil error!", i)
continue
@ -244,7 +244,7 @@ func TestGoodParseRequest(t *testing.T) {
etcdserverpb.Request{
ID: 1234,
Method: "GET",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -258,7 +258,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
Val: "some_value",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -272,7 +272,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
PrevIndex: 98765,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -286,7 +286,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
Recursive: true,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -300,7 +300,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
Sorted: true,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -310,7 +310,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "GET",
Wait: true,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -319,7 +319,7 @@ func TestGoodParseRequest(t *testing.T) {
etcdserverpb.Request{
ID: 1234,
Method: "GET",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
Expiration: 0,
},
},
@ -329,7 +329,7 @@ func TestGoodParseRequest(t *testing.T) {
etcdserverpb.Request{
ID: 1234,
Method: "GET",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
Expiration: fc.Now().Add(5678 * time.Second).UnixNano(),
},
},
@ -339,7 +339,7 @@ func TestGoodParseRequest(t *testing.T) {
etcdserverpb.Request{
ID: 1234,
Method: "GET",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
Expiration: fc.Now().UnixNano(),
},
},
@ -350,7 +350,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "GET",
Dir: true,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -360,7 +360,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "GET",
Dir: false,
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -374,7 +374,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
PrevExist: boolp(true),
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
{
@ -388,7 +388,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
PrevExist: boolp(false),
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
// mix various fields
@ -408,7 +408,7 @@ func TestGoodParseRequest(t *testing.T) {
PrevExist: boolp(true),
PrevValue: "previous value",
Val: "some value",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
// query parameters should be used if given
@ -422,7 +422,7 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
PrevValue: "woof",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
// but form values should take precedence over query parameters
@ -438,13 +438,13 @@ func TestGoodParseRequest(t *testing.T) {
ID: 1234,
Method: "PUT",
PrevValue: "miaow",
Path: "/foo",
Path: path.Join(etcdserver.StoreKeysPrefix, "/foo"),
},
},
}
for i, tt := range tests {
got, err := parseRequest(tt.in, 1234, fc)
got, err := parseKeyRequest(tt.in, 1234, fc)
if err != nil {
t.Errorf("#%d: err = %v, want %v", i, err, nil)
}