@cryptotaxi247 / kubo / commits / 295cc443d

HTTP Gateway: add /ipns/ GET requests

Mildred Ki'Lya committed Jan 28, 2015 at 13:06 UTC 295cc443da69cd0714a28685b2a0662e655fbd46
2 files changed +33 -5
core/corehttp/gateway.go
+1 -1
@@ -13,7 +13,7 @@ func GatewayOption(writable bool) ServeOption {
13 return err
14 }
15 mux.Handle("/ipfs/", gateway)
16 - mux.Handle("/ipns/", gateway)
16 + mux.Handle("/ipns/", gateway)
17 return nil
18 }
19 }
core/corehttp/gateway_handler.go
+32 -4
@@ -75,7 +75,7 @@ func (i *gatewayHandler) loadTemplate() error {
75 return nil
76 }
77
78 -func (i *gatewayHandler) ResolvePath(ctx context.Context, p string) (*dag.Node, string, error) {
78 +func (i *gatewayHandler) resolveNamePath(ctx context.Context, p string) (string, error) {
79 p = gopath.Clean(p)
80
81 if strings.HasPrefix(p, IpnsPathPrefix) {
@@ -83,7 +83,7 @@ func (i *gatewayHandler) ResolvePath(ctx context.Context, p string) (*dag.Node,
83 hash := elements[0]
84 k, err := i.node.Namesys.Resolve(ctx, hash)
85 if err != nil {
86 - return nil, "", err
86 + return "", err
87 }
88
89 elements[0] = k.Pretty()
@@ -92,6 +92,14 @@ func (i *gatewayHandler) ResolvePath(ctx context.Context, p string) (*dag.Node,
92 if !strings.HasPrefix(p, IpfsPathPrefix) {
93 p = gopath.Join(IpfsPathPrefix, p)
94 }
95 + return p, nil
96 +}
97 +
98 +func (i *gatewayHandler) ResolvePath(ctx context.Context, p string) (*dag.Node, string, error) {
99 + p, err := i.resolveNamePath(ctx, p)
100 + if err != nil {
101 + return nil, "", err
102 + }
103
104 node, err := i.node.Resolver.ResolvePath(path.Path(p))
105 if err != nil {
@@ -298,7 +306,17 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
306 }
307 }
308
301 - h, components, err := path.SplitAbsPath(path.Path(urlPath))
309 + ctx, cancel := context.WithCancel(i.node.Context())
310 + defer cancel()
311 +
312 + ipfspath, err := i.resolveNamePath(ctx, urlPath)
313 + if err != nil {
314 + // FIXME HTTP error code
315 + webError(w, "Could not resolve name", err, http.StatusInternalServerError)
316 + return
317 + }
318 +
319 + h, components, err := path.SplitAbsPath(path.Path(ipfspath))
320 if err != nil {
321 webError(w, "Could not split path", err, http.StatusInternalServerError)
322 return
@@ -358,7 +376,17 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
376
377 func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
378 urlPath := r.URL.Path
361 - h, components, err := path.SplitAbsPath(path.Path(urlPath))
379 + ctx, cancel := context.WithCancel(i.node.Context())
380 + defer cancel()
381 +
382 + ipfspath, err := i.resolveNamePath(ctx, urlPath)
383 + if err != nil {
384 + // FIXME HTTP error code
385 + webError(w, "Could not resolve name", err, http.StatusInternalServerError)
386 + return
387 + }
388 +
389 + h, components, err := path.SplitAbsPath(path.Path(ipfspath))
390 if err != nil {
391 webError(w, "Could not split path", err, http.StatusInternalServerError)
392 return