Class: Grack::Server
- Inherits:
-
Object
- Object
- Grack::Server
- Defined in:
- lib/grack/server.rb
Constant Summary collapse
- SERVICES =
[ ["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$"], ]
- CRLF =
Uses chunked (streaming) transfer, otherwise response blocks to calculate Content-Length header en.wikipedia.org/wiki/Chunked_transfer_encoding
"\r\n"
- PLAIN_TYPE =
HTTP error response handling functions
{ "Content-Type" => "text/plain" }
Instance Attribute Summary collapse
-
#git ⇒ Object
readonly
Returns the value of attribute git.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
- #dumb_info_refs ⇒ Object
- #encode_chunk(chunk) ⇒ Object
- #get_git(path) ⇒ Object
- #get_idx_file ⇒ Object
- #get_info_packs ⇒ Object
- #get_info_refs ⇒ Object
- #get_loose_object ⇒ Object
- #get_pack_file ⇒ Object
- #get_service_type ⇒ Object
- #get_text_file ⇒ Object
- #has_access?(rpc, check_content_type = false) ⇒ Boolean
- #hdr_cache_forever ⇒ Object
-
#hdr_nocache ⇒ Object
———————— header writing functions ————————.
-
#initialize(config = false) ⇒ Server
constructor
A new instance of Server.
- #match_routing ⇒ Object
-
#pkt_flush ⇒ Object
—————————— packet-line handling functions ——————————.
- #pkt_write(str) ⇒ Object
- #read_body ⇒ Object
- #render_method_not_allowed ⇒ Object
- #render_no_access ⇒ Object
- #render_not_found ⇒ Object
-
#send_file(reqfile, content_type) ⇒ Object
some of this borrowed from the Rack::File implementation.
- #service_rpc ⇒ Object
- #set_config(config) ⇒ Object
- #set_config_setting(key, value) ⇒ Object
- #smart_http?(rpc = @rpc) ⇒ Boolean
- #terminating_chunk ⇒ Object
Constructor Details
#initialize(config = false) ⇒ Server
Returns a new instance of Server.
28 29 30 |
# File 'lib/grack/server.rb', line 28 def initialize(config = false) set_config(config) end |
Instance Attribute Details
#git ⇒ Object (readonly)
Returns the value of attribute git.
11 12 13 |
# File 'lib/grack/server.rb', line 11 def git @git end |
Instance Method Details
#_call(env) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/grack/server.rb', line 44 def _call(env) @env = env @req = Rack::Request.new(env) cmd, path, @reqfile, @rpc = match_routing return render_method_not_allowed if cmd == 'not_allowed' return render_not_found unless cmd @git = get_git(path) return render_not_found unless git.valid_repo? self.method(cmd).call end |
#call(env) ⇒ Object
40 41 42 |
# File 'lib/grack/server.rb', line 40 def call(env) dup._call(env) end |
#dumb_info_refs ⇒ Object
123 124 125 126 127 128 |
# File 'lib/grack/server.rb', line 123 def dumb_info_refs git.update_server_info send_file(@reqfile, "text/plain; charset=utf-8") do hdr_nocache end end |
#encode_chunk(chunk) ⇒ Object
95 96 97 98 |
# File 'lib/grack/server.rb', line 95 def encode_chunk(chunk) size_in_hex = chunk.size.to_s(16) [size_in_hex, CRLF, chunk, CRLF].join end |
#get_git(path) ⇒ Object
199 200 201 202 203 |
# File 'lib/grack/server.rb', line 199 def get_git(path) root = @config[:project_root] || Dir.pwd path = File.join(root, path) Grack::Git.new(@config[:git_path], path) end |
#get_idx_file ⇒ Object
149 150 151 152 153 |
# File 'lib/grack/server.rb', line 149 def get_idx_file send_file(@reqfile, "application/x-git-packed-objects-toc") do hdr_cache_forever end end |
#get_info_packs ⇒ Object
130 131 132 133 134 135 |
# File 'lib/grack/server.rb', line 130 def get_info_packs # objects/info/packs send_file(@reqfile, "text/plain; charset=utf-8") do hdr_nocache end end |
#get_info_refs ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/grack/server.rb', line 104 def get_info_refs service_name = get_service_type return dumb_info_refs unless smart_http?(service_name) return render_no_access unless has_access?(service_name) refs = git.execute([service_name, '--stateless-rpc', '--advertise-refs', git.repo]) @res = Rack::Response.new @res.status = 200 @res["Content-Type"] = "application/x-git-#{service_name}-advertisement" hdr_nocache @res.write(pkt_write("# service=git-#{service_name}\n")) @res.write(pkt_flush) @res.write(refs) @res.finish end |
#get_loose_object ⇒ Object
137 138 139 140 141 |
# File 'lib/grack/server.rb', line 137 def get_loose_object send_file(@reqfile, "application/x-git-loose-object") do hdr_cache_forever end end |
#get_pack_file ⇒ Object
143 144 145 146 147 |
# File 'lib/grack/server.rb', line 143 def get_pack_file send_file(@reqfile, "application/x-git-packed-objects") do hdr_cache_forever end end |
#get_service_type ⇒ Object
205 206 207 208 209 210 |
# File 'lib/grack/server.rb', line 205 def get_service_type service_type = @req.params['service'] return false unless service_type return false if service_type[0, 4] != 'git-' service_type.gsub('git-', '') end |
#get_text_file ⇒ Object
155 156 157 158 159 |
# File 'lib/grack/server.rb', line 155 def get_text_file send_file(@reqfile, "text/plain") do hdr_nocache end end |
#has_access?(rpc, check_content_type = false) ⇒ Boolean
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/grack/server.rb', line 235 def has_access?(rpc, check_content_type = false) if check_content_type return false unless smart_http?(rpc) end return false unless ['upload-pack', 'receive-pack'].include?(rpc) if rpc == 'receive-pack' return @config[:receive_pack] if @config.include?(:receive_pack) end if rpc == 'upload-pack' return @config[:upload_pack] if @config.include?(:upload_pack) end git.config_setting(rpc) end |
#hdr_cache_forever ⇒ Object
306 307 308 309 310 311 |
# File 'lib/grack/server.rb', line 306 def hdr_cache_forever now = Time.now().to_i @res["Date"] = now.to_s @res["Expires"] = (now + 31536000).to_s; @res["Cache-Control"] = "public, max-age=31536000"; end |
#hdr_nocache ⇒ Object
header writing functions
300 301 302 303 304 |
# File 'lib/grack/server.rb', line 300 def hdr_nocache @res["Expires"] = "Fri, 01 Jan 1980 00:00:00 GMT" @res["Pragma"] = "no-cache" @res["Cache-Control"] = "no-cache, max-age=0, must-revalidate" end |
#match_routing ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/grack/server.rb', line 212 def match_routing cmd = nil path = nil SERVICES.each do |method, handler, match, rpc| next unless m = Regexp.new(match).match(@req.path_info) return ['not_allowed'] unless method == @req.request_method cmd = handler path = m[1] file = @req.path_info.sub(path + '/', '') return [cmd, path, file, rpc] end nil end |
#pkt_flush ⇒ Object
packet-line handling functions
288 289 290 |
# File 'lib/grack/server.rb', line 288 def pkt_flush '0000' end |
#pkt_write(str) ⇒ Object
292 293 294 |
# File 'lib/grack/server.rb', line 292 def pkt_write(str) (str.size + 4).to_s(16).rjust(4, '0') + str end |
#read_body ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/grack/server.rb', line 253 def read_body if @env["HTTP_CONTENT_ENCODING"] =~ /gzip/ Zlib::GzipReader.new(@req.body).read else @req.body.read end end |
#render_method_not_allowed ⇒ Object
267 268 269 270 271 272 273 |
# File 'lib/grack/server.rb', line 267 def render_method_not_allowed if @env['SERVER_PROTOCOL'] == "HTTP/1.1" [405, PLAIN_TYPE, ["Method Not Allowed"]] else [400, PLAIN_TYPE, ["Bad Request"]] end end |
#render_no_access ⇒ Object
279 280 281 |
# File 'lib/grack/server.rb', line 279 def render_no_access [403, PLAIN_TYPE, ["Forbidden"]] end |
#render_not_found ⇒ Object
275 276 277 |
# File 'lib/grack/server.rb', line 275 def render_not_found [404, PLAIN_TYPE, ["Not Found"]] end |
#send_file(reqfile, content_type) ⇒ Object
some of this borrowed from the Rack::File implementation
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/grack/server.rb', line 166 def send_file(reqfile, content_type) reqfile = File.join(git.repo, reqfile) return render_not_found unless File.exists?(reqfile) return render_not_found unless reqfile == File.realpath(reqfile) # reqfile looks legit: no path traversal, no leading '|' @res = Rack::Response.new @res.status = 200 @res["Content-Type"] = content_type @res["Last-Modified"] = File.mtime(reqfile).httpdate yield if size = File.size?(reqfile) @res["Content-Length"] = size.to_s @res.finish do File.open(reqfile, "rb") do |file| while part = file.read(8192) @res.write part end end end else body = [File.read(reqfile)] size = Rack::Utils.bytesize(body.first) @res["Content-Length"] = size @res.write body @res.finish end end |
#service_rpc ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/grack/server.rb', line 69 def service_rpc return render_no_access unless has_access?(@rpc, true) input = read_body @res = Rack::Response.new @res.status = 200 @res["Content-Type"] = "application/x-git-%s-result" % @rpc @res["Transfer-Encoding"] = "chunked" @res["Cache-Control"] = "no-cache" @res.finish do git.execute([@rpc, '--stateless-rpc', git.repo]) do |pipe| pipe.write(input) pipe.close_write while !pipe.eof? block = pipe.read(8192) # 8KB at a time @res.write encode_chunk(block) # stream it to the client end @res.write terminating_chunk end end end |
#set_config(config) ⇒ Object
32 33 34 |
# File 'lib/grack/server.rb', line 32 def set_config(config) @config = config || {} end |
#set_config_setting(key, value) ⇒ Object
36 37 38 |
# File 'lib/grack/server.rb', line 36 def set_config_setting(key, value) @config[key] = value end |
#smart_http?(rpc = @rpc) ⇒ Boolean
231 232 233 |
# File 'lib/grack/server.rb', line 231 def smart_http?(rpc = @rpc) @req.content_type == "application/x-git-#{rpc}-request" end |