refactor(net): pass request header (#8031 close #8008)

* refactor(net): pass request header

* feat(proxy): add `Etag` to response header

* refactor
This commit is contained in:
j2rong4cn
2025-03-01 18:35:34 +08:00
committed by GitHub
parent 646c7bcd21
commit 4145734c18
14 changed files with 56 additions and 44 deletions

View File

@ -382,6 +382,9 @@ func (d *downloader) tryDownloadChunk(params *HttpRequestParams, ch *chunk) (int
if resp == nil {
return 0, err
}
if resp.StatusCode == http.StatusRequestedRangeNotSatisfiable {
return 0, err
}
if ch.id == 0 { //第1个任务 有限的重试,超过重试就会结束请求
switch resp.StatusCode {
default:

View File

@ -114,7 +114,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, name string, modTime time
// 使用请求的Context
// 不然从sendContent读不到数据即使请求断开CopyBuffer也会一直堵塞
ctx := r.Context()
ctx := context.WithValue(r.Context(), "request_header", &r.Header)
switch {
case len(ranges) == 0:
reader, err := RangeReadCloser.RangeRead(ctx, http_range.Range{Length: -1})

View File

@ -71,6 +71,7 @@ func checkIfMatch(w http.ResponseWriter, r *http.Request) condResult {
if im == "" {
return condNone
}
r.Header.Del("If-Match")
for {
im = textproto.TrimString(im)
if len(im) == 0 {
@ -98,7 +99,11 @@ func checkIfMatch(w http.ResponseWriter, r *http.Request) condResult {
func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
ius := r.Header.Get("If-Unmodified-Since")
if ius == "" || isZeroTime(modtime) {
if ius == "" {
return condNone
}
r.Header.Del("If-Unmodified-Since")
if isZeroTime(modtime) {
return condNone
}
t, err := http.ParseTime(ius)
@ -120,6 +125,7 @@ func checkIfNoneMatch(w http.ResponseWriter, r *http.Request) condResult {
if inm == "" {
return condNone
}
r.Header.Del("If-None-Match")
buf := inm
for {
buf = textproto.TrimString(buf)
@ -150,7 +156,11 @@ func checkIfModifiedSince(r *http.Request, modtime time.Time) condResult {
return condNone
}
ims := r.Header.Get("If-Modified-Since")
if ims == "" || isZeroTime(modtime) {
if ims == "" {
return condNone
}
r.Header.Del("If-Modified-Since")
if isZeroTime(modtime) {
return condNone
}
t, err := http.ParseTime(ims)
@ -174,6 +184,7 @@ func checkIfRange(w http.ResponseWriter, r *http.Request, modtime time.Time) con
if ir == "" {
return condNone
}
r.Header.Del("If-Range")
etag, _ := scanETag(ir)
if etag != "" {
if etagStrongMatch(etag, w.Header().Get("Etag")) {