gemirro 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of gemirro might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d800c2fcc4025d9b6c61b262de67bd7fabaa16b
4
- data.tar.gz: 83b03d07f993704b6a944435cc0736f15cb691d1
3
+ metadata.gz: 88720262f04cf437d78990d801473153965d7ee8
4
+ data.tar.gz: 5d05de8d3fdb9b9cc07c69b4483a7263e9eb8690
5
5
  SHA512:
6
- metadata.gz: cc903688bd26f284ecba57a374c2bbd21121fdb0c27b2a358780b5bb6126177428416277052049e7babb987bf2737e984a99d5ea91f1775da21346a3253c54b4
7
- data.tar.gz: c4cfc5462bc6739b1857a5ff411dad6922fe69e8caeeff2605123bfea093377318d0afc6e0038ed93a52a4f6b271e5db8bcb55affbf6b8fdd054339e4b5ef534
6
+ metadata.gz: 61fd8692d6885444ea4505c698b6d7420910a0b8ed9c7315d6e87b6c94b1ed8b3fee10358cfe6cde6c6a97d734f105aad9b9b0e5373810b7af21a69c8f0fb0e7
7
+ data.tar.gz: d614a2c7de6b2ef408afc0194ce48c039831edf36037c8c60b297b5b39d9fea2d75ff99d99c1fac3b9928d4ec32e75e5478eb202e8179fcbcba4a85d05daa347
data/MANIFEST CHANGED
@@ -34,7 +34,6 @@ spec/gemirro/http_spec.rb
34
34
  spec/gemirro/indexer_spec.rb
35
35
  spec/gemirro/mirror_directory_spec.rb
36
36
  spec/gemirro/mirror_file_spec.rb
37
- spec/gemirro/server_spec.rb
38
37
  spec/gemirro/source_spec.rb
39
38
  spec/gemirro/versions_fetcher_spec.rb
40
39
  spec/gemirro/versions_file_spec.rb
data/gemirro.gemspec CHANGED
@@ -21,8 +21,9 @@ Gem::Specification.new do |s|
21
21
  s.add_dependency 'httpclient', '~>2.4'
22
22
  s.add_dependency 'confstruct', '~>0.2'
23
23
  s.add_dependency 'builder', '~>3.2'
24
+ s.add_dependency 'sinatra', '~>1.4'
25
+ s.add_dependency 'thin', '~>1.6'
24
26
 
25
- s.add_development_dependency 'mime-types', '~>2.3'
26
27
  s.add_development_dependency 'rake', '~>10.0'
27
28
  s.add_development_dependency 'rspec', '~>3.0'
28
29
  s.add_development_dependency 'simplecov', '~>0.9'
@@ -14,6 +14,7 @@ Gemirro::CLI.options.command 'server' do
14
14
  abort
15
15
  end
16
16
 
17
- Gemirro::Server.new.run
17
+ require 'gemirro/server'
18
+ Gemirro::Server.run!
18
19
  end
19
20
  end
@@ -1,4 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
+ require 'sinatra/base'
3
+ require 'thin'
2
4
 
3
5
  module Gemirro
4
6
  ##
@@ -13,23 +15,17 @@ module Gemirro
13
15
  # @!attribute [r] gems_fetcher
14
16
  # @return [Gemirro::GemsFetcher]
15
17
  #
16
- class Server
17
- attr_reader :server, :destination, :versions_fetcher, :gems_fetcher
18
-
18
+ class Server < Sinatra::Base
19
19
  ##
20
- # Initialize Server
20
+ # Configure server
21
21
  #
22
- def initialize
23
- configuration.server_host = 'localhost' if configuration.server_host.nil?
24
- configuration.server_port = '2000' if configuration.server_port.nil?
25
- logger.info('Running server on ' \
26
- "#{configuration.server_host}:#{configuration.server_port}")
27
- @server = TCPServer.new(
28
- configuration.server_host,
29
- configuration.server_port
30
- )
31
-
32
- @destination = configuration.destination
22
+ configure do
23
+ config = Gemirro.configuration
24
+ config.server_host = 'localhost' if config.server_host.nil?
25
+ config.server_port = '2000' if config.server_port.nil?
26
+ set :port, config.server_port
27
+ set :bind, config.server_host
28
+ set :destination, config.destination.gsub(/\/$/, '')
33
29
  end
34
30
 
35
31
  ##
@@ -37,42 +33,22 @@ module Gemirro
37
33
  #
38
34
  # @return [nil]
39
35
  #
