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:
|
||||
cache-proxy:
|
||||
image: 100.64.0.2/guochao/cache-proxy:20241218
|
||||
image: 100.64.0.2/guochao/cache-proxy:20250118
|
||||
build: .
|
||||
|
||||
pull_policy: always
|
||||
|
23
server.go
23
server.go
@ -19,6 +19,16 @@ import (
|
||||
|
||||
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 {
|
||||
Headers http.Header
|
||||
Buffer *bytes.Buffer
|
||||
@ -302,8 +312,8 @@ func (server *Server) streamOnline(w http.ResponseWriter, r *http.Request, mtime
|
||||
|
||||
if response.ContentLength > 0 {
|
||||
if memoryObject.Offset == int(response.ContentLength) && err != nil {
|
||||
if err != io.EOF {
|
||||
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")
|
||||
if !(errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF)) {
|
||||
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
|
||||
}
|
||||
@ -422,6 +432,9 @@ func (server *Server) fastesUpstream(r *http.Request, lastModified time.Time) (r
|
||||
if response == nil {
|
||||
return
|
||||
}
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return
|
||||
}
|
||||
locked := returnLock.TryLock()
|
||||
if !locked {
|
||||
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))
|
||||
}
|
||||
|
||||
for _, k := range []string{"User-Agent"} {
|
||||
if _, exists := request.Header[k]; exists {
|
||||
for _, k := range []string{"User-Agent", "Accept"} {
|
||||
if _, exists := r.Header[k]; exists {
|
||||
request.Header.Set(k, r.Header.Get(k))
|
||||
}
|
||||
}
|
||||
response, err = http.DefaultClient.Do(request)
|
||||
response, err = httpClient.Do(request)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user