Class: Gemirro::Server
- Inherits:
-
Object
- Object
- Gemirro::Server
- Defined in:
- lib/gemirro/server.rb
Overview
Launch TCPServer to easily download gems.
Instance Attribute Summary collapse
- #destination ⇒ String readonly
- #gems_fetcher ⇒ Gemirro::GemsFetcher readonly
- #server ⇒ TCPServer readonly
- #versions_fetcher ⇒ Object readonly
Instance Method Summary collapse
- #configuration ⇒ Object
-
#display_directory(session, resource) ⇒ Array
Display directory on the current sesion.
-
#fetch_gem(resource) ⇒ Indexer
Try to fetch gem and download its if it’s possible, and build and install indicies.
-
#generate_index ⇒ Indexer
Generate index and install indicies.
-
#initialize ⇒ Server
constructor
Initialize Server.
- #logger ⇒ Logger
-
#run ⇒ nil
Run the server and accept all connection.
Constructor Details
#initialize ⇒ Server
Initialize Server
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gemirro/server.rb', line 22 def initialize configuration.server_host = 'localhost' if configuration.server_host.nil? configuration.server_port = '2000' if configuration.server_port.nil? logger.info('Running server on ' \ "#{configuration.server_host}:#{configuration.server_port}") @server = TCPServer.new( configuration.server_host, configuration.server_port ) @destination = configuration.destination end |
Instance Attribute Details
#destination ⇒ String (readonly)
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gemirro/server.rb', line 16 class Server attr_reader :server, :destination, :versions_fetcher, :gems_fetcher ## # Initialize Server # def initialize configuration.server_host = 'localhost' if configuration.server_host.nil? configuration.server_port = '2000' if configuration.server_port.nil? logger.info('Running server on ' \ "#{configuration.server_host}:#{configuration.server_port}") @server = TCPServer.new( configuration.server_host, configuration.server_port ) @destination = configuration.destination end ## # Run the server and accept all connection # # @return [nil] # def run while (session = server.accept) request = session.gets logger.info(request) trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp resource = "#{@destination}/#{trimmedrequest}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 unless File.exist?(resource) logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}") session.print "HTTP/1.1 404/Object Not Found\r\n\r\n" session.close next end if File.directory?(resource) display_directory(session, resource) else mime_type = MIME::Types.type_for(resource) session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n" file = open(resource, 'rb') session.puts(file.read) end session.close end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ gem_name, gem_version = name.match(regexp).captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(configuration.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [TCPSocket] session # @param [String] resource # @return [Array] # def display_directory(session, resource) session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n" base_dir = Dir.new(resource) base_dir.entries.sort.each do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(@destination, '') session.print( "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" ) unless ['.', '..'].include?(File.basename(resource_path)) end end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher = Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
#gems_fetcher ⇒ Gemirro::GemsFetcher (readonly)
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gemirro/server.rb', line 16 class Server attr_reader :server, :destination, :versions_fetcher, :gems_fetcher ## # Initialize Server # def initialize configuration.server_host = 'localhost' if configuration.server_host.nil? configuration.server_port = '2000' if configuration.server_port.nil? logger.info('Running server on ' \ "#{configuration.server_host}:#{configuration.server_port}") @server = TCPServer.new( configuration.server_host, configuration.server_port ) @destination = configuration.destination end ## # Run the server and accept all connection # # @return [nil] # def run while (session = server.accept) request = session.gets logger.info(request) trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp resource = "#{@destination}/#{trimmedrequest}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 unless File.exist?(resource) logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}") session.print "HTTP/1.1 404/Object Not Found\r\n\r\n" session.close next end if File.directory?(resource) display_directory(session, resource) else mime_type = MIME::Types.type_for(resource) session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n" file = open(resource, 'rb') session.puts(file.read) end session.close end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ gem_name, gem_version = name.match(regexp).captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(configuration.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [TCPSocket] session # @param [String] resource # @return [Array] # def display_directory(session, resource) session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n" base_dir = Dir.new(resource) base_dir.entries.sort.each do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(@destination, '') session.print( "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" ) unless ['.', '..'].include?(File.basename(resource_path)) end end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher = Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
#server ⇒ TCPServer (readonly)
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gemirro/server.rb', line 16 class Server attr_reader :server, :destination, :versions_fetcher, :gems_fetcher ## # Initialize Server # def initialize configuration.server_host = 'localhost' if configuration.server_host.nil? configuration.server_port = '2000' if configuration.server_port.nil? logger.info('Running server on ' \ "#{configuration.server_host}:#{configuration.server_port}") @server = TCPServer.new( configuration.server_host, configuration.server_port ) @destination = configuration.destination end ## # Run the server and accept all connection # # @return [nil] # def run while (session = server.accept) request = session.gets logger.info(request) trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp resource = "#{@destination}/#{trimmedrequest}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 unless File.exist?(resource) logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}") session.print "HTTP/1.1 404/Object Not Found\r\n\r\n" session.close next end if File.directory?(resource) display_directory(session, resource) else mime_type = MIME::Types.type_for(resource) session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n" file = open(resource, 'rb') session.puts(file.read) end session.close end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ gem_name, gem_version = name.match(regexp).captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(configuration.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [TCPSocket] session # @param [String] resource # @return [Array] # def display_directory(session, resource) session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n" base_dir = Dir.new(resource) base_dir.entries.sort.each do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(@destination, '') session.print( "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" ) unless ['.', '..'].include?(File.basename(resource_path)) end end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher = Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
#versions_fetcher ⇒ Object (readonly)
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gemirro/server.rb', line 16 class Server attr_reader :server, :destination, :versions_fetcher, :gems_fetcher ## # Initialize Server # def initialize configuration.server_host = 'localhost' if configuration.server_host.nil? configuration.server_port = '2000' if configuration.server_port.nil? logger.info('Running server on ' \ "#{configuration.server_host}:#{configuration.server_port}") @server = TCPServer.new( configuration.server_host, configuration.server_port ) @destination = configuration.destination end ## # Run the server and accept all connection # # @return [nil] # def run while (session = server.accept) request = session.gets logger.info(request) trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp resource = "#{@destination}/#{trimmedrequest}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 unless File.exist?(resource) logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}") session.print "HTTP/1.1 404/Object Not Found\r\n\r\n" session.close next end if File.directory?(resource) display_directory(session, resource) else mime_type = MIME::Types.type_for(resource) session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n" file = open(resource, 'rb') session.puts(file.read) end session.close end end ## # Try to fetch gem and download its if it's possible, and # build and install indicies. # # @param [String] resource # @return [Indexer] # def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ gem_name, gem_version = name.match(regexp).captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end ## # Generate index and install indicies. # # @return [Indexer] # def generate_index indexer = Indexer.new(configuration.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end ## # Display directory on the current sesion # # @param [TCPSocket] session # @param [String] resource # @return [Array] # def display_directory(session, resource) session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n" base_dir = Dir.new(resource) base_dir.entries.sort.each do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(@destination, '') session.print( "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" ) unless ['.', '..'].include?(File.basename(resource_path)) end end ## # @see Gemirro::Configuration#logger # @return [Logger] # def logger configuration.logger end ## # @see Gemirro.configuration # def configuration Gemirro.configuration end ## # @see Gemirro::VersionsFetcher.fetch # def versions_fetcher @versions_fetcher ||= Gemirro::VersionsFetcher.new(configuration.source).fetch end ## # @return [Gemirro::GemsFetcher] # def gems_fetcher @gems_fetcher = Gemirro::GemsFetcher.new( configuration.source, versions_fetcher) end end |
Instance Method Details
#configuration ⇒ Object
145 146 147 |
# File 'lib/gemirro/server.rb', line 145 def configuration Gemirro.configuration end |
#display_directory(session, resource) ⇒ Array
Display directory on the current sesion
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gemirro/server.rb', line 118 def display_directory(session, resource) session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n" base_dir = Dir.new(resource) base_dir.entries.sort.each do |f| dir_sign = '' resource_path = resource.gsub(/\/$/, '') + '/' + f dir_sign = '/' if File.directory?(resource_path) resource_path = resource_path.gsub(/^public\//, '') resource_path = resource_path.gsub(@destination, '') session.print( "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" ) unless ['.', '..'].include?(File.basename(resource_path)) end end |
#fetch_gem(resource) ⇒ Indexer
Try to fetch gem and download its if it’s possible, and build and install indicies.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/gemirro/server.rb', line 79 def fetch_gem(resource) name = File.basename(resource) regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/ gem_name, gem_version = name.match(regexp).captures return unless gem_name && gem_version logger.info("Try to download #{gem_name} with version #{gem_version}") begin gems_fetcher.source.gems.clear gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version)) gems_fetcher.fetch rescue StandardError => e logger.error(e.) end generate_index end |
#generate_index ⇒ Indexer
Generate index and install indicies.
103 104 105 106 107 108 109 |
# File 'lib/gemirro/server.rb', line 103 def generate_index indexer = Indexer.new(configuration.destination) indexer.ui = ::Gem::SilentUI.new logger.info('Generating indexes') indexer.generate_index end |
#logger ⇒ Logger
138 139 140 |
# File 'lib/gemirro/server.rb', line 138 def logger configuration.logger end |
#run ⇒ nil
Run the server and accept all connection
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 |
# File 'lib/gemirro/server.rb', line 40 def run while (session = server.accept) request = session.gets logger.info(request) trimmedrequest = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp resource = "#{@destination}/#{trimmedrequest}" # Try to download gem if file doesn't exists fetch_gem(resource) unless File.exist?(resource) # If not found again, return a 404 unless File.exist?(resource) logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}") session.print "HTTP/1.1 404/Object Not Found\r\n\r\n" session.close next end if File.directory?(resource) display_directory(session, resource) else mime_type = MIME::Types.type_for(resource) session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n" file = open(resource, 'rb') session.puts(file.read) end session.close end end |