diff options
| author | adnano <[email protected]> | 2020-09-24 00:30:21 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-24 00:30:21 -0400 |
| commit | 63696fc7c8d8f3be972d464ae847c8f1b8fcd8fd (patch) | |
| tree | 171a8ae1ba79b07767df1f9c0b45006b8127e9cb /client.go | |
| parent | Handle more than one request at a time (diff) | |
| download | go-gemini-63696fc7c8d8f3be972d464ae847c8f1b8fcd8fd.tar.xz go-gemini-63696fc7c8d8f3be972d464ae847c8f1b8fcd8fd.zip | |
Refactor
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/client.go b/client.go deleted file mode 100644 index 01912c6..0000000 --- a/client.go +++ /dev/null @@ -1,99 +0,0 @@ -package gemini - -import ( - "bytes" - "crypto/tls" - "io/ioutil" - "strconv" - "strings" -) - -// Client is a Gemini client. -type Client struct{} - -// Request makes a request for the provided URL. The host is inferred from the URL. -func (c *Client) Request(url string) (*Response, error) { - req, err := NewRequest(url) - if err != nil { - return nil, err - } - return c.Do(req) -} - -// ProxyRequest requests the provided URL from the provided host. -func (c *Client) ProxyRequest(host, url string) (*Response, error) { - req, err := NewProxyRequest(host, url) - if err != nil { - return nil, err - } - return c.Do(req) -} - -// Do sends a Gemini request and returns a Gemini response. -func (c *Client) Do(req *Request) (*Response, error) { - host := req.Host - if strings.LastIndex(host, ":") == -1 { - // The default port is 1965 - host += ":1965" - } - - // Allow self signed certificates - config := tls.Config{} - config.InsecureSkipVerify = true - config.Certificates = req.Certificates - - conn, err := tls.Dial("tcp", host, &config) - if err != nil { - return nil, err - } - defer conn.Close() - - // Write the request - if err := req.Write(conn); err != nil { - return nil, err - } - - // Read the response - b, err := ioutil.ReadAll(conn) - if err != nil { - return nil, err - } - - // Ensure that the response is long enough - // The minimum response: <STATUS><SPACE><CR><LF> (5 bytes) - if len(b) < 5 { - return nil, ErrProtocol - } - - // Parse the response header - status, err := strconv.Atoi(string(b[:2])) - if err != nil { - return nil, err - } - - // Read one space - if b[2] != ' ' { - return nil, ErrProtocol - } - - // Find the first <CR><LF> - i := bytes.Index(b, []byte("\r\n")) - if i < 3 { - return nil, ErrProtocol - } - - // Read the meta - meta := string(b[3:i]) - if len(meta) > 1024 { - return nil, ErrProtocol - } - - // Read the response body - body := b[i+2:] - - return &Response{ - Status: status, - Meta: meta, - Body: body, - }, nil -} |