Skip to content

Registry unnecessarily redirects blob HEAD requests to storage URL #3691

Open

Description

@aaronlehmann

When the "redirect" option is set to true, it applies to both HEAD and GET requests for blobs. The use case for GETs makes sense - this avoids the registry needing to proxy potentially large blobs from the storage backend to the client. But in the case of HEAD requests, the registry has already done a Stat, and could simply return that information to the client, but instead is issuing a redirect which can add considerable latency.

Am I missing anything that justifies this behavior? Would you be open to a PR to change ServeBlob to avoid redirecting HEAD requests?

Activity

  1. aaronlehmann commented on Jul 20, 2022

    @aaronlehmann
    Author

    @milosgajdos: Any thoughts on this?

  2. thaJeztah commented on Jul 27, 2022

    @thaJeztah
    Member

    From your description, the current behavior seems to comply with the HTTP specs; from RFC9110

    9.3.2. HEAD

    The HEAD method is identical to GET except that the server MUST NOT send content in the response. HEAD is used to obtain metadata about the selected representation without transferring its representation data, often for the sake of testing hypertext links or finding recent modifications.
    The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request method had been GET. However, a server MAY omit header fields for which a value is determined only while generating the content. For example, some servers buffer a dynamic response to GET until a minimum amount of data is generated so that they can more efficiently delimit small responses or make late decisions with regard to content selection. Such a response to GET might contain Content-Length and Vary fields, for example, that are not generated within a HEAD response. These minor inconsistencies are considered preferable to generating and discarding the content for a HEAD request, since HEAD is usually requested for the sake of efficiency.

    I guess this part may be relevant here;

    However, a server MAY omit header fields for which a value is determined only while generating the content.

    Performing the Stat would fall within that bit, but I guess it still needs to do the Stat in order to get the redirect location?

  3. aaronlehmann commented on Jul 27, 2022

    @aaronlehmann
    Author

    I don't think it would violate the RFC for the registry to generate its own response to a HEAD using the stat information it already has. In fact, the RFC seems to encourage this kind of optimization:

    These minor inconsistencies are considered preferable to generating and discarding the content for a HEAD request, since HEAD is usually requested for the sake of efficiency.

  4. thaJeztah commented on Jul 27, 2022

    @thaJeztah
    Member

    Thinking a bit further; so, in your case, there's HEAD request, but the client sees the redirect, so follows it?

    Wondering if that's the part to look into; basically; if the HEAD has the headers it need, it wouldn't have to follow the redirect and do another request. I think that would still comply to the RFC if we do the same for both GET and HEAD

    The cache headers should probably be omitted though in that case, as (IIUC) the redirect-to URL would be an ephemeral URL, so result should not be cached?

    w.Header().Set("ETag", fmt.Sprintf(`"%s"`, desc.Digest)) // If-None-Match handled by ServeContent
    w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%.f", blobCacheControlMaxAge.Seconds()))

  5. thaJeztah commented on Jul 27, 2022

    @thaJeztah
    Member

    I don't think it would violate the RFC for the registry to generate its own response to a HEAD using the stat information

    Right, so I think there's two bits here;

    1. Add the extra headers

      if w.Header().Get("Docker-Content-Digest") == "" {
      w.Header().Set("Docker-Content-Digest", desc.Digest.String())
      }

    2. Remove the Location header

    From the above, it feels like 1. is fine, but 2. may not be.

    However, I think clients should be allowed to ignore a Location header (don't follow redirects), so in case the response contains the headers they need, they can ignore the Location header and be done. Clients that don't have that logic would continue to follow the redirect.

  6. aaronlehmann commented on Jul 27, 2022

    @aaronlehmann
    Author

    However, I think clients should be allowed to ignore a Location header (don't follow redirects), so in case the response contains the headers they need, they can ignore the Location header and be done. Clients that don't have that logic would continue to follow the redirect.

    I'm pretty sure existing clients do not have htis logic. Anything written in Go (such as Docker) will follow redirects automatically by default, unless this is explicitly disabled. I do not see the distribution client disabling redirect following:

    req, err := http.NewRequestWithContext(ctx, "HEAD", u, nil)
    if err != nil {
    return distribution.Descriptor{}, err
    }
    resp, err := bs.client.Do(req)

    Anyway, I think it would make a lot of sense to at least provide an opt-in setting for people who prefer efficiency and cost savings over strict RFC compliance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions