From f76407c2b80f5dd655175d383b1254c89a8884fe Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 16 May 2023 14:14:39 +0200 Subject: [PATCH] refactor: rename option to DeserializedResponses --- examples/gateway/common/handler.go | 12 ++++++------ gateway/gateway.go | 19 +++++++++---------- gateway/gateway_test.go | 12 ++++++------ gateway/handler.go | 10 +++++----- 4 files changed, 26 insertions(+), 27 deletions(-) diff --git a/examples/gateway/common/handler.go b/examples/gateway/common/handler.go index 236d05461..5d75c380d 100644 --- a/examples/gateway/common/handler.go +++ b/examples/gateway/common/handler.go @@ -21,7 +21,7 @@ func NewHandler(gwAPI gateway.IPFSBackend) http.Handler { // For these examples we have the trusted mode enabled by default. That is, // all types of requests will be accepted. By default, only Trustless Gateway // requests work: https://specs.ipfs.tech/http-gateways/trustless-gateway/ - TrustedMode: true, + DeserializedResponses: true, // Initialize the public gateways that we will want to have available through // Host header rewriting. This step is optional and only required if you're @@ -35,14 +35,14 @@ func NewHandler(gwAPI gateway.IPFSBackend) http.Handler { UseSubdomains: true, // This gateway is used for testing and therefore we make non-trustless // requests. Thus, we have to manually turn on the trusted mode. - TrustedMode: true, + DeserializedResponses: true, }, // Support local requests "localhost": { - Paths: []string{"/ipfs", "/ipns"}, - NoDNSLink: false, - UseSubdomains: true, - TrustedMode: true, + Paths: []string{"/ipfs", "/ipns"}, + NoDNSLink: false, + UseSubdomains: true, + DeserializedResponses: true, }, }, } diff --git a/gateway/gateway.go b/gateway/gateway.go index e1ea3e09c..1c1db3b9b 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -20,13 +20,12 @@ type Config struct { // headers via AddAccessControlHeaders. Headers map[string][]string - // TrustedMode configures this gateway to allow trusted requests. By default, - // the gateway will operate in trustless mode, as defined in the specification: - // https://specs.ipfs.tech/http-gateways/trustless-gateway/. - // - // This only applies to hostnames not defined under PublicGateways. In addition, - // the hostnames localhost, 127.0.0.1 and ::1 are considered trusted by default. - TrustedMode bool + // DeserializedResponses configures this gateway to support returning data + // in deserialized format. By default, the gateway will only provide raw responses, + // operating as a trustless gateway, as defined in the specification: + // https://specs.ipfs.tech/http-gateways/trustless-gateway/. This flag can be + // overridden per FQDN in PublicGateways. + DeserializedResponses bool // NoDNSLink configures the gateway to _not_ perform DNS TXT record lookups in // response to requests with values in `Host` HTTP header. This flag can be @@ -68,9 +67,9 @@ type Specification struct { // This should be set to true if you use HTTPS. InlineDNSLink bool - // TrustedMode configures this gateway to allow trusted requests. This setting - // overrides the global setting. Not setting TrustedMode enables Trustless Mode. - TrustedMode bool + // DeserializedResponses configures this gateway to support returning data + // in deserialized format. This setting overrides the global setting. + DeserializedResponses bool } // TODO: Is this what we want for ImmutablePath? diff --git a/gateway/gateway_test.go b/gateway/gateway_test.go index b578e0c6f..bd413f4ee 100644 --- a/gateway/gateway_test.go +++ b/gateway/gateway_test.go @@ -199,8 +199,8 @@ func newTestServerAndNode(t *testing.T, ns mockNamesys) (*httptest.Server, *mock func newTestServer(t *testing.T, api IPFSBackend) *httptest.Server { return newTestServerWithConfig(t, api, Config{ - Headers: map[string][]string{}, - TrustedMode: true, + Headers: map[string][]string{}, + DeserializedResponses: true, }) } @@ -591,8 +591,8 @@ func TestIpfsTrustlessMode(t *testing.T) { Paths: []string{"/ipfs", "/ipns"}, }, "trusted.com": { - Paths: []string{"/ipfs", "/ipns"}, - TrustedMode: true, + Paths: []string{"/ipfs", "/ipns"}, + DeserializedResponses: true, }, }, }) @@ -673,8 +673,8 @@ func TestIpnsTrustlessMode(t *testing.T) { Paths: []string{"/ipfs", "/ipns"}, }, "trusted.com": { - Paths: []string{"/ipfs", "/ipns"}, - TrustedMode: true, + Paths: []string{"/ipfs", "/ipns"}, + DeserializedResponses: true, }, }, }) diff --git a/gateway/handler.go b/gateway/handler.go index bcac67c1f..4378cd65f 100644 --- a/gateway/handler.go +++ b/gateway/handler.go @@ -240,7 +240,7 @@ func (i *handler) getOrHeadHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("X-Ipfs-Path", contentPath.String()) // Trustless gateway. - if !i.isTrustedMode(r) && !i.isValidTrustlessRequest(contentPath, responseFormat) { + if !i.onlyDeserializedResponses(r) && !i.isSerializedRequest(contentPath, responseFormat) { err := errors.New("only trustless requests are accepted: https://specs.ipfs.tech/http-gateways/trustless-gateway/") webError(w, err, http.StatusNotImplemented) return @@ -329,7 +329,7 @@ func (i *handler) addUserHeaders(w http.ResponseWriter) { } } -func (i *handler) isTrustedMode(r *http.Request) bool { +func (i *handler) onlyDeserializedResponses(r *http.Request) bool { // Get the host, by default the request's Host. If this request went through // WithHostname, also check for the key in the context. If that is not present, // also check X-Forwarded-Host to support reverse proxies. @@ -342,14 +342,14 @@ func (i *handler) isTrustedMode(r *http.Request) bool { // If the gateway is defined, return whatever is set. if gw, ok := i.config.PublicGateways[host]; ok { - return gw.TrustedMode + return gw.DeserializedResponses } // Otherwise, the default. - return i.config.TrustedMode + return i.config.DeserializedResponses } -func (i *handler) isValidTrustlessRequest(contentPath ipath.Path, responseFormat string) bool { +func (i *handler) isSerializedRequest(contentPath ipath.Path, responseFormat string) bool { // Only allow "/{#1}/{#2}"-like paths. trimmedPath := strings.Trim(contentPath.String(), "/") pathComponents := strings.Split(trimmedPath, "/")