parse.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package util
  2. import (
  3. "fmt"
  4. "net/url"
  5. "strconv"
  6. "strings"
  7. )
  8. func ParseInt(text string, defaultValue int) int {
  9. count, parseError := strconv.ParseInt(text, 10, 64)
  10. if parseError != nil {
  11. if len(text) > 0 {
  12. return 0
  13. }
  14. return defaultValue
  15. }
  16. return int(count)
  17. }
  18. func ParseUint64(text string, defaultValue uint64) uint64 {
  19. count, parseError := strconv.ParseUint(text, 10, 64)
  20. if parseError != nil {
  21. if len(text) > 0 {
  22. return 0
  23. }
  24. return defaultValue
  25. }
  26. return count
  27. }
  28. func ParseFilerUrl(entryPath string) (filerServer string, filerPort int64, path string, err error) {
  29. if !strings.HasPrefix(entryPath, "http://") && !strings.HasPrefix(entryPath, "https://") {
  30. entryPath = "http://" + entryPath
  31. }
  32. var u *url.URL
  33. u, err = url.Parse(entryPath)
  34. if err != nil {
  35. return
  36. }
  37. filerServer = u.Hostname()
  38. portString := u.Port()
  39. if portString != "" {
  40. filerPort, err = strconv.ParseInt(portString, 10, 32)
  41. }
  42. path = u.Path
  43. return
  44. }
  45. func ParseHostPort(hostPort string) (filerServer string, filerPort int64, err error) {
  46. parts := strings.Split(hostPort, ":")
  47. if len(parts) != 2 {
  48. err = fmt.Errorf("failed to parse %s\n", hostPort)
  49. return
  50. }
  51. filerPort, err = strconv.ParseInt(parts[1], 10, 64)
  52. if err == nil {
  53. filerServer = parts[0]
  54. }
  55. return
  56. }
  57. func CanonicalizeETag(etag string) string {
  58. canonicalETag := strings.TrimPrefix(etag, "\"")
  59. return strings.TrimSuffix(canonicalETag, "\"")
  60. }