Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add REPLACE method #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// lock the key while a PUT or DELETE is in progress
if r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE" || r.Method == "UNLINK" || r.Method == "REBALANCE" {
if r.Method == "POST" || r.Method == "PUT" || r.Method == "REPLACE" || r.Method == "DELETE" || r.Method == "UNLINK" || r.Method == "REBALANCE" {
if !a.LockKey(lkey) {
// Conflict, retry later
w.WriteHeader(409)
Expand Down Expand Up @@ -324,19 +324,21 @@ func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<CompleteMultipartUploadResult></CompleteMultipartUploadResult>"))
return
}
case "PUT":
case "PUT", "REPLACE":
// no empty values
if r.ContentLength == 0 {
w.WriteHeader(411)
return
}

// check if we already have the key, and it's not deleted
rec := a.GetRecord(key)
if rec.deleted == NO {
// Forbidden to overwrite with PUT
w.WriteHeader(403)
return
if r.Method == "PUT" {
// check if we already have the key, and it's not deleted
rec := a.GetRecord(key)
if rec.deleted == NO {
// Forbidden to overwrite with PUT
w.WriteHeader(403)
return
}
}

if pn := r.URL.Query().Get("partNumber"); pn != "" {
Expand Down
36 changes: 36 additions & 0 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ def test_head_request(self):
# redirect, content length should be size of data
self.assertEqual(int(r.headers['content-length']), len(data))

def test_put_replace(self):
key = self.get_fresh_key()

r = requests.put(key, data="abc")
self.assertEqual(r.status_code, 201)

r = requests.request("REPLACE", key, data="123")
self.assertEqual(r.status_code, 201)

r = requests.get(key)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "123")

r = requests.delete(key)
self.assertEqual(r.status_code, 204)

def test_replace_replace(self):
key = self.get_fresh_key()

r = requests.request("REPLACE", key, data="123")
self.assertEqual(r.status_code, 201)

r = requests.get(key)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "123")

r = requests.request("REPLACE", key, data="456")
self.assertEqual(r.status_code, 201)

r = requests.get(key)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "456")

r = requests.delete(key)
self.assertEqual(r.status_code, 204)

def test_large_key(self):
key = self.get_fresh_key()

Expand Down
17 changes: 11 additions & 6 deletions tools/thrasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func remote_delete(remote string) error {
return nil
}

func remote_put(remote string, length int64, body io.Reader) error {
req, err := http.NewRequest("PUT", remote, body)
func remote_set(method string, remote string, length int64, body io.Reader) error {
req, err := http.NewRequest(method, remote, body)
if err != nil {
return err
}
Expand All @@ -41,7 +41,7 @@ func remote_put(remote string, length int64, body io.Reader) error {
}
defer resp.Body.Close()
if resp.StatusCode != 201 && resp.StatusCode != 204 {
return fmt.Errorf("remote_put: wrong status code %d", resp.StatusCode)
return fmt.Errorf("remote_set: wrong status code %d", resp.StatusCode)
}
return nil
}
Expand Down Expand Up @@ -76,9 +76,14 @@ func main() {
go func() {
for {
key := <-reqs
value := fmt.Sprintf("value-%d", rand.Int())
if err := remote_put("http://localhost:3000/"+key, int64(len(value)), strings.NewReader(value)); err != nil {
fmt.Println("PUT FAILED", err)
val := rand.Int()
value := fmt.Sprintf("value-%d", val)
method := "PUT"
if val % 4 == 0 {
method = "REPLACE"
}
if err := remote_set(method, "http://localhost:3000/"+key, int64(len(value)), strings.NewReader(value)); err != nil {
fmt.Println(method + " FAILED", err)
resp <- false
continue
}
Expand Down
Loading