<commit_before>local util = require'util'
local httpserver = require'handler.http.server'
local ev = require'ev'
require'logging.console'
local log = logging.console()
local loop = ev.Loop.default

local server

local webserver = {}

local handlers = {
	['/favicon.ico'] = function(req, res)
		-- return 404 Not found error
		res:set_status(404)
		res:send()
	end,
}

local on_response_sent = function(res)
end

local on_error = function(req, res, err)
	log:info('webserver> error: req %s, res %s, err %s', req, res, err)
end

-- Will be called for every chunk
local on_data = function(req, res, data)
	if data then
		-- Save the chunks into a temp file
		if not req.fd then
			local filename = os.tmpname()
			-- Append mode, owner only
			req.filename = filename
			req.fd = nixio.open(filename, 'a', 0400)
		end
		req.fd:write(data)
	end
end

local on_finish = function(req, handler)
	-- If file upload has been in progress, close the tmpfile
	if req.fd then
		fd:sync()
		fd:close()
	end
	return handler
end

local on_request = function(server, req, res)
	local found
	for pattern, handler in pairs(handlers) do
		if req.url:match(pattern) then
			log:info('webserver> request for pattern :%s', pattern)
			found = true
			req.on_finished = on_finish(req, handler)
			req.on_data = on_data
			req.on_error = on_error
			-- Stream incoming data
			req.stream_response = true
			break
		end
	end
	if not found then
		log:info('webserver> returning 404 for request: %s', req.url)<commit_msg>webserver: save one chunk in memory<commit_after>local util = require'util'
local httpserver = require'handler.http.server'
local ev = require'ev'
require'logging.console'
local log = logging.console()
local loop = ev.Loop.default

local server

local webserver = {}

local handlers = {
	['/favicon.ico'] = function(req, res)
		-- return 404 Not found error
		res:set_status(404)
		res:send()
	end,
}

local on_response_sent = function(res)
end

local on_error = function(req, res, err)
	log:info('webserver> error: req %s, res %s, err %s', req, res, err)
end

-- Will be called for every chunk
local on_data = function(req, res, data)
	if data then
		-- Save the chunks into a temp file
		if not req.fd then
			local filename = os.tmpname()
			-- Append mode, owner only
			req.filename = filename
			req.fd = nixio.open(filename, 'a', 0400)
		end
		req.fd:write(data)
		req.body = data
	end
end

local on_finish = function(req, handler)
	-- If file upload has been in progress, close the tmpfile
	if req.fd then
		fd:sync()
		fd:close()
	end
	-- Check size of tmpfile, if it's small, read into memory
	return handler
end

local on_request = function(server, req, res)
	local found
	for pattern, handler in pairs(handlers) do
		if req.url:match(pattern) then
			log:info('webserver> request for pattern :%s', pattern)
			found = true
			req.on_finished = on_finish(req, handler)
			req.on_data = on_data
			req.on_error = on_error
			-- Stream incoming data
			req.stream_response = true
			break
		end
	end
	if not found then
		log:info('webserver> returning 404 for request: %s', req.url)
{'fraction_non_alphanumeric': 0.1083360363282517, 'fraction_numerical': 0.008433344145313007, 'mean_word_length': 3.7446153846153845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4074414', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1042, 'n_words': 354}