Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#5072 from Bren2010/review/core
Browse files Browse the repository at this point in the history
Fix panic. Don't handle errors with fallthrough.

This commit was moved from ipfs/kubo@cdc3497
  • Loading branch information
whyrusleeping authored Jun 13, 2018
2 parents fbf0c20 + db4fdcd commit 65e603e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
4 changes: 2 additions & 2 deletions gateway/core/corehttp/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ func CheckVersionOption() ServeOption {
pth := path.SplitList(cmdqry)

// backwards compatibility to previous version check
if pth[1] != "version" {
if len(pth) >= 2 && pth[1] != "version" {
clientVersion := r.UserAgent()
// skips check if client is not go-ipfs
if clientVersion != "" && strings.Contains(clientVersion, "/go-ipfs/") && daemonVersion != clientVersion {
if strings.Contains(clientVersion, "/go-ipfs/") && daemonVersion != clientVersion {
http.Error(w, fmt.Sprintf("%s (%s != %s)", errAPIVersionMismatch, daemonVersion, clientVersion), http.StatusBadRequest)
return
}
Expand Down
21 changes: 7 additions & 14 deletions gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,14 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request)
}

func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {

urlPath := r.URL.Path
escapedURLPath := r.URL.EscapedPath()

// If the gateway is behind a reverse proxy and mounted at a sub-path,
// the prefix header can be set to signal this sub-path.
// It will be prepended to links in directory listings and the index.html redirect.
prefix := ""
if prefixHdr := r.Header["X-Ipfs-Gateway-Prefix"]; len(prefixHdr) > 0 {
prfx := prefixHdr[0]
if prfx := r.Header.Get("X-Ipfs-Gateway-Prefix"); len(prfx) > 0 {
for _, p := range i.config.PathPrefixes {
if prfx == p || strings.HasPrefix(prfx, p+"/") {
prefix = prfx
Expand All @@ -157,8 +155,8 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
// the redirects and links would end up as http://example.net/ipns/example.net
originalUrlPath := prefix + urlPath
ipnsHostname := false
if hdr := r.Header["X-Ipns-Original-Path"]; len(hdr) > 0 {
originalUrlPath = prefix + hdr[0]
if hdr := r.Header.Get("X-Ipns-Original-Path"); len(hdr) > 0 {
originalUrlPath = prefix + hdr
ipnsHostname = true
}

Expand All @@ -170,15 +168,10 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr

// Resolve path to the final DAG node for the ETag
resolvedPath, err := i.api.ResolvePath(ctx, parsedPath)
switch err {
case nil:
case coreiface.ErrOffline:
if !i.node.OnlineMode() {
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
return
}
fallthrough
default:
if err == coreiface.ErrOffline && !i.node.OnlineMode() {
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
return
} else if err != nil {
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusNotFound)
return
}
Expand Down
2 changes: 1 addition & 1 deletion gateway/core/corehttp/ipns_hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func IPNSHostnameOption() ServeOption {
if len(host) > 0 && isd.IsDomain(host) {
name := "/ipns/" + host
if _, err := n.Namesys.Resolve(ctx, name, nsopts.Depth(1)); err == nil {
r.Header["X-Ipns-Original-Path"] = []string{r.URL.Path}
r.Header.Set("X-Ipns-Original-Path", r.URL.Path)
r.URL.Path = name + r.URL.Path
}
}
Expand Down

0 comments on commit 65e603e

Please sign in to comment.