github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/swarm/api/uri.go (about) 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 17 package api 18 19 import ( 20 "fmt" 21 "net/url" 22 "strings" 23 ) 24 25 // URI is a reference to content stored in swarm. 26 type URI struct { 27 // Scheme has one of the following values: 28 // 29 // * bzz - an entry in a swarm manifest 30 // * bzz-raw - raw swarm content 31 // * bzz-immutable - immutable URI of an entry in a swarm manifest 32 // (address is not resolved) 33 // * bzz-list - list of all files contained in a swarm manifest 34 // 35 // Deprecated Schemes: 36 // * bzzr - raw swarm content 37 // * bzzi - immutable URI of an entry in a swarm manifest 38 // (address is not resolved) 39 // * bzz-hash - hash of swarm content 40 // 41 Scheme string 42 43 // Addr is either a hexadecimal storage key or it an address which 44 // resolves to a storage key 45 Addr string 46 47 // Path is the path to the content within a swarm manifest 48 Path string 49 } 50 51 // Parse parses rawuri into a URI struct, where rawuri is expected to have one 52 // of the following formats: 53 // 54 // * <scheme>:/ 55 // * <scheme>:/<addr> 56 // * <scheme>:/<addr>/<path> 57 // * <scheme>:// 58 // * <scheme>://<addr> 59 // * <scheme>://<addr>/<path> 60 // 61 // with scheme one of bzz, bzz-raw, bzz-immutable, bzz-list or bzz-hash 62 // or deprecated ones bzzr and bzzi 63 func Parse(rawuri string) (*URI, error) { 64 u, err := url.Parse(rawuri) 65 if err != nil { 66 return nil, err 67 } 68 uri := &URI{Scheme: u.Scheme} 69 70 // check the scheme is valid 71 switch uri.Scheme { 72 case "bzz", "bzz-raw", "bzz-immutable", "bzz-list", "bzz-hash", "bzzr", "bzzi": 73 default: 74 return nil, fmt.Errorf("unknown scheme %q", u.Scheme) 75 } 76 77 // handle URIs like bzz://<addr>/<path> where the addr and path 78 // have already been split by url.Parse 79 if u.Host != "" { 80 uri.Addr = u.Host 81 uri.Path = strings.TrimLeft(u.Path, "/") 82 return uri, nil 83 } 84 85 // URI is like bzz:/<addr>/<path> so split the addr and path from 86 // the raw path (which will be /<addr>/<path>) 87 parts := strings.SplitN(strings.TrimLeft(u.Path, "/"), "/", 2) 88 uri.Addr = parts[0] 89 if len(parts) == 2 { 90 uri.Path = parts[1] 91 } 92 return uri, nil 93 } 94 95 func (u *URI) Raw() bool { 96 return u.Scheme == "bzz-raw" 97 } 98 99 func (u *URI) Immutable() bool { 100 return u.Scheme == "bzz-immutable" 101 } 102 103 func (u *URI) List() bool { 104 return u.Scheme == "bzz-list" 105 } 106 107 func (u *URI) DeprecatedRaw() bool { 108 return u.Scheme == "bzzr" 109 } 110 111 func (u *URI) DeprecatedImmutable() bool { 112 return u.Scheme == "bzzi" 113 } 114 115 func (u *URI) Hash() bool { 116 return u.Scheme == "bzz-hash" 117 } 118 119 func (u *URI) String() string { 120 return u.Scheme + ":/" + u.Addr + "/" + u.Path 121 }