Skip to content

ESI include does not process during getting 304 header #8291

Closed

Description

@david187

version: ats 9.1.0

When ATS receipt "304 Not Modified" header from backend, ESI does not execute and both of the esi:include and "x-esi: 1" header will be responsed to client.

P.S. http client does not send IF-NONE-MATCH header, just ATS cache revalidate

==========

The problem seem caused by content-type header checking:

if ((!checkHeaderValue(bufp, hdr_loc, TS_MIME_FIELD_CONTENT_TYPE, TS_MIME_LEN_CONTENT_TYPE, "text/", 5, true)) &&

I have tried to add contype-type header in php, but it seem droped by nginx (may be ?)

Here is my lua content-type patch for ESI + 304:

================
esi_patch.lua

function do_global_read_response()
ts.debug('do_global_read_response')

local ct = ts.server_response.header['Content-Type']

local cct = ''

local cache_status = ts.http.get_cache_lookup_status()
if cache_status == TS_LUA_CACHE_LOOKUP_HIT_FRESH then
    ts.debug('hit')

    cct = ts.cached_response.header['Content-Type']
elseif cache_status == TS_LUA_CACHE_LOOKUP_HIT_STALE then
    ts.debug('hit, stale')

    cct = ts.cached_response.header['Content-Type']
else
    ts.debug('not hit')
end



local code = ts.server_response.get_status()
local xesi = ts.server_response.header['X-ESI']

ts.debug('code:'..code)
ts.debug(cct)
ts.debug(xesi)

if xesi ~= nil and ct == nil and code == 304 and cct ~= nil then
         ts.debug('use cct')
         ts.server_response.header['Content-Type'] = cct
         ts.server_response.header['X-Lua-Esi-Patch'] = 1
end

return 0
end

=======
esi_etag.php

Hello, $etag = '"'.md5($output).'"';

$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
$_SERVER['HTTP_IF_NONE_MATCH'] :
false ;

if($if_none_match == $etag ){
header("HTTP/1.1 304 Not Modified");
exit;
}

header('cache-control: public, max-age=10');
header('etag: '.$etag);

echo $output;

Activity

  1. self-assigned this
    on Aug 30, 2021
  2. shukitchan commented on Aug 30, 2021

    @shukitchan
    Contributor

    I don't think the ESI plugin currently supports the If-None-Match scenario. It can only transform a response from origin or a response from the cache. When status 304 is returned from origin, the body is empty and so there is nothing to transform.

    you can instead try to remove "If-None-Match" header in the send request hook and so your origin will then not be able to send back 304 response.

  3. david187 commented on Aug 30, 2021

    @david187
    Author

    um.
    I applied my lua patch, it seem to get cached response from cache store and process ESI include successfully.

    If the ESI plugin check the status code, and using the content-type in cached response if status code is 304.
    It should make ESI in ATS work fine with 304 header.

    Supporting 304 header should make ATS response faster and can help to reduce bandwidth usage.

  4. shukitchan commented on Aug 30, 2021

    @shukitchan
    Contributor

    I see. So the lua script actually works for you? and it makes the plugin do a transform correctly.

    Let me take a look.

  5. david187 commented on Aug 30, 2021

    @david187
    Author

    Yes, It work fine for me.
    Please review for the logic is correct or not.
    Thanks.

  6. shukitchan commented on Dec 21, 2021

    @shukitchan
    Contributor

    The script looks fine. I understand your intention of making ESI plugin to support 304 status response from origin as well.

    However, my question is that status 304 is normally without a response body and so why would you want an ESI processing to happen on a 304 status response from origin? Can you give me a use case?

  7. shukitchan commented on Dec 21, 2021

    @shukitchan
    Contributor

    After rereading this thread, I know I misunderstand something. Now I understand that you are trying to trigger ESI processing on the cached response while ATS is receiving the 304 response from origin.

    This does look like a problem that I can provide a fix inside the plugin.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions