Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rtzui opened this issue Feb 10, 2013 · 1 comment

Comments

@rtzui
Copy link

rtzui commented Feb 10, 2013

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
Copy link

ttyS0 commented Jan 17, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants