keplerproject / wsapi

WSAPI is an API that abstracts the web server from Lua web applications.
http://keplerproject.github.io/wsapi
74 stars 33 forks source link

The hello world from the website is broken/function _M.find_file(filename) is called incorrectly #11

Open rtzui opened 11 years ago

rtzui commented 11 years ago

How to reproduce the error:

  1. Install WSAPI with luarocks (locally) create a new file test.cgi with mode +x:
#!/usr/bin/env /home/user/.luarocks/bin/wsapi.cgi

module("test.cgi", package.seeall)

function run(wsapi_env)
  local headers = { ["Content-type"] = "text/html" }

  local function hello_text()
    coroutine.yield("<html><body>")
    coroutine.yield("<p>Hello Wsapi!</p>")
    coroutine.yield("<p>PATH_INFO: " .. wsapi_env.PATH_INFO .. "</p>")
    coroutine.yield("<p>SCRIPT_NAME: " .. wsapi_env.SCRIPT_NAME .. "</p>")
    coroutine.yield("</body></html>")
  end

  return 200, headers, coroutine.wrap(hello_text)
end

Run thttpd -p 8000 -d . -c \*.cgi as a webserver

Start web browser, get:

There was an error in the specified application. The full error message follows:

...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:322: bad argument #1 to 'match' (string expected, got nil)
stack traceback:
    [C]: in function 'match'
    ...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:322: in function 'splitext'
    ...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:342: in function <...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:332>
    (tail call): ?
    ...uarocks/lib/luarocks/rocks/wsapi/cvs-4/bin/wsapi.cgi:15: in function <...uarocks/lib/luarocks/rocks/wsapi/cvs-4/bin/wsapi.cgi:13>
    (tail call): ?
    [C]: in function 'xpcall'
    ...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:264: in function 'run_app'
    ...me/user/.luarocks/share/lua/5.1//wsapi/common.lua:291: in function 'run'
    /home/user/.luarocks/share/lua/5.1//wsapi/cgi.lua:19: in function 'run'
    ...uarocks/lib/luarocks/rocks/wsapi/cvs-4/bin/wsapi.cgi:25: in main chunk
    [C]: ?

The Problem seems to be that _M.find_file(filename) from common.lua gets only the name of the file to be run, not a path to it. But I'm not sure. I'm, by the way, using lua5.1.

ttyS0 commented 2 years ago

Workaround:

tl;dr patch splitpath in wsapi/common.lua as following:

function _M.splitpath(filename)
    local path, file = string.match(filename, "(.-)/?([^/\\]-)$")
    if #path == 0 then path = "." end                                                                                                       
    return path, file
end

Problem

I met this problem recently. The main cause is that all HTTP servers implement CGI differently. In my case, I am using Caddy with caddy-cgi/v2 plugin.

File wsapi/common.lua

    path, file = _M.splitpath(filename)
    modname, ext = _M.splitext(file)

Here filename is derived from CGI variable SCRIPT_FILENAME and PATH_TRANSLATED (see find_module called in wsapi.cgi).

function _M.splitpath(filename)
    -- local path, file = string.match(filename, "^(.*)[/\\]([^/\\]*)$")
    local path, file = string.match(filename, "(.-)/?([^/\\]-)$")                                                                           
    return path, file
end

splitpath has a corner case, where strings like file.ext, abc.lua will fail. In my case, filename becomes app.lua so path & file both become nil.