diff options
Diffstat (limited to 'gemini/gemini.go')
-rw-r--r-- | gemini/gemini.go | 50 |
1 files changed, 22 insertions, 28 deletions
diff --git a/gemini/gemini.go b/gemini/gemini.go index 8f4c6ef..bd341e4 100644 --- a/gemini/gemini.go +++ b/gemini/gemini.go @@ -12,53 +12,47 @@ import ( const GeminiNetwork = "tcp" const GeminiPort = 1965 const GeminiPrefix = "gemini://" +const requestSuffix = "\r\n" -func FormatRequest(request string) []byte { - if !strings.HasPrefix(request, GeminiPrefix) { - request = GeminiPrefix + request - } - request = request + "\r\n" - return []byte(request) -} - -type GeminiConn struct { - host string - config *tls.Config +type GeminiURI struct { + host string + request []byte } -func NewGeminiConn(host string, port int) *GeminiConn { - conf := tls.Config{ - MinVersion: tls.VersionTLS12, - InsecureSkipVerify: true, +func Parse(request, host string, port int) (*GeminiURI, error) { + // add gemini:// scheme if missing + if !strings.HasPrefix(request, GeminiPrefix) { + request = GeminiPrefix + request } + // create GeminiURI if host has been specified if strings.Contains(host, ":") { - return &GeminiConn{host, &conf} - } else { - return &GeminiConn{host + ":" + strconv.Itoa(port), &conf} - } -} - -func NewGeminiConnFromRequest(request string, port int) (*GeminiConn, error) { - if !strings.HasPrefix(request, GeminiPrefix) { - request = GeminiPrefix + request + return &GeminiURI{host, []byte(request + requestSuffix)}, nil + } else if host != "" { + return &GeminiURI{host + ":" + strconv.Itoa(port), []byte(request + requestSuffix)}, nil } + // get host from request u, err := url.Parse(request) if err != nil { return nil, err } - return NewGeminiConn(u.Host, port), nil + + if strings.Contains(u.Host, ":") { + return &GeminiURI{u.Host, []byte(request + requestSuffix)}, nil + } else { + return &GeminiURI{u.Host + ":" + strconv.Itoa(port), []byte(request + requestSuffix)}, nil + } } -func (c *GeminiConn) Get(request []byte, out *os.File) error { - conn, err := tls.Dial(GeminiNetwork, c.host, c.config) +func (c *GeminiURI) Get(config *tls.Config, out *os.File) error { + conn, err := tls.Dial(GeminiNetwork, c.host, config) if err != nil { return err } defer conn.Close() - n, err := conn.Write(request) + n, err := conn.Write(c.request) if err != nil { return err } |