Skip to content

Commit

Permalink
http: replace io with fio module
Browse files Browse the repository at this point in the history
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'
(e0d8f82) that was implemented for http
v2 and later reverted in scope of issue with discard v2.

Follows up #90
Part of #134
  • Loading branch information
ligurio committed Oct 28, 2021
1 parent 37ce554 commit 07f2863
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 21 additions & 8 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 07f2863

Please sign in to comment.