40
- def run
41
- while (session = server.accept)
42
- request = session.gets.chomp
43
- logger.info(request)
44
-
45
- trimmedrequest = request
46
- .gsub(/(HEAD|GET)\ \//, '')
47
- .gsub(/\ HTTP.*/, '')
48
- .chomp
49
- resource = "#{@destination}/#{trimmedrequest}"
50
-
51
- # Try to download gem if file doesn't exists
52
- fetch_gem(resource) unless File.exist?(resource)
53
-
54
- # If not found again, return a 404
55
- unless File.exist?(resource)
56
- logger.warn("404 - #{trimmedrequest.gsub(/^public\//, '')}")
57
- session.print "HTTP/1.1 404/Object Not Found\r\n\r\n"
58
- session.close
59
- next
60
- end
36
+ get('*') do |path|
37
+ resource = "#{settings.destination}#{path}"
61
38
 
62
- begin
63
- if File.directory?(resource)
64
- display_directory(session, resource)
65
- else
66
- mime_type = MIME::Types.type_for(resource)
67
- session.print "HTTP/1.1 200/OK\r\nContent-type:#{mime_type}\r\n\r\n"
68
- file = open(resource, 'rb')
69
- session.puts(file.read)
70
- end
71
- rescue Errno::ECONNRESET, Errno::EPIPE => e
72
- logger.info(e.message)
73
- end
39
+ # Try to download gem if file doesn't exists
40
+ fetch_gem(resource) unless File.exist?(resource)
41
+ # If not found again, return a 404
42
+ return not_found unless File.exist?(resource)
74
43
 
75
- session.close
44
+ begin
45
+ if File.directory?(resource)
46
+ display_directory(resource)
47
+ else
48
+ send_file resource
49
+ end
50
+ rescue Errno::ECONNRESET, Errno::EPIPE => e
51
+ logger.info(e.message)
76
52
  end
77
53
  end
78
54
 
@@ -88,10 +64,12 @@ module Gemirro
88
64
  regexp = /^(.*)-(\d+(?:\.\d+){,4})\.gem(?:spec\.rz)?$/
89
65
  result = name.match(regexp)
90
66
  return unless result
67
+
91
68
  gem_name, gem_version = result.captures
92
69
  return unless gem_name && gem_version
93
70
 
94
71
  logger.info("Try to download #{gem_name} with version #{gem_version}")
72
+
95
73
  begin
96
74
  gems_fetcher.source.gems.clear
97
75
  gems_fetcher.source.gems.push(Gemirro::Gem.new(gem_name, gem_version))
@@ -109,7 +87,7 @@ module Gemirro
109
87
  # @return [Indexer]
110
88
  #
111
89
  def generate_index
112
- indexer = Indexer.new(configuration.destination)
90
+ indexer = Indexer.new(settings.destination)
113
91
  indexer.ui = ::Gem::SilentUI.new
114
92
 
115
93
  logger.info('Generating indexes')
@@ -119,24 +97,20 @@ module Gemirro
119
97
  ##
120
98
  # Display directory on the current sesion
121
99
  #
122
- # @param [TCPSocket] session
123
100
  # @param [String] resource
124
101
  # @return [Array]
125
102
  #
126
- def display_directory(session, resource)
127
- session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
103
+ def display_directory(resource)
128
104
  base_dir = Dir.new(resource)
129
- base_dir.entries.sort.each do |f|
105
+ base_dir.entries.sort.map do |f|
130
106
  dir_sign = ''
131
107
  resource_path = resource.gsub(/\/$/, '') + '/' + f
132
108
  dir_sign = '/' if File.directory?(resource_path)
133
109
  resource_path = resource_path.gsub(/^public\//, '')
134
- resource_path = resource_path.gsub(@destination, '')
135
-
136
- session.print(
137
- "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>"
138
- ) unless ['.', '..'].include?(File.basename(resource_path))
139
- end
110
+ resource_path = resource_path.gsub(settings.destination, '')
111
+ "<a href=\"#{resource_path}\">#{f}#{dir_sign}</a><br>" \
112
+ unless ['.', '..'].include?(File.basename(resource_path))
113
+ end.compact
140
114
  end
141
115
 
142
116
  ##
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # Gemirro Version
3
3
  module Gemirro
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/gemirro.rb CHANGED
@@ -10,8 +10,6 @@ require 'zlib'
10
10
  require 'httpclient'
11
11
  require 'logger'
12
12
  require 'stringio'
13
- require 'socket'
14
- require 'mime/types'
15
13
 
16
14
  unless $LOAD_PATH.include?(File.expand_path('../', __FILE__))
17
15
  $LOAD_PATH.unshift(File.expand_path('../', __FILE__))
@@ -22,7 +20,6 @@ require 'gemirro/configuration'
22
20
  require 'gemirro/gem'
23
21
  require 'gemirro/http'
24
22
  require 'gemirro/indexer'
25
- require 'gemirro/server'
26
23
  require 'gemirro/source'
27
24
  require 'gemirro/mirror_directory'
28
25
  require 'gemirro/mirror_file'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gemirro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Rambaud
@@ -67,19 +67,33 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.2'
69
69
  - !ruby/object:Gem::Dependency
70
- name: mime-types
70
+ name: sinatra
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ~>
74
74
  - !ruby/object:Gem::Version
75
- version: '2.3'
76
- type: :development
75
+ version: '1.4'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: thin
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :runtime
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - ~>
81
95
  - !ruby/object:Gem::Version
82
- version: '2.3'
96
+ version: '1.6'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -193,7 +207,6 @@ files:
193
207
  - spec/gemirro/indexer_spec.rb
194
208
  - spec/gemirro/mirror_directory_spec.rb
195
209
  - spec/gemirro/mirror_file_spec.rb
196
- - spec/gemirro/server_spec.rb
197
210
  - spec/gemirro/source_spec.rb
198
211
  - spec/gemirro/versions_fetcher_spec.rb
199
212
  - spec/gemirro/versions_file_spec.rb
@@ -1,96 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'spec_helper'
3
- require 'gemirro/source'
4
- require 'gemirro/server'
5
- require 'gemirro/mirror_file'
6
- require 'socket'
7
- require 'mime/types'
8
-
9
- # tests
10
- module Gemirro
11
- describe 'Server' do
12
- include FakeFS::SpecHelpers
13
-
14
- it 'should be initialized' do
15
- Gemirro.configuration.logger.should_receive(:info)
16
- .once.with('Running server on localhost:2000')
17
- TCPServer.should_receive(:new).with('localhost', '2000')
18
- server = Server.new
19
- expect(server).to be_a(Server)
20
- expect(server.destination).to eq(Gemirro.configuration.destination)
21
- end
22
-
23
- it 'should be initialized with host and port' do
24
- Gemirro.configuration.logger.should_receive(:info)
25
- .once.with('Running server on gemirro:1337')
26
- Gemirro.configuration.should_receive(:server_host)
27
- .at_least(:once).and_return('gemirro')
28
- Gemirro.configuration.should_receive(:server_port)
29
- .at_least(:once).and_return('1337')
30
- TCPServer.should_receive(:new).with('gemirro', '1337')
31
- server = Server.new
32
- expect(server).to be_a(Server)
33
- expect(server.destination).to eq(Gemirro.configuration.destination)
34
- end
35
-
36
- it 'should return logger' do
37
- TCPServer.should_receive(:new).once
38
- Gemirro.configuration.logger.should_receive(:info)
39
- .once.with('Running server on localhost:2000')
40
- server = Server.new
41
- expect(server.logger).to be(Gemirro.configuration.logger)
42
- end
43
-
44
- it 'should return configuration' do
45
- TCPServer.should_receive(:new).once
46
- Gemirro.configuration.logger.should_receive(:info)
47
- .once.with('Running server on localhost:2000')
48
- server = Server.new
49
- expect(server.configuration).to be(Gemirro.configuration)
50
- end
51
-
52
- it 'should return versions fetcher' do
53
- TCPServer.should_receive(:new).once
54
- Gemirro.configuration.logger.should_receive(:info)
55
- .once.with('Running server on localhost:2000')
56
- server = Server.new
57
- server.configuration.source = Source.new(
58
- 'rubygems', 'https://rubygems.org')
59
- Struct.new('ServerVersionsFetcher', :fetch)
60
-
61
- VersionsFetcher.should_receive(:new)
62
- .once
63
- .with(server.configuration.source).and_return(Struct::ServerVersionsFetcher.new(true))
64
- expect(server.gems_fetcher).to be_a(GemsFetcher)
65
- end
66
-
67
- it 'should display directory informations' do
68
- TCPServer.should_receive(:new).once
69
- Gemirro.configuration.logger.should_receive(:info)
70
- .once.with('Running server on localhost:2000')
71
- server = Server.new
72
- session = MockTCPSocket.new
73
- FileUtils.mkdir_p('public/directory')
74
- MirrorFile.new('public/directory/file').write('content')
75
- expect(server.display_directory(session, 'public/directory'))
76
- .to eq(['.', '..', 'file'])
77
- expect(session.output)
78
- .to eq("HTTP/1.1 200/OK\r\nContent-type:text/html"\
79
- "\r\n\r\n<a href=\"directory/file\">file</a><br>")
80
- end
81
- end
82
-
83
- ##
84
- # Mock TCP Socket
85
- class MockTCPSocket
86
- attr_accessor :response, :output
87
- def initialize
88
- @responses = []
89
- @output = ''
90
- end
91
-
92
- def print(line)
93
- @output << line
94
- end
95
- end
96
- end