From 7ae71affcee0eb504745acae28add063b6b6b4fc Mon Sep 17 00:00:00 2001 From: ns Date: Thu, 21 Dec 2023 18:27:21 +0000 Subject: [PATCH 1/2] legacy rpc: handle /api/v0/name/resolve/ --- handler_test.go | 1 + handlers.go | 1 + 2 files changed, 2 insertions(+) diff --git a/handler_test.go b/handler_test.go index 2db09ac..fc5df68 100644 --- a/handler_test.go +++ b/handler_test.go @@ -54,6 +54,7 @@ func TestRPCRedirectsToKubo(t *testing.T) { tests := []rpcRedirectTest{ {"/api/v0/name/resolve?arg=some-arg", "http://example.com/api/v0/name/resolve?arg=some-arg", http.StatusTemporaryRedirect}, + {"/api/v0/name/resolve/ipfs.io?arg=some-arg", "http://example.com/api/v0/name/resolve/ipfs.io?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/resolve?arg=some-arg", "http://example.com/api/v0/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dag/resolve?arg=some-arg", "http://example.com/api/v0/dag/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dns?arg=some-arg", "http://example.com/api/v0/dns?arg=some-arg", http.StatusTemporaryRedirect}, diff --git a/handlers.go b/handlers.go index d422636..e2f0d3d 100644 --- a/handlers.go +++ b/handlers.go @@ -259,6 +259,7 @@ func newKuboRPCHandler(endpoints []string) http.Handler { } mux.HandleFunc("/api/v0/name/resolve", redirectToKubo) + mux.HandleFunc("/api/v0/name/resolve/", redirectToKubo) mux.HandleFunc("/api/v0/resolve", redirectToKubo) mux.HandleFunc("/api/v0/dag/resolve", redirectToKubo) mux.HandleFunc("/api/v0/dns", redirectToKubo) From 6011cdbf17c33d8ac4fbdf404730e3d985ad8dca Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Mon, 29 Jan 2024 16:17:51 +0100 Subject: [PATCH 2/2] refactor: test arg in path alone --- handler_test.go | 2 +- handlers.go | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/handler_test.go b/handler_test.go index fc5df68..959bc31 100644 --- a/handler_test.go +++ b/handler_test.go @@ -54,7 +54,7 @@ func TestRPCRedirectsToKubo(t *testing.T) { tests := []rpcRedirectTest{ {"/api/v0/name/resolve?arg=some-arg", "http://example.com/api/v0/name/resolve?arg=some-arg", http.StatusTemporaryRedirect}, - {"/api/v0/name/resolve/ipfs.io?arg=some-arg", "http://example.com/api/v0/name/resolve/ipfs.io?arg=some-arg", http.StatusTemporaryRedirect}, + {"/api/v0/name/resolve/some-arg", "http://example.com/api/v0/name/resolve/some-arg", http.StatusTemporaryRedirect}, {"/api/v0/resolve?arg=some-arg", "http://example.com/api/v0/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dag/resolve?arg=some-arg", "http://example.com/api/v0/dag/resolve?arg=some-arg", http.StatusTemporaryRedirect}, {"/api/v0/dns?arg=some-arg", "http://example.com/api/v0/dns?arg=some-arg", http.StatusTemporaryRedirect}, diff --git a/handlers.go b/handlers.go index e2f0d3d..5834aa1 100644 --- a/handlers.go +++ b/handlers.go @@ -253,7 +253,10 @@ func newKuboRPCHandler(endpoints []string) http.Handler { redirectToKubo := func(w http.ResponseWriter, r *http.Request) { // Naively choose one of the Kubo RPC clients. endpoint := endpoints[rand.Intn(len(endpoints))] - url := endpoint + r.URL.Path + "?" + r.URL.RawQuery + url := endpoint + r.URL.Path + if r.URL.RawQuery != "" { + url += "?" + r.URL.RawQuery + } goLog.Debugw("api request redirected to kubo", "url", r.URL, "redirect", url) http.Redirect(w, r, url, http.StatusTemporaryRedirect) }