revert gitreq body stream

This commit is contained in:
wjqserver 2025-04-29 22:13:11 +08:00
parent 3f51e5319a
commit ace795fe9d
3 changed files with 30 additions and 4 deletions

View file

@ -1,5 +1,9 @@
# 更新日志 # 更新日志
25w33b - 2025-04-29
---
- REVERT: 为`git clone`部分回滚 3.1.0中的 "使用`bodystream`进行req方向的body复制, 而不是使用额外的`buffer reader`" 修改
25w33a - 2025-04-29 25w33a - 2025-04-29
--- ---
- ADD: 实验性的raw Header处置, 用于应对Github对zh-CN的限制 - ADD: 实验性的raw Header处置, 用于应对Github对zh-CN的限制

View file

@ -1 +1 @@
25w33a 25w33b

View file

@ -1,6 +1,7 @@
package proxy package proxy
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"ghproxy/config" "ghproxy/config"
@ -13,6 +14,10 @@ import (
func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, mode string) { func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Config, mode string) {
method := string(c.Request.Method()) method := string(c.Request.Method())
bodyReader := bytes.NewBuffer(c.Request.Body())
//bodyReader := c.Request.BodyStream()
if cfg.GitClone.Mode == "cache" { if cfg.GitClone.Mode == "cache" {
userPath, repoPath, remainingPath, queryParams, err := extractParts(u) userPath, repoPath, remainingPath, queryParams, err := extractParts(u)
if err != nil { 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" { 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 { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
} }
setRequestHeaders(c, req, cfg, "clone") setRequestHeaders(c, req, cfg, "clone")
AuthPassThrough(c, cfg, req) AuthPassThrough(c, cfg, req)
@ -42,11 +52,16 @@ func GitReq(ctx context.Context, c *app.RequestContext, u string, cfg *config.Co
return return
} }
} else { } 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 { if err != nil {
HandleError(c, fmt.Sprintf("Failed to create request: %v", err)) HandleError(c, fmt.Sprintf("Failed to create request: %v", err))
return return
} }
setRequestHeaders(c, req, cfg, "clone") setRequestHeaders(c, req, cfg, "clone")
AuthPassThrough(c, cfg, req) 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 key, values := range resp.Header {
for _, value := range values { 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") c.Response.Header.Set("Expires", "0")
} }
bodySize, _ := strconv.Atoi(contentLength)
if contentLength != "" {
c.SetBodyStream(resp.Body, bodySize)
return
}
c.SetBodyStream(resp.Body, -1) c.SetBodyStream(resp.Body, -1)
} }