Class | Gem::Server |
In: |
lib/rubygems/server.rb
|
Parent: | Object |
Gem::Server and allows users to serve gems for consumption by `gem —remote-install`.
gem_server starts an HTTP server on the given port and serves the folowing:
gem server [-p portnum] [-d gem_path]
port_num: | The TCP port the HTTP server will bind to |
gem_path: | Root gem directory containing both "cache" and "specifications" subdirectories. |
DOC_TEMPLATE | = | <<-'WEBPAGE' <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>RubyGems Documentation Index</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <link rel="stylesheet" href="gem-server-rdoc-style.css" type="text/css" media="screen" /> </head> <body> <div id="fileHeader"> <h1>RubyGems Documentation Index</h1> </div> <!-- banner header --> <div id="bodyContent"> <div id="contextContent"> <div id="description"> <h1>Summary</h1> <p>There are <%=values["gem_count"]%> gems installed:</p> <p> <%= values["specs"].map { |v| "<a href=\"##{v["name"]}\">#{v["name"]}</a>" }.join ', ' %>. <h1>Gems</h1> <dl> <% values["specs"].each do |spec| %> <dt> <% if spec["first_name_entry"] then %> <a name="<%=spec["name"]%>"></a> <% end %> <b><%=spec["name"]%> <%=spec["version"]%></b> <% if spec["rdoc_installed"] then %> <a href="<%=spec["doc_path"]%>">[rdoc]</a> <% else %> <span title="rdoc not installed">[rdoc]</span> <% end %> <% if spec["homepage"] then %> <a href="<%=spec["homepage"]%>" title="<%=spec["homepage"]%>">[www]</a> <% else %> <span title="no homepage available">[www]</span> <% end %> <% if spec["has_deps"] then %> - depends on <%= spec["dependencies"].map { |v| "<a href=\"##{v["name"]}\">#{v["name"]}</a>" }.join ', ' %>. <% end %> </dt> <dd> <%=spec["summary"]%> <% if spec["executables"] then %> <br/> <% if spec["only_one_executable"] then %> Executable is <% else %> Executables are <%end%> <%= spec["executables"].map { |v| "<span class=\"context-item-name\">#{v["executable"]}</span>"}.join ', ' %>. <%end%> <br/> <br/> </dd> <% end %> </dl> </div> </div> </div> <div id="validator-badges"> <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p> </div> </body> </html> WEBPAGE | ||
RDOC_CSS | = | <<-RDOCCSS body { font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 90%; margin: 0; margin-left: 40px; padding: 0; background: white; } h1,h2,h3,h4 { margin: 0; color: #efefef; background: transparent; } h1 { font-size: 150%; } h2,h3,h4 { margin-top: 1em; } a { background: #eef; color: #039; text-decoration: none; } a:hover { background: #039; color: #eef; } /* Override the base stylesheets Anchor inside a table cell */ td > a { background: transparent; color: #039; text-decoration: none; } /* and inside a section title */ .section-title > a { background: transparent; color: #eee; text-decoration: none; } /* === Structural elements =================================== */ div#index { margin: 0; margin-left: -40px; padding: 0; font-size: 90%; } div#index a { margin-left: 0.7em; } div#index .section-bar { margin-left: 0px; padding-left: 0.7em; background: #ccc; font-size: small; } div#classHeader, div#fileHeader { width: auto; color: white; padding: 0.5em 1.5em 0.5em 1.5em; margin: 0; margin-left: -40px; border-bottom: 3px solid #006; } div#classHeader a, div#fileHeader a { background: inherit; color: white; } div#classHeader td, div#fileHeader td { background: inherit; color: white; } div#fileHeader { background: #057; } div#classHeader { background: #048; } .class-name-in-header { font-size: 180%; font-weight: bold; } div#bodyContent { padding: 0 1.5em 0 1.5em; } div#description { padding: 0.5em 1.5em; background: #efefef; border: 1px dotted #999; } div#description h1,h2,h3,h4,h5,h6 { color: #125;; background: transparent; } div#validator-badges { text-align: center; } div#validator-badges img { border: 0; } div#copyright { color: #333; background: #efefef; font: 0.75em sans-serif; margin-top: 5em; margin-bottom: 0; padding: 0.5em 2em; } /* === Classes =================================== */ table.header-table { color: white; font-size: small; } .type-note { font-size: small; color: #DEDEDE; } .xxsection-bar { background: #eee; color: #333; padding: 3px; } .section-bar { color: #333; border-bottom: 1px solid #999; margin-left: -20px; } .section-title { background: #79a; color: #eee; padding: 3px; margin-top: 2em; margin-left: -30px; border: 1px solid #999; } .top-aligned-row { vertical-align: top } .bottom-aligned-row { vertical-align: bottom } /* --- Context section classes ----------------------- */ .context-row { } .context-item-name { font-family: monospace; font-weight: bold; color: black; } .context-item-value { font-size: small; color: #448; } .context-item-desc { color: #333; padding-left: 2em; } /* --- Method classes -------------------------- */ .method-detail { background: #efefef; padding: 0; margin-top: 0.5em; margin-bottom: 1em; border: 1px dotted #ccc; } .method-heading { color: black; background: #ccc; border-bottom: 1px solid #666; padding: 0.2em 0.5em 0 0.5em; } .method-signature { color: black; background: inherit; } .method-name { font-weight: bold; } .method-args { font-style: italic; } .method-description { padding: 0 0.5em 0 0.5em; } /* --- Source code sections -------------------- */ a.source-toggle { font-size: 90%; } div.method-source-code { background: #262626; color: #ffdead; margin: 1em; padding: 0.5em; border: 1px dashed #999; overflow: hidden; } div.method-source-code pre { color: #ffdead; overflow: hidden; } /* --- Ruby keyword styles --------------------- */ .standalone-code { background: #221111; color: #ffdead; overflow: hidden; } .ruby-constant { color: #7fffd4; background: transparent; } .ruby-keyword { color: #00ffff; background: transparent; } .ruby-ivar { color: #eedd82; background: transparent; } .ruby-operator { color: #00ffee; background: transparent; } .ruby-identifier { color: #ffdead; background: transparent; } .ruby-node { color: #ffa07a; background: transparent; } .ruby-comment { color: #b22222; font-weight: bold; background: transparent; } .ruby-regexp { color: #ffa07a; background: transparent; } .ruby-value { color: #7fffd4; background: transparent; } RDOCCSS | CSS is copy & paste from rdoc-style.css, RDoc V1.0.1 - 20041108 |
# File lib/rubygems/server.rb, line 330 330: def initialize(gemdir, port, daemon) 331: Socket.do_not_reverse_lookup = true 332: 333: @gemdir = gemdir 334: @port = port 335: @daemon = daemon 336: logger = WEBrick::Log.new nil, WEBrick::BasicLog::FATAL 337: @server = WEBrick::HTTPServer.new :DoNotListen => true, :Logger => logger 338: 339: @spec_dir = File.join @gemdir, "specifications" 340: @source_index = Gem::SourceIndex.from_gems_in @spec_dir 341: end
# File lib/rubygems/server.rb, line 326 326: def self.run(options) 327: new(options[:gemdir], options[:port], options[:daemon]).run 328: end
# File lib/rubygems/server.rb, line 343 343: def quick(req, res) 344: res['content-type'] = 'text/plain' 345: res['date'] = File.stat(@spec_dir).mtime 346: 347: case req.request_uri.request_uri 348: when '/quick/index' then 349: res.body << @source_index.map { |name,_| name }.join("\n") 350: when '/quick/index.rz' then 351: index = @source_index.map { |name,_| name }.join("\n") 352: res.body << Zlib::Deflate.deflate(index) 353: when %r|^/quick/(Marshal.#{Regexp.escape Gem.marshal_version}/)?(.*?)-([0-9.]+)(-.*?)?\.gemspec\.rz$| then 354: dep = Gem::Dependency.new $2, $3 355: specs = @source_index.search dep 356: 357: selector = [$2, $3, $4].map { |s| s.inspect }.join ' ' 358: 359: platform = if $4 then 360: Gem::Platform.new $4.sub(/^-/, '') 361: else 362: Gem::Platform::RUBY 363: end 364: 365: specs = specs.select { |s| s.platform == platform } 366: 367: if specs.empty? then 368: res.status = 404 369: res.body = "No gems found matching #{selector}" 370: elsif specs.length > 1 then 371: res.status = 500 372: res.body = "Multiple gems found matching #{selector}" 373: elsif $1 then # marshal quickindex instead of YAML 374: res.body << Zlib::Deflate.deflate(Marshal.dump(specs.first)) 375: else # deprecated YAML format 376: res.body << Zlib::Deflate.deflate(specs.first.to_yaml) 377: end 378: else 379: res.status = 404 380: res.body = "#{req.request_uri} not found" 381: end 382: end
# File lib/rubygems/server.rb, line 384 384: def run 385: @server.listen nil, @port 386: 387: say "Starting gem server on http://localhost:#{@port}/" 388: 389: WEBrick::Daemon.start if @daemon 390: 391: @server.mount_proc("/yaml") do |req, res| 392: res['content-type'] = 'text/plain' 393: res['date'] = File.stat(@spec_dir).mtime 394: if req.request_method == 'HEAD' then 395: res['content-length'] = @source_index.to_yaml.length 396: else 397: res.body << @source_index.to_yaml 398: end 399: end 400: 401: @server.mount_proc("/Marshal") do |req, res| 402: res['content-type'] = 'text/plain' 403: res['date'] = File.stat(@spec_dir).mtime 404: if req.request_method == 'HEAD' then 405: res['content-length'] = Marshal.dump(@source_index).length 406: else 407: res.body << Marshal.dump(@source_index) 408: end 409: end 410: 411: @server.mount_proc("/quick/", &method(:quick)) 412: 413: @server.mount_proc("/gem-server-rdoc-style.css") do |req, res| 414: res['content-type'] = 'text/css' 415: res['date'] = File.stat(@spec_dir).mtime 416: res.body << RDOC_CSS 417: end 418: 419: @server.mount_proc("/") do |req, res| 420: specs = [] 421: total_file_count = 0 422: 423: @source_index.each do |path, spec| 424: total_file_count += spec.files.size 425: deps = spec.dependencies.collect { |dep| 426: { "name" => dep.name, 427: "version" => dep.version_requirements.to_s, } 428: } 429: deps = deps.sort_by { |dep| [dep["name"].downcase, dep["version"]] } 430: deps.last["is_last"] = true unless deps.empty? 431: 432: # executables 433: executables = spec.executables.sort.collect { |exec| {"executable" => exec} } 434: executables = nil if executables.empty? 435: executables.last["is_last"] = true if executables 436: 437: specs << { 438: "authors" => spec.authors.sort.join(", "), 439: "date" => spec.date.to_s, 440: "dependencies" => deps, 441: "doc_path" => ('/doc_root/' + spec.full_name + '/rdoc/index.html'), 442: "executables" => executables, 443: "only_one_executable" => (executables && executables.size==1), 444: "full_name" => spec.full_name, 445: "has_deps" => !deps.empty?, 446: "homepage" => spec.homepage, 447: "name" => spec.name, 448: "rdoc_installed" => Gem::DocManager.new(spec).rdoc_installed?, 449: "summary" => spec.summary, 450: "version" => spec.version.to_s, 451: } 452: end 453: 454: specs << { 455: "authors" => "Chad Fowler, Rich Kilmer, Jim Weirich, Eric Hodel and others", 456: "dependencies" => [], 457: "doc_path" => "/doc_root/rubygems-#{Gem::RubyGemsVersion}/rdoc/index.html", 458: "executables" => [{"executable" => 'gem', "is_last" => true}], 459: "only_one_executable" => true, 460: "full_name" => "rubygems-#{Gem::RubyGemsVersion}", 461: "has_deps" => false, 462: "homepage" => "http://rubygems.org/", 463: "name" => 'rubygems', 464: "rdoc_installed" => true, 465: "summary" => "RubyGems itself", 466: "version" => Gem::RubyGemsVersion, 467: } 468: 469: specs = specs.sort_by { |spec| [spec["name"].downcase, spec["version"]] } 470: specs.last["is_last"] = true 471: 472: # tag all specs with first_name_entry 473: last_spec = nil 474: specs.each do |spec| 475: is_first = last_spec.nil? || (last_spec["name"].downcase != spec["name"].downcase) 476: spec["first_name_entry"] = is_first 477: last_spec = spec 478: end 479: 480: # create page from template 481: template = ERB.new(DOC_TEMPLATE) 482: res['content-type'] = 'text/html' 483: values = { "gem_count" => specs.size.to_s, "specs" => specs, 484: "total_file_count" => total_file_count.to_s } 485: result = template.result binding 486: res.body = result 487: end 488: 489: paths = { "/gems" => "/cache/", "/doc_root" => "/doc/" } 490: paths.each do |mount_point, mount_dir| 491: @server.mount(mount_point, WEBrick::HTTPServlet::FileHandler, 492: File.join(@gemdir, mount_dir), true) 493: end 494: 495: trap("INT") { @server.shutdown; exit! } 496: trap("TERM") { @server.shutdown; exit! } 497: 498: @server.start 499: end