@@ -187,10 +187,10 @@ func NewMitmProxyHandler(opt ...Option) (MitmProxyHandler, error) {
187187 }
188188
189189 dialFn := func (ctx context.Context , network , addr string ) (net.Conn , error ) {
190- if preboundConn , ok := ctx .Value (connContextKey ).(net.Conn ); ok {
191- return preboundConn , nil
190+ if conn , ok := ctx .Value (connContextKey ).(net.Conn ); ok {
191+ return conn , nil
192192 }
193- return nil , errors .New ("no prebound connection " )
193+ return nil , errors .New ("connContextKey missing in context " )
194194 }
195195
196196 includeMatcher , excludeMatcher := newTrieNode (), newTrieNode ()
@@ -612,12 +612,11 @@ func (r *mitmProxyHandler) handleH2CRequest(ctx context.Context, rw http.Respons
612612 }
613613 // Handle Upgrade to h2c (RFC 7540 Section 3.2)
614614 if isH2CUpgrade (req .Header ) {
615- removeHopByHopRequestHeaders (req .Header )
615+ removeProxyHeaders (req .Header )
616616 conn , settings , err := upgradeH2C (rw , req )
617617 if err != nil {
618618 return false , err
619619 }
620- req .Header .Del (HttpHeaderHttp2Settings )
621620 ctx = context .WithValue (ctx , connContextKey , dstConn )
622621 r .h2s .ServeConn (conn , & http2.ServeConnOpts {
623622 Context : ctx ,
@@ -674,6 +673,7 @@ func (r *mitmProxyHandler) distinguishHTTPRequest(ctx context.Context, srcConn,
674673 }
675674 }
676675
676+ removeProxyHeaders (request .Header )
677677 // patch the new request to the request context
678678 reqCtx .Request = request
679679 newCtx = AppendToRequestContext (ctx , reqCtx .Hostport , reqCtx .Request , false )
@@ -797,7 +797,7 @@ func (r *mitmProxyHandler) relayConnForWS(ctx context.Context, srcConn, dstConn
797797func (r * mitmProxyHandler ) relayConnForHTTP (ctx context.Context , srcConn , dstConn net.Conn ) (err error ) {
798798 reqCtx , _ := FromRequestContext (ctx )
799799 // set to request context
800- reqCtx . Request = reqCtx . Request . WithContext ( context .WithValue (ctx , connContextKey , dstConn ) )
800+ ctx = context .WithValue (ctx , connContextKey , dstConn )
801801
802802 response , err := r .roundTripWithContext (ctx , reqCtx .Request )
803803 if err != nil {
@@ -809,6 +809,8 @@ func (r *mitmProxyHandler) relayConnForHTTP(ctx context.Context, srcConn, dstCon
809809}
810810
811811func (r * mitmProxyHandler ) roundTripWithContext (ctx context.Context , req * http.Request ) (response * http.Response , err error ) {
812+ conn , _ := ctx .Value (connContextKey ).(net.Conn )
813+ req = req .WithContext (context .WithValue (req .Context (), connContextKey , conn ))
812814 // Only one http interceptor will be invoked
813815 if r .httpInt != nil {
814816 response , err = r .httpInt (ctx , req , HTTPDelegatedInvokerFunc (r .transport .RoundTrip ))
@@ -843,13 +845,20 @@ func (r *mitmProxyHandler) serveHTTP2Handler(ctx context.Context) http.Handler {
843845 }
844846 */
845847 if req .URL .Scheme == "" {
846- req .URL .Scheme = "https"
848+ if req .TLS != nil {
849+ req .URL .Scheme = "https"
850+ } else {
851+ req .URL .Scheme = "http"
852+ }
847853 }
848854 if req .URL .Host == "" {
849855 req .URL .Host = req .Host
850856 }
851857 // the request body size may be zero
852858 if req .ContentLength == 0 {
859+ if req .Body != nil {
860+ req .Body .Close ()
861+ }
853862 req .Body = http .NoBody
854863 req .GetBody = func () (io.ReadCloser , error ) { return http .NoBody , nil }
855864 }
0 commit comments