From ace795fe9df55689ed529696dd9e9d127d1308bb Mon Sep 17 00:00:00 2001 From: wjqserver <114663932+WJQSERVER@users.noreply.github.com> Date: Tue, 29 Apr 2025 22:13:11 +0800 Subject: [PATCH] revert gitreq body stream --- CHANGELOG.md | 4 ++++ DEV-VERSION | 2 +- proxy/gitreq.go | 28 +++++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a8c609..f6bdb26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # 更新日志 +25w33b - 2025-04-29 +--- +- REVERT: 为`git clone`部分回滚 3.1.0中的 "使用`bodystream`进行req方向的body复制, 而不是使用额外的`buffer reader`" 修改 + 25w33a - 2025-04-29 --- - ADD: 实验性的raw Header处置, 用于应对Github对zh-CN的限制 diff --git a/DEV-VERSION b/DEV-VERSION index 23f75d9..58d1f65 100644 --- a/DEV-VERSION +++ b/DEV-VERSION @@ -1 +1 @@ -25w33a \ No newline at end of file +25w33b \ No newline at end of file diff --git a/proxy/gitreq.go b/proxy/gitreq.go index e48598f..a050e29 100644 --- a/proxy/gitreq.go +++ b/proxy/gitreq.go @@ -1,6 +1,7 @@ package proxy import ( + "bytes" "context" "fmt" "ghproxy/config" @@ -13,6 +14,10 @@ import ( func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, mode string) { method := string(c.Request.Method()) + bodyReader := bytes.NewBuffer(c.Request.Body()) + + //bodyReader := c.Request.BodyStream() + if cfg.GitClone.Mode == "cache" { userPath, repoPath, remainingPath, queryParams, err := extractParts(u) if err != nil { @@ -28,11 +33,16 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co ) if cfg.GitClone.Mode == "cache" { - req, err := gitclient.NewRequest(method, u, c.Request.BodyStream()) + rb := gitclient.NewRequestBuilder(method, u) + rb.NoDefaultHeaders() + rb.SetBody(bodyReader) + + req, err := rb.Build() if err != nil { HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) return } + setRequestHeaders(c, req, cfg, "clone") AuthPassThrough(c, cfg, req) @@ -42,11 +52,16 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co return } } else { - req, err := client.NewRequest(method, u, c.Request.BodyStream()) + rb := client.NewRequestBuilder(string(c.Request.Method()), u) + rb.NoDefaultHeaders() + rb.SetBody(bodyReader) + + req, err := rb.Build() if err != nil { HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) return } + setRequestHeaders(c, req, cfg, "clone") AuthPassThrough(c, cfg, req) @@ -74,7 +89,8 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co for key, values := range resp.Header { for _, value := range values { - c.Header(key, value) + //c.Header(key, value) + c.Response.Header.Add(key, value) } } @@ -106,5 +122,11 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co c.Response.Header.Set("Expires", "0") } + bodySize, _ := strconv.Atoi(contentLength) + + if contentLength != "" { + c.SetBodyStream(resp.Body, bodySize) + return + } c.SetBodyStream(resp.Body, -1) }