Set default mimetype if META is empty

This commit is contained in:
Adnan Maolood 2020-10-31 20:32:38 -04:00
parent 63b9b484d1
commit a2fc1772bf
2 changed files with 13 additions and 16 deletions

View File

@ -9,7 +9,7 @@ import (
// Response is a Gemini response. // Response is a Gemini response.
type Response struct { type Response struct {
// Status represents the response status. // Status contains the response status code.
Status Status Status Status
// Meta contains more information related to the response status. // Meta contains more information related to the response status.
@ -21,7 +21,7 @@ type Response struct {
// Body contains the response body for successful responses. // Body contains the response body for successful responses.
Body io.ReadCloser Body io.ReadCloser
// Request is the request that was sent to obtain this Response. // Request is the request that was sent to obtain this response.
Request *Request Request *Request
// TLS contains information about the TLS connection on which the response // TLS contains information about the TLS connection on which the response
@ -68,6 +68,10 @@ func (resp *Response) read(rc io.ReadCloser) error {
if len(meta) > 1024 { if len(meta) > 1024 {
return ErrInvalidResponse return ErrInvalidResponse
} }
// Default mime type of text/gemini; charset=utf-8
if statusClass == StatusClassSuccess && meta == "" {
meta = "text/gemini; charset=utf-8"
}
resp.Meta = meta resp.Meta = meta
// Read terminating newline // Read terminating newline

View File

@ -37,15 +37,9 @@ type responderKey struct {
// Register registers a responder for the given pattern. // Register registers a responder for the given pattern.
// //
// Patterns must be in the form of hostname or scheme://hostname // Patterns must be in the form of "hostname" or "scheme://hostname".
// (e.g. gemini://example.com). // If no scheme is specified, a scheme of "gemini://" is implied.
// If no scheme is specified, a default scheme of gemini:// is implied. // Wildcard patterns are supported (e.g. "*.example.com").
//
// Wildcard patterns are supported (e.g. *.example.com).
// To register a certificate for a wildcard hostname, call Certificates.Add:
//
// var s gemini.Server
// s.Certificates.Add("*.example.com", cert)
func (s *Server) Register(pattern string, responder Responder) { func (s *Server) Register(pattern string, responder Responder) {
if pattern == "" { if pattern == "" {
panic("gemini: invalid pattern") panic("gemini: invalid pattern")
@ -258,13 +252,13 @@ func (w *ResponseWriter) WriteHeader(status Status, meta string) {
} }
// WriteStatus writes the response header with the given status code. // WriteStatus writes the response header with the given status code.
//
// WriteStatus is equivalent to WriteHeader(status, status.Message())
func (w *ResponseWriter) WriteStatus(status Status) { func (w *ResponseWriter) WriteStatus(status Status) {
w.WriteHeader(status, status.Message()) w.WriteHeader(status, status.Message())
} }
// SetMimetype sets the mimetype that will be written for a successful response. // SetMimetype sets the mimetype that will be written for a successful response.
// The provided mimetype will only be used if Write is called without calling
// WriteHeader.
// If the mimetype is not set, it will default to "text/gemini". // If the mimetype is not set, it will default to "text/gemini".
func (w *ResponseWriter) SetMimetype(mimetype string) { func (w *ResponseWriter) SetMimetype(mimetype string) {
w.mimetype = mimetype w.mimetype = mimetype
@ -274,9 +268,8 @@ func (w *ResponseWriter) SetMimetype(mimetype string) {
// If the response status does not allow for a response body, Write returns // If the response status does not allow for a response body, Write returns
// ErrBodyNotAllowed. // ErrBodyNotAllowed.
// //
// If WriteHeader has not yet been called, Write calls // If the response header has not yet been written, Write calls WriteHeader
// WriteHeader(StatusSuccess, mimetype) where mimetype is the mimetype set in // with StatusSuccess and the mimetype set in SetMimetype.
// SetMimetype. If no mimetype is set, a default of "text/gemini" will be used.
func (w *ResponseWriter) Write(b []byte) (int, error) { func (w *ResponseWriter) Write(b []byte) (int, error) {
if !w.wroteHeader { if !w.wroteHeader {
mimetype := w.mimetype mimetype := w.mimetype