2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2020-10-31 21:04:47 -06:00
|
|
|
"bytes"
|
2020-10-21 15:07:28 -06:00
|
|
|
"crypto/tls"
|
2020-10-27 17:16:55 -06:00
|
|
|
"io"
|
2020-10-31 21:04:47 -06:00
|
|
|
"io/ioutil"
|
2020-10-21 15:07:28 -06:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Response is a Gemini response.
|
|
|
|
type Response struct {
|
2020-10-31 18:32:38 -06:00
|
|
|
// Status contains the response status code.
|
2020-10-27 11:30:35 -06:00
|
|
|
Status Status
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
// Meta contains more information related to the response status.
|
|
|
|
// For successful responses, Meta should contain the mimetype of the response.
|
|
|
|
// For failure responses, Meta should contain a short description of the failure.
|
|
|
|
// Meta should not be longer than 1024 bytes.
|
|
|
|
Meta string
|
|
|
|
|
2020-10-27 17:16:55 -06:00
|
|
|
// Body contains the response body for successful responses.
|
2020-10-31 21:04:47 -06:00
|
|
|
// Body is guaranteed to be non-nil.
|
2020-10-27 17:16:55 -06:00
|
|
|
Body io.ReadCloser
|
2020-10-21 15:07:28 -06:00
|
|
|
|
2020-10-31 18:32:38 -06:00
|
|
|
// Request is the request that was sent to obtain this response.
|
2020-10-27 22:21:27 -06:00
|
|
|
Request *Request
|
|
|
|
|
2020-10-21 15:07:28 -06:00
|
|
|
// TLS contains information about the TLS connection on which the response
|
|
|
|
// was received.
|
|
|
|
TLS tls.ConnectionState
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:16:55 -06:00
|
|
|
// read reads a Gemini response from the provided io.ReadCloser.
|
|
|
|
func (resp *Response) read(rc io.ReadCloser) error {
|
|
|
|
br := bufio.NewReader(rc)
|
2020-10-21 15:07:28 -06:00
|
|
|
// Read the status
|
|
|
|
statusB := make([]byte, 2)
|
2020-10-27 17:16:55 -06:00
|
|
|
if _, err := br.Read(statusB); err != nil {
|
2020-10-21 15:07:28 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
status, err := strconv.Atoi(string(statusB))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 11:30:35 -06:00
|
|
|
resp.Status = Status(status)
|
2020-10-21 15:07:28 -06:00
|
|
|
|
|
|
|
// Disregard invalid status codes
|
|
|
|
const minStatus, maxStatus = 1, 6
|
2020-10-27 11:30:35 -06:00
|
|
|
statusClass := resp.Status.Class()
|
2020-10-21 15:07:28 -06:00
|
|
|
if statusClass < minStatus || statusClass > maxStatus {
|
|
|
|
return ErrInvalidResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read one space
|
2020-10-27 17:16:55 -06:00
|
|
|
if b, err := br.ReadByte(); err != nil {
|
2020-10-21 15:07:28 -06:00
|
|
|
return err
|
|
|
|
} else if b != ' ' {
|
|
|
|
return ErrInvalidResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the meta
|
2020-10-27 17:16:55 -06:00
|
|
|
meta, err := br.ReadString('\r')
|
2020-10-21 15:07:28 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Trim carriage return
|
|
|
|
meta = meta[:len(meta)-1]
|
|
|
|
// Ensure meta is less than or equal to 1024 bytes
|
|
|
|
if len(meta) > 1024 {
|
|
|
|
return ErrInvalidResponse
|
|
|
|
}
|
2020-10-31 18:32:38 -06:00
|
|
|
// Default mime type of text/gemini; charset=utf-8
|
|
|
|
if statusClass == StatusClassSuccess && meta == "" {
|
|
|
|
meta = "text/gemini; charset=utf-8"
|
|
|
|
}
|
2020-10-21 15:07:28 -06:00
|
|
|
resp.Meta = meta
|
|
|
|
|
|
|
|
// Read terminating newline
|
2020-10-27 17:16:55 -06:00
|
|
|
if b, err := br.ReadByte(); err != nil {
|
2020-10-21 15:07:28 -06:00
|
|
|
return err
|
|
|
|
} else if b != '\n' {
|
|
|
|
return ErrInvalidResponse
|
|
|
|
}
|
|
|
|
|
2020-10-27 11:30:35 -06:00
|
|
|
if resp.Status.Class() == StatusClassSuccess {
|
2020-10-27 17:16:55 -06:00
|
|
|
resp.Body = newReadCloserBody(br, rc)
|
2020-10-31 21:04:47 -06:00
|
|
|
} else {
|
|
|
|
resp.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
|
2020-10-21 15:07:28 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-27 17:16:55 -06:00
|
|
|
|
|
|
|
type readCloserBody struct {
|
|
|
|
br *bufio.Reader // used until empty
|
|
|
|
io.ReadCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func newReadCloserBody(br *bufio.Reader, rc io.ReadCloser) io.ReadCloser {
|
|
|
|
body := &readCloserBody{ReadCloser: rc}
|
|
|
|
if br.Buffered() != 0 {
|
|
|
|
body.br = br
|
|
|
|
}
|
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *readCloserBody) Read(p []byte) (n int, err error) {
|
|
|
|
if b.br != nil {
|
|
|
|
if n := b.br.Buffered(); len(p) > n {
|
|
|
|
p = p[:n]
|
|
|
|
}
|
|
|
|
n, err = b.br.Read(p)
|
|
|
|
if b.br.Buffered() == 0 {
|
|
|
|
b.br = nil
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
return b.ReadCloser.Read(p)
|
|
|
|
}
|