Skip to content

Commit

Permalink
fix(gw): preserve query on website redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Oct 13, 2020
1 parent e1e577f commit 6ffd0aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
goget := r.URL.Query().Get("go-get") == "1"
if dirwithoutslash && !goget {
// See comment above where originalUrlPath is declared.
http.Redirect(w, r, originalUrlPath+"/", 302)
suffix := "/"
if r.URL.RawQuery != "" {
// preserve query parameters
suffix = suffix + "?" + r.URL.RawQuery
}
http.Redirect(w, r, originalUrlPath+suffix, 302)
return
}

Expand Down
13 changes: 12 additions & 1 deletion test/sharness/t0110-gateway.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ test_expect_success "GET IPFS path output looks good" '
'

test_expect_success "GET IPFS directory path succeeds" '
mkdir dir &&
mkdir -p dir/dirwithindex &&
echo "12345" >dir/test &&
echo "hello i am a webpage" >dir/dirwithindex/index.html &&
ipfs add -r -q dir >actual &&
HASH2=$(tail -n 1 actual) &&
curl -sf "http://127.0.0.1:$port/ipfs/$HASH2"
Expand All @@ -69,6 +70,16 @@ test_expect_success "GET IPFS directory file output looks good" '
test_cmp dir/test actual
'

test_expect_success "GET IPFS directory with index.html returns redirect to add trailing slash" "
curl -sI -o response_without_slash \"http://127.0.0.1:$port/ipfs/$HASH2/dirwithindex?query=to-remember\" &&
test_should_contain \"Location: /ipfs/$HASH2/dirwithindex/?query=to-remember\" response_without_slash
"

test_expect_success "GET IPFS directory with index.html and trailing slash returns expected output" "
curl -s -o response_with_slash \"http://127.0.0.1:$port/ipfs/$HASH2/dirwithindex/?query=to-remember\" &&
test_should_contain \"hello i am a webpage\" response_with_slash
"

test_expect_success "GET IPFS nonexistent file returns code expected (404)" '
test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/$HASH2/pleaseDontAddMe" "HTTP/1.1 404 Not Found"
'
Expand Down

0 comments on commit 6ffd0aa

Please sign in to comment.