49 Commits

Author SHA1 Message Date
Adnan Maolood
25f441f573 fs: Remove build constraint 2021-02-24 11:01:02 -05:00
Adnan Maolood
cb7879c966 Add NewResponse function 2021-02-24 10:48:17 -05:00
Adnan Maolood
19bfca1cc3 examples/server: Cancel context 2021-02-24 10:27:28 -05:00
Adnan Maolood
991b18d526 examples/server: Add logging middleware 2021-02-24 10:25:08 -05:00
Adnan Maolood
b66b287f94 ResponseWriter: Remove unexported method 2021-02-24 10:01:46 -05:00
Adnan Maolood
bd29d76f66 client: Fix copying of request 2021-02-24 09:48:23 -05:00
Adnan Maolood
1d20a6c3c8 examples/client: Use Response methods 2021-02-24 09:22:25 -05:00
Adnan Maolood
6f46b2fa47 examples/auth: Use Request.TLS method 2021-02-24 09:22:01 -05:00
Adnan Maolood
15385e3095 fs: Fix Go build constraint comment 2021-02-24 09:00:28 -05:00
Adnan Maolood
3101856afa response: Move to methods 2021-02-24 08:43:57 -05:00
Adnan Maolood
094c16297b server: Fix comment 2021-02-24 08:38:08 -05:00
Adnan Maolood
08f5ddd41a TimeoutHandler: Mention returned error 2021-02-24 08:37:52 -05:00
Adnan Maolood
41c95add81 Add unexported method to timeout writer 2021-02-24 08:34:26 -05:00
Adnan Maolood
de339490f4 Move ResponseWriter Conn and TLS methods to Request 2021-02-24 08:24:49 -05:00
Adnan Maolood
b488146cc6 Remove ResponseWriter.Hijack method 2021-02-24 08:22:12 -05:00
Adnan Maolood
069b473c28 Implement TimeoutHandler by wrapping ResponseWriter 2021-02-24 08:18:23 -05:00
Kaleb Elwert
2c2d74bcb2 Only use fs.go when fs.FS is available 2021-02-24 07:41:53 -05:00
Adnan Maolood
3660698a4b Make ResponseWriter an interface
Make ResponseWriter an interface with an unexported method. Implementors
must embed a ResponseWriter from elsewhere. This gives us the
flexibility of an interface while allowing us to add new methods in the
future.
2021-02-24 00:13:46 -05:00
Adnan Maolood
526d232ab0 Remove ErrHandlerTimeout 2021-02-23 22:15:19 -05:00
Adnan Maolood
f08efa330f Move TimeoutHandler to handler.go 2021-02-23 22:12:04 -05:00
Adnan Maolood
310bd16344 Unexport NewResponseWriter 2021-02-23 22:02:47 -05:00
Adnan Maolood
9eae88f00c Reimplement TimeoutHandler 2021-02-23 21:59:16 -05:00
Adnan Maolood
b386a9ba41 response: Change field names 2021-02-23 21:51:42 -05:00
Adnan Maolood
f28a63ff0c Add ResponseWriter.Hijack method 2021-02-23 21:36:29 -05:00
Adnan Maolood
d35dd3d867 ResponseWriter: Add TLS and Conn methods 2021-02-23 20:59:04 -05:00
Adnan Maolood
75abb99518 request: Remove TLS and Conn methods 2021-02-23 20:57:53 -05:00
Adnan Maolood
e8d98ef4ec Move I/O utilities to io.go 2021-02-23 20:49:55 -05:00
Adnan Maolood
a65c3c3d4f Make ResponseWriter a struct
Make ResponseWriter a struct again so that it can be extended in a
backwards-compatible way.
2021-02-23 20:41:16 -05:00
Adnan Maolood
64f9381bbc handler: Mention when the context is canceled 2021-02-23 19:01:12 -05:00
Adnan Maolood
a34cf6dd1b handler: Mention ResponseWriter.Close method 2021-02-23 18:59:20 -05:00
Adnan Maolood
b3e8d9ccf3 client: Clarify usage of contexts 2021-02-23 18:56:18 -05:00
Adnan Maolood
a7c449a3cf Use HandlerFunc to implement StatusHandler 2021-02-23 18:52:00 -05:00
Adnan Maolood
02bbedc330 Update documentation 2021-02-23 18:45:58 -05:00
Adnan Maolood
5cf936d304 Update documentation 2021-02-23 17:52:47 -05:00
Adnan Maolood
f1f933925c Update examples/client.go 2021-02-23 17:52:34 -05:00
Adnan Maolood
e1c04ee605 Make Response an io.ReadCloser 2021-02-23 17:50:47 -05:00
Adnan Maolood
ae3fc2fc73 response: Add Close method 2021-02-23 17:32:23 -05:00
Adnan Maolood
311233a012 request: Fix documentation for TLS 2021-02-23 17:30:46 -05:00
Adnan Maolood
c688defefd request: Add Conn and TLS methods 2021-02-23 17:29:50 -05:00
Adnan Maolood
83c904913f response: Add Conn and TLS methods 2021-02-23 16:36:17 -05:00
Adnan Maolood
833edaf63d server: Cancel context on IO errors 2021-02-23 16:06:57 -05:00
Adnan Maolood
d07e9d99d1 client: Move context handling to do 2021-02-23 16:01:29 -05:00
Adnan Maolood
31e16d5a4c examples/client: Stream response body 2021-02-23 15:56:44 -05:00
Adnan Maolood
9974071657 client: Cancel context on IO errors
Also close the connection when the context expires.
2021-02-23 15:52:47 -05:00
Adnan Maolood
09e3393e4c examples/stream: Simplify 2021-02-23 15:31:41 -05:00
Adnan Maolood
1aa85d0683 timeout: Don't recover from panics 2021-02-23 15:30:44 -05:00
Adnan Maolood
62e22b4cf2 response: Remove TLS field 2021-02-23 15:29:27 -05:00
Adnan Maolood
eee7156b3a Update documentation 2021-02-23 14:29:37 -05:00
Adnan Maolood
d8b5fa716a client: Use present tense in documentation 2021-02-23 12:21:05 -05:00
16 changed files with 440 additions and 339 deletions

