From 56668dc7018eb9b59a59b6848f2042dec4d67752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Wed, 8 May 2013 15:59:42 +0200 Subject: [PATCH] Implement offset exceeded handling --- src/http/handler.go | 13 ++++++++++++- src/http/handler_test.go | 17 +++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/http/handler.go b/src/http/handler.go index 360acb4..91e60e2 100644 --- a/src/http/handler.go +++ b/src/http/handler.go @@ -139,7 +139,18 @@ func (h *Handler) patchFile(w http.ResponseWriter, r *http.Request, id string) { return } - // @TODO Reject if offset > current offset + info, err := h.store.GetInfo(id) + if err != nil { + h.err(err, w, http.StatusInternalServerError) + return + } + + if offset > info.Offset { + err = fmt.Errorf("Offset: %d exceeds current offset: %d", offset, info.Offset) + h.err(err, w, http.StatusForbidden) + return + } + // @TODO Test offset < current offset err = h.store.WriteFileChunk(id, offset, r.Body) diff --git a/src/http/handler_test.go b/src/http/handler_test.go index 4f87077..305ce31 100644 --- a/src/http/handler_test.go +++ b/src/http/handler_test.go @@ -177,7 +177,7 @@ var Protocol_Core_Tests = []struct { }, }, { - Description: "Resume", + Description: "Simple Resume", FinalLength: 11, ExpectFileContent: "hello world", Requests: []TestRequest{ @@ -200,7 +200,20 @@ var Protocol_Core_Tests = []struct { }, }, }, - // @TODO Test applying PATCH at offset > current offset (error) + { + Description: "Offset exceeded", + FinalLength: 5, + Requests: []TestRequest{ + { + Method: "PATCH", + Headers: map[string]string{"Offset": "1"}, + // Not sure if this is the right status to use. Once the parallel + // chunks protocol spec is done, we can use NotImplemented as a + // status until we implement support for this. + ExpectStatusCode: http.StatusForbidden, + }, + }, + }, } func TestProtocol_Core(t *testing.T) {