1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-06-01 17:56:04 +02:00

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.
type Response struct {
// Status represents the response status.
// Status contains the response status code.
Status 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 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
// 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 {
return ErrInvalidResponse
}
// Default mime type of text/gemini; charset=utf-8
if statusClass == StatusClassSuccess && meta == "" {
meta = "text/gemini; charset=utf-8"
}
resp.Meta = meta
// Read terminating newline

View File

@ -37,15 +37,9 @@ type responderKey struct {
// Register registers a responder for the given pattern.
//
// Patterns must be in the form of hostname or scheme://hostname
// (e.g. gemini://example.com).
// If no scheme is specified, a default scheme of gemini:// is implied.
//
// 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)
// Patterns must be in the form of "hostname" or "scheme://hostname".
// If no scheme is specified, a scheme of "gemini://" is implied.
// Wildcard patterns are supported (e.g. "*.example.com").
func (s *Server) Register(pattern string, responder Responder) {
if 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 is equivalent to WriteHeader(status, status.Message())
func (w *ResponseWriter) WriteStatus(status Status) {
w.WriteHeader(status, status.Message())
}
// 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".
func (w *ResponseWriter) SetMimetype(mimetype string) {
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
// ErrBodyNotAllowed.
//
// If WriteHeader has not yet been called, Write calls
// WriteHeader(StatusSuccess, mimetype) where mimetype is the mimetype set in
// SetMimetype. If no mimetype is set, a default of "text/gemini" will be used.
// If the response header has not yet been written, Write calls WriteHeader
// with StatusSuccess and the mimetype set in SetMimetype.
func (w *ResponseWriter) Write(b []byte) (int, error) {
if !w.wroteHeader {
mimetype := w.mimetype