Skip to content
This repository has been archived by the owner on Jan 23, 2018. It is now read-only.

Commit

Permalink
Unify method dispatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Nov 8, 2012
1 parent dbaec81 commit a8e0e66
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions lib/sokoban/receiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ module Sokoban
class Receiver

ROUTES =
[["POST", 'service_rpc', /(.*?)\/git-upload-pack$/, 'upload-pack'],
["POST", 'service_rpc', /(.*?)\/git-receive-pack$/, 'receive-pack'],

["GET", 'get_info_refs', /(.*?)\/info\/refs$/],
["GET", 'get_text_file', /(.*?)\/HEAD$/],
["GET", 'get_text_file', /(.*?)\/objects\/info\/alternates$/],
["GET", 'get_text_file', /(.*?)\/objects\/info\/http-alternates$/],
["GET", 'get_info_packs', /(.*?)\/objects\/info\/packs$/],
["GET", 'get_text_file', /(.*?)\/objects\/info\/[^\/]*$/],
["GET", 'get_loose_object', /(.*?)\/objects\/[0-9a-f]{2}\/[0-9a-f]{38}$/],
["GET", 'get_pack_file', /(.*?)\/objects\/pack\/pack-[0-9a-f]{40}\\.pack$/],
["GET", 'get_idx_file', /(.*?)\/objects\/pack\/pack-[0-9a-f]{40}\\.idx$/],
[["POST", :service_rpc, /(.*?)\/git-upload-pack$/, 'upload-pack'],
["POST", :service_rpc, /(.*?)\/git-receive-pack$/, 'receive-pack'],

["GET", :get_info_refs, /(.*?)\/info\/refs$/],
["GET", :get_text_file, /(.*?)\/HEAD$/],
["GET", :get_text_file, /(.*?)\/objects\/info\/alternates$/],
["GET", :get_text_file, /(.*?)\/objects\/info\/http-alternates$/],
["GET", :get_info_packs, /(.*?)\/objects\/info\/packs$/],
["GET", :get_text_file, /(.*?)\/objects\/info\/[^\/]*$/],
["GET", :get_loose_object, /(.*?)\/objects\/[0-9a-f]{2}\/[0-9a-f]{38}$/],
["GET", :get_pack_file, /(.*?)\/objects\/pack\/pack-[0-9a-f]{40}\\.pack$/],
["GET", :get_idx_file, /(.*?)\/objects\/pack\/pack-[0-9a-f]{40}\\.idx$/],
]

def initialize(repo_url)
Expand All @@ -35,13 +35,11 @@ def call(env)
@env = env
@req = Rack::Request.new(env)

cmd, path, @reqfile, @rpc = route(@req.request_method, @req.path_info)

return render_method_not_allowed if cmd == :not_allowed
return render_not_found if !cmd
method, path, @reqfile, @rpc = route(@req.request_method,
@req.path_info)

Dir.chdir(@repo_dir) do
self.send(cmd.to_sym)
self.send(method || :not_found)
end
end

Expand Down Expand Up @@ -213,19 +211,19 @@ def update_server_info

PLAIN_TYPE = {"Content-Type" => "text/plain"}

def render_method_not_allowed
def not_allowed
if @env['SERVER_PROTOCOL'] == "HTTP/1.1"
[405, PLAIN_TYPE, ["Method Not Allowed"]]
else
[400, PLAIN_TYPE, ["Bad Request"]]
end
end

def render_not_found
def not_found
[404, PLAIN_TYPE, ["Not Found"]]
end

def render_no_access
def no_access
[403, PLAIN_TYPE, ["Forbidden"]]
end

Expand Down

0 comments on commit a8e0e66

Please sign in to comment.