fix weird problem that the request redirect from mirrors.ustc.edu.cn to down.988884.xyz
This commit is contained in:
parent
b2445cf774
commit
08eed185cf
@ -1,6 +1,6 @@
|
|||||||
services:
|
services:
|
||||||
cache-proxy:
|
cache-proxy:
|
||||||
image: 100.64.0.2/guochao/cache-proxy:20241218
|
image: 100.64.0.2/guochao/cache-proxy:20250118
|
||||||
build: .
|
build: .
|
||||||
|
|
||||||
pull_policy: always
|
pull_policy: always
|
||||||
|
23
server.go
23
server.go
@ -19,6 +19,16 @@ import (
|
|||||||
|
|
||||||
var zeroTime time.Time
|
var zeroTime time.Time
|
||||||
|
|
||||||
|
var (
|
||||||
|
httpClient = http.Client{
|
||||||
|
|
||||||
|
// No redirect!
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
return http.ErrUseLastResponse
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type StreamObject struct {
|
type StreamObject struct {
|
||||||
Headers http.Header
|
Headers http.Header
|
||||||
Buffer *bytes.Buffer
|
Buffer *bytes.Buffer
|
||||||
@ -302,8 +312,8 @@ func (server *Server) streamOnline(w http.ResponseWriter, r *http.Request, mtime
|
|||||||
|
|
||||||
if response.ContentLength > 0 {
|
if response.ContentLength > 0 {
|
||||||
if memoryObject.Offset == int(response.ContentLength) && err != nil {
|
if memoryObject.Offset == int(response.ContentLength) && err != nil {
|
||||||
if err != io.EOF {
|
if !(errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF)) {
|
||||||
slog.With("length", memoryObject.Offset, "error", err, "upstreamIdx", selectedIdx).Debug("something happened during download. but response body is read as whole. so error is reset to nil")
|
slog.With("read-length", memoryObject.Offset, "content-length", response.ContentLength, "error", err, "upstreamIdx", selectedIdx).Debug("something happened during download. but response body is read as whole. so error is reset to nil")
|
||||||
}
|
}
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
@ -422,6 +432,9 @@ func (server *Server) fastesUpstream(r *http.Request, lastModified time.Time) (r
|
|||||||
if response == nil {
|
if response == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if response.StatusCode != http.StatusOK {
|
||||||
|
return
|
||||||
|
}
|
||||||
locked := returnLock.TryLock()
|
locked := returnLock.TryLock()
|
||||||
if !locked {
|
if !locked {
|
||||||
return
|
return
|
||||||
@ -487,12 +500,12 @@ func (server *Server) tryUpstream(ctx context.Context, upstreamIdx int, r *http.
|
|||||||
request.Header.Set("If-Modified-Since", lastModified.Format(time.RFC1123))
|
request.Header.Set("If-Modified-Since", lastModified.Format(time.RFC1123))
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, k := range []string{"User-Agent"} {
|
for _, k := range []string{"User-Agent", "Accept"} {
|
||||||
if _, exists := request.Header[k]; exists {
|
if _, exists := r.Header[k]; exists {
|
||||||
request.Header.Set(k, r.Header.Get(k))
|
request.Header.Set(k, r.Header.Get(k))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
response, err = http.DefaultClient.Do(request)
|
response, err = httpClient.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user