Skip to content

fix(handler): probe with GET when the upstream refuses HEAD - #463

Open
ravi-pplx wants to merge 2 commits into
block:mainfrom
perplexityai:fix/handler-head-get-probe
Open

ravi-pplx wants to merge 2 commits into
block:mainfrom
perplexityai:fix/handler-head-get-probe

Conversation

@ravi-pplx

@ravi-pplx ravi-pplx commented Nov 22, 2026 •

Copy link
Copy Markdown

A HEAD miss is forwarded upstream as HEAD, and the client gets whatever the upstream answers. Some upstreams refuse HEAD for objects they serve on GET:

  • A presigned GET URL includes the method in its signature, so S3-compatible stores answer HEAD with 403 whether or not the object exists. This also applies when the upstream redirects to such a URL, because Go's client follows the redirect with HEAD.
  • Servers that only route GET answer 405 or 501.

A client probing through the cache, for example for the size before a download, then sees an error for an object it can download.

Change

  • When a HEAD miss gets 403, 405 or 501, forwardHead repeats the probe as a GET and answers with that response's status and headers.
  • Every other status is returned as before. This includes 401 and 404, which usually mean the caller or the object is the problem, and 429 and 5xx, so a struggling upstream isn't asked twice.
  • The handler never reads the GET body and caches nothing. Closing the response drops the HTTP/1.1 connection or resets the HTTP/2 stream. Since Go 1.27, the transport first drains a body of up to 256 KiB in the background, for at most 50 ms, so it can reuse the connection.
  • When the client didn't send Accept-Encoding, the GET sends Accept-Encoding: identity. Otherwise Go's transport asks for gzip on its own, which it never does for HEAD, and when the server compresses, the transport decompresses and removes Content-Length, so the probe would lose the size. A client that sends Accept-Encoding has it forwarded unchanged, as before.
  • Revalidation still uses HEAD. For these upstreams a refused revalidation already counts as changed and refetches the entry, so that path stays correct.

Behaviour changes

  • A HEAD miss that gets 403, 405 or 501 now costs a second upstream request, and the client gets the GET response's status and headers.
  • For a presigned URL to a missing object, that turns the 403 into the store's 404.

Testing

  • TestHeadMissRefusedByUpstream serves a HEAD miss from an upstream that answers HEAD and GET with different statuses. It covers:
    • 403, 405 and 501 followed by a successful GET, and 403 followed by 404;
    • a client that accepts gzip;
    • 401, 404, 429 and 502, which must not be retried.
  • The test's upstream compresses whenever the request allows it, and holds the GET body back until the client hangs up. The test checks the status, Content-Length, Content-Encoding, which methods reached the upstream, and that the probe hung up without waiting for the body.
  • On main, the five cases where HEAD is refused fail. Without the identity encoding, the successful GET cases that don't send Accept-Encoding lose Content-Length. If the probe reads the body, every case with a successful GET fails.
  • The test also passes with Go 1.27.
  • Manual check: cachewd from main and from this branch, with host strategies in front of MinIO and of a local server:
HEAD through cachewd main This PR
Presigned GET URL, object exists 403 200, Content-Length: 7, ETag
Presigned GET URL, object missing 403 404
302 to a presigned GET URL 403 200, Content-Length: 7, ETag
Server answering HEAD with 405, gzip-capable 405 200, identity Content-Length
Same, client sends Accept-Encoding: gzip 405 200, Content-Encoding: gzip, compressed length
Missing object, HEAD answers 404 404 404, no GET
1 GiB object, HEAD answers 403 403 200 in about 1.5 ms

For the 1 GiB object, the upstream had sent at most 384 KiB over HTTP/1.1 when the connection closed, and at most 64 KiB over HTTP/2 when the stream was reset.

Created with Perplexity

A HEAD miss is forwarded upstream as HEAD. A presigned GET URL includes
the method in its signature, so S3-compatible stores answer HEAD with 403
even when GET would return the object, and servers without a HEAD route
answer 405 or 501. Clients probing through the cache then got an error
for an object they could download, including when the upstream redirects
to such a URL.

When the HEAD miss gets 403, 405 or 501, the probe is repeated as a GET
and only its status and headers are used. The handler never reads the
body and caches nothing; closing it drops the connection, although Go
1.27's transport first drains a body of up to 256 KiB to reuse it. The GET
asks for identity encoding when the client didn't send Accept-Encoding,
since otherwise the transport requests gzip and drops Content-Length as
it decompresses. Other statuses, including 401, 404, 429 and server
errors, are returned as before.

Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx
ravi-pplx force-pushed the fix/handler-head-get-probe branch from 43d6381 to 42903a0 Compare November 22, 2026 17:29
@ravi-pplx

Copy link
Copy Markdown
Author

@codex review

Created with Perplexity

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T18:15:28.370215Z 9d91452 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 42903a03ff

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread internal/strategy/handler/handler.go Resolved
Comment thread internal/strategy/handler/handler.go Resolved
Signed-off-by: Ravi Agarwal <ravi@perplexity.ai>
Co-authored-by: Perplexity <noreply@perplexity.ai>
@ravi-pplx

Copy link
Copy Markdown
Author

@codex review

Created with Perplexity

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 9d914528a0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@ravi-pplx
ravi-pplx marked this pull request as ready for review November 22, 2026 18:10
@ravi-pplx
ravi-pplx requested a review from a team as a code owner November 22, 2026 18:10
@ravi-pplx
ravi-pplx requested review from luispadron and removed request for a team November 22, 2026 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant