util.go 874 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package server
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. func readBoolParam(r *http.Request, defaultValue bool, names ...string) bool {
  7. value := strings.ToLower(readParam(r, names...))
  8. if value == "" {
  9. return defaultValue
  10. }
  11. return value == "1" || value == "yes" || value == "true"
  12. }
  13. func readParam(r *http.Request, names ...string) string {
  14. value := readHeaderParam(r, names...)
  15. if value != "" {
  16. return value
  17. }
  18. return readQueryParam(r, names...)
  19. }
  20. func readHeaderParam(r *http.Request, names ...string) string {
  21. for _, name := range names {
  22. value := r.Header.Get(name)
  23. if value != "" {
  24. return strings.TrimSpace(value)
  25. }
  26. }
  27. return ""
  28. }
  29. func readQueryParam(r *http.Request, names ...string) string {
  30. for _, name := range names {
  31. value := r.URL.Query().Get(strings.ToLower(name))
  32. if value != "" {
  33. return strings.TrimSpace(value)
  34. }
  35. }
  36. return ""
  37. }