0

I am writing an HTTP server in Delphi on top of HTTP.sys. I want to let the kernel cache certain GET responses so they are served directly without my code seeing the request again.

Here’s the simplified code:

AResponse.StatusCode := 200;
AResponse.Reason := 'OK';
AResponse.CachePolicyType := TALHttpServerResponse.TCachePolicyType.TimeToLive;
AResponse.CacheSecondsToLive := 500;
AResponse.BodyString := 'OK';
AResponse.Headers.ContentLength := '2';
HttpSendHttpResponse(
 FRequestQueueHandle,
 LRequestId,
 0,
 @LHttpResponse,
 @LCachePolicy, // points to the above cache policy
 nil,
 nil,
 0,
 nil,
 nil);

I expected the next request for the same URL to be served from the kernel cache. But when I request the URL again, HttpReceiveHttpRequest still fires and the request reaches my server code.

If I check with:

netsh http show cachestate url=http://localhost:23456/echo/
there are no entries in the cache.

Question

  • Why isn’t my response cached in HTTP.sys ?

  • What exact conditions must be met for HTTP.sys to actually store and serve the response from its kernel cache?

asked Sep 24 at 6:38
5
  • 1
    This is documented here Kernel Mode Cache Commented Sep 24 at 7:57
  • If you believe there wasn’t enough documentation to read, you might want to dig into github.com/dotnet/aspnetcore/tree/main/src/Servers/HttpSys as Microsoft uses the related API in ASP.NET Core. Commented Sep 24 at 8:06
  • @Lex but, if you actually search that repo, there is only 1 spot where HttpSendHttpResponse is called (HttpSysListener.cs line 364), and it doesn't use a caching policy. Commented Sep 25 at 23:52
  • @zeus in the code you have shown above, how is AResponse related to LHttpResponse or LCachePolicy? How are you actually populating LCachePolicy? Commented Sep 25 at 23:55
  • @RemyLebeau I found the reason it's because i must set AResponse.Headers.ContentType := 'Content-Type: text/html; charset=UTF-8'; Without this the response is not cached :( this was explained nowhere in the microsoft doc :( Commented Sep 26 at 7:41

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.