uri.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "fmt"
  19. "net/url"
  20. "strings"
  21. )
  22. // URI is a reference to content stored in swarm.
  23. type URI struct {
  24. // Scheme has one of the following values:
  25. //
  26. // * bzz - an entry in a swarm manifest
  27. // * bzz-raw - raw swarm content
  28. // * bzz-immutable - immutable URI of an entry in a swarm manifest
  29. // (address is not resolved)
  30. // * bzz-list - list of all files contained in a swarm manifest
  31. //
  32. // Deprecated Schemes:
  33. // * bzzr - raw swarm content
  34. // * bzzi - immutable URI of an entry in a swarm manifest
  35. // (address is not resolved)
  36. // * bzz-hash - hash of swarm content
  37. //
  38. Scheme string
  39. // Addr is either a hexadecimal storage key or it an address which
  40. // resolves to a storage key
  41. Addr string
  42. // Path is the path to the content within a swarm manifest
  43. Path string
  44. }
  45. // Parse parses rawuri into a URI struct, where rawuri is expected to have one
  46. // of the following formats:
  47. //
  48. // * <scheme>:/
  49. // * <scheme>:/<addr>
  50. // * <scheme>:/<addr>/<path>
  51. // * <scheme>://
  52. // * <scheme>://<addr>
  53. // * <scheme>://<addr>/<path>
  54. //
  55. // with scheme one of bzz, bzz-raw, bzz-immutable, bzz-list or bzz-hash
  56. // or deprecated ones bzzr and bzzi
  57. func Parse(rawuri string) (*URI, error) {
  58. u, err := url.Parse(rawuri)
  59. if err != nil {
  60. return nil, err
  61. }
  62. uri := &URI{Scheme: u.Scheme}
  63. // check the scheme is valid
  64. switch uri.Scheme {
  65. case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzz-hash", "bzzr", "bzzi":
  66. default:
  67. return nil, fmt.Errorf("unknown scheme %q", u.Scheme)
  68. }
  69. // handle URIs like bzz://<addr>/<path> where the addr and path
  70. // have already been split by url.Parse
  71. if u.Host != "" {
  72. uri.Addr = u.Host
  73. uri.Path = strings.TrimLeft(u.Path, "/")
  74. return uri, nil
  75. }
  76. // URI is like bzz:/<addr>/<path> so split the addr and path from
  77. // the raw path (which will be /<addr>/<path>)
  78. parts := strings.SplitN(strings.TrimLeft(u.Path, "/"), "/", 2)
  79. uri.Addr = parts[0]
  80. if len(parts) == 2 {
  81. uri.Path = parts[1]
  82. }
  83. return uri, nil
  84. }
  85. func (u *URI) Raw() bool {
  86. return u.Scheme == "bzz-raw"
  87. }
  88. func (u *URI) Immutable() bool {
  89. return u.Scheme == "bzz-immutable"
  90. }
  91. func (u *URI) List() bool {
  92. return u.Scheme == "bzz-list"
  93. }
  94. func (u *URI) DeprecatedRaw() bool {
  95. return u.Scheme == "bzzr"
  96. }
  97. func (u *URI) DeprecatedImmutable() bool {
  98. return u.Scheme == "bzzi"
  99. }
  100. func (u *URI) Hash() bool {
  101. return u.Scheme == "bzz-hash"
  102. }
  103. func (u *URI) String() string {
  104. return u.Scheme + ":/" + u.Addr + "/" + u.Path
  105. }