MCPcopy Index your code
hub / github.com/docker/docker-agent / Read

Method Read

pkg/config/sources.go:298–397  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

296}
297
298func (a urlSource) Read(ctx context.Context) ([]byte, error) {
299 if !a.unsafe {
300 if err := validateAgentURL(a.url); err != nil {
301 return nil, err
302 }
303 }
304
305 cacheDir := getURLCacheDir()
306 urlHash := hashURL(a.url)
307 cachePath := filepath.Join(cacheDir, urlHash)
308 etagPath := cachePath + ".etag"
309
310 // Read cached ETag if available
311 cachedETag := ""
312 if etagData, err := os.ReadFile(etagPath); err == nil {
313 cachedETag = string(etagData)
314 }
315
316 req, err := http.NewRequestWithContext(ctx, http.MethodGet, a.url, http.NoBody)
317 if err != nil {
318 return nil, fmt.Errorf("creating request: %w", err)
319 }
320
321 // Include If-None-Match header if we have a cached ETag
322 if cachedETag != "" {
323 req.Header.Set("If-None-Match", cachedETag)
324 }
325
326 a.addGitHubAuth(ctx, req)
327 a.addDockerAuth(ctx, req)
328
329 client := httpclient.NewHTTPClient(ctx)
330 if !a.unsafe {
331 if isLocalhostHTTP(a.url) {
332 client = &http.Client{
333 Timeout: 60 * time.Second,
334 CheckRedirect: httpclient.LocalhostOnlyRedirects(10),
335 }
336 } else {
337 client = &http.Client{
338 Timeout: 60 * time.Second,
339 Transport: httpclient.NewSSRFSafeTransport(),
340 CheckRedirect: httpclient.HTTPSOnlyRedirects(10),
341 }
342 }
343 }
344
345 resp, err := client.Do(req)
346 if err != nil {
347 // Network error - try to use cached version
348 if cachedData, cacheErr := os.ReadFile(cachePath); cacheErr == nil {
349 slog.DebugContext(ctx, "Network error fetching URL, using cached version", "url", a.url, "error", err)
350 return cachedData, nil
351 }
352 return nil, fmt.Errorf("fetching %s: %w", a.url, err)
353 }
354 defer resp.Body.Close()
355

Callers

nothing calls this directly

Calls 15

addGitHubAuthMethod · 0.95
addDockerAuthMethod · 0.95
DoMethod · 0.95
NewHTTPClientFunction · 0.92
LocalhostOnlyRedirectsFunction · 0.92
NewSSRFSafeTransportFunction · 0.92
HTTPSOnlyRedirectsFunction · 0.92
validateAgentURLFunction · 0.85
getURLCacheDirFunction · 0.85
hashURLFunction · 0.85
isLocalhostHTTPFunction · 0.85
ReadFileMethod · 0.80

Tested by

no test coverage detected