name.go 1009 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package web
  2. import (
  3. "strings"
  4. )
  5. const encodedIDPrefix = ":"
  6. var bannedNames = map[string]bool{
  7. "api": true,
  8. "edit": true,
  9. "healthz": true,
  10. "links": true,
  11. "s": true,
  12. "version": true,
  13. }
  14. // Parse the shortcut name from the given URL path, given the base URL that is
  15. // handling the request.
  16. func parseName(base, path string) string {
  17. t := path[len(base):]
  18. ix := strings.Index(t, "/")
  19. if ix == -1 {
  20. return t
  21. }
  22. return t[:ix]
  23. }
  24. // Clean a shortcut name. Currently this just means stripping any leading
  25. // ":" to avoid collisions with auto generated names.
  26. func cleanName(name string) string {
  27. for strings.HasPrefix(name, encodedIDPrefix) {
  28. name = name[1:]
  29. }
  30. return name
  31. }
  32. // Is this name one that was generated from the incrementing id.
  33. func isGenerated(name string) bool {
  34. return strings.HasPrefix(name, string(genURLPrefix))
  35. }
  36. // isBannedName indicates if the name is one that is reserved by the server?
  37. func isBannedName(name string) bool {
  38. return bannedNames[name]
  39. }