gemini

package
v0.0.0-...-91ab29c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package gemini implements the protocol for use with a file server, certificate generation and facilities for interceptors and middlewares.

Index

Constants

View Source
const (
	StatusInput                     = 10
	StatusSensitiveInput            = 11
	StatusSuccess                   = 20
	StatusRedirectTemporary         = 30
	StatusRedirectPermanent         = 31
	StatusTemporaryFailure          = 40
	StatusServerUnavailable         = 41
	StatusCgiError                  = 42
	StatusProxyError                = 43
	StatusSlowDown                  = 44
	StatusPermanentFailure          = 50
	StatusNotFound                  = 51
	StatusGone                      = 52
	StatusProxyRequestRefused       = 53
	StatusBadRequest                = 59
	StatusClientCertificateRequired = 60
	StatusCertificateNotAuthorized  = 61
	StatusCertificateNotValid       = 62
)
View Source
const (
	Termination = "\r\n"
	URLMaxBytes = 1024
	IndexFile   = "index.gmi"
	MimeType    = "text/gemini; charset=utf-8"
	MimeTypeCS  = "text/gemini; charset=utf-8; lang=cs"
)

Variables

View Source
var (
	ErrServerClosed    = errors.New("gemini: server closed")
	ErrHeaderTooLong   = errors.New("gemini: header too long")
	ErrMissingFile     = errors.New("gemini: no such file")
	ErrEmptyRequest    = errors.New("gemini: empty request")
	ErrEmptyRequestURL = errors.New("gemini: empty request URL")
	ErrInvalidPath     = errors.New("gemini: path error")
	ErrInvalidHost     = errors.New("gemini: empty host")
	ErrInvalidUtf8     = errors.New("gemini: empty request URL")
	ErrUnknownProtocol = fmt.Errorf("gemini: unknown protocol scheme")
)

Functions

func Error

func Error(code int, err error) error

func GenX509KeyPair

func GenX509KeyPair(host string, daysvalid int) (tls.Certificate, error)

GenX509KeyPair generates a TLS keypair with one week validity.

func TLSConfig

func TLSConfig(sni string, cert tls.Certificate) *tls.Config

Types

type GmiError

type GmiError struct {
	Code int
	// contains filtered or unexported fields
}

func (*GmiError) Error

func (e *GmiError) Error() string

func (*GmiError) Unwrap

func (e *GmiError) Unwrap() error

type Handler

type Handler interface {
	ServeGemini(ResponseWriter, *Request)
}

type HandlerFunc

type HandlerFunc func(ResponseWriter, *Request)

The HandlerFunc type is an adapter to allow the use of ordinary functions as Gemini handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) ServeGemini

func (f HandlerFunc) ServeGemini(w ResponseWriter, r *Request)

ServeGemini calls f(w, r).

type Interceptor

type Interceptor struct {

	// Interceptor is the underlying io.Writer that buffers the response body
	Body bytes.Buffer

	// Code is the status code.
	Code int

	// Meta is the header addition, such as the mimetype.
	Meta string
	// contains filtered or unexported fields
}

Interceptor is a ResponseWriter wrapper that may be used as buffer.

A middleware may pass it to the next handlers ServeGemini method as a drop in replacement for the response writer. See the logger and cache middlewares for examples.

Note that the body being written two times and the complete caching of the body in the memory.

func NewInterceptor

func NewInterceptor(responseWriter ResponseWriter) (m *Interceptor)

NewInterceptor creates a new Interceptor by wrapping the given response writer.

func (*Interceptor) Flush

func (m *Interceptor) Flush()

FlushAll flushes headers, status code and body to the underlying ResponseWriter.

func (*Interceptor) FlushBody

func (m *Interceptor) FlushBody()

FlushBody flushes to the underlying responsewriter.

func (*Interceptor) FlushHeader

func (m *Interceptor) FlushHeader()

FlushHeader writes the header to the underlying ResponseWriter.

func (*Interceptor) HasBody

func (m *Interceptor) HasBody() bool

func (*Interceptor) HasHeader

func (m *Interceptor) HasHeader() bool

func (*Interceptor) Write

func (m *Interceptor) Write(body []byte) (int, error)

Write writes to the underlying buffer and tracks this call as change

func (*Interceptor) WriteHeader

func (m *Interceptor) WriteHeader(code int, message string) (int, error)

WriteHeader writes the cached status code and tracks this call as change

type Middleware

type Middleware func(Handler) Handler

Middlewares type is a slice of gemini middleware handlers.

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

func NewMux

func NewMux() *Mux

func (*Mux) Handle

func (m *Mux) Handle(endpoint Handler) Handler

func (*Mux) ServeGemini

func (m *Mux) ServeGemini(w ResponseWriter, r *Request)

func (*Mux) Use

func (m *Mux) Use(handlers ...Middleware)

Use appends a handler to the Mux handler stack.

type Request

type Request struct {
	URL        *url.URL
	RemoteAddr string

	// RequestURI is the unmodified request-target of the Request-Line  as sent by the client
	// to a server. Usually the URL field should be used instead.
	RequestURI string
	// contains filtered or unexported fields
}

type ResponseWriter

type ResponseWriter interface {
	WriteHeader(code int, message string) (int, error)
	Write(body []byte) (int, error)
}

type Server

type Server struct {
	// Addr is the address the server is listening on.
	Addr string

	// Hostname or common name of the server. This is used for absolute redirects.
	Hostname string

	// Logger enables logging of the gemini server for debugging purposes.
	Logger *log.Logger

	TLSConfig       *tls.Config
	TLSConfigLoader func() (*tls.Config, error)

	Handler      Handler // handler to invoke
	ReadTimeout  time.Duration
	MaxOpenConns int
	// contains filtered or unexported fields
}

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown uses the self-pipe trick to gracefully allow the accept handler to exit and the listener to close within the given context deadline. If unsuccessful the listener is forcefully terminated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL