accept.go 685 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package httptoo
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/anacrolix/missinggo/mime"
  7. )
  8. func ParseAccept(line string) (parsed AcceptDirectives, err error) {
  9. dirs := strings.Split(line, ",")
  10. for _, d := range dirs {
  11. p := AcceptDirective{
  12. Q: 1,
  13. }
  14. ss := strings.Split(d, ";")
  15. switch len(ss) {
  16. case 2:
  17. p.Q, err = strconv.ParseFloat(ss[1], 32)
  18. if err != nil {
  19. return
  20. }
  21. fallthrough
  22. case 1:
  23. p.MimeType.FromString(ss[0])
  24. default:
  25. err = fmt.Errorf("error parsing %q", d)
  26. return
  27. }
  28. parsed = append(parsed, p)
  29. }
  30. return
  31. }
  32. type (
  33. AcceptDirectives []AcceptDirective
  34. AcceptDirective struct {
  35. MimeType mime.Type
  36. Q float64
  37. }
  38. )