summaryrefslogtreecommitdiff
path: root/bin/gitano-smart-http.cgi.in
diff options
context:
space:
mode:
authorRichard Ipsum <richard.ipsum@codethink.co.uk>2014-01-28 17:15:08 +0000
committerRichard Ipsum <richard.ipsum@codethink.co.uk>2014-02-18 17:31:19 +0000
commita6b180769aedd9331ae53a738cdc6ee21e77b148 (patch)
tree0ed450bb2caa08c0af8a3be3d8efc5ad76fe7d74 /bin/gitano-smart-http.cgi.in
parent43b322b7f0541735528cb4ad24c8d8565e6e3682 (diff)
downloadgitano-a6b180769aedd9331ae53a738cdc6ee21e77b148.tar.gz
Add gitano-smart-http cgi
Diffstat (limited to 'bin/gitano-smart-http.cgi.in')
-rwxr-xr-xbin/gitano-smart-http.cgi.in76
1 files changed, 76 insertions, 0 deletions
diff --git a/bin/gitano-smart-http.cgi.in b/bin/gitano-smart-http.cgi.in
new file mode 100755
index 0000000..8fb0240
--- /dev/null
+++ b/bin/gitano-smart-http.cgi.in
@@ -0,0 +1,76 @@
+-- @@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
+
+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