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