Skip to content
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
16 changes: 9 additions & 7 deletions docs/doc-site/tutorials/media-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,13 +575,15 @@ regardless of which form was registered.

## `Accept-Encoding`

[`negotiate.ContentEncoding(r, offers)`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/negotiate#ContentEncoding)
implements `Accept-Encoding` negotiation against a list of offered
encoding tokens (`gzip`, `deflate`, …). Encoding tokens have no
parameters, so the v0.30 parameter-honouring change does not apply.

The runtime itself does not transparently encode response bodies; this
helper is for handlers that want to make the choice explicitly.
The runtime does not handle `Accept-Encoding` or transparently encode
response bodies. The historical `negotiate.ContentEncoding` helper is
deprecated — it was never paired with a real encoder, so on its own it
produces no `Vary`, no `Content-Length` rewrite, and no minimum-size
guard. Compose a proper compression middleware at the `http.Handler`
level instead: the
[compression recipe](../../usage/examples/middleware/compression/) wraps
`middleware.Serve` with
[`CAFxX/httpcompression`](https://github.com/CAFxX/httpcompression).

## Common gotchas

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ The full module pulls only the standard library at runtime
no offer is acceptable, the third argument (the *default offer*) is
returned.

## Pick a Content-Encoding

{{< code file="contenttypes/negotiatestandalone/main.go" lang="go" region="pickEncoding" >}}

`""` means "no offer is acceptable" — let your handler decide
whether to send the unencoded body or 406.

## Exercise

```sh
Expand Down
2 changes: 1 addition & 1 deletion docs/doc-site/usage/server/deprecated-shims.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the right-hand column directly.
|----------------------------------------------------------|--------------------------------------------------------------------|
| `middleware.NegotiateOption` | `negotiate.Option` |
| `middleware.NegotiateContentType(r, offers, def, opts…)` | `negotiate.ContentType(r, offers, def, opts…)` |
| `middleware.NegotiateContentEncoding(r, offers)` | `negotiate.ContentEncoding(r, offers)` |
| `middleware.NegotiateContentEncoding(r, offers)` | _deprecated, no direct replacement_ — see the [compression recipe](../../examples/middleware/compression/) |
| `middleware.WithIgnoreParameters(true)` | `negotiate.WithIgnoreParameters(true)` |

Same signatures, same semantics. The deprecated forms in
Expand Down
2 changes: 1 addition & 1 deletion docs/doc-site/usage/standalone/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ go get github.com/go-openapi/runtime/server-middleware
| Package | Use it for |
|--------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`mediatype`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/mediatype) | Parsed RFC 7231 media-type values, `Set` lists and asymmetric matching — the building block both `negotiate` and the runtime's own server pipeline use. |
| [`negotiate`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/negotiate) | Server-side selection from `Accept` (`ContentType`) and `Accept-Encoding` (`ContentEncoding`). Honours MIME parameters by default; opt out with `WithIgnoreParameters`. |
| [`negotiate`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/negotiate) | Server-side selection from `Accept` via `ContentType`. Honours MIME parameters by default; opt out with `WithIgnoreParameters`. |
| [`negotiate/header`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/negotiate/header) | Low-level RFC 7231 header parsing primitives reused by `negotiate`. Use it directly if you need raw `Accept`/`Accept-Encoding` parsing without the typed media-type layer. |
| [`docui`](https://pkg.go.dev/github.com/go-openapi/runtime/server-middleware/docui) | Stdlib-only handlers that serve Swagger UI, RapiDoc or Redoc, plus the spec document itself. Mountable on any `net/http` mux. |

Expand Down
25 changes: 10 additions & 15 deletions docs/doc-site/usage/standalone/content-negotiation.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,16 @@ version params that you treat as informational, opt out per call —

{{< code file="standalone/contentnegotiation/main.go" lang="go" region="serverWideIgnoreParameters" >}}

## `ContentEncoding` — pick a response encoding

```go
func ContentEncoding(r *http.Request, offers []string) string
```

Returns the best-matching offered encoding for the request's
`Accept-Encoding` header. Two offers tied on q go to the earlier one;
no acceptable offer returns `""` (so the caller can choose to send no
encoding rather than substituting `identity`).

Encoding tokens have no parameters, so this function is unaffected by
the v0.30 parameter-honouring change.

{{< code file="standalone/contentnegotiation/main.go" lang="go" region="pickEncoding" >}}
## `Accept-Encoding` — not handled here

`negotiate.ContentEncoding` is **deprecated**. The runtime does not ship
response compression, and surfacing a half-feature negotiator without a
matching encoder leads to subtle correctness traps (no `Vary`,
no `Content-Length` rewrite, no minimum-size guard). Use a real
compression middleware at the `http.Handler` level — see the
[compression recipe](../../examples/middleware/compression/) for a worked
example using
[`CAFxX/httpcompression`](https://github.com/CAFxX/httpcompression).

## Direct header parsing

Expand Down
15 changes: 0 additions & 15 deletions docs/examples/contenttypes/negotiatestandalone/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func use(_ ...any) {}
const readHeaderTimeout = 5 * time.Second

func main() {
pickEncoding(nil, nil)
ignoreParameters(nil)
if len(os.Args) > 1 && os.Args[1] == "serve" {
pickContentType()
Expand Down Expand Up @@ -73,20 +72,6 @@ func pickContentType() {

// endsnippet:pickContentType

func pickEncoding(w http.ResponseWriter, r *http.Request) {
if r == nil || w == nil {
return
}
// snippet:pickEncoding
chosen := negotiate.ContentEncoding(r, []string{"gzip", "deflate"})
if chosen != "" {
w.Header().Set("Content-Encoding", chosen)
}
// endsnippet:pickEncoding

use(chosen)
}

func ignoreParameters(r *http.Request) {
if r == nil {
return
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/middleware/compression/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//
// The runtime does not ship compression itself; this example is a
// recipe for composing the existing ecosystem with the go-openapi
// pipeline. See server-middleware/negotiate.ContentEncoding (now
// deprecated) for context.
// pipeline.
//
// Run:
//
Expand Down
15 changes: 0 additions & 15 deletions docs/examples/standalone/contentnegotiation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ var (
)

func main() {
pickEncoding(nil, nil)
ignoreParameters(nil)
serverWideIgnoreParameters()
parseAcceptHeader(nil)
Expand Down Expand Up @@ -111,20 +110,6 @@ func serverWideIgnoreParameters() {
use(ctx)
}

func pickEncoding(w http.ResponseWriter, r *http.Request) {
if r == nil || w == nil {
return
}
// snippet:pickEncoding
chosen := negotiate.ContentEncoding(r, []string{"gzip", "deflate"})
if chosen != "" {
w.Header().Set("Content-Encoding", chosen)
}
// endsnippet:pickEncoding

use(chosen)
}

func parseAcceptHeader(r *http.Request) {
if r == nil {
return
Expand Down
Loading