From aefa53f827cb911fa71bb5e544c846ac517714e0 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 3 Jan 2017 23:09:51 +0100 Subject: [PATCH] refine http header --- transport/internet/headers/http/http.go | 59 ++++++++++++------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/transport/internet/headers/http/http.go b/transport/internet/headers/http/http.go index d8c6c79f..61d4242d 100644 --- a/transport/internet/headers/http/http.go +++ b/transport/internet/headers/http/http.go @@ -109,15 +109,15 @@ type HttpConn struct { readBuffer *buf.Buffer oneTimeReader Reader oneTimeWriter Writer - isServer bool + errorWriter Writer } -func NewHttpConn(conn net.Conn, reader Reader, writer Writer, isServer bool) *HttpConn { +func NewHttpConn(conn net.Conn, reader Reader, writer Writer, errorWriter Writer) *HttpConn { return &HttpConn{ Conn: conn, oneTimeReader: reader, oneTimeWriter: writer, - isServer: isServer, + errorWriter: errorWriter, } } @@ -157,33 +157,10 @@ func (v *HttpConn) Write(b []byte) (int, error) { // Close implements net.Conn.Close(). func (v *HttpConn) Close() error { - if v.isServer && v.oneTimeWriter != nil { + if v.oneTimeWriter != nil && v.errorWriter != nil { // Connection is being closed but header wasn't sent. This means the client request // is probably not valid. Sending back a server error header in this case. - writer := formResponseHeader(&ResponseConfig{ - Version: &Version{ - Value: "1.1", - }, - Status: &Status{ - Code: "500", - Reason: "Internal Server Error", - }, - Header: []*Header{ - { - Name: "Connection", - Value: []string{"close"}, - }, - { - Name: "Cache-Control", - Value: []string{"private"}, - }, - { - Name: "Content-Length", - Value: []string{"0"}, - }, - }, - }) - writer.Write(v.Conn) + v.errorWriter.Write(v.Conn) } return v.Conn.Close() @@ -248,14 +225,36 @@ func (v HttpAuthenticator) Client(conn net.Conn) net.Conn { if v.config.Response != nil { writer = v.GetClientWriter() } - return NewHttpConn(conn, reader, writer, false) + return NewHttpConn(conn, reader, writer, new(NoOpWriter)) } func (v HttpAuthenticator) Server(conn net.Conn) net.Conn { if v.config.Request == nil && v.config.Response == nil { return conn } - return NewHttpConn(conn, new(HeaderReader), v.GetServerWriter(), true) + return NewHttpConn(conn, new(HeaderReader), v.GetServerWriter(), formResponseHeader(&ResponseConfig{ + Version: &Version{ + Value: "1.1", + }, + Status: &Status{ + Code: "500", + Reason: "Internal Server Error", + }, + Header: []*Header{ + { + Name: "Connection", + Value: []string{"close"}, + }, + { + Name: "Cache-Control", + Value: []string{"private"}, + }, + { + Name: "Content-Length", + Value: []string{"0"}, + }, + }, + })) } type HttpAuthenticatorFactory struct{}