go-gemini/request.go

136 lines
3.8 KiB
Go
Raw Normal View History

2020-10-24 13:15:32 -06:00
package gemini
2020-10-21 15:07:28 -06:00
import (
"bufio"
2020-11-25 22:42:25 -07:00
"context"
2020-10-21 15:07:28 -06:00
"crypto/tls"
"io"
2020-10-21 15:07:28 -06:00
"net"
"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.
//
// The field semantics differ slightly between client and server usage.
2020-10-21 15:07:28 -06:00
type Request struct {
2021-02-14 13:50:41 -07:00
// URL specifies the URL being requested (for server
// requests) or the URL to access (for client requests).
2020-10-21 15:07:28 -06:00
URL *url.URL
2021-02-14 13:50:41 -07:00
// For client requests, Host specifies the server to connect to.
2020-10-21 15:07:28 -06:00
// Host must contain a port.
2021-02-14 13:50:41 -07:00
// This field is ignored by the Gemini server.
2020-10-21 15:07:28 -06:00
Host string
// 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-14 13:50:41 -07:00
// RemoteAddr allows Gemini servers and other software to record
// the network address that sent the request, usually for
// logging. This field is not filled in by ReadRequest and
// has no defined format. The Gemini server in this package
// sets RemoteAddr to an "IP:port" address before invoking a
// handler.
// This field is ignored by the Gemini client.
2020-10-21 15:07:28 -06:00
RemoteAddr net.Addr
2021-02-14 13:50:41 -07:00
// TLS allows Gemini servers and other software to record
// information about the TLS connection on which the request
// was received. This field is not filled in by ReadRequest.
// The Gemini server in this package sets the field for
// TLS-enabled connections before invoking a handler;
// otherwise it leaves the field nil.
// This field is ignored by the Gemini client.
TLS *tls.ConnectionState
2020-11-25 22:42:25 -07:00
// Context specifies the context to use for outgoing requests.
// The context controls the entire lifetime of a request and its
// response: obtaining a connection, sending the request, and
// reading the response header and body.
2020-12-17 15:16:55 -07:00
// If Context is nil, the background context will be used.
2021-02-14 13:50:41 -07:00
// This field is ignored by the Gemini server.
2020-11-25 22:42:25 -07:00
Context context.Context
2020-10-21 15:07:28 -06:00
}
2020-10-27 11:27:52 -06:00
// NewRequest returns a new request. The host is inferred from the URL.
//
// The returned Request is suitable for use with Client.Do.
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
}
2020-11-04 21:46:05 -07:00
return NewRequestFromURL(u), nil
2020-10-27 11:27:52 -06:00
}
2020-10-21 15:07:28 -06:00
2020-10-27 11:27:52 -06:00
// NewRequestFromURL returns a new request for the given URL.
// The host is inferred from the URL.
2020-11-27 20:26:22 -07:00
//
// Callers should be careful that the URL query is properly escaped.
// See the documentation for QueryEscape for more information.
2020-11-04 21:46:05 -07:00
func NewRequestFromURL(url *url.URL) *Request {
2020-10-27 11:27:52 -06:00
host := url.Host
if url.Port() == "" {
2020-10-21 15:07:28 -06:00
host += ":1965"
}
return &Request{
2020-12-17 15:16:55 -07:00
URL: url,
Host: host,
2020-11-04 21:46:05 -07:00
}
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.
func ReadRequest(r io.Reader) (*Request, error) {
// Read URL
2021-02-14 13:50:41 -07:00
br := bufio.NewReaderSize(r, 1026)
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
}
if u.User != nil {
// User is not allowed
return nil, ErrInvalidURL
}
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.
func (r *Request) Write(w *bufio.Writer) error {
2020-10-21 15:07:28 -06:00
url := r.URL.String()
// User is invalid
if r.URL.User != nil || len(url) > 1024 {
return ErrInvalidURL
}
if _, err := w.WriteString(url); err != nil {
return err
}
if _, err := w.Write(crlf); err != nil {
return err
}
return nil
}