Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
fix #91: use github.com/golang/gddo/httputil to parse Accept header
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed May 11, 2018
1 parent 4691a36 commit df6b1a2
Show file tree
Hide file tree
Showing 155 changed files with 17,679 additions and 24 deletions.
11 changes: 10 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
name = "github.com/go-chi/chi"
version = "^3.3.0"

[[constraint]]
name = "github.com/golang/gddo"
branch = "master"

[[constraint]]
name = "github.com/golang/mock"
branch = "master"
Expand All @@ -11,16 +15,16 @@
branch = "master"

[[constraint]]
name = "github.com/rubenv/sql-migrate"
name = "github.com/pkg/errors"
branch = "master"

[[constraint]]
name = "github.com/pkg/errors"
version = "*"
name = "github.com/rubenv/sql-migrate"
branch = "master"

[[constraint]]
name = "github.com/spf13/cobra"
version = "*"
branch = "master"

[[constraint]]
name = "github.com/spf13/viper"
Expand Down
28 changes: 10 additions & 18 deletions server/middleware/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@ package middleware
import (
"context"
"net/http"
"strings"

"github.com/golang/gddo/httputil"
"github.com/kamilsk/form-api/transfer/encoding"
)

// Notes:
// - related issue https://github.com/golang/go/issues/19307

// Encoder injects required response encoder to the request context.
func Encoder(next http.HandlerFunc) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
// Accept: */*
// Accept: text/html
// Accept: image/*
// Accept: text/html, application/xhtml+xml, application/xml; q=0.9, */*; q=0.8
accept := fallback(req.Header.Get("Accept"), encoding.XML)
contentType := strings.TrimSpace(strings.Split(strings.Split(accept, ";")[0], ",")[0])
var contentType string
if req.Header.Get("Accept") == "" {
contentType = encoding.XML
} else {
contentType = httputil.NegotiateContentType(req, encoding.Offers(), "")
}
if !encoding.IsSupported(contentType) {
rw.WriteHeader(http.StatusNotAcceptable)
return
}
next(rw, req.WithContext(context.WithValue(req.Context(), EncoderKey{}, encoding.NewEncoder(rw, contentType))))
}
}

func fallback(value string, fallbackValues ...string) string {
if value == "" || value == "*/*" {
for _, value := range fallbackValues {
if value != "" {
return value
}
}
}
return value
}
8 changes: 7 additions & 1 deletion transfer/encoding/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ const (
XML = "application/xml"
)

// This var is related to `Offers`.
var supported = map[string]func(io.Writer) Encoder{
HTML: func(stream io.Writer) Encoder { return htmlEncoder{stream} },
JSON: func(stream io.Writer) Encoder { return json.NewEncoder(stream) },
TEXT: func(stream io.Writer) Encoder { return yamlEncoder{stream, yaml.Marshal} },
XML: func(stream io.Writer) Encoder { return xml.NewEncoder(stream) },
}

// IsSupported returns true if provided content type is supported by encoder.
// Offers returns supported content types.
func Offers() []string {
return []string{HTML, JSON, TEXT, XML}
}

// IsSupported returns true if the provided content type is supported by encoder.
func IsSupported(contentType string) bool {
_, ok := supported[contentType]
return ok
Expand Down
21 changes: 21 additions & 0 deletions vendor/github.com/golang/gddo/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/github.com/golang/gddo/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions vendor/github.com/golang/gddo/Dockerfile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit df6b1a2

Please sign in to comment.