๐พ Archived View for source.community โบ ckaznocha โบ gemini โบ blob โบ main โบ url.go captured on 2023-04-26 at 13:24:23. Gemini links have been rewritten to link to archived content
โฌ ๏ธ Previous capture (2023-01-29)
โก๏ธ Next capture (2024-02-05)
-=-=-=-=-=-=-
. ,-. ,-. . . ,-. ,-. ,-. ,-. ,-. ,-,-. ,-,-. . . ,-. . |- . . `-. | | | | | | |-' | | | | | | | | | | | | | | | | | `-' `-' `-^ ' `-' `-' :: `-' `-' ' ' ' ' ' ' `-^ ' ' ' `' `-| /| `-'
git clone https://source.community/ckaznocha/gemini.git
View raw contents of /url.go (main)
โโโโโฎ 1โ package gemini 2โ 3โ import ( 4โ "fmt" 5โ "net/url" 6โ "unicode/utf8" 7โ ) 8โ 9โ // URI represents a Gemini URI. 10โ // 11โ // Resources hosted via Gemini are identified using URIs with the scheme 12โ // "gemini". This scheme is syntactically compatible with the generic URI 13โ // syntax defined in RFC 3986, but does not support all components of the 14โ // generic syntax. In particular, the authority component is allowed and 15โ // required, but its userinfo subcomponent is NOT allowed. The host 16โ // subcomponent is required. The port subcomponent is optional, with a default 17โ // value of 1965. The path, query and fragment components are allowed and have 18โ // no special meanings beyond those defined by the generic syntax. Spaces in 19โ // gemini URIs should be encoded as %20, not +. 20โ type URI struct { 21โ url *url.URL 22โ 23โ Host string 24โ Port string 25โ Path string 26โ Fragment string 27โ 28โ // Query is the decoded query section. The original query is stored in 29โ // RawQuery. 30โ Query string 31โ 32โ // RawQuery is the original query section as received by the server. Use 33โ // this in the event you need to parse a query section into a url.Values. 34โ RawQuery string 35โ } 36โ 37โ // ParseRequestURI parses a raw URI string into a Gemini URI. It returns an error if 38โ // the URI is invalid. 39โ func ParseRequestURI(rawURI string) (*URI, error) { 40โ if !utf8.ValidString(rawURI) { 41โ return nil, fmt.Errorf("%w: request URI must be valid utf-8", ErrMalformedURI) 42โ } 43โ 44โ uri, err := url.ParseRequestURI(rawURI) 45โ if err != nil { 46โ return nil, fmt.Errorf("%w: %s", ErrMalformedURI, err) 47โ } 48โ 49โ if !uri.IsAbs() { 50โ return nil, fmt.Errorf("%w: request URI cannot be relative", ErrMalformedURI) 51โ } 52โ 53โ if uri.User != nil { 54โ return nil, fmt.Errorf("%w: URI cannot have a user", ErrMalformedURI) 55โ } 56โ 57โ if uri.Host == "" { 58โ return nil, fmt.Errorf("%w: URI must have a host", ErrMalformedURI) 59โ } 60โ 61โ query, err := url.PathUnescape(uri.RawQuery) 62โ if err != nil { 63โ return nil, fmt.Errorf("%w: URI has a malformed query section: %s", ErrMalformedURI, err) 64โ } 65โ 66โ return &URI{ 67โ Host: uri.Hostname(), 68โ Port: uri.Port(), 69โ Path: uri.Path, 70โ Fragment: uri.Fragment, 71โ Query: query, 72โ RawQuery: uri.RawQuery, 73โ url: uri, 74โ }, nil 75โ } 76โ 77โ func (u *URI) String() string { 78โ if u.url == nil { 79โ u.url = &url.URL{ 80โ Scheme: "gemini", 81โ Host: u.Host, 82โ Path: u.Path, 83โ RawQuery: u.RawQuery, 84โ Fragment: u.Fragment, 85โ } 86โ } 87โ 88โ return u.url.String() 89โ } โโโโโฏ
ยท ยท ยท
ยฉ 2023 source.community