2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-02-23 14:36:17 -07:00
|
|
|
"crypto/tls"
|
2021-02-28 18:50:18 -07:00
|
|
|
"fmt"
|
2020-10-27 17:16:55 -06:00
|
|
|
"io"
|
2021-02-23 14:36:17 -07:00
|
|
|
"net"
|
2020-10-21 15:07:28 -06:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2021-02-17 11:36:16 -07:00
|
|
|
// The default media type for responses.
|
2021-03-20 10:04:42 -06:00
|
|
|
const defaultMediaType = "text/gemini"
|
2021-02-17 11:36:16 -07:00
|
|
|
|
2021-02-14 14:23:38 -07:00
|
|
|
// Response represents the response from a Gemini request.
|
|
|
|
//
|
|
|
|
// The Client returns Responses from servers once the response
|
|
|
|
// header has been received. The response body is streamed on demand
|
2021-02-24 16:50:39 -07:00
|
|
|
// as the Body field is read.
|
2020-10-21 15:07:28 -06:00
|
|
|
type Response struct {
|
2021-02-24 16:50:39 -07:00
|
|
|
// Status is the response status code.
|
|
|
|
Status Status
|
2020-10-21 15:07:28 -06:00
|
|
|
|
2021-02-24 16:50:39 -07:00
|
|
|
// Meta returns the response meta.
|
|
|
|
// For successful responses, the meta should contain the media type of the response.
|
|
|
|
// For failure responses, the meta should contain a short description of the failure.
|
|
|
|
Meta string
|
|
|
|
|
|
|
|
// Body represents the response body.
|
|
|
|
//
|
|
|
|
// The response body is streamed on demand as the Body field
|
|
|
|
// is read. If the network connection fails or the server
|
|
|
|
// terminates the response, Body.Read calls return an error.
|
|
|
|
//
|
|
|
|
// The Gemini client guarantees that Body is always
|
|
|
|
// non-nil, even on responses without a body or responses with
|
|
|
|
// a zero-length body. It is the caller's responsibility to
|
|
|
|
// close Body.
|
|
|
|
Body io.ReadCloser
|
|
|
|
|
|
|
|
conn net.Conn
|
2021-02-24 08:48:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-17 23:41:14 -07:00
|
|
|
// ReadResponse reads a Gemini response from the provided io.ReadCloser.
|
2021-02-23 19:51:28 -07:00
|
|
|
func ReadResponse(r io.ReadCloser) (*Response, error) {
|
2020-12-17 23:41:14 -07:00
|
|
|
resp := &Response{}
|
2021-03-20 12:01:45 -06:00
|
|
|
|
|
|
|
// Limit response header size
|
|
|
|
lr := io.LimitReader(r, 1029)
|
|
|
|
// Wrap the reader to remove the limit later on
|
|
|
|
wr := &struct{ io.Reader }{lr}
|
|
|
|
br := bufio.NewReader(wr)
|
2020-12-17 23:41:14 -07:00
|
|
|
|
2021-03-20 10:27:20 -06:00
|
|
|
// Read response header
|
|
|
|
b, err := br.ReadBytes('\n')
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil, ErrInvalidResponse
|
|
|
|
}
|
2020-12-17 23:41:14 -07:00
|
|
|
return nil, err
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2021-03-20 10:27:20 -06:00
|
|
|
if len(b) < 3 {
|
|
|
|
return nil, ErrInvalidResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the status
|
|
|
|
status, err := strconv.Atoi(string(b[:2]))
|
2020-10-21 15:07:28 -06:00
|
|
|
if err != nil {
|
2021-02-15 17:18:21 -07:00
|
|
|
return nil, ErrInvalidResponse
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2021-02-24 16:50:39 -07:00
|
|
|
resp.Status = Status(status)
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
// Read one space
|
2021-03-20 10:27:20 -06:00
|
|
|
if b[2] != ' ' {
|
2020-12-17 23:41:14 -07:00
|
|
|
return nil, ErrInvalidResponse
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the meta
|
2021-03-20 10:27:20 -06:00
|
|
|
meta, ok := trimCRLF(b[3:])
|
|
|
|
if !ok {
|
2020-12-17 23:41:14 -07:00
|
|
|
return nil, ErrInvalidResponse
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2021-03-20 16:29:40 -06:00
|
|
|
if len(meta) == 0 {
|
2020-12-17 23:41:14 -07:00
|
|
|
return nil, ErrInvalidResponse
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2021-03-20 10:27:20 -06:00
|
|
|
resp.Meta = string(meta)
|
2020-10-21 15:07:28 -06:00
|
|
|
|
2021-02-24 16:50:39 -07:00
|
|
|
if resp.Status.Class() == StatusSuccess {
|
2021-03-20 12:01:45 -06:00
|
|
|
// Use unlimited reader
|
|
|
|
wr.Reader = r
|
|
|
|
|
2021-03-20 11:41:53 -06:00
|
|
|
type readCloser struct {
|
|
|
|
io.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
resp.Body = readCloser{br, r}
|
2020-12-22 17:21:59 -07:00
|
|
|
} else {
|
2021-02-24 16:50:39 -07:00
|
|
|
resp.Body = nopReadCloser{}
|
2021-02-23 19:51:28 -07:00
|
|
|
r.Close()
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2020-12-17 23:41:14 -07:00
|
|
|
return resp, nil
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
2020-10-27 17:16:55 -06:00
|
|
|
|
2021-02-23 14:36:17 -07:00
|
|
|
// Conn returns the network connection on which the response was received.
|
|
|
|
func (r *Response) Conn() net.Conn {
|
|
|
|
return r.conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// TLS returns information about the TLS connection on which the
|
|
|
|
// response was received.
|
|
|
|
func (r *Response) TLS() *tls.ConnectionState {
|
|
|
|
if tlsConn, ok := r.conn.(*tls.Conn); ok {
|
|
|
|
state := tlsConn.ConnectionState()
|
|
|
|
return &state
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-28 20:21:54 -07:00
|
|
|
// WriteTo writes r to w in the Gemini response format, including the
|
2021-02-28 18:50:18 -07:00
|
|
|
// header and body.
|
|
|
|
//
|
|
|
|
// This method consults the Status, Meta, and Body fields of the response.
|
|
|
|
// The Response Body is closed after it is sent.
|
2021-02-28 20:21:54 -07:00
|
|
|
func (r *Response) WriteTo(w io.Writer) (int64, error) {
|
|
|
|
var wrote int64
|
|
|
|
n, err := fmt.Fprintf(w, "%02d %s\r\n", r.Status, r.Meta)
|
|
|
|
wrote += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
return wrote, err
|
2021-02-28 18:50:18 -07:00
|
|
|
}
|
|
|
|
if r.Body != nil {
|
|
|
|
defer r.Body.Close()
|
2021-02-28 20:21:54 -07:00
|
|
|
n, err := io.Copy(w, r.Body)
|
|
|
|
wrote += n
|
|
|
|
if err != nil {
|
|
|
|
return wrote, err
|
2021-02-28 18:50:18 -07:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 20:21:54 -07:00
|
|
|
return wrote, nil
|
2021-02-28 18:50:18 -07:00
|
|
|
}
|
|
|
|
|
2021-02-17 11:36:16 -07:00
|
|
|
// A ResponseWriter interface is used by a Gemini handler to construct
|
|
|
|
// a Gemini response.
|
|
|
|
//
|
|
|
|
// A ResponseWriter may not be used after the Handler.ServeGemini method
|
|
|
|
// has returned.
|
2021-02-23 22:13:44 -07:00
|
|
|
type ResponseWriter interface {
|
|
|
|
// SetMediaType sets the media type that will be sent by Write for a
|
2021-03-20 10:04:42 -06:00
|
|
|
// successful response. If no media type is set, a default media type of
|
|
|
|
// "text/gemini" will be used.
|
2021-02-23 22:13:44 -07:00
|
|
|
//
|
|
|
|
// Setting the media type after a call to Write or WriteHeader has
|
|
|
|
// no effect.
|
|
|
|
SetMediaType(mediatype string)
|
|
|
|
|
|
|
|
// Write writes the data to the connection as part of a Gemini response.
|
|
|
|
//
|
|
|
|
// If WriteHeader has not yet been called, Write calls WriteHeader with
|
|
|
|
// StatusSuccess and the media type set in SetMediaType before writing the data.
|
|
|
|
// If no media type was set, Write uses a default media type of
|
2021-03-20 10:04:42 -06:00
|
|
|
// "text/gemini".
|
2021-02-23 22:13:44 -07:00
|
|
|
Write([]byte) (int, error)
|
|
|
|
|
|
|
|
// WriteHeader sends a Gemini response header with the provided
|
|
|
|
// status code and meta.
|
|
|
|
//
|
|
|
|
// If WriteHeader is not called explicitly, the first call to Write
|
|
|
|
// will trigger an implicit call to WriteHeader with a successful
|
|
|
|
// status code and the media type set in SetMediaType.
|
|
|
|
//
|
|
|
|
// The provided code must be a valid Gemini status code.
|
|
|
|
// The provided meta must not be longer than 1024 bytes.
|
|
|
|
// Only one header may be written.
|
|
|
|
WriteHeader(status Status, meta string)
|
|
|
|
|
|
|
|
// Flush sends any buffered data to the client.
|
|
|
|
Flush() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type responseWriter struct {
|
2021-02-23 19:51:28 -07:00
|
|
|
bw *bufio.Writer
|
2021-02-17 11:36:16 -07:00
|
|
|
mediatype string
|
2021-01-09 22:50:35 -07:00
|
|
|
wroteHeader bool
|
|
|
|
bodyAllowed bool
|
|
|
|
}
|
|
|
|
|
2021-02-24 17:00:09 -07:00
|
|
|
func newResponseWriter(w io.Writer) *responseWriter {
|
2021-02-23 22:13:44 -07:00
|
|
|
return &responseWriter{
|
2021-02-23 19:51:28 -07:00
|
|
|
bw: bufio.NewWriter(w),
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 22:13:44 -07:00
|
|
|
func (w *responseWriter) SetMediaType(mediatype string) {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.mediatype = mediatype
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|
|
|
|
|
2021-02-23 22:13:44 -07:00
|
|
|
func (w *responseWriter) Write(b []byte) (int, error) {
|
2021-01-09 22:50:35 -07:00
|
|
|
if !w.wroteHeader {
|
2021-02-17 11:36:16 -07:00
|
|
|
meta := w.mediatype
|
|
|
|
if meta == "" {
|
|
|
|
// Use default media type
|
|
|
|
meta = defaultMediaType
|
|
|
|
}
|
|
|
|
w.WriteHeader(StatusSuccess, meta)
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|
|
|
|
if !w.bodyAllowed {
|
|
|
|
return 0, ErrBodyNotAllowed
|
|
|
|
}
|
2021-02-23 19:51:28 -07:00
|
|
|
return w.bw.Write(b)
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|
|
|
|
|
2021-02-23 22:13:44 -07:00
|
|
|
func (w *responseWriter) WriteHeader(status Status, meta string) {
|
2021-02-17 21:05:22 -07:00
|
|
|
if w.wroteHeader {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-20 14:15:26 -07:00
|
|
|
if status.Class() == StatusSuccess {
|
2021-01-09 22:50:35 -07:00
|
|
|
w.bodyAllowed = true
|
|
|
|
}
|
|
|
|
|
2021-02-23 19:51:28 -07:00
|
|
|
w.bw.WriteString(strconv.Itoa(int(status)))
|
|
|
|
w.bw.WriteByte(' ')
|
|
|
|
w.bw.WriteString(meta)
|
|
|
|
w.bw.Write(crlf)
|
2021-01-09 22:50:35 -07:00
|
|
|
w.wroteHeader = true
|
|
|
|
}
|
|
|
|
|
2021-02-23 22:13:44 -07:00
|
|
|
func (w *responseWriter) Flush() error {
|
2021-01-09 22:50:35 -07:00
|
|
|
if !w.wroteHeader {
|
2021-02-17 11:36:16 -07:00
|
|
|
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|
2021-02-23 16:45:58 -07:00
|
|
|
// Write errors from WriteHeader will be returned here.
|
2021-02-23 19:51:28 -07:00
|
|
|
return w.bw.Flush()
|
2021-01-09 22:50:35 -07:00
|
|
|
}
|