/
client.go
70 lines (57 loc) · 1.74 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package stellartoml
import (
"fmt"
"io"
"net/http"
"github.com/BurntSushi/toml"
"github.com/stellar/go/address"
"github.com/stellar/go/support/errors"
)
// GetStellarToml returns stellar.toml file for a given domain
func (c *Client) GetStellarToml(domain string) (resp *Response, err error) {
var hresp *http.Response
hresp, err = c.HTTP.Get(c.url(domain))
if err != nil {
err = errors.Wrap(err, "http request errored")
return
}
defer hresp.Body.Close()
if !(hresp.StatusCode >= 200 && hresp.StatusCode < 300) {
err = errors.New("http request failed with non-200 status code")
return
}
limitReader := io.LimitReader(hresp.Body, StellarTomlMaxSize)
_, err = toml.DecodeReader(limitReader, &resp)
// There is one corner case not handled here: response is exactly
// StellarTomlMaxSize long and is incorrect toml. Check discussion:
// https://github.com/stellar/go/pull/24#discussion_r89909696
if err != nil && limitReader.(*io.LimitedReader).N == 0 {
err = errors.Errorf("stellar.toml response exceeds %d bytes limit", StellarTomlMaxSize)
return
}
if err != nil {
err = errors.Wrap(err, "toml decode failed")
return
}
return
}
// GetStellarTomlByAddress returns stellar.toml file of a domain fetched from a
// given address
func (c *Client) GetStellarTomlByAddress(addr string) (*Response, error) {
_, domain, err := address.Split(addr)
if err != nil {
return nil, errors.Wrap(err, "parse address failed")
}
return c.GetStellarToml(domain)
}
// url returns the appropriate url to load for resolving domain's stellar.toml
// file
func (c *Client) url(domain string) string {
var scheme string
if c.UseHTTP {
scheme = "http"
} else {
scheme = "https"
}
return fmt.Sprintf("%s://%s%s", scheme, domain, WellKnownPath)
}