Skip to content

Instantly share code, notes, and snippets.

@dpsk
Forked from abadc0de/bicycle.lua
Created March 5, 2013 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpsk/5088646 to your computer and use it in GitHub Desktop.
Save dpsk/5088646 to your computer and use it in GitHub Desktop.
_G.bicycle = {
action = { },
basket = { },
access = function (path)
local file = io.open(path, "r")
if file == nil then return false end
io.close(file)
return true
end,
bounce = function (uri)
if uri == nil or uri == "" then uri = "/" end
if uri == request_info.uri then
print(
'HTTP/1.0 404 Not Found\r\nContent-Type: text/html\r\n\r\n' ..
'Not found: ' .. uri)
else
print(
("HTTP/1.0 302 Found\r\nLocation: %s://%s%s\r\n\r\n"):format(
request_info.is_ssl == 1 and 'https' or 'http',
request_info.http_headers.Host,
uri == "." and request_info.uri or uri))
end
end,
bundle = function (query)
local parsed = {}
local pos = 0
local unescape = bicycle.uri.unescape;
if query == nil then return parsed end
local function insert(s)
local first, last = s:find("=")
if first then
parsed[unescape(s:sub(0, first - 1))] = unescape(s:sub(first + 1))
end
end
while true do
local first, last = query:find("&", pos)
if first then
insert(query:sub(pos, first - 1))
pos = last + 1
else
insert(query:sub(pos));
break;
end
end
return parsed
end,
ride = function (config)
local bounce = bicycle.bounce
local bundle = bicycle.bundle
local access = bicycle.access
local destination = request_info.uri:gsub("/+$", ""):gsub("^$", "/index")
local route = config and config.route or "route"
local path = route .. destination
local method = request_info.request_method
local content_type = request_info.http_headers["Content-Type"]
local uri = request_info.uri
local query_string = request_info.query_string
local file
local s, e = uri:find("/index/?")
if s == 1 and (e == 7 or e == # uri) then return bounce() end
if method == "POST" then
local realpath = path:gsub("/[^/]*$", "")
local action = path:gsub("^.*/", "")
if access(realpath .. ".lua") == false then return bounce() end
if content_type == "application/x-www-form-urlencoded" then
local posted = ""
local buffer
local fields
while true do
buffer = read()
if buffer == "" then break end
posted = posted .. buffer
end
bicycle.basket = bundle(query_string)
fields = bundle(posted)
require(realpath)
bicycle.action[action](fields)
return bounce(uri:gsub("/[^/]*$", "/"))
else
-- TODO: support "multipart/form-data"
print("Content type " .. content_type .. " not supported.")
return
end
end
if method ~= "GET" then return bounce() end
if access(path .. ".lp") == false then return bounce() end
if destination ~= "/index" and destination .. "/" ~= uri then
return bounce(destination .. '/')
end
bicycle.basket = bundle(query_string)
if access(path .. ".lua") then
require(path)
if bicycle.onrequest ~= nil then
bicycle.onrequest()
bicycle.onrequest = nil
end
end
-- print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n')
mg.include(path .. ".lp")
end,
uri = {
escape = function (s)
return s:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")
:gsub(" ", "+"):gsub("([^A-Za-z0-9_])", function(c)
return string.format("%%%02x", string.byte(c))
end)
end,
unescape = function (s)
return s:gsub("&amp;", "&"):gsub("&lt;", "<"):gsub("&gt;", ">")
:gsub("+", " "):gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
}
}
MONGOOSE_PATH=./mongoose
$MONGOOSE_PATH -w **.lp=/dev/null/,**.lua=/dev/null/,/route/=/dev/null/,/static/=static/,**=router.lp -d no
<?
require "bicycle"
bicycle.ride()
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment