<commit_before>local mixin = require 'core.mixin'
local base = require 'http.adapters.nginx.base'
local setmetatable, tostring = setmetatable, tostring


local ResponseWriter = {__index = mixin(base.ResponseWriter, {
    write = function(self, c)
        local b = self.b
        b[#b+1] = c
        self.s = self.s + c:len()
    end
})}

local Request = {__index = base.Request}

return function(app)
    return function(ngx)
        local var = ngx.var
        local b = {}
        local w = setmetatable({
            ngx = ngx,
            headers = ngx.header,
            b = b,
            s = 0
        }, ResponseWriter)
        local req = setmetatable({
            ngx = ngx,
            method = var.request_method,
            path = var.uri
        }, Request)
        app(w, req)
        ngx.header['Content-Length'] = tostring(w.s)
        ngx.print(b)
        ngx.eof()
    end
end<commit_msg>Reacted buffered nginx adapter to support iterative headers and replaceable buffer.<commit_after>local mixin = require 'core.mixin'
local base = require 'http.adapters.nginx.base'
local setmetatable, tostring, next = setmetatable, tostring, next


local ResponseWriter = {__index = mixin(base.ResponseWriter, {
    write = function(self, c)
        local b = self.b
        b[#b+1] = c
        self.s = self.s + c:len()
    end
})}

local Request = {__index = base.Request}

return function(app)
    return function(ngx)
        local var = ngx.var
        local headers = ngx.header
        local w = setmetatable({
            ngx = ngx,
            headers = {},
            b = {},
            s = 0
        }, ResponseWriter)
        local req = setmetatable({
            ngx = ngx,
            method = var.request_method,
            path = var.uri
        }, Request)
        app(w, req)
        headers['Content-Length'] = tostring(w.s)
        for name, value in next, w.headers do
            headers[name] = value
        end
        ngx.print(w.b)
        ngx.eof()
    end
end
{'fraction_non_alphanumeric': 0.10939060939060939, 'fraction_numerical': 0.001998001998001998, 'mean_word_length': 1.8371104815864023, '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': '2871430', 'n_tokens_mistral': 630, 'n_tokens_neox': 620, 'n_words': 164}