This commit is contained in:
wjqserver 2025-06-14 06:47:47 +08:00
parent a5bf7686bd
commit d2a0177015
6 changed files with 32 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package proxy
import (
"bytes"
"context"
"fmt"
"ghproxy/config"
@ -23,16 +24,17 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
go func() {
<-ctx.Done()
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
if req != nil {
req.Body.Close()
err := resp.Body.Close()
if err != nil {
logError("Failed to close response body: %v", err)
}
}
}()
rb := client.NewRequestBuilder(string(c.Request.Method()), u)
rb.NoDefaultHeaders()
rb.SetBody(c.Request.BodyStream())
rb.SetBody(bytes.NewBuffer(c.Request.Body()))
//rb.SetBody(c.RequestBodyStream())
rb.WithContext(ctx)
req, err = rb.Build()
@ -110,7 +112,12 @@ func ChunkedProxyRequest(ctx context.Context, c *app.RequestContext, u string, c
bodyReader = limitreader.NewRateLimitedReader(bodyReader, bandwidthLimit, int(bandwidthBurst), ctx)
}
defer bodyReader.Close()
defer func() {
err := bodyReader.Close()
if err != nil {
logError("Failed to close response body: %v", err)
}
}()
if MatcherShell(u) && matchString(matcher) && cfg.Shell.Editor {
// 判断body是不是gzip

View file

@ -17,15 +17,16 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
var (
req *http.Request
resp *http.Response
err error
)
go func() {
<-ctx.Done()
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
if req != nil {
req.Body.Close()
err = resp.Body.Close()
if err != nil {
logError("Failed to close response body: %v", err)
}
}
}()
@ -51,7 +52,7 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
rb.SetBody(reqBodyReader)
rb.WithContext(ctx)
req, err := rb.Build()
req, err = rb.Build()
if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return