gemini

package
v0.0.0-...-66cf4e1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BodyNotAllowed error = errors.New("Body Not allowed for non 2X status codes")

The error that is returned when a call to Write was made when the status code is not 2X

Functions

func Error

func Error(w ResponseWriter, message string, status StatusCode)

Send a response to the client with a given message

func Input

func Input(w ResponseWriter, prompt string, sensitive bool)

Request input from the client

func Redirect

func Redirect(w ResponseWriter, location string, permanent bool)

Redirect the client to a new location

func RedirectURL

func RedirectURL(w ResponseWriter, location url.URL, permanent bool)

Redirect the client to a new location using a url object

func RequireCert

func RequireCert(w ResponseWriter, r *Request, message ...string) bool

Require a certificate to be provided by the client

If no certificate is provided, then a CertRequired response is sent with the optional message and true is returned

If a certificate is provided, then no action is taken and false is returned

Returns true when a response was sent to the client

func SlowDown

func SlowDown(w ResponseWriter)

Send a slowdown response to the client

func VerifyCert

func VerifyCert(w ResponseWriter, r *Request, verify func(chain []*x509.Certificate) (verified bool, err string), message ...string) bool

Require a certificate to be provided by the client and verify the certificate with the provided function

If no certificate is sent, then a CertRequired response is sent with the optional message and true is returned

Verify is called with the certificate chain. If the certificate is verified, then true is returned. If the certificate is unauthorized, then false is returned with an optional error message.

Returns true when a response was sent to the client

Types

type Flusher

type Flusher interface {
	// Send any pending data to the client
	Flush() error
}

ResponseWriters which are Flushers have an internal buffer which can be sent to the client with the Flush method.

type GeminiServer

type GeminiServer struct {
	HandshakeTimeout time.Duration
	Config           *tls.Config
	// contains filtered or unexported fields
}

func NewGeminiServer

func NewGeminiServer(tlsConfig *tls.Config) *GeminiServer

Create a new Gemini server using the provided tls config

Because the Gemini protocol supports client certificates, the default setting for tlsConfig.ClientAuth is set to RequestClientCert.

func (*GeminiServer) Handler

func (self *GeminiServer) Handler(handler Handler)

Set the handler for the server

func (*GeminiServer) HandlerFunc

func (self *GeminiServer) HandlerFunc(handler func(ResponseWriter, *Request))

Set the handler for the server

func (*GeminiServer) ListenAndServe

func (self *GeminiServer) ListenAndServe(addr string) error

Start listening and server the Gemini server.

if addr is empty, then the default port 1965 will be used.

If the host in the addr parameter is empty or a literal unspecified IP addr, ListenAndServe listens on all available unicast and anycast IP addresses of the local system. To only use IPv4, use network "tcp4". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty as in "127.0.0.1:" or "", then the default port 1965 is used. If the port is instead "0", as in ":0" or "\[::1]:0", a port number is automatically chosen. The \[Addr] method of \[Listener] can be used to discover the chosen port.

func (*GeminiServer) Serve

func (self *GeminiServer) Serve(listener net.Listener) error

Serve the Gemini server using an existing listener.

type Handler

type Handler interface {
	// Serve a gemini response for a given request
	ServeGemini(ResponseWriter, *Request)
}

Gemini Handler can process a gemini request by defining the ServeGemini method.

type HandlerFunc

type HandlerFunc func(ResponseWriter, *Request)

A gemini handler as a function

func (HandlerFunc) ServeGemini

func (self HandlerFunc) ServeGemini(w ResponseWriter, r *Request)
type Header string

func (*Header) MimeType

func (self *Header) MimeType(contentType string, langs ...string)

Set the mimetype of the response

Optional languages can be added to the mimetype. These languages must be IETF language tags.

This should be used with the 2X OK response

func (*Header) Prompt

func (self *Header) Prompt(message string)

Prompt the client for input

This should be used with the 1X input response

func (*Header) Redirect

func (self *Header) Redirect(uri url.URL)

Set the redirect url for redirects

This should be used with the 3X redirect responses

func (*Header) Set

func (self *Header) Set(header string)

Set the raw header for the response

type Method

type Method string
const (
	MethodGemini Method = "gemini"
	MethodTitan  Method = "titan"
)

type Request

type Request struct {
	// The Url of the request
	URL *url.URL
	// The scheme of the request (titan or gemini)
	Method Method
	// The query of the request with any urlencodings decoded
	Query string
	// The host of the request
	Host string
	// The path of the request with any urlencodings decoded
	Path string
	// ip address of the client
	RemoteAddr string
	// The original uri of the request
	RequestURI string
	// The size of the body (if method is titan)
	Size int64
	// The content type of the body (if method is titan)
	ContentType string
	// The token for the titan request
	Token string
	// The body of the titan request
	Body io.Reader
	// Any provided client certificates
	ClientCerts []*x509.Certificate
	// The tls state
	TLS tls.ConnectionState
}

type ResponseWriter

type ResponseWriter interface {
	// Get a reference to the header
	Header() *Header

	// Write to the client
	//
	// It is an error to call this method if a status other than 2X was sent
	//
	// If WriteHeader was not already called, then WriteHeader(20) will be called
	// for you.
	Write([]byte) (int, error)
	// Send the header to the client
	//
	// If the status 20 is sent and no header was provided, then the mimetype
	// text/gemini will be set by default.
	WriteHeader(status StatusCode) error
}

Response object for gemini requests.

All ResponseWriters provide a header and writer.

Most ResponseWriters also are Flusher

type StatusCode

type StatusCode int
const (
	StatusInput            StatusCode = 10
	StatusSensitiveInput   StatusCode = 11
	StatusSuccess          StatusCode = 20
	StatusRedirect         StatusCode = 30
	StatusRedirectPerm     StatusCode = 31
	StatusTempFail         StatusCode = 40
	StatusUnavailable      StatusCode = 41
	StatusCGIError         StatusCode = 42
	StatusProxyError       StatusCode = 43
	StatusSlowDown         StatusCode = 44
	StatusPermFail         StatusCode = 50
	StatusNotFound         StatusCode = 51
	StatusGone             StatusCode = 52
	StatusProxyRefused     StatusCode = 53
	StatusBadRequest       StatusCode = 59
	StatusCertRequired     StatusCode = 60
	StatusCertUnauthorized StatusCode = 61
	StatusCertNotValid     StatusCode = 62
)

type Unwrapper

type Unwrapper interface {
	// Get the wrapped interface from this interface
	Unwrap() any
}

Interfaces which wrap another interface should implement Unwrapper.

Jump to

Keyboard shortcuts

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