View File

@@ -14,10 +14,10 @@ import (
// A Client is a Gemini client. Its zero value is a usable client.
type Client struct {
// TrustCertificate is called to determine whether the client
// should trust the certificate provided by the server.
// If TrustCertificate is nil, the client will accept any certificate.
// If the returned error is not nil, the certificate will not be trusted
// TrustCertificate is called to determine whether the client should
// trust the certificate provided by the server.
// If TrustCertificate is nil or returns nil, the client will accept
// any certificate. Otherwise, the certificate will not be trusted
// and the request will be aborted.
//
// See the tofu submodule for an implementation of trust on first use.
@@ -29,14 +29,14 @@ type Client struct {
}
// Get sends a Gemini request for the given URL.
// If the provided context is canceled or times out, the request
// will be aborted and the context's error will be returned.
// 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.
//
// An error is returned if there was a Gemini protocol error.
// A non-2x status code doesn't cause an error.
//
// If the returned error is nil, the Response will contain a non-nil Body
// which the user is expected to close.
// If the returned error is nil, the user is expected to close the Response.
//
// For more control over requests, use NewRequest and Client.Do.
func (c *Client) Get(ctx context.Context, url string) (*Response, error) {
@@ -48,16 +48,14 @@ func (c *Client) Get(ctx context.Context, url string) (*Response, error) {
}
// Do sends a Gemini request and returns a Gemini response.
// If the provided context is canceled or times out, the request
// will be aborted and the context's error will be returned.
// 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.
//
// An error is returned if there was a Gemini protocol error.
// A non-2x status code doesn't cause an error.
//
// If the returned error is nil, the Response will contain a non-nil Body
// which the user is expected to close.
//
// Generally Get will be used instead of Do.
// If the returned error is nil, the user is expected to close the Response.
func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
if ctx == nil {
panic("nil context")
@@ -79,9 +77,10 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
// Use the new URL in the request so that the server gets
// the punycoded hostname
req = &Request{
URL: u,
}
r := new(Request)
*r = *req
r.URL = u
req = r
}
// Use request host if provided
@@ -124,7 +123,7 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
res := make(chan result, 1)
go func() {
resp, err := c.do(conn, req)
resp, err := c.do(ctx, conn, req)
res <- result{resp, err}
}()
@@ -137,23 +136,33 @@ func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
}
}
func (c *Client) do(conn net.Conn, req *Request) (*Response, error) {
func (c *Client) do(ctx context.Context, conn net.Conn, req *Request) (*Response, error) {
ctx, cancel := context.WithCancel(ctx)
done := ctx.Done()
w := &contextWriter{
ctx: ctx,
done: done,
cancel: cancel,
wc: conn,
}
rc := &contextReader{
ctx: ctx,
done: done,
cancel: cancel,
rc: conn,
}
// Write the request
if err := req.Write(conn); err != nil {
if err := req.Write(w); err != nil {
return nil, err
}
// Read the response
resp, err := ReadResponse(conn)
resp, err := ReadResponse(rc)
if err != nil {
return nil, err
}
// Store TLS connection state
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
resp.TLS = &state
}
resp.conn = conn
return resp, nil
}

11
doc.go
View File

@@ -4,11 +4,12 @@ Package gemini provides Gemini client and server implementations.
Client is a Gemini client.
client := &gemini.Client{}
resp, err := client.Get("gemini://example.com")
ctx := context.Background()
resp, err := client.Get(ctx, "gemini://example.com")
if err != nil {
// handle error
}
defer resp.Body.Close()
defer resp.Close()
// ...
Server is a Gemini server.
@@ -21,11 +22,12 @@ Server is a Gemini server.
Servers should be configured with certificates:
certificates := &certificate.Store{}
certificates.Register("localhost")
err := certificates.Load("/var/lib/gemini/certs")
if err != nil {
// handle error
}
server.GetCertificate = certificates.GetCertificate
server.GetCertificate = certificates.Get
ServeMux is a Gemini request multiplexer.
ServeMux can handle requests for multiple hosts and schemes.
@@ -44,7 +46,8 @@ ServeMux can handle requests for multiple hosts and schemes.
To start the server, call ListenAndServe:
err := server.ListenAndServe(context.Background())
ctx := context.Background()
err := server.ListenAndServe(ctx)
if err != nil {
// handle error
}

View File

@@ -52,11 +52,12 @@ func fingerprint(cert *x509.Certificate) string {
}
func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
if len(r.TLS.PeerCertificates) == 0 {
tls := r.TLS()
if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return
}
fingerprint := fingerprint(r.TLS.PeerCertificates[0])
fingerprint := fingerprint(tls.PeerCertificates[0])
user, ok := users[fingerprint]
if !ok {
user = &User{}
@@ -67,7 +68,8 @@ func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
}
func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
if len(r.TLS.PeerCertificates) == 0 {
tls := r.TLS()
if len(tls.PeerCertificates) == 0 {
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
return
}
@@ -77,7 +79,7 @@ func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Requ
w.WriteHeader(gemini.StatusInput, "Username")
return
}
fingerprint := fingerprint(r.TLS.PeerCertificates[0])
fingerprint := fingerprint(tls.PeerCertificates[0])
user, ok := users[fingerprint]
if !ok {
user = &User{}

View File

@@ -97,14 +97,15 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
client := gemini.Client{
TrustCertificate: trustCertificate,
}
resp, err := client.Do(context.Background(), req)
ctx := context.Background()
resp, err := client.Do(ctx, req)
if err != nil {
return resp, err
}
switch resp.Status.Class() {
switch resp.Status().Class() {
case gemini.StatusInput:
input, ok := getInput(resp.Meta, resp.Status == gemini.StatusSensitiveInput)
input, ok := getInput(resp.Meta(), resp.Status() == gemini.StatusSensitiveInput)
if !ok {
break
}
@@ -118,7 +119,7 @@ func do(req *gemini.Request, via []*gemini.Request) (*gemini.Response, error) {
return resp, errors.New("too many redirects")
}
target, err := url.Parse(resp.Meta)
target, err := url.Parse(resp.Meta())
if err != nil {
return resp, err
}
@@ -152,15 +153,14 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
defer resp.Close()
// Handle response
if resp.Status.Class() == gemini.StatusSuccess {
body, err := io.ReadAll(resp.Body)
if resp.Status().Class() == gemini.StatusSuccess {
_, err := io.Copy(os.Stdout, resp)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(body))
} else {
fmt.Printf("%d %s\n", resp.Status, resp.Meta)
os.Exit(1)

View File

@@ -26,7 +26,7 @@ func main() {
mux.Handle("/", gemini.FileServer(os.DirFS("/var/www")))
server := &gemini.Server{
Handler: mux,
Handler: logMiddleware(mux),
ReadTimeout: 30 * time.Second,
WriteTimeout: 1 * time.Minute,
GetCertificate: certificates.Get,
@@ -48,10 +48,61 @@ func main() {
case <-c:
// Shutdown the server
log.Println("Shutting down...")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
}
}
func logMiddleware(h gemini.Handler) gemini.Handler {
return gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
lw := &logResponseWriter{rw: w}
h.ServeGemini(ctx, lw, r)
host := r.TLS().ServerName
log.Printf("gemini: %s %q %d %d", host, r.URL, lw.status, lw.wrote)
})
}
type logResponseWriter struct {
rw gemini.ResponseWriter
status gemini.Status
meta string
mediatype string
wroteHeader bool
wrote int
}
func (w *logResponseWriter) SetMediaType(mediatype string) {
w.mediatype = mediatype
}
func (w *logResponseWriter) Write(b []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(gemini.StatusSuccess, w.mediatype)
}
n, err := w.rw.Write(b)
w.wrote += n
return n, err
}
func (w *logResponseWriter) WriteHeader(status gemini.Status, meta string) {
if w.wroteHeader {
return
}
w.status = status
w.meta = meta
w.wroteHeader = true
w.rw.WriteHeader(status, meta)
w.wrote += len(meta) + 5
}
func (w *logResponseWriter) Flush() error {
return nil
}
func (w *logResponseWriter) Close() error {
return nil
}

View File

@@ -39,33 +39,16 @@ func main() {
// stream writes an infinite stream to w.
func stream(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
ch := make(chan string)
ctx, cancel := context.WithCancel(ctx)
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
ch <- fmt.Sprint(time.Now().UTC())
}
time.Sleep(time.Second)
}
// Close channel when finished.
// In this example this will never be reached.
close(ch)
}(ctx)
for {
s, ok := <-ch
if !ok {
break
select {
case <-ctx.Done():
return
default:
}
fmt.Fprintln(w, s)
fmt.Fprintln(w, time.Now().UTC())
if err := w.Flush(); err != nil {
cancel()
return
}
time.Sleep(time.Second)
}
}

6
fs.go
View File

@@ -65,8 +65,8 @@ func serveContent(w ResponseWriter, name string, content io.Reader) {
//
// As a precaution, ServeFile will reject requests where r.URL.Path contains a
// ".." path element; this protects against callers who might unsafely use
// filepath.Join on r.URL.Path without sanitizing it and then use that
// filepath.Join result as the name argument.
// path.Join on r.URL.Path without sanitizing it and then use that
// path.Join result as the name argument.
//
// As another special case, ServeFile redirects any request where r.URL.Path
// ends in "/index.gmi" to the same path, without the final "index.gmi". To
@@ -81,7 +81,7 @@ func ServeFile(w ResponseWriter, r *Request, fsys fs.FS, name string) {
// serveFile. Reject the request under the assumption that happened
// here and ".." may not be wanted.
// Note that name might not contain "..", for example if code (still
// incorrectly) used filepath.Join(myDir, r.URL.Path).
// incorrectly) used path.Join(myDir, r.URL.Path).
w.WriteHeader(StatusBadRequest, "invalid URL path")
return
}

View File

@@ -16,8 +16,4 @@ var (
// ErrBodyNotAllowed is returned by ResponseWriter.Write calls
// when the response status code does not permit a body.
ErrBodyNotAllowed = errors.New("gemini: response status code does not allow body")
// ErrHandlerTimeout is returned on ResponseWriter Write calls
// in handlers which have timed out.
ErrHandlerTimeout = errors.New("gemini: Handler timeout")
)

View File

@@ -1,9 +1,12 @@
package gemini
import (
"bytes"
"context"
"io"
"net/url"
"strings"
"time"
)
// A Handler responds to a Gemini request.
@@ -11,7 +14,11 @@ import (
// ServeGemini should write the response header and data to the ResponseWriter
// and then return. Returning signals that the request is finished; it is not
// valid to use the ResponseWriter after or concurrently with the completion
// of the ServeGemini call.
// of the ServeGemini call. Handlers may also call ResponseWriter.Close to
// manually close the connection.
//
// The provided context is canceled when the client's connection is closed,
// when ResponseWriter.Close is called, or when the ServeGemini method returns.
//
// Handlers should not modify the provided Request.
type Handler interface {
@@ -31,16 +38,9 @@ func (f HandlerFunc) ServeGemini(ctx context.Context, w ResponseWriter, r *Reque
// StatusHandler returns a request handler that responds to each request
// with the provided status code and meta.
func StatusHandler(status Status, meta string) Handler {
return &statusHandler{status, meta}
}
type statusHandler struct {
status Status
meta string
}
func (h *statusHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
w.WriteHeader(h.status, h.meta)
return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
w.WriteHeader(status, meta)
})
}
// NotFoundHandler returns a simple request handler that replies to each
@@ -75,3 +75,86 @@ func StripPrefix(prefix string, h Handler) Handler {
}
})
}
// TimeoutHandler returns a Handler that runs h with the given time limit.
//
// The new Handler calls h.ServeGemini to handle each request, but
// if a call runs for longer than its time limit, the handler responds with a
// 40 Temporary Failure error. After such a timeout, writes by h to
// its ResponseWriter will return context.DeadlineExceeded.
func TimeoutHandler(h Handler, dt time.Duration) Handler {
return &timeoutHandler{
h: h,
dt: dt,
}
}
type timeoutHandler struct {
h Handler
dt time.Duration
}
func (t *timeoutHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
ctx, cancel := context.WithTimeout(ctx, t.dt)
defer cancel()
buf := &bytes.Buffer{}
tw := &timeoutWriter{
wc: &contextWriter{
ctx: ctx,
cancel: cancel,
done: ctx.Done(),
wc: nopCloser{buf},
},
}
done := make(chan struct{})
go func() {
t.h.ServeGemini(ctx, tw, r)
close(done)
}()
select {
case <-done:
w.WriteHeader(tw.status, tw.meta)
w.Write(buf.Bytes())
case <-ctx.Done():
w.WriteHeader(StatusTemporaryFailure, "Timeout")
}
}
type timeoutWriter struct {
wc io.WriteCloser
status Status
meta string
mediatype string
wroteHeader bool
}
func (w *timeoutWriter) SetMediaType(mediatype string) {
w.mediatype = mediatype
}
func (w *timeoutWriter) Write(b []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(StatusSuccess, w.mediatype)
}
return w.wc.Write(b)
}
func (w *timeoutWriter) WriteHeader(status Status, meta string) {
if w.wroteHeader {
return
}
w.status = status
w.meta = meta
w.wroteHeader = true
}
func (w *timeoutWriter) Flush() error {
return nil
}
func (w *timeoutWriter) Close() error {
return w.wc.Close()
}

104
io.go Normal file
View File

@@ -0,0 +1,104 @@
package gemini
import (
"bufio"
"context"
"io"
)
type contextReader struct {
ctx context.Context
done <-chan struct{}
cancel func()
rc io.ReadCloser
}
func (r *contextReader) Read(p []byte) (int, error) {
select {
case <-r.done:
r.rc.Close()
return 0, r.ctx.Err()
default:
}
n, err := r.rc.Read(p)
if err != nil {
r.cancel()
}
return n, err
}
func (r *contextReader) Close() error {
r.cancel()
return r.rc.Close()
}
type contextWriter struct {
ctx context.Context
done <-chan struct{}
cancel func()
wc io.WriteCloser
}
func (w *contextWriter) Write(b []byte) (int, error) {
select {
case <-w.done:
w.wc.Close()
return 0, w.ctx.Err()
default:
}
n, err := w.wc.Write(b)
if err != nil {
w.cancel()
}
return n, err
}
func (w *contextWriter) Close() error {
w.cancel()
return w.wc.Close()
}
type nopCloser struct {
io.Writer
}
func (nopCloser) Close() error {
return nil
}
type nopReadCloser struct{}
func (nopReadCloser) Read(p []byte) (int, error) {
return 0, io.EOF
}
func (nopReadCloser) Close() error {
return nil
}
type bufReadCloser struct {
br *bufio.Reader // used until empty
io.ReadCloser
}
func newBufReadCloser(br *bufio.Reader, rc io.ReadCloser) io.ReadCloser {
body := &bufReadCloser{ReadCloser: rc}
if br.Buffered() != 0 {
body.br = br
}
return body
}
func (b *bufReadCloser) 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)
}

View File

@@ -12,7 +12,8 @@ func QueryEscape(query string) string {
return strings.ReplaceAll(url.PathEscape(query), "+", "%2B")
}
// QueryUnescape is identical to url.PathUnescape.
// QueryUnescape unescapes a Gemini URL query.
// It is identical to url.PathUnescape.
func QueryUnescape(query string) (string, error) {
return url.PathUnescape(query)
}

View File

@@ -10,11 +10,8 @@ import (
// 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.
type Request struct {
// URL specifies the URL being requested (for server
// requests) or the URL to access (for client requests).
// URL specifies the URL being requested.
URL *url.URL
// For client requests, Host optionally specifies the server to
@@ -23,9 +20,7 @@ type Request struct {
// 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.
//
// For server requests, Host specifies the host on which the URL
// is sought.
// This field is ignored by the Gemini server.
Host string
// For client requests, Certificate optionally specifies the
@@ -33,24 +28,10 @@ type Request struct {
// This field is ignored by the Gemini server.
Certificate *tls.Certificate
// 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.
// This field is ignored by the Gemini client.
RemoteAddr net.Addr
// 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
conn net.Conn
}
// NewRequest returns a new request.
//
// The returned Request is suitable for use with Client.Do.
//
// Callers should be careful that the URL query is properly escaped.
@@ -111,3 +92,18 @@ func (r *Request) Write(w io.Writer) error {
}
return bw.Flush()
}
// 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 {
if tlsConn, ok := r.conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
return &state
}
return nil
}

