@cryptotaxi247 / kubo / commits / c1847e9f6

putHandler: addressed CR from @jbenet

License: MIT Signed-off-by: Henry <cryptix@riseup.net>

Henry committed Oct 31, 2015 at 01:32 UTC c1847e9f693e716a54e0223e44729c76ef64308b
2 files changed +12 -9
core/corehttp/gateway_handler.go
+8 -5
@@ -6,7 +6,6 @@ import (
6 "io"
7 "net/http"
8 gopath "path"
9 - "path/filepath"
9 "strings"
10 "time"
11
@@ -276,6 +275,10 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
275 }
276
277 func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
278 + // TODO(cryptix): move me to ServeHTTP and pass into all handlers
279 + ctx, cancel := context.WithCancel(i.node.Context())
280 + defer cancel()
281 +
282 rootPath, err := path.ParsePath(r.URL.Path)
283 if err != nil {
284 webError(w, "putHandler: ipfs path not valid", err, http.StatusBadRequest)
@@ -306,20 +309,20 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
309 }
310
311 var newkey key.Key
309 - rnode, err := core.Resolve(i.node.Context(), i.node, rootPath)
312 + rnode, err := core.Resolve(ctx, i.node, rootPath)
313 switch ev := err.(type) {
314 case path.ErrNoLink:
315 // ev.Node < node where resolve failed
316 // ev.Name < new link
317 // but we need to patch from the root
315 - rnode, err := i.node.DAG.Get(i.node.Context(), key.B58KeyDecode(rsegs[1]))
318 + rnode, err := i.node.DAG.Get(ctx, key.B58KeyDecode(rsegs[1]))
319 if err != nil {
320 webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError)
321 return
322 }
323
324 e := dagutils.NewDagEditor(i.node.DAG, rnode)
322 - err = e.InsertNodeAtPath(i.node.Context(), newPath, newnode, uio.NewEmptyDirectory)
325 + err = e.InsertNodeAtPath(ctx, newPath, newnode, uio.NewEmptyDirectory)
326 if err != nil {
327 webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError)
328 return
@@ -350,7 +353,7 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
353
354 i.addUserHeaders(w) // ok, _now_ write user's headers.
355 w.Header().Set("IPFS-Hash", newkey.String())
353 - http.Redirect(w, r, filepath.Join(ipfsPathPrefix, newkey.String(), newPath), http.StatusCreated)
356 + http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newkey.String(), newPath), http.StatusCreated)
357 }
358
359 func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
test/sharness/t0111-gateway-writeable.sh
+4 -4
@@ -14,10 +14,10 @@ test_launch_ipfs_daemon
14
15 port=$PORT_GWAY
16
17 -# TODO(cryptix): netstat not avail on testing system?
18 -#test_expect_success "ipfs daemon listening to TCP port $port" '
19 -# test_wait_open_tcp_port_10_sec "$PORT_GWAY"
20 -#'
17 +test_expect_success "ipfs daemon up" '
18 + pollEndpoint -host $ADDR_GWAY -ep=/version -v -tout=1s -tries=60 2>poll_apierr > poll_apiout ||
19 + test_fsh cat poll_apierr || test_fsh cat poll_apiout
20 +'
21
22 test_expect_success "HTTP gateway gives access to sample file" '
23 curl -s -o welcome "http://localhost:$PORT_GWAY/ipfs/$HASH_WELCOME_DOCS/readme" &&