refine http header

pull/362/head
Darien Raymond 8 years ago
parent 588c43f291
commit aefa53f827
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -109,15 +109,15 @@ type HttpConn struct {
readBuffer *buf.Buffer readBuffer *buf.Buffer
oneTimeReader Reader oneTimeReader Reader
oneTimeWriter Writer 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{ return &HttpConn{
Conn: conn, Conn: conn,
oneTimeReader: reader, oneTimeReader: reader,
oneTimeWriter: writer, oneTimeWriter: writer,
isServer: isServer, errorWriter: errorWriter,
} }
} }
@ -157,33 +157,10 @@ func (v *HttpConn) Write(b []byte) (int, error) {
// Close implements net.Conn.Close(). // Close implements net.Conn.Close().
func (v *HttpConn) Close() error { 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 // 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. // is probably not valid. Sending back a server error header in this case.
writer := formResponseHeader(&ResponseConfig{ v.errorWriter.Write(v.Conn)
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)
} }
return v.Conn.Close() return v.Conn.Close()
@ -248,14 +225,36 @@ func (v HttpAuthenticator) Client(conn net.Conn) net.Conn {
if v.config.Response != nil { if v.config.Response != nil {
writer = v.GetClientWriter() 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 { func (v HttpAuthenticator) Server(conn net.Conn) net.Conn {
if v.config.Request == nil && v.config.Response == nil { if v.config.Request == nil && v.config.Response == nil {
return conn 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{} type HttpAuthenticatorFactory struct{}

Loading…
Cancel
Save