diff --git a/simplehlfhr/simple.go b/simplehlfhr/simple.go index 9d13817..b275f87 100644 --- a/simplehlfhr/simple.go +++ b/simplehlfhr/simple.go @@ -48,20 +48,21 @@ func ListenAndServeTLS(srv *http.Server, certFile, keyFile string) error { } func NewListener(inner net.Listener, srv *http.Server) net.Listener { - l, ok := inner.(*Listener) - if !ok { - l = &Listener{Listener: inner} + if _, ok := inner.(isMyListener); ok { + return inner + } + return &listener{ + Listener: inner, + srv: srv, } - l.HttpServer = srv - return l } -type Listener struct { +type listener struct { net.Listener - HttpServer *http.Server + srv *http.Server } -func (l *Listener) Accept() (c net.Conn, err error) { +func (l *listener) Accept() (c net.Conn, err error) { c, err = l.Listener.Accept() if err == nil { c = &conn{ @@ -72,15 +73,21 @@ func (l *Listener) Accept() (c net.Conn, err error) { return } +type isMyListener interface { + IsHttpsListenerForHttpRedirect() +} + +func (l *listener) IsHttpsListenerForHttpRedirect() {} + type conn struct { net.Conn // Reading TLS if nil - l *Listener + l *listener } func (c *conn) maxHeaderLen() int { - if c.l.HttpServer != nil && c.l.HttpServer.MaxHeaderBytes != 0 { - return c.l.HttpServer.MaxHeaderBytes + if c.l.srv != nil && c.l.srv.MaxHeaderBytes != 0 { + return c.l.srv.MaxHeaderBytes } return http.DefaultMaxHeaderBytes } diff --git a/src_conn.go b/src_conn.go index bf14aaf..93e5688 100644 --- a/src_conn.go +++ b/src_conn.go @@ -12,12 +12,12 @@ var ErrHttpOnHttpsPort = errors.New("client sent an HTTP request to an HTTPS ser type conn struct { net.Conn // Reading TLS if nil - l *Listener + l *listener } func (c *conn) log(v ...any) { - if c.l.HttpServer != nil && c.l.HttpServer.ErrorLog != nil { - c.l.HttpServer.ErrorLog.Print(v...) + if c.l.srv != nil && c.l.srv.ErrorLog != nil { + c.l.srv.ErrorLog.Print(v...) } else { log.Print(v...) } @@ -25,7 +25,7 @@ func (c *conn) log(v ...any) { func (c *conn) readRequest(b []byte, n int) (req *http.Request, errStr string) { rd := &MaxHeaderBytesReader{Rd: c.Conn} - rd.SetMax(c.l.HttpServer) + rd.SetMax(c.l.srv) rd.Max -= n br := NewBufioReaderWithBytes(b, n, rd) @@ -68,9 +68,9 @@ func (c *conn) Read(b []byte) (int, error) { // Response w := newResponse() - if c.l.HttpOnHttpsPortErrorHandler != nil { + if c.l.handler != nil { // Handler - c.l.HttpOnHttpsPortErrorHandler.ServeHTTP(w, r) + c.l.handler.ServeHTTP(w, r) } else { // Redirect RedirectToHttps(w, r, 302) diff --git a/src_listener.go b/src_listener.go index 37dc54a..b4868ef 100644 --- a/src_listener.go +++ b/src_listener.go @@ -5,28 +5,28 @@ import ( "net/http" ) -type Listener struct { +type listener struct { net.Listener - - HttpServer *http.Server - HttpOnHttpsPortErrorHandler http.Handler + srv *http.Server + handler http.Handler } func NewListener( inner net.Listener, - httpServer *http.Server, + srv *http.Server, httpOnHttpsPortErrorHandler http.Handler, ) net.Listener { - l, ok := inner.(*Listener) - if !ok { - l = &Listener{Listener: inner} + if _, ok := inner.(isMyListener); ok { + return inner + } + return &listener{ + Listener: inner, + srv: srv, + handler: httpOnHttpsPortErrorHandler, } - l.HttpServer = httpServer - l.HttpOnHttpsPortErrorHandler = httpOnHttpsPortErrorHandler - return l } -func (l *Listener) Accept() (c net.Conn, err error) { +func (l *listener) Accept() (c net.Conn, err error) { c, err = l.Listener.Accept() if err == nil { // Hijacking net.Conn @@ -37,3 +37,9 @@ func (l *Listener) Accept() (c net.Conn, err error) { } return } + +type isMyListener interface { + IsHttpsListenerForHttpRedirect() +} + +func (l *listener) IsHttpsListenerForHttpRedirect() {}