MCPcopy Create free account
hub / github.com/compozy/agh / finalizeDownloadResponse

Function finalizeDownloadResponse

internal/registry/github/client.go:566–618  ·  view source on GitHub ↗
(
	response *http.Response,
	slug string,
	contentSize int64,
	maxArchiveSize int64,
)

Source from the content-addressed store, hash-verified

564}
565
566func finalizeDownloadResponse(
567 response *http.Response,
568 slug string,
569 contentSize int64,
570 maxArchiveSize int64,
571) (_ io.ReadCloser, contentType string, finalSize int64, err error) {
572 if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
573 err := responseError(response, "download", slug)
574 return nil, "", 0, joinErrors(
575 err,
576 closeResponseBody(response.Body, fmt.Sprintf("download response for %q", slug)),
577 )
578 }
579
580 contentType = strings.TrimSpace(response.Header.Get("Content-Type"))
581 if err := validateDownloadContentType(contentType); err != nil {
582 return nil, "", 0, joinErrors(
583 err,
584 closeResponseBody(response.Body, fmt.Sprintf("download response for %q", slug)),
585 )
586 }
587
588 if response.ContentLength > 0 {
589 if response.ContentLength > maxArchiveSize {
590 return nil, "", 0, joinErrors(
591 fmt.Errorf(
592 "github: download for %q: %w: size=%d limit=%d",
593 slug,
594 registry.ErrArchiveTooLargeCompressed,
595 response.ContentLength,
596 maxArchiveSize,
597 ),
598 closeResponseBody(response.Body, fmt.Sprintf("download response for %q", slug)),
599 )
600 }
601 contentSize = response.ContentLength
602 }
603
604 reader, written, err := spoolDownloadResponse(response.Body, slug, maxArchiveSize)
605 closeErr := closeResponseBody(response.Body, fmt.Sprintf("download response for %q", slug))
606 if err != nil {
607 return nil, "", 0, joinErrors(fmt.Errorf("github: spool download for %q: %w", slug, err), closeErr)
608 }
609 if closeErr != nil {
610 return nil, "", 0, closeErr
611 }
612 if contentSize <= 0 {
613 contentSize = written
614 }
615 response.Body = http.NoBody
616
617 return reader, contentType, contentSize, nil
618}
619
620func (c *Client) newRequest(ctx context.Context, rawURL string, accept string) (*http.Request, error) {
621 request, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, http.NoBody)

Callers 1

DownloadMethod · 0.85

Calls 6

closeResponseBodyFunction · 0.85
responseErrorFunction · 0.70
joinErrorsFunction · 0.70
spoolDownloadResponseFunction · 0.70
GetMethod · 0.65

Tested by

no test coverage detected