MCPcopy Index your code
hub / github.com/dagger/dagger / FetchHTTPFile

Function FetchHTTPFile

core/http.go:434–523  ·  view source on GitHub ↗
(
	ctx context.Context,
	query *Query,
	opts FetchHTTPRequestOpts,
)

Source from the content-addressed store, hash-verified

432}
433
434func FetchHTTPFile(
435 ctx context.Context,
436 query *Query,
437 opts FetchHTTPRequestOpts,
438) (_ *HTTPFetchResult, rerr error) {
439 expectedChecksum, err := parseOptionalChecksum(opts.Checksum)
440 if err != nil {
441 return nil, fmt.Errorf("invalid checksum %q: %w", opts.Checksum.Value, err)
442 }
443
444 req, err := http.NewRequestWithContext(ctx, http.MethodGet, opts.URL, nil)
445 if err != nil {
446 return nil, err
447 }
448 req.Header.Set("Accept-Encoding", "identity")
449 if opts.AuthorizationHeader != "" {
450 req.Header.Set("Authorization", opts.AuthorizationHeader)
451 }
452
453 resp, err := doHTTPClientRequest(ctx, req)
454 if err != nil {
455 return nil, err
456 }
457 defer resp.Body.Close()
458
459 bkref, err := query.SnapshotManager().New(ctx, nil,
460 bkcache.WithRecordType(bkclient.UsageRecordTypeRegular),
461 bkcache.WithDescription(fmt.Sprintf("http url %s", opts.URL)),
462 )
463 if err != nil {
464 return nil, err
465 }
466 defer func() {
467 if rerr != nil && bkref != nil {
468 _ = bkref.Release(context.WithoutCancel(ctx))
469 }
470 }()
471
472 h := sha256.New()
473 err = MountRef(ctx, bkref, func(out string, _ *mount.Mount) error {
474 dest := filepath.Join(out, opts.Filename)
475 f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(opts.Permissions))
476 if err != nil {
477 return err
478 }
479 if _, err := io.Copy(io.MultiWriter(f, h), resp.Body); err != nil {
480 _ = f.Close()
481 return err
482 }
483 if err := f.Close(); err != nil {
484 return err
485 }
486
487 timestamp := time.Unix(0, 0)
488 if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
489 if parsed, err := http.ParseTime(lastModified); err == nil {
490 timestamp = parsed
491 }

Callers 1

httpMethod · 0.92

Calls 15

parseOptionalChecksumFunction · 0.85
doHTTPClientRequestFunction · 0.85
OpenFileMethod · 0.80
MountRefFunction · 0.70
SetMethod · 0.65
CloseMethod · 0.65
NewMethod · 0.65
SnapshotManagerMethod · 0.65
ReleaseMethod · 0.65
GetMethod · 0.65
CommitMethod · 0.65
PlatformMethod · 0.65

Tested by

no test coverage detected