From 62734dc8d02645bef1559f567b98f3e82b43bb75 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Sun, 11 Sep 2016 05:18:25 +0200 Subject: [PATCH 1/3] gateway: use core api for serving GET/HEAD requests License: MIT Signed-off-by: Lars Gierth This commit was moved from ipfs/kubo@029f971d9cb7e89e523568758c01e2c9924e5af9 --- gateway/core/corehttp/gateway.go | 5 +- gateway/core/corehttp/gateway_handler.go | 65 +++++++++--------------- 2 files changed, 26 insertions(+), 44 deletions(-) diff --git a/gateway/core/corehttp/gateway.go b/gateway/core/corehttp/gateway.go index 85b56e723..6663e8b73 100644 --- a/gateway/core/corehttp/gateway.go +++ b/gateway/core/corehttp/gateway.go @@ -6,6 +6,7 @@ import ( "net/http" core "github.com/ipfs/go-ipfs/core" + coreapi "github.com/ipfs/go-ipfs/core/coreapi" config "github.com/ipfs/go-ipfs/repo/config" id "gx/ipfs/QmQfvKShQ2v7nkfCE4ygisxpcSBFvBYaorQ54SibY6PGXV/go-libp2p/p2p/protocol/identify" ) @@ -27,7 +28,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption { Headers: cfg.Gateway.HTTPHeaders, Writable: writable, PathPrefixes: cfg.Gateway.PathPrefixes, - }) + }, coreapi.NewUnixfsAPI(n)) for _, p := range paths { mux.Handle(p+"/", gateway) @@ -37,7 +38,7 @@ func GatewayOption(writable bool, paths ...string) ServeOption { } func VersionOption() ServeOption { - return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { + return func(_ *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Commit: %s\n", config.CurrentCommit) fmt.Fprintf(w, "Client Version: %s\n", id.ClientVersion) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index ea0ef5c5d..c1da7d2d5 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -16,9 +16,10 @@ import ( chunk "github.com/ipfs/go-ipfs/importer/chunk" dag "github.com/ipfs/go-ipfs/merkledag" dagutils "github.com/ipfs/go-ipfs/merkledag/utils" + + coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" path "github.com/ipfs/go-ipfs/path" ft "github.com/ipfs/go-ipfs/unixfs" - uio "github.com/ipfs/go-ipfs/unixfs/io" humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize" routing "gx/ipfs/QmQKEgGgYCDyk8VNY6A65FpuE4YwbspvjXHco1rdb75PVc/go-libp2p-routing" @@ -36,12 +37,14 @@ const ( type gatewayHandler struct { node *core.IpfsNode config GatewayConfig + api coreiface.UnixfsAPI } -func newGatewayHandler(node *core.IpfsNode, conf GatewayConfig) *gatewayHandler { +func newGatewayHandler(n *core.IpfsNode, c GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler { i := &gatewayHandler{ - node: node, - config: conf, + node: n, + config: c, + api: api, } return i } @@ -154,27 +157,19 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request ipnsHostname = true } - p, err := path.ParsePath(urlPath) - if err != nil { - webError(w, "Invalid Path Error", err, http.StatusBadRequest) - return - } - - nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p) - // If node is in offline mode the error code and message should be different - if err == core.ErrNoNamesys && !i.node.OnlineMode() { + dr, err := i.api.Cat(ctx, urlPath) + dir := false + if err == coreiface.ErrIsDir { + dir = true + } else if err == coreiface.ErrOffline { w.WriteHeader(http.StatusServiceUnavailable) fmt.Fprint(w, "Could not resolve path. Node is in offline mode.") return } else if err != nil { webError(w, "Path Resolve error", err, http.StatusBadRequest) return - } - - pbnd, ok := nd.(*dag.ProtoNode) - if !ok { - webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest) - return + } else { + defer dr.Close() } etag := gopath.Base(urlPath) @@ -204,13 +199,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request w.Header().Set("Suborigin", pathRoot) } - dr, err := uio.NewDagReader(ctx, pbnd, i.node.DAG) - if err != nil && err != uio.ErrIsDir { - // not a directory and still an error - internalWebError(w, err) - return - } - // set these headers _after_ the error, for we may just not have it // and dont want the client to cache a 500 response... // and only if it's /ipfs! @@ -224,18 +212,23 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request modtime = time.Unix(1, 0) } - if err == nil { - defer dr.Close() + if !dir { name := gopath.Base(urlPath) http.ServeContent(w, r, name, modtime, dr) return } + links, err := i.api.Ls(ctx, urlPath) + if err != nil { + internalWebError(w, err) + return + } + // storage for directory listing var dirListing []directoryItem // loop through files foundIndex := false - for _, link := range nd.Links() { + for _, link := range links { if link.Name == "index.html" { log.Debugf("found index.html link for %s", urlPath) foundIndex = true @@ -254,19 +247,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request } // return index page instead. - nd, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, p) - if err != nil { - internalWebError(w, err) - return - } - - pbnd, ok := nd.(*dag.ProtoNode) - if !ok { - internalWebError(w, dag.ErrNotProtobuf) - return - } - - dr, err := uio.NewDagReader(ctx, pbnd, i.node.DAG) + dr, err := i.api.Cat(ctx, p.String()) if err != nil { internalWebError(w, err) return From ffb9dc17cdcc722d50c93e5408b5bbaf2a13c661 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Tue, 20 Sep 2016 04:31:57 +0200 Subject: [PATCH 2/3] gateway: move context/close-notify wiring License: MIT Signed-off-by: Lars Gierth This commit was moved from ipfs/kubo@c31e4f7226467ed8e703bfbff553ef1c2f586018 --- gateway/core/corehttp/gateway_handler.go | 33 ++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index c1da7d2d5..542229cdd 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -60,6 +60,21 @@ func (i *gatewayHandler) newDagFromReader(r io.Reader) (node.Node, error) { // TODO(btc): break this apart into separate handlers using a more expressive muxer func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour) + // the hour is a hard fallback, we don't expect it to happen, but just in case + defer cancel() + + if cn, ok := w.(http.CloseNotifier); ok { + clientGone := cn.CloseNotify() + go func() { + select { + case <-clientGone: + case <-ctx.Done(): + } + cancel() + }() + } + defer func() { if r := recover(); r != nil { log.Error("A panic occurred in the gateway handler!") @@ -83,7 +98,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if r.Method == "GET" || r.Method == "HEAD" { - i.getOrHeadHandler(w, r) + i.getOrHeadHandler(ctx, w, r) return } @@ -113,21 +128,7 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request) i.addUserHeaders(w) // return all custom headers (including CORS ones, if set) } -func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { - ctx, cancel := context.WithTimeout(i.node.Context(), time.Hour) - // the hour is a hard fallback, we don't expect it to happen, but just in case - defer cancel() - - if cn, ok := w.(http.CloseNotifier); ok { - clientGone := cn.CloseNotify() - go func() { - select { - case <-clientGone: - case <-ctx.Done(): - } - cancel() - }() - } +func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { urlPath := r.URL.Path From 5e0b554671e2f72212211a767f5da95a1b12bb81 Mon Sep 17 00:00:00 2001 From: Lars Gierth Date: Tue, 20 Sep 2016 04:32:37 +0200 Subject: [PATCH 3/3] gateway: use core api for serving POST requests License: MIT Signed-off-by: Lars Gierth This commit was moved from ipfs/kubo@0097b422de1ecc612bc04ca3e7c1dbd8f8495788 --- gateway/core/corehttp/gateway_handler.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/gateway/core/corehttp/gateway_handler.go b/gateway/core/corehttp/gateway_handler.go index 542229cdd..8bf781ef7 100644 --- a/gateway/core/corehttp/gateway_handler.go +++ b/gateway/core/corehttp/gateway_handler.go @@ -86,7 +86,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if i.config.Writable { switch r.Method { case "POST": - i.postHandler(w, r) + i.postHandler(ctx, w, r) return case "PUT": i.putHandler(w, r) @@ -314,14 +314,8 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } } -func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) { - nd, err := i.newDagFromReader(r.Body) - if err != nil { - internalWebError(w, err) - return - } - - k, err := i.node.DAG.Add(nd) +func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { + k, err := i.api.Add(ctx, r.Body) if err != nil { internalWebError(w, err) return