-- @@SHEBANG -- -*- Lua -*- -- gitano-smart-http -- -- Git (with) Augmented network operations -- User authentication wrapper -- -- Copyright 2014 Codethink Ltd -- -- -- @@GITANO_LUA_PATH local gitano = require "gitano" local gall = require "gall" local luxio = require "luxio" local subprocess = require "luxio.subprocess" local sio = require "luxio.simple" -- @@GITANO_BIN_PATH -- @@GITANO_SHARE_PATH -- @@GITANO_PLUGIN_PATH local stdout = sio.stdout function parse_get_request() query_string = os.getenv("QUERY_STRING") if query_string then command = string.gsub(query_string, "^service=", "") repo = string.match(os.getenv("PATH_INFO"), '/(.+)/info/refs') return command .. " '" .. repo .. "'" end return nil end function parse_post_request() path_info = os.getenv("PATH_INFO") if path_info then repo, command = string.match(path_info, "/(.+)/(.+)") return command .. " '" .. repo .. "'" end return nil end function parse_request(request_method) if request_method == "GET" then return parse_get_request() elseif request_method == "POST" then return parse_post_request() end end request_method = os.getenv("REQUEST_METHOD") if request_method == "GET" or request_method == "POST" then local user = os.getenv("REMOTE_USER") or "gitano/anonymous" local cmdline = parse_request(request_method) if cmdline and gitano.auth.is_authorized(user, "http", cmdline) then local proc = subprocess.spawn_simple({"git", "http-backend"}) local exit_code _, exit_code = proc:wait() if exit_code ~= 0 then stdout:write("Status: 500 Internal Server Error\r\n\r\n") end else stdout:write("Status: 403 Forbidden\r\n\r\n") end else stdout:write("Status: 405 Method Not Allowed\r\n") stdout:write("Allow: GET, POST\r\n\r\n") end