Skip to content

Instantly share code, notes, and snippets.

@dpsk
Forked from abadc0de/bicycle.lua
Created March 5, 2013 07:42
Show Gist options
  • Select an option

  • Save dpsk/5088646 to your computer and use it in GitHub Desktop.

Select an option

Save dpsk/5088646 to your computer and use it in GitHub Desktop.

Revisions

  1. @abadc0de abadc0de revised this gist Mar 4, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions router.lp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    <?
    require "bicycle"
    bicycle.ride()
    ?>
  2. @abadc0de abadc0de revised this gist Mar 4, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion bicycle.lua
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    -- Launch with: -w /route/=route/,**=./bicycle.lp
    _G.bicycle = {

    action = { },
  3. @abadc0de abadc0de revised this gist Mar 4, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions launch.sh
    Original 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
  4. @abadc0de abadc0de revised this gist Mar 4, 2013. 2 changed files with 141 additions and 117 deletions.
    117 changes: 0 additions & 117 deletions bicycle.lp.lua
    Original file line number Diff line number Diff line change
    @@ -1,117 +0,0 @@
    <?
    -- 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("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")
    :gsub(" ", "+"):gsub("([^A-Za-z0-9_])", function(c)
    return string.format("%%%02x", string.byte(c))
    end)
    end

    function unescape (s)
    return s:gsub("&amp;", "&"):gsub("&lt;", "<"):gsub("&gt;", ">")
    :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" 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()

    ?>
    141 changes: 141 additions & 0 deletions bicycle.lua
    Original 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("&", "&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

    }


    }
  5. @abadc0de abadc0de revised this gist Feb 27, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bicycle.lp.lua
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@ _G.bicycle = {
    end
    end

    if method ~= "GET" and method ~= "POST" then return bounce() end
    if method ~= "GET" then return bounce() end

    local file = io.open(path .. ".lp", "r")
    if file == nil then return bounce() end
  6. @abadc0de abadc0de revised this gist Feb 27, 2013. 1 changed file with 12 additions and 12 deletions.
    24 changes: 12 additions & 12 deletions bicycle.lp.lua
    Original 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
    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
    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
  7. @abadc0de abadc0de created this gist Feb 27, 2013.
    117 changes: 117 additions & 0 deletions bicycle.lp.lua
    Original 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("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")
    :gsub(" ", "+"):gsub("([^A-Za-z0-9_])", function(c)
    return string.format("%%%02x", string.byte(c))
    end)
    end

    function unescape (s)
    return s:gsub("&amp;", "&"):gsub("&lt;", "<"):gsub("&gt;", ">")
    :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()

    ?>