Skip to content

Commit

Permalink
Merge pull request #144 from Tieske/fix/laulin
Browse files Browse the repository at this point in the history
fix error thrown, and example comments
  • Loading branch information
EvandroLG authored May 30, 2024
2 parents fed2c3b + 8aa2102 commit 5adc7df
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
15 changes: 7 additions & 8 deletions example/app_stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ local function sleep(sec)
end

server:start(function(req, res)
res:write('a', true)
sleep(3)
res:write('b', true)
sleep(3)
res:write('c', true)
res:close()
res:write('a', true)
sleep(3)
res:write('b', true)
sleep(3)
res:write('c', true)

-- true means the callback run without error, else false or nil
return true
-- return a truthy value to indicate the request was handled, no further handling needed
return res:close()
end)
4 changes: 2 additions & 2 deletions example/querystring.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ server:start(function (req, res)
printTable(req['querystring'])
res:addHeader('Content-Type', 'text/html'):write('hello pegasus world!')

-- true means the callback run without error, else false or nil
return true
-- return a truthy value to indicate the request was handled, no further handling needed
return res:close()
end)
4 changes: 2 additions & 2 deletions example/write.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ local server = Pegasus:new()
server:start(function (req, res)
res:addHeader('Content-Type', 'text/html'):write('hello pegasus world!')

-- true means the callback run without error, else false or nil
return true
-- return a truthy value to indicate the request was handled, no further handling needed
return res:close()
end)
4 changes: 3 additions & 1 deletion src/pegasus/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ function Handler:processRequest(port, client, server)
end
end

response:writeDefaultErrorMessage(404)
if not response.closed then
pcall(response.writeDefaultErrorMessage, response, 404)
end
end


Expand Down
16 changes: 9 additions & 7 deletions src/pegasus/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,20 @@ function Response:writeDefaultErrorMessage(statusCode, errMessage)
end

function Response:close()
local body = self._writeHandler:processBodyData(nil, true, self)
if not self.closed then
local body = self._writeHandler:processBodyData(nil, true, self)

if not self._skipBody then
if body and #body > 0 then
self._client:send(toHex(#body) .. '\r\n' .. body .. '\r\n')
if not self._skipBody then
if body and #body > 0 then
self._client:send(toHex(#body) .. '\r\n' .. body .. '\r\n')
end

self._client:send('0\r\n\r\n')
end

self._client:send('0\r\n\r\n')
self.closed = true
end

self.close = true -- TODO: this seems unused??

return self
end

Expand Down

0 comments on commit 5adc7df

Please sign in to comment.