|
| 1 | +// ================================================================= |
| 2 | +// |
| 3 | +// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved |
| 4 | +// Released as open source under the MIT License. See LICENSE file. |
| 5 | +// |
| 6 | +// ================================================================= |
| 7 | + |
| 8 | +package http |
| 9 | + |
| 10 | +import ( |
| 11 | + "net/http" |
| 12 | + "sort" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/pkg/errors" |
| 17 | + |
| 18 | + "github.com/spatialcurrent/go-simple-serializer/pkg/registry" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + ErrMissingAcceptHeader = errors.New("missing accept header") |
| 23 | + ErrMissingRegistry = errors.New("missing file type registry") |
| 24 | +) |
| 25 | + |
| 26 | +// NegotiateFormat negotitates the format for the response based on the incoming request and the given file type registry. |
| 27 | +// Returns the matching content type, followed by the format known to GSS, and then an error if any. |
| 28 | +func NegotiateFormat(r *http.Request, reg *registry.Registry) (string, string, error) { |
| 29 | + |
| 30 | + accept := strings.TrimSpace(r.Header.Get(HeaderAccept)) |
| 31 | + |
| 32 | + if len(accept) == 0 { |
| 33 | + return "", "", ErrMissingAcceptHeader |
| 34 | + } |
| 35 | + |
| 36 | + if reg == nil { |
| 37 | + return "", "", ErrMissingRegistry |
| 38 | + } |
| 39 | + |
| 40 | + // Parse accept header into map of weights to accepted values |
| 41 | + values := map[float64][]string{} |
| 42 | + for _, str := range strings.SplitN(accept, ",", -1) { |
| 43 | + v := strings.TrimSpace(str) |
| 44 | + if strings.Contains(v, ";q=") { |
| 45 | + parts := strings.SplitN(v, ";q=", 2) |
| 46 | + w, err := strconv.ParseFloat(parts[1], 64) |
| 47 | + if err != nil { |
| 48 | + return "", "", errors.Wrapf(err, "could not parse quality value for value %q", v) |
| 49 | + } |
| 50 | + if _, ok := values[w]; !ok { |
| 51 | + values[w] = make([]string, 0) |
| 52 | + } |
| 53 | + values[w] = append(values[w], strings.TrimSpace(parts[0])) |
| 54 | + |
| 55 | + } else { |
| 56 | + if _, ok := values[1.0]; !ok { |
| 57 | + values[1.0] = make([]string, 0) |
| 58 | + } |
| 59 | + values[1.0] = append(values[1.0], v) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Create list of weights |
| 64 | + weights := make([]float64, 0, len(values)) |
| 65 | + for w := range values { |
| 66 | + weights = append(weights, w) |
| 67 | + } |
| 68 | + |
| 69 | + // Sort by weigt in descending order |
| 70 | + sort.SliceStable(weights, func(i, j int) bool { |
| 71 | + return weights[i] > weights[j] |
| 72 | + }) |
| 73 | + |
| 74 | + // Iterate through accepted values in order of highest weight first |
| 75 | + for _, w := range weights { |
| 76 | + for _, contentType := range values[w] { |
| 77 | + if item, ok := reg.LookupContentType(contentType); ok { |
| 78 | + return contentType, item.Format, nil |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return "", "", &ErrNotNegotiable{Value: accept} |
| 83 | +} |
0 commit comments