Skip to content

Commit

Permalink
refactor: rename option to DeserializedResponses
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed May 16, 2023
1 parent c50d89f commit f76407c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
12 changes: 6 additions & 6 deletions examples/gateway/common/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
},
},
}
Expand Down
19 changes: 9 additions & 10 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
12 changes: 6 additions & 6 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down Expand Up @@ -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,
},
},
})
Expand Down Expand Up @@ -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,
},
},
})
Expand Down
10 changes: 5 additions & 5 deletions gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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, "/")
Expand Down

0 comments on commit f76407c

Please sign in to comment.