parsers.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package server
  5. import (
  6. "apiote.xyz/p/szczanieckiej/traffic"
  7. traffic_errors "apiote.xyz/p/szczanieckiej/traffic/errors"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. func parseAccept(headers []string) (map[uint]struct{}, error) {
  14. result := map[uint]struct{}{}
  15. for _, header := range headers {
  16. if header == "" {
  17. header = "EMPTY"
  18. }
  19. header, _ := strings.CutPrefix(header, "application/")
  20. header, _ = strings.CutSuffix(header, "+bare")
  21. a, err := strconv.ParseUint(header, 10, 0)
  22. if err != nil {
  23. continue
  24. }
  25. result[uint(a)] = struct{}{}
  26. }
  27. if len(result) == 0 {
  28. return result, ServerError{
  29. code: http.StatusBadRequest,
  30. field: "Accept",
  31. value: "",
  32. }
  33. }
  34. return result, nil
  35. }
  36. func parseDate(dateString string, feedID string, t *traffic.Traffic) (traffic.Validity, time.Time, error) {
  37. versionCode, date, err := traffic.ParseDate(dateString, feedID, t)
  38. if err != nil {
  39. if _, ok := err.(traffic_errors.NoVersionError); ok {
  40. return versionCode, date, ServerError{
  41. code: http.StatusNotFound,
  42. field: "date",
  43. value: dateString,
  44. err: err,
  45. }
  46. } else {
  47. return versionCode, date, ServerError{
  48. code: http.StatusBadRequest,
  49. field: "date",
  50. value: dateString,
  51. err: err,
  52. }
  53. }
  54. }
  55. return versionCode, date, nil
  56. }