View File

@@ -3,8 +3,8 @@ package gemini
import (
"bufio"
"crypto/tls"
"fmt"
"io"
"net"
"strconv"
)
@@ -15,38 +15,30 @@ const defaultMediaType = "text/gemini; charset=utf-8"
//
// The Client returns Responses from servers once the response
// header has been received. The response body is streamed on demand
// as the Body field is read.
// as the response is read. If the network connection fails or the server
// terminates the response, Read calls return an error.
//
// It is the caller's responsibility to close the response.
type Response struct {
// Status contains the response status code.
Status Status
status Status
meta string
body io.ReadCloser
conn net.Conn
}
// Meta contains more information related to the response status.
// For successful responses, Meta should contain the media type 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
// 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
// TLS contains information about the TLS connection on which the
// response was received. It is nil for unencrypted responses.
TLS *tls.ConnectionState
// NewResponse returns a new response with the provided status, meta, and body.
func NewResponse(status Status, meta string, body io.ReadCloser) *Response {
return &Response{
status: status,
meta: meta,
body: body,
}
}
// ReadResponse reads a Gemini response from the provided io.ReadCloser.
func ReadResponse(rc io.ReadCloser) (*Response, error) {
func ReadResponse(r io.ReadCloser) (*Response, error) {
resp := &Response{}
br := bufio.NewReader(rc)
br := bufio.NewReader(r)
// Read the status
statusB := make([]byte, 2)
@@ -57,7 +49,7 @@ func ReadResponse(rc io.ReadCloser) (*Response, error) {
if err != nil {
return nil, ErrInvalidResponse
}
resp.Status = Status(status)
resp.status = Status(status)
// Read one space
if b, err := br.ReadByte(); err != nil {
@@ -77,11 +69,11 @@ func ReadResponse(rc io.ReadCloser) (*Response, error) {
if len(meta) > 1024 {
return nil, ErrInvalidResponse
}
if resp.Status.Class() == StatusSuccess && meta == "" {
if resp.status.Class() == StatusSuccess && meta == "" {
// Use default media type
meta = defaultMediaType
}
resp.Meta = meta
resp.meta = meta
// Read terminating newline
if b, err := br.ReadByte(); err != nil {
@@ -90,66 +82,49 @@ func ReadResponse(rc io.ReadCloser) (*Response, error) {
return nil, ErrInvalidResponse
}
if resp.Status.Class() == StatusSuccess {
resp.Body = newReadCloserBody(br, rc)
if resp.status.Class() == StatusSuccess {
resp.body = newBufReadCloser(br, r)
} else {
resp.Body = nopReadCloser{}
rc.Close()
resp.body = nopReadCloser{}
r.Close()
}
return resp, nil
}
type nopReadCloser struct{}
func (nopReadCloser) Read(p []byte) (int, error) {
return 0, io.EOF
// Status returns the response status code.
func (r *Response) Status() Status {
return r.status
}
func (nopReadCloser) Close() error {
return nil
// 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.
func (r *Response) Meta() string {
return r.meta
}
type readCloserBody struct {
br *bufio.Reader // used until empty
io.ReadCloser
// Read reads data from the response body.
// The response body is streamed on demand as Read is called.
func (r *Response) Read(p []byte) (n int, err error) {
return r.body.Read(p)
}
func newReadCloserBody(br *bufio.Reader, rc io.ReadCloser) io.ReadCloser {
body := &readCloserBody{ReadCloser: rc}
if br.Buffered() != 0 {
body.br = br
}
return body
// Close closes the response body.
func (r *Response) Close() error {
return r.body.Close()
}
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)
// Conn returns the network connection on which the response was received.
func (r *Response) Conn() net.Conn {
return r.conn
}
// Write writes r to w in the Gemini response format, including the
// header and body.
//
// This method consults the Status, Meta, and Body fields of the response.
// The Response Body is closed after it is sent.
func (r *Response) Write(w io.Writer) error {
if _, err := fmt.Fprintf(w, "%02d %s\r\n", r.Status, r.Meta); err != nil {
return err
}
if r.Body != nil {
defer r.Body.Close()
if _, err := io.Copy(w, r.Body); err != nil {
return err
}
// 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
}
@@ -166,7 +141,7 @@ type ResponseWriter interface {
//
// Setting the media type after a call to Write or WriteHeader has
// no effect.
SetMediaType(string)
SetMediaType(mediatype string)
// Write writes the data to the connection as part of a Gemini response.
//
@@ -190,23 +165,24 @@ type ResponseWriter interface {
// Flush sends any buffered data to the client.
Flush() error
// Close closes the connection.
// Any blocked Write operations will be unblocked and return errors.
Close() error
}
type responseWriter struct {
b *bufio.Writer
bw *bufio.Writer
cl io.Closer
mediatype string
wroteHeader bool
bodyAllowed bool
}
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
func NewResponseWriter(w io.Writer) ResponseWriter {
return newResponseWriter(w)
}
func newResponseWriter(w io.Writer) *responseWriter {
func newResponseWriter(w io.WriteCloser) *responseWriter {
return &responseWriter{
b: bufio.NewWriter(w),
bw: bufio.NewWriter(w),
cl: w,
}
}
@@ -226,7 +202,7 @@ func (w *responseWriter) Write(b []byte) (int, error) {
if !w.bodyAllowed {
return 0, ErrBodyNotAllowed
}
return w.b.Write(b)
return w.bw.Write(b)
}
func (w *responseWriter) WriteHeader(status Status, meta string) {
@@ -238,10 +214,10 @@ func (w *responseWriter) WriteHeader(status Status, meta string) {
w.bodyAllowed = true
}
w.b.WriteString(strconv.Itoa(int(status)))
w.b.WriteByte(' ')
w.b.WriteString(meta)
w.b.Write(crlf)
w.bw.WriteString(strconv.Itoa(int(status)))
w.bw.WriteByte(' ')
w.bw.WriteString(meta)
w.bw.Write(crlf)
w.wroteHeader = true
}
@@ -249,6 +225,10 @@ func (w *responseWriter) Flush() error {
if !w.wroteHeader {
w.WriteHeader(StatusTemporaryFailure, "Temporary failure")
}
// Write errors from writeHeader will be returned here.
return w.b.Flush()
// Write errors from WriteHeader will be returned here.
return w.bw.Flush()
}
func (w *responseWriter) Close() error {
return w.cl.Close()
}

View File

@@ -2,6 +2,7 @@ package gemini
import (
"io"
"io/ioutil"
"strings"
"testing"
)
@@ -82,7 +83,7 @@ func TestReadWriteResponse(t *testing.T) {
for _, test := range tests {
t.Logf("%#v", test.Raw)
resp, err := ReadResponse(io.NopCloser(strings.NewReader(test.Raw)))
resp, err := ReadResponse(ioutil.NopCloser(strings.NewReader(test.Raw)))
if err != test.Err {
t.Errorf("expected err = %v, got %v", test.Err, err)
}
@@ -90,13 +91,13 @@ func TestReadWriteResponse(t *testing.T) {
// No response
continue
}
if resp.Status != test.Status {
t.Errorf("expected status = %d, got %d", test.Status, resp.Status)
if resp.status != test.Status {
t.Errorf("expected status = %d, got %d", test.Status, resp.status)
}
if resp.Meta != test.Meta {
t.Errorf("expected meta = %s, got %s", test.Meta, resp.Meta)
if resp.meta != test.Meta {
t.Errorf("expected meta = %s, got %s", test.Meta, resp.meta)
}
b, _ := io.ReadAll(resp.Body)
b, _ := ioutil.ReadAll(resp.body)
body := string(b)
if body != test.Body {
t.Errorf("expected body = %#v, got %#v", test.Body, body)
@@ -107,14 +108,12 @@ func TestReadWriteResponse(t *testing.T) {
if test.Err != nil || test.SkipWrite {
continue
}
resp := &Response{
Status: test.Status,
Meta: test.Meta,
Body: io.NopCloser(strings.NewReader(test.Body)),
}
var b strings.Builder
if err := resp.Write(&b); err != nil {
w := newResponseWriter(nopCloser{&b})
w.WriteHeader(test.Status, test.Meta)
io.Copy(w, strings.NewReader(test.Body))
if err := w.Flush(); err != nil {
t.Error(err)
continue
}

View File

@@ -52,7 +52,7 @@ type Server struct {
listeners map[*net.Listener]context.CancelFunc
conns map[*net.Conn]context.CancelFunc
closed bool // true if Closed or Shutdown called
closed bool // true if Close or Shutdown called
shutdown bool // true if Shutdown called
doneChan chan struct{}
mu sync.Mutex
@@ -229,8 +229,8 @@ func (srv *Server) deleteListener(l *net.Listener) {
}
// Serve accepts incoming connections on the Listener l, creating a new
// service goroutine for each. The service goroutines reads the request and
// then calls the appropriate Handler to reply to them. If the provided
// service goroutine for each. The service goroutines read the requests and
// then call the appropriate Handler to reply to them. If the provided
// context expires, Serve closes l and returns the context's error.
//
// Serve always closes l and returns a non-nil error.
@@ -344,23 +344,29 @@ func (srv *Server) ServeConn(ctx context.Context, conn net.Conn) error {
}
func (srv *Server) serveConn(ctx context.Context, conn net.Conn) error {
w := newResponseWriter(conn)
ctx, cancel := context.WithCancel(ctx)
done := ctx.Done()
cw := &contextWriter{
ctx: ctx,
done: done,
cancel: cancel,
wc: conn,
}
r := &contextReader{
ctx: ctx,
done: done,
cancel: cancel,
rc: conn,
}
req, err := ReadRequest(conn)
w := newResponseWriter(cw)
req, err := ReadRequest(r)
if err != nil {
w.WriteHeader(StatusBadRequest, "Bad request")
return w.Flush()
}
// Store the TLS connection state
if tlsConn, ok := conn.(*tls.Conn); ok {
state := tlsConn.ConnectionState()
req.TLS = &state
req.Host = state.ServerName
}
// Store remote address
req.RemoteAddr = conn.RemoteAddr()
req.conn = conn
h := srv.Handler
if h == nil {

View File

@@ -1,112 +0,0 @@
package gemini
import (
"bytes"
"context"
"sync"
"time"
)
// TimeoutHandler returns a Handler that runs h with the given time limit.
//
// The new Handler calls h.ServeGemini to handle each request, but
// if a call runs for longer than its time limit, the handler responds with a
// 40 Temporary Failure error. After such a timeout, writes by h to its
// ResponseWriter will return ErrHandlerTimeout.
func TimeoutHandler(h Handler, dt time.Duration) Handler {
return &timeoutHandler{
h: h,
dt: dt,
}
}
type timeoutHandler struct {
h Handler
dt time.Duration
}
func (t *timeoutHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
ctx, cancel := context.WithTimeout(ctx, t.dt)
defer cancel()
done := make(chan struct{})
tw := &timeoutWriter{}
panicChan := make(chan interface{}, 1)
go func() {
defer func() {
if p := recover(); p != nil {
panicChan <- p
}
}()
t.h.ServeGemini(ctx, tw, r)
close(done)
}()
select {
case p := <-panicChan:
panic(p)
case <-done:
tw.mu.Lock()
defer tw.mu.Unlock()
if !tw.wroteHeader {
tw.status = StatusSuccess
}
w.WriteHeader(tw.status, tw.meta)
w.Write(tw.b.Bytes())
case <-ctx.Done():
tw.mu.Lock()
defer tw.mu.Unlock()
w.WriteHeader(StatusTemporaryFailure, "Timeout")
tw.timedOut = true
}
}
type timeoutWriter struct {
mu sync.Mutex
b bytes.Buffer
status Status
meta string
mediatype string
wroteHeader bool
timedOut bool
}
func (w *timeoutWriter) SetMediaType(mediatype string) {
w.mu.Lock()
defer w.mu.Unlock()
w.mediatype = mediatype
}
func (w *timeoutWriter) Write(b []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.timedOut {
return 0, ErrHandlerTimeout
}
if !w.wroteHeader {
w.writeHeaderLocked(StatusSuccess, w.mediatype)
}
return w.b.Write(b)
}
func (w *timeoutWriter) WriteHeader(status Status, meta string) {
w.mu.Lock()
defer w.mu.Unlock()
if w.timedOut {
return
}
w.writeHeaderLocked(status, meta)
}
func (w *timeoutWriter) writeHeaderLocked(status Status, meta string) {
if w.wroteHeader {
return
}
w.status = status
w.meta = meta
w.wroteHeader = true
}
func (w *timeoutWriter) Flush() error {
return nil
}