Merge Request and RequestInfo
This commit is contained in:
		
							parent
							
								
									63696fc7c8
								
							
						
					
					
						commit
						c6802d9d9a
					
				@ -26,8 +26,8 @@ func main() {
 | 
				
			|||||||
	config.ClientAuth = tls.RequestClientCert
 | 
						config.ClientAuth = tls.RequestClientCert
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mux := &gemini.Mux{}
 | 
						mux := &gemini.Mux{}
 | 
				
			||||||
	mux.HandleFunc("/", func(req *gemini.RequestInfo) *gemini.Response {
 | 
						mux.HandleFunc("/", func(req *gemini.Request) *gemini.Response {
 | 
				
			||||||
		log.Printf("Request from %s for %s with certificates %v", req.RemoteAddr.String(), req.URL.String(), req.Certificates)
 | 
							log.Printf("Request from %s for %s with certificates %v", req.RemoteAddr.String(), req.URL.String(), req.TLS.PeerCertificates)
 | 
				
			||||||
		return &gemini.Response{
 | 
							return &gemini.Response{
 | 
				
			||||||
			Status: gemini.StatusSuccess,
 | 
								Status: gemini.StatusSuccess,
 | 
				
			||||||
			Meta:   "text/gemini",
 | 
								Meta:   "text/gemini",
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										105
									
								
								gemini.go
									
									
									
									
									
								
							
							
						
						
									
										105
									
								
								gemini.go
									
									
									
									
									
								
							@ -5,7 +5,6 @@ import (
 | 
				
			|||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"crypto/tls"
 | 
						"crypto/tls"
 | 
				
			||||||
	"crypto/x509"
 | 
					 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
@ -64,12 +63,33 @@ var (
 | 
				
			|||||||
//     if err != nil {
 | 
					//     if err != nil {
 | 
				
			||||||
//         panic(err)
 | 
					//         panic(err)
 | 
				
			||||||
//     }
 | 
					//     }
 | 
				
			||||||
//     req.Certificates = append(req.Certificates, cert)
 | 
					//     req.TLSConfig.Certificates = append(req.TLSConfig.Certificates, cert)
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
type Request struct {
 | 
					type Request struct {
 | 
				
			||||||
	Host         string            // host or host:port
 | 
						// URL specifies the URL being requested.
 | 
				
			||||||
	URL          *url.URL          // the requested URL
 | 
						URL *url.URL
 | 
				
			||||||
	Certificates []tls.Certificate // client certificates
 | 
					
 | 
				
			||||||
 | 
						// For client requests, Host specifies the host on which the URL is sought.
 | 
				
			||||||
 | 
						// If this field is not set, the host will be inferred from the URL.
 | 
				
			||||||
 | 
						// This field is ignored by the server.
 | 
				
			||||||
 | 
						Host string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TLSConfig provides a TLS configuration for use by the client.
 | 
				
			||||||
 | 
						// It is recommended that clients set `InsecureSkipVerify` to true to skip
 | 
				
			||||||
 | 
						// verifying TLS certificates, and instead adopt a Trust-On-First-Use
 | 
				
			||||||
 | 
						// method of verifying certificates.
 | 
				
			||||||
 | 
						// This field is ignored by the server.
 | 
				
			||||||
 | 
						TLSConfig tls.Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// RemoteAddr allows servers and other software to record the network
 | 
				
			||||||
 | 
						// address that sent the request.
 | 
				
			||||||
 | 
						// This field is ignored by the client.
 | 
				
			||||||
 | 
						RemoteAddr net.Addr
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TLS allows servers and other software to record information about the TLS
 | 
				
			||||||
 | 
						// connection on which the request was recieved.
 | 
				
			||||||
 | 
						// This field is ignored by the client.
 | 
				
			||||||
 | 
						TLS tls.ConnectionState
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewRequest returns a new request. The host is inferred from the provided url.
 | 
					// NewRequest returns a new request. The host is inferred from the provided url.
 | 
				
			||||||
@ -112,9 +132,23 @@ func (r *Request) Write(w io.Writer) error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Response is a Gemini response.
 | 
					// Response is a Gemini response.
 | 
				
			||||||
type Response struct {
 | 
					type Response struct {
 | 
				
			||||||
 | 
						// Status represents the response status.
 | 
				
			||||||
	Status int
 | 
						Status int
 | 
				
			||||||
	Meta   string
 | 
					
 | 
				
			||||||
	Body   []byte
 | 
						// 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Body contains the response body.
 | 
				
			||||||
 | 
						// Body is only used by the server for successful responses.
 | 
				
			||||||
 | 
						Body []byte
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TLS contains information about the TLS connection on which the response
 | 
				
			||||||
 | 
						// was received.
 | 
				
			||||||
 | 
						// This field is ignored by the server.
 | 
				
			||||||
 | 
						TLS tls.ConnectionState
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Write writes the Gemini response header and body to the provided io.Writer.
 | 
					// Write writes the Gemini response header and body to the provided io.Writer.
 | 
				
			||||||
@ -157,18 +191,8 @@ func (c *Client) ProxyRequest(host, url string) (*Response, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Do sends a Gemini request and returns a Gemini response.
 | 
					// Do sends a Gemini request and returns a Gemini response.
 | 
				
			||||||
func (c *Client) Do(req *Request) (*Response, error) {
 | 
					func (c *Client) Do(req *Request) (*Response, error) {
 | 
				
			||||||
	host := req.Host
 | 
						// Connect to the host
 | 
				
			||||||
	if strings.LastIndex(host, ":") == -1 {
 | 
						conn, err := tls.Dial("tcp", req.Host, &req.TLSConfig)
 | 
				
			||||||
		// The default port is 1965
 | 
					 | 
				
			||||||
		host += ":1965"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Allow self signed certificates
 | 
					 | 
				
			||||||
	config := tls.Config{}
 | 
					 | 
				
			||||||
	config.InsecureSkipVerify = true
 | 
					 | 
				
			||||||
	config.Certificates = req.Certificates
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	conn, err := tls.Dial("tcp", host, &config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -226,9 +250,16 @@ func (c *Client) Do(req *Request) (*Response, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Server is a Gemini server.
 | 
					// Server is a Gemini server.
 | 
				
			||||||
type Server struct {
 | 
					type Server struct {
 | 
				
			||||||
	Addr      string
 | 
						// Addr specifies the address that the server should listen on.
 | 
				
			||||||
 | 
						// If Addr is empty, the server will listen on the address ":1965".
 | 
				
			||||||
 | 
						Addr string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// TLSConfig provides a TLS configuration for use by the server.
 | 
				
			||||||
	TLSConfig tls.Config
 | 
						TLSConfig tls.Config
 | 
				
			||||||
	Handler   Handler
 | 
					
 | 
				
			||||||
 | 
						// Handler specifies the Handler for requests.
 | 
				
			||||||
 | 
						// If Handler is not set, the server will error.
 | 
				
			||||||
 | 
						Handler Handler
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ListenAndServe listens for requests at the server's configured address.
 | 
					// ListenAndServe listens for requests at the server's configured address.
 | 
				
			||||||
@ -291,38 +322,32 @@ func (s *Server) respond(rw net.Conn) {
 | 
				
			|||||||
	} else if len(rawurl) > 1024 {
 | 
						} else if len(rawurl) > 1024 {
 | 
				
			||||||
		resp = &Response{
 | 
							resp = &Response{
 | 
				
			||||||
			Status: StatusBadRequest,
 | 
								Status: StatusBadRequest,
 | 
				
			||||||
			Meta:   "URL exceeds 1024 bytes",
 | 
								Meta:   "Requested URL exceeds 1024 bytes",
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else if url, err := url.Parse(rawurl); err != nil || url.User != nil {
 | 
						} else if url, err := url.Parse(rawurl); err != nil || url.User != nil {
 | 
				
			||||||
 | 
							// Note that we return an error if User is specified in the URL.
 | 
				
			||||||
		resp = &Response{
 | 
							resp = &Response{
 | 
				
			||||||
			Status: StatusBadRequest,
 | 
								Status: StatusBadRequest,
 | 
				
			||||||
			Meta:   "Invalid URL",
 | 
								Meta:   "Requested URL is invalid",
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		// Gather information about the request
 | 
							// Gather information about the request
 | 
				
			||||||
		reqInfo := &RequestInfo{
 | 
							req := &Request{
 | 
				
			||||||
			URL:          url,
 | 
								URL:        url,
 | 
				
			||||||
			Certificates: rw.(*tls.Conn).ConnectionState().PeerCertificates,
 | 
								RemoteAddr: rw.RemoteAddr(),
 | 
				
			||||||
			RemoteAddr:   rw.RemoteAddr(),
 | 
								TLS:        rw.(*tls.Conn).ConnectionState(),
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		resp = s.Handler.Serve(reqInfo)
 | 
							resp = s.Handler.Serve(req)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	resp.Write(rw)
 | 
						resp.Write(rw)
 | 
				
			||||||
	rw.Close()
 | 
						rw.Close()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RequestInfo contains information about a request.
 | 
					 | 
				
			||||||
type RequestInfo struct {
 | 
					 | 
				
			||||||
	URL          *url.URL            // the requested URL
 | 
					 | 
				
			||||||
	Certificates []*x509.Certificate // client certificates
 | 
					 | 
				
			||||||
	RemoteAddr   net.Addr            // client remote address
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// A Handler responds to a Gemini request.
 | 
					// A Handler responds to a Gemini request.
 | 
				
			||||||
type Handler interface {
 | 
					type Handler interface {
 | 
				
			||||||
	// Serve accepts a Request and returns a Response.
 | 
						// Serve accepts a Request and returns a Response.
 | 
				
			||||||
	Serve(*RequestInfo) *Response
 | 
						Serve(*Request) *Response
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Mux is a Gemini request multiplexer.
 | 
					// Mux is a Gemini request multiplexer.
 | 
				
			||||||
@ -366,13 +391,13 @@ func (m *Mux) Handle(pattern string, handler Handler) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// HandleFunc registers a HandlerFunc for the given pattern.
 | 
					// HandleFunc registers a HandlerFunc for the given pattern.
 | 
				
			||||||
func (m *Mux) HandleFunc(pattern string, handlerFunc func(req *RequestInfo) *Response) {
 | 
					func (m *Mux) HandleFunc(pattern string, handlerFunc func(req *Request) *Response) {
 | 
				
			||||||
	handler := HandlerFunc(handlerFunc)
 | 
						handler := HandlerFunc(handlerFunc)
 | 
				
			||||||
	m.Handle(pattern, handler)
 | 
						m.Handle(pattern, handler)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Serve responds to the request with the appropriate handler.
 | 
					// Serve responds to the request with the appropriate handler.
 | 
				
			||||||
func (m *Mux) Serve(req *RequestInfo) *Response {
 | 
					func (m *Mux) Serve(req *Request) *Response {
 | 
				
			||||||
	h := m.match(req.URL)
 | 
						h := m.match(req.URL)
 | 
				
			||||||
	if h == nil {
 | 
						if h == nil {
 | 
				
			||||||
		return &Response{
 | 
							return &Response{
 | 
				
			||||||
@ -384,9 +409,9 @@ func (m *Mux) Serve(req *RequestInfo) *Response {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// A wrapper around a bare function that implements Handler.
 | 
					// A wrapper around a bare function that implements Handler.
 | 
				
			||||||
type HandlerFunc func(req *RequestInfo) *Response
 | 
					type HandlerFunc func(req *Request) *Response
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (f HandlerFunc) Serve(req *RequestInfo) *Response {
 | 
					func (f HandlerFunc) Serve(req *Request) *Response {
 | 
				
			||||||
	return f(req)
 | 
						return f(req)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user