2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
2020-12-17 23:41:14 -07:00
|
|
|
"io"
|
2021-02-24 06:24:47 -07:00
|
|
|
"net"
|
2020-10-21 15:07:28 -06:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2021-02-14 13:50:41 -07:00
|
|
|
// A Request represents a Gemini request received by a server or to be sent
|
|
|
|
// by a client.
|
2020-10-21 15:07:28 -06:00
|
|
|
type Request struct {
|
2021-02-23 16:45:58 -07:00
|
|
|
// URL specifies the URL being requested.
|
2020-10-21 15:07:28 -06:00
|
|
|
URL *url.URL
|
|
|
|
|
2021-02-14 17:02:34 -07:00
|
|
|
// For client requests, Host optionally specifies the server to
|
2021-02-20 11:30:55 -07:00
|
|
|
// connect to. It may be of the form "host" or "host:port".
|
2021-02-14 17:02:34 -07:00
|
|
|
// If empty, the value of URL.Host is used.
|
|
|
|
// For international domain names, Host may be in Punycode or
|
|
|
|
// Unicode form. Use golang.org/x/net/idna to convert it to
|
|
|
|
// either format if needed.
|
2021-02-23 16:45:58 -07:00
|
|
|
// This field is ignored by the Gemini server.
|
2020-10-21 15:07:28 -06:00
|
|
|
Host string
|
|
|
|
|
2021-02-14 15:34:57 -07:00
|
|
|
// For client requests, Certificate optionally specifies the
|
|
|
|
// TLS certificate to present to the other side of the connection.
|
|
|
|
// This field is ignored by the Gemini server.
|
2020-10-21 15:07:28 -06:00
|
|
|
Certificate *tls.Certificate
|
2021-02-24 06:24:47 -07:00
|
|
|
|
|
|
|
conn net.Conn
|
2021-02-27 11:59:45 -07:00
|
|
|
tls *tls.ConnectionState
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
|
|
|
|
2021-02-14 17:02:34 -07:00
|
|
|
// NewRequest returns a new request.
|
2021-02-14 15:34:57 -07:00
|
|
|
// The returned Request is suitable for use with Client.Do.
|
2021-02-15 15:23:54 -07:00
|
|
|
//
|
|
|
|
// Callers should be careful that the URL query is properly escaped.
|
|
|
|
// See the documentation for QueryEscape for more information.
|
2020-10-21 15:07:28 -06:00
|
|
|
func NewRequest(rawurl string) (*Request, error) {
|
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-15 15:23:54 -07:00
|
|
|
return &Request{URL: u}, nil
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
|
|
|
|
2021-02-14 13:50:41 -07:00
|
|
|
// ReadRequest reads and parses an incoming request from r.
|
|
|
|
//
|
|
|
|
// ReadRequest is a low-level function and should only be used
|
|
|
|
// for specialized applications; most code should use the Server
|
|
|
|
// to read requests and handle them via the Handler interface.
|
2020-12-17 23:41:14 -07:00
|
|
|
func ReadRequest(r io.Reader) (*Request, error) {
|
|
|
|
// Read URL
|
2021-02-14 22:16:21 -07:00
|
|
|
r = io.LimitReader(r, 1026)
|
2021-02-14 13:50:41 -07:00
|
|
|
br := bufio.NewReaderSize(r, 1026)
|
2020-12-17 23:41:14 -07:00
|
|
|
rawurl, err := br.ReadString('\r')
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Read terminating line feed
|
|
|
|
if b, err := br.ReadByte(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if b != '\n' {
|
|
|
|
return nil, ErrInvalidRequest
|
|
|
|
}
|
|
|
|
// Trim carriage return
|
|
|
|
rawurl = rawurl[:len(rawurl)-1]
|
|
|
|
// Validate URL
|
|
|
|
if len(rawurl) > 1024 {
|
|
|
|
return nil, ErrInvalidRequest
|
|
|
|
}
|
|
|
|
u, err := url.Parse(rawurl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Request{URL: u}, nil
|
|
|
|
}
|
|
|
|
|
2021-02-14 13:50:41 -07:00
|
|
|
// Write writes a Gemini request in wire format.
|
|
|
|
// This method consults the request URL only.
|
2021-02-18 19:58:35 -07:00
|
|
|
func (r *Request) Write(w io.Writer) error {
|
|
|
|
bw := bufio.NewWriterSize(w, 1026)
|
2021-02-14 21:33:16 -07:00
|
|
|
url := r.URL.String()
|
|
|
|
if len(url) > 1024 {
|
|
|
|
return ErrInvalidRequest
|
|
|
|
}
|
2021-02-18 19:58:35 -07:00
|
|
|
if _, err := bw.WriteString(url); err != nil {
|
2020-10-21 15:07:28 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-02-18 19:58:35 -07:00
|
|
|
if _, err := bw.Write(crlf); err != nil {
|
2020-10-21 15:07:28 -06:00
|
|
|
return err
|
|
|
|
}
|
2021-02-18 19:58:35 -07:00
|
|
|
return bw.Flush()
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2021-02-24 06:24:47 -07:00
|
|
|
|
|
|
|
// Conn returns the network connection on which the request was received.
|
|
|
|
func (r *Request) Conn() net.Conn {
|
|
|
|
return r.conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS returns information about the TLS connection on which the
|
|
|
|
// request was received.
|
|
|
|
func (r *Request) TLS() *tls.ConnectionState {
|
2021-02-27 11:59:45 -07:00
|
|
|
if r.tls == nil {
|
|
|
|
if tlsConn, ok := r.conn.(*tls.Conn); ok {
|
|
|
|
state := tlsConn.ConnectionState()
|
|
|
|
r.tls = &state
|
|
|
|
}
|
2021-02-24 06:24:47 -07:00
|
|
|
}
|
2021-02-27 11:59:45 -07:00
|
|
|
return r.tls
|
2021-02-24 06:24:47 -07:00
|
|
|
}
|