summaryrefslogtreecommitdiff
path: root/bin/gitano-smart-http.cgi.in
blob: 8fb0240c55fdaca40b60110302971d0d146c260e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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