Rename rw to w
This commit is contained in:
parent
78280070ae
commit
39645eb921
38
server.go
38
server.go
|
@ -132,7 +132,7 @@ func (s *Server) Serve(l net.Listener) error {
|
||||||
|
|
||||||
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
// ResponseWriter is used by a Gemini handler to construct a Gemini response.
|
||||||
type ResponseWriter struct {
|
type ResponseWriter struct {
|
||||||
w *bufio.Writer
|
b *bufio.Writer
|
||||||
bodyAllowed bool
|
bodyAllowed bool
|
||||||
wroteHeader bool
|
wroteHeader bool
|
||||||
mimetype string
|
mimetype string
|
||||||
|
@ -140,7 +140,7 @@ type ResponseWriter struct {
|
||||||
|
|
||||||
func newResponseWriter(conn net.Conn) *ResponseWriter {
|
func newResponseWriter(conn net.Conn) *ResponseWriter {
|
||||||
return &ResponseWriter{
|
return &ResponseWriter{
|
||||||
w: bufio.NewWriter(conn),
|
b: bufio.NewWriter(conn),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,28 +151,28 @@ func newResponseWriter(conn net.Conn) *ResponseWriter {
|
||||||
// For successful responses, Meta should contain the mimetype of the response.
|
// For successful responses, Meta should contain the mimetype of the response.
|
||||||
// For failure responses, Meta should contain a short description of the failure.
|
// For failure responses, Meta should contain a short description of the failure.
|
||||||
// Meta should not be longer than 1024 bytes.
|
// Meta should not be longer than 1024 bytes.
|
||||||
func (r *ResponseWriter) WriteHeader(status int, meta string) {
|
func (w *ResponseWriter) WriteHeader(status int, meta string) {
|
||||||
if r.wroteHeader {
|
if w.wroteHeader {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.w.WriteString(strconv.Itoa(status))
|
w.b.WriteString(strconv.Itoa(status))
|
||||||
r.w.WriteByte(' ')
|
w.b.WriteByte(' ')
|
||||||
r.w.WriteString(meta)
|
w.b.WriteString(meta)
|
||||||
r.w.Write(crlf)
|
w.b.Write(crlf)
|
||||||
|
|
||||||
// Only allow body to be written on successful status codes.
|
// Only allow body to be written on successful status codes.
|
||||||
if status/10 == StatusClassSuccess {
|
if status/10 == StatusClassSuccess {
|
||||||
r.bodyAllowed = true
|
w.bodyAllowed = true
|
||||||
}
|
}
|
||||||
r.wroteHeader = true
|
w.wroteHeader = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// The provided mimetype will only be used if Write is called without calling
|
||||||
// WriteHeader.
|
// 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 (r *ResponseWriter) SetMimetype(mimetype string) {
|
func (w *ResponseWriter) SetMimetype(mimetype string) {
|
||||||
r.mimetype = mimetype
|
w.mimetype = mimetype
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write writes the response body.
|
// Write writes the response body.
|
||||||
|
@ -182,18 +182,18 @@ func (r *ResponseWriter) SetMimetype(mimetype string) {
|
||||||
// If WriteHeader has not yet been called, Write calls
|
// If WriteHeader has not yet been called, Write calls
|
||||||
// WriteHeader(StatusSuccess, mimetype) where mimetype is the mimetype set in
|
// WriteHeader(StatusSuccess, mimetype) where mimetype is the mimetype set in
|
||||||
// SetMimetype. If no mimetype is set, a default of "text/gemini" will be used.
|
// SetMimetype. If no mimetype is set, a default of "text/gemini" will be used.
|
||||||
func (r *ResponseWriter) Write(b []byte) (int, error) {
|
func (w *ResponseWriter) Write(b []byte) (int, error) {
|
||||||
if !r.wroteHeader {
|
if !w.wroteHeader {
|
||||||
mimetype := r.mimetype
|
mimetype := w.mimetype
|
||||||
if mimetype == "" {
|
if mimetype == "" {
|
||||||
mimetype = "text/gemini"
|
mimetype = "text/gemini"
|
||||||
}
|
}
|
||||||
r.WriteHeader(StatusSuccess, mimetype)
|
w.WriteHeader(StatusSuccess, mimetype)
|
||||||
}
|
}
|
||||||
if !r.bodyAllowed {
|
if !w.bodyAllowed {
|
||||||
return 0, ErrBodyNotAllowed
|
return 0, ErrBodyNotAllowed
|
||||||
}
|
}
|
||||||
return r.w.Write(b)
|
return w.b.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// respond responds to a connection.
|
// respond responds to a connection.
|
||||||
|
@ -228,7 +228,7 @@ func (s *Server) respond(conn net.Conn) {
|
||||||
}
|
}
|
||||||
s.handler(req).Serve(w, req)
|
s.handler(req).Serve(w, req)
|
||||||
}
|
}
|
||||||
w.w.Flush()
|
w.b.Flush()
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user