-
-
Save dpsk/5088646 to your computer and use it in GitHub Desktop.
Revisions
-
abadc0de revised this gist
Mar 4, 2013 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ <? require "bicycle" bicycle.ride() ?> -
abadc0de revised this gist
Mar 4, 2013 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ _G.bicycle = { action = { }, -
abadc0de revised this gist
Mar 4, 2013 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ MONGOOSE_PATH=./mongoose $MONGOOSE_PATH -w **.lp=/dev/null/,**.lua=/dev/null/,/route/=/dev/null/,/static/=static/,**=router.lp -d no -
abadc0de revised this gist
Mar 4, 2013 . 2 changed files with 141 additions and 117 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,117 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,141 @@ -- Launch with: -w /route/=route/,**=./bicycle.lp _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("&", "&"):gsub("<", "<"):gsub(">", ">") :gsub(" ", "+"):gsub("([^A-Za-z0-9_])", function(c) return string.format("%%%02x", string.byte(c)) end) end, unescape = function (s) return s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">") :gsub("+", " "):gsub("%%(%x%x)", function(hex) return string.char(tonumber(hex, 16)) end) end } } -
abadc0de revised this gist
Feb 27, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -96,7 +96,7 @@ _G.bicycle = { end end if method ~= "GET" then return bounce() end local file = io.open(path .. ".lp", "r") if file == nil then return bounce() end -
abadc0de revised this gist
Feb 27, 2013 . 1 changed file with 12 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,20 +50,20 @@ _G.bicycle = { local pos = 0 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 -
abadc0de created this gist
Feb 27, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,117 @@ <? -- Launch with: -w /route/=route/,**=./bicycle.lp _G.bicycle = { action = { }, basket = { }, ride = function (config) 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 headers = request_info.http_headers local host = headers.Host local content_type = headers["Content-Type"] local file function bounce(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.') 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', host, uri == "." and request_info.uri or uri)) end end function escape (s) return s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">") :gsub(" ", "+"):gsub("([^A-Za-z0-9_])", function(c) return string.format("%%%02x", string.byte(c)) end) end function unescape (s) return s:gsub("&", "&"):gsub("<", "<"):gsub(">", ">") :gsub("+", " "):gsub("%%(%x%x)", function(hex) return string.char(tonumber(hex, 16)) end) end function bundle(query) local parsed = {} local pos = 0 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 if method == "POST" then local realpath = path:gsub("/[^/]*$", "") local action = path:gsub("^.*/", "") file = io.open(realpath .. ".lua", "r") if file == nil then return bounce() end io.close(file) if content_type == "application/x-www-form-urlencoded" then local query = "" local buffer local fields while true do buffer = read() if buffer == "" then break end query = query .. buffer end bicycle.basket = bundle(request_info.query_string) fields = bundle(query) require(realpath) bicycle.action[action](fields) return bounce(request_info.uri:gsub("/[^/]*$", "/")) else -- TODO: support "multipart/form-data" print("Content type " .. content_type .. " not supported.") return end end if method ~= "GET" and method ~= "POST" then return bounce() end local file = io.open(path .. ".lp", "r") if file == nil then return bounce() end io.close(file) if destination .. "/" ~= request_info.uri then return bounce(destination .. '/') end bicycle.basket = bundle(request_info.query_string) print('HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n') mg.include(path .. ".lp") end } bicycle.ride() ?>