Skip to content

Commit acbd7f8

Browse files
committed
fix h2c request
1 parent 57f1cb5 commit acbd7f8

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

examples/dumper/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ func httpInterceptor(ctx context.Context, req *http.Request, invoker mitmpgo.HTT
253253
slog.Duration("request_latency", time.Since(md.RequestProcessedTs)),
254254
slog.String("status", rsp.Status),
255255
slog.String("protocol", rsp.Proto),
256+
slog.Any("headers", map[string][]string(rsp.Header)),
256257
)
257258

258259
rsp.Body, err = newBodyDecoder(rsp.Body, rsp.Header.Get("Content-Encoding"), CHUNK_TYPE_RSP)

header.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ var (
4949
}
5050
)
5151

52+
func removeProxyHeaders(header http.Header) {
53+
header.Del(HttpHeaderProxyAuthenticate)
54+
header.Del(HttpHeaderProxyAuthorization)
55+
header.Del(HttpHeaderProxyConnection)
56+
header.Del(HttpHeaderProxyAgent)
57+
}
58+
5259
func removeHopByHopRequestHeaders(header http.Header) {
5360
for _, h := range hopByHopHeaders {
5461
header.Del(h)

mitm.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
797797
func (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

811811
func (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

Comments
 (0)