Skip to content

Commit

Permalink
Merge pull request #469 from ipfs/release-v0.13.1
Browse files Browse the repository at this point in the history
Release v0.13.1
  • Loading branch information
hacdias committed Sep 21, 2023
2 parents 33ff595 + c891b6d commit e0d4b3e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ The following emojis are used to highlight certain changes:

### Security

## [v0.13.1]

### Added

* 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

### Removed

### Fixed

### Security

## [v0.13.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion gateway/assets/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="ipfs-logo">&nbsp;</div>
<nav>
<a href="https://ipfs.tech" target="_blank" rel="noopener noreferrer">About<span class="dn-mobile"> IPFS</span></a>
<a href="https://ipfs.tech#install" target="_blank" rel="noopener noreferrer">Install<span class="dn-mobile"> IPFS</span></a>
<a href="https://docs.ipfs.tech/install/" target="_blank" rel="noopener noreferrer">Install<span class="dn-mobile"> IPFS</span></a>
{{ range .Menu }}
<a href="{{ .URL }}" target="_blank" rel="noopener noreferrer">{{ .Title }}</a>
{{ end }}
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
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.13.0"
"version": "v0.13.1"
}

0 comments on commit e0d4b3e

Please sign in to comment.