docs: record auth hash alignment and validation results
This commit is contained in:
1 parent
22887fef03
commit
f7819904b9
51 files changed
+1792
-542
No files matched your search
@@ -22,12 +22,25 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// httpClient has no blanket timeout since it streams large media files;
|
||||
// callers bound individual calls with a context deadline instead.
|
||||
var httpClient = &http.Client{}
|
||||
// httpClient has no total timeout since it streams large media files. The
|
||||
// transport still bounds how long a request may wait for response headers;
|
||||
// callers that need a shorter bound add a context deadline as well.
|
||||
var httpClient = newHTTPClient()
|
||||
|
||||
func newHTTPClient() *http.Client {
|
||||
var transport *http.Transport
|
||||
if defaultTransport, ok := http.DefaultTransport.(*http.Transport); ok {
|
||||
transport = defaultTransport.Clone()
|
||||
} else {
|
||||
transport = &http.Transport{Proxy: http.ProxyFromEnvironment}
|
||||
}
|
||||
transport.ResponseHeaderTimeout = 10 * time.Second
|
||||
return &http.Client{Transport: transport}
|
||||
}
|
||||
|
||||
var forwardRequestHeaders = []string{"Range", "If-Modified-Since", "If-None-Match", "If-Range"}
|
||||
var forwardResponseHeaders = []string{"Content-Type", "Content-Length", "Content-Range", "Accept-Ranges", "Last-Modified", "ETag", "Content-Disposition"}
|
||||
@@ -69,6 +82,17 @@ type Client struct {
|
||||
// multiple candidates are actually used (union for discovery, first-match
|
||||
// for reads, primary-only for writes).
|
||||
BaseURLs func(ctx context.Context) []string
|
||||
|
||||
thumbnailMu sync.Mutex
|
||||
thumbnailPaths map[string]thumbnailPathEntry
|
||||
}
|
||||
|
||||
const thumbnailPathCacheTTL = 5 * time.Minute
|
||||
const thumbnailPathCacheLimit = 2048
|
||||
|
||||
type thumbnailPathEntry struct {
|
||||
path string
|
||||
expires time.Time
|
||||
}
|
||||
|
||||
// New returns a Client backed by a single CarrierNode instance. baseURL is
|
||||
@@ -536,15 +560,103 @@ func (c *Client) ProxyThumbnail(w http.ResponseWriter, r *http.Request, project,
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
cacheKey := project + "\x00" + id
|
||||
cachedPath := c.cachedThumbnailPath(cacheKey)
|
||||
if cachedPath != "" {
|
||||
ordered := make([]string, 0, len(paths))
|
||||
ordered = append(ordered, cachedPath)
|
||||
for _, path := range paths {
|
||||
if path != cachedPath {
|
||||
ordered = append(ordered, path)
|
||||
}
|
||||
}
|
||||
paths = ordered
|
||||
}
|
||||
for _, path := range paths {
|
||||
if exists, _, _, probeErr := c.Probe(r.Context(), path); probeErr == nil && exists {
|
||||
c.Proxy(w, r, path)
|
||||
for _, baseURL := range c.candidateBaseURLs(r.Context()) {
|
||||
request, err := c.newRequest(r.Context(), baseURL, r.Method, path, nil)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, header := range forwardRequestHeaders {
|
||||
if value := r.Header.Get(header); value != "" {
|
||||
request.Header.Set(header, value)
|
||||
}
|
||||
}
|
||||
response, err := httpClient.Do(request)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if response.StatusCode >= http.StatusBadRequest {
|
||||
response.Body.Close()
|
||||
if path == cachedPath {
|
||||
c.forgetThumbnailPath(cacheKey)
|
||||
}
|
||||
continue
|
||||
}
|
||||
for _, header := range forwardResponseHeaders {
|
||||
if value := response.Header.Get(header); value != "" {
|
||||
w.Header().Set(header, value)
|
||||
}
|
||||
}
|
||||
w.WriteHeader(response.StatusCode)
|
||||
if r.Method != http.MethodHead {
|
||||
_, _ = io.Copy(w, response.Body)
|
||||
}
|
||||
response.Body.Close()
|
||||
c.rememberThumbnailPath(cacheKey, path)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *Client) cachedThumbnailPath(key string) string {
|
||||
c.thumbnailMu.Lock()
|
||||
defer c.thumbnailMu.Unlock()
|
||||
entry, ok := c.thumbnailPaths[key]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
if !time.Now().Before(entry.expires) {
|
||||
delete(c.thumbnailPaths, key)
|
||||
return ""
|
||||
}
|
||||
return entry.path
|
||||
}
|
||||
|
||||
func (c *Client) rememberThumbnailPath(key, path string) {
|
||||
c.thumbnailMu.Lock()
|
||||
defer c.thumbnailMu.Unlock()
|
||||
if c.thumbnailPaths == nil {
|
||||
c.thumbnailPaths = make(map[string]thumbnailPathEntry)
|
||||
}
|
||||
now := time.Now()
|
||||
for cacheKey, entry := range c.thumbnailPaths {
|
||||
if !now.Before(entry.expires) {
|
||||
delete(c.thumbnailPaths, cacheKey)
|
||||
}
|
||||
}
|
||||
if len(c.thumbnailPaths) >= thumbnailPathCacheLimit {
|
||||
var oldestKey string
|
||||
var oldest time.Time
|
||||
first := true
|
||||
for cacheKey, entry := range c.thumbnailPaths {
|
||||
if first || entry.expires.Before(oldest) {
|
||||
oldestKey, oldest, first = cacheKey, entry.expires, false
|
||||
}
|
||||
}
|
||||
delete(c.thumbnailPaths, oldestKey)
|
||||
}
|
||||
c.thumbnailPaths[key] = thumbnailPathEntry{path: path, expires: now.Add(thumbnailPathCacheTTL)}
|
||||
}
|
||||
|
||||
func (c *Client) forgetThumbnailPath(key string) {
|
||||
c.thumbnailMu.Lock()
|
||||
delete(c.thumbnailPaths, key)
|
||||
c.thumbnailMu.Unlock()
|
||||
}
|
||||
|
||||
// FetchThumbnailToScratch downloads a stored thumbnail into an ephemeral local file, looking
|
||||
// in the project's thumbnails mount and then the legacy root. Callers must invoke the returned
|
||||
// cleanup once done.
|
||||
|
||||
Reference in new issue
Block a user