2020-09-21 13:48:42 -06:00
|
|
|
package gemini
|
|
|
|
|
|
|
|
import (
|
2020-09-21 19:31:09 -06:00
|
|
|
"bytes"
|
2020-09-21 13:48:42 -06:00
|
|
|
"crypto/tls"
|
|
|
|
"io/ioutil"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client is a Gemini client.
|
2020-09-21 17:17:10 -06:00
|
|
|
type Client struct{}
|
2020-09-21 13:48:42 -06:00
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
// Request makes a request for the provided URL. The host is inferred from the URL.
|
|
|
|
func (c *Client) Request(url string) (*Response, error) {
|
2020-09-21 19:31:09 -06:00
|
|
|
if len(url) > 1024 {
|
|
|
|
return nil, ErrInvalidURL
|
|
|
|
}
|
|
|
|
|
2020-09-21 13:48:42 -06:00
|
|
|
req, err := NewRequest(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:23:51 -06:00
|
|
|
// ProxyRequest requests the provided URL from the provided host.
|
|
|
|
func (c *Client) ProxyRequest(host, url string) (*Response, error) {
|
2020-09-21 19:31:09 -06:00
|
|
|
if len(url) > 1024 {
|
|
|
|
return nil, ErrInvalidURL
|
|
|
|
}
|
|
|
|
|
2020-09-21 13:48:42 -06:00
|
|
|
req, err := NewProxyRequest(host, url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:36:09 -06:00
|
|
|
// Do sends a Gemini request and returns a Gemini response.
|
2020-09-21 13:48:42 -06:00
|
|
|
func (c *Client) Do(req *Request) (*Response, error) {
|
|
|
|
host := req.Host
|
|
|
|
if strings.LastIndex(host, ":") == -1 {
|
|
|
|
// The default port is 1965
|
|
|
|
host += ":1965"
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:21:42 -06:00
|
|
|
// Allow self signed certificates
|
2020-09-21 19:31:09 -06:00
|
|
|
config := tls.Config{}
|
2020-09-21 16:21:42 -06:00
|
|
|
config.InsecureSkipVerify = true
|
2020-09-21 17:17:10 -06:00
|
|
|
config.Certificates = req.Certificates
|
2020-09-21 16:21:42 -06:00
|
|
|
|
|
|
|
conn, err := tls.Dial("tcp", host, &config)
|
2020-09-21 13:48:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// Write the request
|
2020-09-21 20:09:50 -06:00
|
|
|
if err := req.Write(conn); err != nil {
|
2020-09-21 13:48:42 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-21 19:31:09 -06:00
|
|
|
// Read the response
|
|
|
|
b, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
2020-09-21 13:48:42 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-21 19:31:09 -06:00
|
|
|
|
|
|
|
// 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]))
|
2020-09-21 13:48:42 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read one space
|
2020-09-21 19:31:09 -06:00
|
|
|
if b[2] != ' ' {
|
|
|
|
return nil, ErrProtocol
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first <CR><LF>
|
|
|
|
i := bytes.Index(b, []byte("\r\n"))
|
|
|
|
if i < 3 {
|
2020-09-21 15:23:51 -06:00
|
|
|
return nil, ErrProtocol
|
2020-09-21 13:48:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the meta
|
2020-09-21 19:31:09 -06:00
|
|
|
meta := string(b[3:i])
|
|
|
|
if len(meta) > 1024 {
|
|
|
|
return nil, ErrProtocol
|
2020-09-21 15:23:51 -06:00
|
|
|
}
|
2020-09-21 13:48:42 -06:00
|
|
|
|
|
|
|
// Read the response body
|
2020-09-21 19:31:09 -06:00
|
|
|
body := b[i+2:]
|
2020-09-21 13:48:42 -06:00
|
|
|
|
|
|
|
return &Response{
|
|
|
|
Status: status,
|
|
|
|
Meta: meta,
|
|
|
|
Body: body,
|
|
|
|
}, nil
|
|
|
|
}
|