EvandroLG / pegasus.lua

:rocket: Pegasus.lua is an http server to work with web applications written in Lua language.
http://evandrolg.github.io/pegasus.lua/
MIT License
418 stars 36 forks source link

Basic examples simply crash #142

Closed laulin closed 5 months ago

laulin commented 5 months ago

Hello,

I was just looking at this library, I install it and when I run basic example (write.lua, querystring.lua, ...), it crashes with following error :

Pegasus is up on 0.0.0.0:9090
Request for: GET /index.html
lua5.3: /usr/local/share/lua/5.3/pegasus/response.lua:122: can't set status code, it was already sent
stack traceback:
    [C]: in function 'assert'
    /usr/local/share/lua/5.3/pegasus/response.lua:122: in function 'pegasus.response.statusCode'
    /usr/local/share/lua/5.3/pegasus/response.lua:154: in function 'pegasus.response.writeDefaultErrorMessage'
    /usr/local/share/lua/5.3/pegasus/handler.lua:142: in function 'pegasus.handler.processRequest'
    /usr/local/share/lua/5.3/pegasus/init.lua:31: in function 'pegasus.start'
    write.lua:5: in main chunk
    [C]: in ?

I read response.lua, I saw the flag "_headersSended" but I dont catch the meaning of assert(not _headersSended ...). May be someone can explain that part ?

Thank you for your time !

Tieske commented 5 months ago

Can you be more specific? which example are you trying to run?

laulin commented 5 months ago

I found the problem : in Handler:processRequest (handler.lua:137), the fonction expectes the callback return something (eg true) otherwise it will return the error message. So the example should be :

local Pegasus = require 'pegasus'

local server = Pegasus:new({location="/index.html"})

server:start(function (req, res)
  res:addHeader('Content-Type', 'text/html'):write('hello pegasus world!')
  return true
end)
Tieske commented 5 months ago

Mind creating a PR with a fix for the example?

laulin commented 5 months ago

Sure, I just did it. But examples "copas.lua" and "app.lua" can't be fixed : pegasus.json is missing in the repository. Do you know why this feature was removed ?

Tieske commented 5 months ago

dang, yup, that was never merged, see #138 .

I'll send a fix for that one.

EDIT: fix is in #145

Tieske commented 5 months ago

@laulin I think this can be closed now, PR's have been merged.

EvandroLG commented 5 months ago

Closing it, thanks guys! @laulin please feel free to reopen this if needed.