fix(gw): preserve query on website redirect
Marcin Rataj committed
Oct 14, 2020 at 00:46 UTC
6ffd0aa22ea6a454740c3b8c83a566d4faa3dc62
2 files changed
+18
-2
core/corehttp/gateway_handler.go
+6
-1
@@ -290,7 +290,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
290
goget := r.URL.Query().Get("go-get") == "1"
291
if dirwithoutslash && !goget {
292
// See comment above where originalUrlPath is declared.
293
- http.Redirect(w, r, originalUrlPath+"/", 302)
293
+ suffix := "/"
294
+ if r.URL.RawQuery != "" {
295
+ // preserve query parameters
296
+ suffix = suffix + "?" + r.URL.RawQuery
297
+ }
298
+ http.Redirect(w, r, originalUrlPath+suffix, 302)
299
return
300
}
301
test/sharness/t0110-gateway.sh
+12
-1
@@ -54,8 +54,9 @@ test_expect_success "GET IPFS path output looks good" '
54
'
55
56
test_expect_success "GET IPFS directory path succeeds" '
57
- mkdir dir &&
57
+ mkdir -p dir/dirwithindex &&
58
echo "12345" >dir/test &&
59
+ echo "hello i am a webpage" >dir/dirwithindex/index.html &&
60
ipfs add -r -q dir >actual &&
61
HASH2=$(tail -n 1 actual) &&
62
curl -sf "http://127.0.0.1:$port/ipfs/$HASH2"
@@ -69,6 +70,16 @@ test_expect_success "GET IPFS directory file output looks good" '
70
test_cmp dir/test actual
71
'
72
73
+test_expect_success "GET IPFS directory with index.html returns redirect to add trailing slash" "
74
+ curl -sI -o response_without_slash \"http://127.0.0.1:$port/ipfs/$HASH2/dirwithindex?query=to-remember\" &&
75
+ test_should_contain \"Location: /ipfs/$HASH2/dirwithindex/?query=to-remember\" response_without_slash
76
+"
77
+
78
+test_expect_success "GET IPFS directory with index.html and trailing slash returns expected output" "
79
+ curl -s -o response_with_slash \"http://127.0.0.1:$port/ipfs/$HASH2/dirwithindex/?query=to-remember\" &&
80
+ test_should_contain \"hello i am a webpage\" response_with_slash
81
+"
82
+
83
test_expect_success "GET IPFS nonexistent file returns code expected (404)" '
84
test_curl_resp_http_code "http://127.0.0.1:$port/ipfs/$HASH2/pleaseDontAddMe" "HTTP/1.1 404 Not Found"
85
'