Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gateway): add DisableHTMLErrors option #463

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The following emojis are used to highlight certain changes:
### Added

* ✨ The `routing/http` implements Delegated Peer Routing introduced in [IPIP-417](https://github.com/ipfs/specs/pull/417).
* An option `DisableHTMLErrors` has been added to `gateway.Config`. When this option
is `true`, pretty HTML error pages for web browsers are disabled. Instead, a
`text/plain` page with the raw error message as the body is returned.

### Changed

Expand Down
2 changes: 1 addition & 1 deletion gateway/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func webError(w http.ResponseWriter, r *http.Request, c *Config, err error, defa
code = gwErr.StatusCode
}

acceptsHTML := strings.Contains(r.Header.Get("Accept"), "text/html")
acceptsHTML := !c.DisableHTMLErrors && strings.Contains(r.Header.Get("Accept"), "text/html")
if acceptsHTML {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(code)
Expand Down
34 changes: 34 additions & 0 deletions gateway/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,38 @@ func TestWebError(t *testing.T) {
webError(w, r, config, err, http.StatusInternalServerError)
require.Equal(t, http.StatusTeapot, w.Result().StatusCode)
})

t.Run("Error is sent as HTML when 'Accept' header contains 'text/html'", func(t *testing.T) {
t.Parallel()

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
r.Header.Set("Accept", "something/else, text/html")
webError(w, r, config, NewErrorStatusCodeFromStatus(http.StatusTeapot), http.StatusInternalServerError)
require.Equal(t, http.StatusTeapot, w.Result().StatusCode)
require.Contains(t, w.Result().Header.Get("Content-Type"), "text/html")
})

t.Run("Error is sent as plain text when 'Accept' header does not contain 'text/html'", func(t *testing.T) {
t.Parallel()

w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
r.Header.Set("Accept", "application/json")
webError(w, r, config, NewErrorStatusCodeFromStatus(http.StatusTeapot), http.StatusInternalServerError)
require.Equal(t, http.StatusTeapot, w.Result().StatusCode)
require.Contains(t, w.Result().Header.Get("Content-Type"), "text/plain")
})

t.Run("Error is sent as plain text when 'Accept' header contains 'text/html' and config.DisableHTMLErrors is true", func(t *testing.T) {
t.Parallel()

config := &Config{Headers: map[string][]string{}, DisableHTMLErrors: true}
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
r.Header.Set("Accept", "something/else, text/html")
webError(w, r, config, NewErrorStatusCodeFromStatus(http.StatusTeapot), http.StatusInternalServerError)
require.Equal(t, http.StatusTeapot, w.Result().StatusCode)
require.Contains(t, w.Result().Header.Get("Content-Type"), "text/plain")
})
}
5 changes: 5 additions & 0 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ type Config struct {
// overridden per FQDN in PublicGateways. To be used with WithHostname.
NoDNSLink bool

// DisableHTMLErrors disables pretty HTML pages when an error occurs. Instead, a `text/plain`
// page will be sent with the raw error message. This can be useful if this gateway
// is being proxied by other service, which wants to use the error message.
DisableHTMLErrors bool

// PublicGateways configures the behavior of known public gateways. Each key is
// a fully qualified domain name (FQDN). To be used with WithHostname.
PublicGateways map[string]*PublicGateway
Expand Down
Loading