Skip to content

Commit 65e603e

Browse files
Merge pull request ipfs/kubo#5072 from Bren2010/review/core
Fix panic. Don't handle errors with fallthrough. This commit was moved from ipfs/kubo@cdc3497
2 parents fbf0c20 + db4fdcd commit 65e603e

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

gateway/core/corehttp/commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ func CheckVersionOption() ServeOption {
162162
pth := path.SplitList(cmdqry)
163163

164164
// backwards compatibility to previous version check
165-
if pth[1] != "version" {
165+
if len(pth) >= 2 && pth[1] != "version" {
166166
clientVersion := r.UserAgent()
167167
// skips check if client is not go-ipfs
168-
if clientVersion != "" && strings.Contains(clientVersion, "/go-ipfs/") && daemonVersion != clientVersion {
168+
if strings.Contains(clientVersion, "/go-ipfs/") && daemonVersion != clientVersion {
169169
http.Error(w, fmt.Sprintf("%s (%s != %s)", errAPIVersionMismatch, daemonVersion, clientVersion), http.StatusBadRequest)
170170
return
171171
}

gateway/core/corehttp/gateway_handler.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,14 @@ func (i *gatewayHandler) optionsHandler(w http.ResponseWriter, r *http.Request)
132132
}
133133

134134
func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
135-
136135
urlPath := r.URL.Path
137136
escapedURLPath := r.URL.EscapedPath()
138137

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

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

171169
// Resolve path to the final DAG node for the ETag
172170
resolvedPath, err := i.api.ResolvePath(ctx, parsedPath)
173-
switch err {
174-
case nil:
175-
case coreiface.ErrOffline:
176-
if !i.node.OnlineMode() {
177-
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
178-
return
179-
}
180-
fallthrough
181-
default:
171+
if err == coreiface.ErrOffline && !i.node.OnlineMode() {
172+
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
173+
return
174+
} else if err != nil {
182175
webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusNotFound)
183176
return
184177
}

gateway/core/corehttp/ipns_hostname.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func IPNSHostnameOption() ServeOption {
2626
if len(host) > 0 && isd.IsDomain(host) {
2727
name := "/ipns/" + host
2828
if _, err := n.Namesys.Resolve(ctx, name, nsopts.Depth(1)); err == nil {
29-
r.Header["X-Ipns-Original-Path"] = []string{r.URL.Path}
29+
r.Header.Set("X-Ipns-Original-Path", r.URL.Path)
3030
r.URL.Path = name + r.URL.Path
3131
}
3232
}

0 commit comments

Comments
 (0)