<commit_before>local request_key = require 'http.request_key'
local concat, next = table.concat, next


local function pack(w)
    local status_code = w:get_status_code()
    local headers = w.headers
    local r = {
        b = concat(w.b)
    }
    if status_code ~= 0 and status_code ~= 200 then
        r.s = status_code
    end
    if next(headers) ~= nil then
        r.h = headers
    end
    return r
end

local function unpack(w, r)
    local status_code = r.s
    local headers = r.h
    local body = r.b
    if status_code then
        w:set_status_code(status_code)
    end
    if headers then
        w.headers = headers
    end
    w.b = body
    w.s = #body
end

return function(options, following)
    local cache = options.cache
    assert(cache, 'cache')
    local make_key = request_key.new '$m:$p'
    local profiles = {}
    return function(w, req)
        local key
        local mkey = make_key(req)
        local mprofile = profiles[mkey]
        if mprofile then
            key = mprofile.key(req)
            local r = cache:get(key)
            if r then
                return unpack(w, r)
            end
        end
        following(w, req)
        local profile = w.cache_profile
        if profile then
            if profile ~= mprofile then
                profiles[mkey] = profile
                key = profile.key(req)
            end<commit_msg>Applied etag http header in caching middleware.<commit_after>local digest = require 'security.crypto.digest'
local etag = require 'http.etag'
local request_key = require 'http.request_key'
local concat, next = table.concat, next


local function pack(w)
    local status_code = w:get_status_code()
    local headers = w.headers
    local r = {b = w.b}
    if status_code ~= 0 and status_code ~= 200 then
        r.s = status_code
    end
    if next(headers) ~= nil then
        r.h = headers
    end
    return r
end

local function unpack(w, r)
    local status_code = r.s
    local headers = r.h
    local body = r.b
    if status_code then
        w:set_status_code(status_code)
    end
    if headers then
        w.headers = headers
    end
    w.b = body
    w.s = #body
end

return function(options, following)
    local cache = options.cache
    assert(cache, 'cache')
    local make_key = request_key.new '$m:$p'
    local make_etag = etag.new(digest.new('md5'))
    local profiles = {}
    return function(w, req)
        local key
        local mkey = make_key(req)
        local mprofile = profiles[mkey]
        if mprofile then
            key = mprofile.key(req)
            local r = cache:get(key)
            if r then
                return unpack(w, r)
            end
        end
        following(w, req)
        local profile = w.cache_profile
        if profile then
            if profile ~= mprofile then
                profiles[mkey] = profile
                key = profile.key(req)
            end
            w.b = concat(w.b)
            w.headers['ETag'] = make_etag(w.b)
{'fraction_non_alphanumeric': 0.08467202141900937, 'fraction_numerical': 0.0030120481927710845, 'mean_word_length': 1.8657718120805369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2991320', 'n_tokens_mistral': 933, 'n_tokens_neox': 916, 'n_words': 278}