From 7a803c7fbee41d99b12a8b4f6d04fe5236210a43 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Fri, 22 Oct 2021 17:45:37 +0300 Subject: [PATCH] http: replace io with fio module In tarantool the standart built-in module for working with files is "fio". So we should use it instead of "io". Patch adds a commit 'Get rid of io module' (e0d8f8266c98916dfce4c74c5b40adef6b714f70) that was implemented for http v2 and later reverted in scope of issue with discard v2. Follows up #90 Part of #134 --- CHANGELOG.md | 1 + http/server.lua | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a88df1f..bc0ae92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Revert all changes related to http v2 (#134). - Rewrite TAP tests with luatest. - Create a separate target for running tests in CMake. +- Replace io with fio module. ### Added diff --git a/http/server.lua b/http/server.lua index 92b0424..166d3c5 100644 --- a/http/server.lua +++ b/http/server.lua @@ -2,7 +2,7 @@ local lib = require('http.lib') -local io = io +local fio = require('fio') local require = require local package = package local mime_types = require('http.mime_types') @@ -349,8 +349,17 @@ local function load_template(self, r, format) local tpl = catfile(self.options.app_dir, 'templates', file) - local fh = io.input(tpl) - local template = fh:read('*a') + local fh, err = fio.open(tpl) + if err ~= nil then + errorf("Can not load template for '%s': '%s'", r.path, err) + end + + local template + template, err = fh:read() + if err ~= nil then + errorf("Can not load template for '%s': '%s'", r.path, err) + end + fh:close() if self.options.cache_templates then @@ -540,14 +549,18 @@ local function static_file(self, request, format) } end - local s, fh = pcall(io.input, file) - - if not s then + local fh, err = fio.open(file, {'O_RDONLY'}) + if err ~= nil then return { status = 404 } end - local body = fh:read('*a') - io.close(fh) + local body + body, err = fh:read() + if err ~= nil then + errorf("Can not return static file for '%s': '%s'", request:path(), err) + end + + fh:close() if self.options.cache_static then self.cache.static[ file ] = body