Skip to content

Commit

Permalink
gateway: use core api for GET/HEAD
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Lars Gierth <larsg@systemli.org>
  • Loading branch information
Lars Gierth committed Sep 11, 2016
1 parent a039cbe commit e02fc54
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
6 changes: 4 additions & 2 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/Qmf4ETeAWXuThBfWwonVyFqGFSgTWepUDEr1txcctvpTXS/go-libp2p/p2p/protocol/identify"
)
Expand All @@ -23,11 +24,12 @@ func GatewayOption(paths ...string) ServeOption {
return nil, err
}

api := &coreapi.UnixfsAPI{Node: n}
gateway := newGatewayHandler(n, GatewayConfig{
Headers: cfg.Gateway.HTTPHeaders,
Writable: cfg.Gateway.Writable,
PathPrefixes: cfg.Gateway.PathPrefixes,
})
}, api)

for _, p := range paths {
mux.Handle(p+"/", gateway)
Expand All @@ -37,7 +39,7 @@ func GatewayOption(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)
Expand Down
41 changes: 21 additions & 20 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ 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"
"github.com/ipfs/go-ipfs/routing"
uio "github.com/ipfs/go-ipfs/unixfs/io"
Expand All @@ -34,12 +36,14 @@ const (
type gatewayHandler struct {
node *core.IpfsNode
config GatewayConfig
api coreiface.UnixfsAPI
}

func newGatewayHandler(node *core.IpfsNode, conf GatewayConfig) *gatewayHandler {
func newGatewayHandler(node *core.IpfsNode, conf GatewayConfig, api coreiface.UnixfsAPI) *gatewayHandler {
i := &gatewayHandler{
node: node,
config: conf,
api: api,
}
return i
}
Expand Down Expand Up @@ -152,15 +156,19 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
ipnsHostname = true
}

nd, err := core.Resolve(ctx, i.node, path.Path(urlPath))
// 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(r.Context(), urlPath)
dir := false
if err == coreiface.ErrOffline {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, "Could not resolve path. Node is in offline mode.")
return
} else if err == coreiface.ErrIsDir {
dir = true
} else if err != nil {
webError(w, "Path Resolve error", err, http.StatusBadRequest)
return
} else {
defer dr.Close()
}

etag := gopath.Base(urlPath)
Expand Down Expand Up @@ -190,13 +198,6 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
w.Header().Set("Suborigin", pathRoot)
}

dr, err := uio.NewDagReader(ctx, nd, 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!
Expand All @@ -210,18 +211,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(r.Context(), 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
Expand All @@ -234,12 +240,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
}

// return index page instead.
nd, err := core.Resolve(ctx, i.node, path.Path(urlPath+"/index.html"))
if err != nil {
internalWebError(w, err)
return
}
dr, err := uio.NewDagReader(ctx, nd, i.node.DAG)
dr, err := i.api.Cat(r.Context(), urlPath+"/index.html")
if err != nil {
internalWebError(w, err)
return
Expand Down

0 comments on commit e02fc54

Please sign in to comment.