ramaze 0.3.9 → 0.3.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,10 +32,10 @@ module Ramaze
32
32
  start_adapter
33
33
 
34
34
  Timeout.timeout(3) do
35
- sleep 0.01 until Global.adapters.any?
35
+ sleep 0.01 until Global.server
36
36
  end
37
37
 
38
- Global.adapters.each{|a| a.join} unless Global.run_loose
38
+ Global.server.join unless Global.run_loose
39
39
 
40
40
  rescue SystemExit
41
41
  Ramaze.shutdown
@@ -44,25 +44,24 @@ module Ramaze
44
44
  Ramaze.shutdown
45
45
  end
46
46
 
47
- # Takes Global.adapter and starts if test_connections is positive that
48
- # connections can be made to the specified host and ports.
47
+ # Takes Global.adapter and starts if test_connections is positive that a
48
+ # connection can be made to the specified host and port.
49
49
  # If you set Global.adapter to false it won't start any but deploy a
50
50
  # dummy which is useful for testing purposes where you just send fake
51
51
  # requests to Dispatcher.
52
52
 
53
53
  def start_adapter
54
54
  if adapter = Global.adapter
55
- host, ports = Global.host, Global.ports
55
+ host, port = Global.host, Global.port
56
56
 
57
57
  if Global.test_connections
58
- Log.info("Adapter: #{adapter}, testing connection to #{host}:#{ports}")
59
- test_connections(host, ports)
60
- Log.info("and we're running: #{host}:#{ports}")
58
+ test_connection(host, port)
59
+ Log.info("Ramaze is ready to run on: #{host}:#{port}")
61
60
  end
62
61
 
63
- adapter.start(host, ports)
62
+ adapter.start(host, port)
64
63
  else # run dummy
65
- Global.adapters.add Thread.new{ sleep }
64
+ Global.server = Thread.new{ sleep }
66
65
  Log.warn("Seems like Global.adapter is turned off", "Continue without adapter.")
67
66
  end
68
67
  rescue LoadError => ex
@@ -73,34 +72,20 @@ module Ramaze
73
72
  # them to finish, then goes on to kill them and exit still.
74
73
 
75
74
  def shutdown
76
- Timeout.timeout(1) do
77
- Global.adapters.each do |adapter|
78
- a = adapter[:adapter]
79
- a.shutdown if a.respond_to?(:shutdown)
80
- end
75
+ Timeout.timeout(3) do
76
+ a = Global.server[:adapter]
77
+ a.shutdown if a.respond_to?(:shutdown)
81
78
  end
82
79
  rescue Timeout::Error
83
- Global.adapters.each{|a| a.kill! }
80
+ Global.server.kill!
84
81
  # Hard exit! because it won't be able to kill Webrick otherwise
85
82
  exit!
86
83
  end
87
84
 
88
- # check the given host and ports via test_connection.
89
- # Shuts down if no connection is possible.
90
-
91
- def test_connections host, ports
92
- ports.each do |port|
93
- unless test_connection(host, port)
94
- Log.error("Cannot open connection on #{host}:#{port}")
95
- Ramaze.shutdown
96
- end
97
- end
98
- end
99
-
100
85
  # Opens a TCPServer temporarily and returns true if a connection is
101
86
  # possible and false if none can be made
102
87
 
103
- def test_connection host, port
88
+ def test_connection(host, port)
104
89
  Timeout.timeout(1) do
105
90
  testsock = TCPServer.new(host, port)
106
91
  testsock.close
@@ -108,7 +93,8 @@ module Ramaze
108
93
  end
109
94
  rescue => ex
110
95
  Log.error(ex)
111
- false
96
+ Log.error("Cannot open connection on #{host}:#{port}")
97
+ Ramaze.shutdown
112
98
  end
113
99
  end
114
100
  end
@@ -14,11 +14,9 @@ module Ramaze
14
14
  # Afterwards adds a trap for the value of Global.shutdown_trap which
15
15
  # calls Ramaze.shutdown when triggered (usually by SIGINT).
16
16
 
17
- def start host, ports
18
- ports.each do |port|
19
- Global.adapters.add(run_server(host, port))
20
- trap(Global.shutdown_trap){ Ramaze.shutdown }
21
- end
17
+ def start host, port
18
+ Global.server = run_server(host, port)
19
+ trap(Global.shutdown_trap){ Ramaze.shutdown }
22
20
  end
23
21
 
24
22
  # Does nothing
@@ -49,7 +47,11 @@ module Ramaze
49
47
  # Then goes on and calls Dispatcher::handle with request and response.
50
48
 
51
49
  def respond env
52
- Current.call env
50
+ if Global.server == Thread.current
51
+ Thread.new{ Current.call(env) }.value
52
+ else
53
+ Current.call(env)
54
+ end
53
55
  end
54
56
  end
55
57
  end
@@ -1,8 +1,6 @@
1
1
  require 'ebb'
2
2
 
3
3
  module Ramaze
4
- Global.test_connections = true
5
-
6
4
  module Adapter
7
5
  class Ebb < Base
8
6
  class << self
@@ -5,8 +5,6 @@ require 'mongrel'
5
5
  require 'rack/handler/mongrel'
6
6
 
7
7
  module Ramaze
8
- Global.test_connections = true
9
-
10
8
  module Adapter
11
9
 
12
10
  # Our Mongrel adapter acts as wrapper for the Rack::Handler::Mongrel.
@@ -4,8 +4,6 @@
4
4
  require 'rack/handler/scgi'
5
5
 
6
6
  module Ramaze
7
- Global.test_connections = true
8
-
9
7
  module Adapter
10
8
  # Our Scgi adapter acts as wrapper for the Rack::Handler::SCGI.
11
9
  class Scgi < Base
@@ -2,8 +2,6 @@ require 'thin'
2
2
  require 'rack/handler/thin'
3
3
 
4
4
  module Ramaze
5
- Global.test_connections = true
6
-
7
5
  module Adapter
8
6
  class Thin < Base
9
7
  class << self
@@ -5,8 +5,6 @@ require 'webrick'
5
5
  require 'rack/handler/webrick'
6
6
 
7
7
  module Ramaze
8
- Global.test_connections = true
9
-
10
8
  module Adapter
11
9
  # Our WEBrick adapter acts as wrapper for the Rack::Handler::WEBrick.
12
10
  class WEBrick < Base
@@ -53,7 +53,7 @@ module Facebook
53
53
 
54
54
  begin
55
55
  ret = post(data)
56
- rescue Errno::ECONNRESET
56
+ rescue Errno::ECONNRESET, Errno::EPIPE
57
57
  @server = connect
58
58
  retry
59
59
  end while ret.empty? and @server = connect
@@ -161,9 +161,11 @@ module Ramaze
161
161
  ancestor <= Ramaze::Controller
162
162
  } + Helper::LOOKUP.to_a
163
163
 
164
- meths = ancs.map{|a|
165
- a.instance_methods(false).map{|iv| iv.to_s }
166
- }.flatten.uniq
164
+ ancs.reverse.inject [] do |meths, anc|
165
+ meths +
166
+ anc.public_instance_methods(false).map{|im| im.to_s } -
167
+ anc.private_instance_methods(false).map{|im| im.to_s }
168
+ end
167
169
  end
168
170
 
169
171
  # Generate all possible permutations for given path.
@@ -46,7 +46,7 @@ module Ramaze
46
46
  Dispatcher::Error.process(error, meta)
47
47
  end
48
48
 
49
- # protcets against recursive dispatch and reassigns the path_info in the
49
+ # protects against recursive dispatch and reassigns the path_info in the
50
50
  # request, the rest of the request is kept intact.
51
51
  def dispatch_to(path)
52
52
  if request.path_info == path
@@ -29,7 +29,8 @@ module Ramaze
29
29
  body = Controller.handle(path)
30
30
  response = Response.current.build(body)
31
31
  }
32
- FILTER.inject(response){|r,f| f.call(r) }
32
+ FILTER.each{|f| f.call(response)}
33
+ response
33
34
  rescue Ramaze::Error => ex
34
35
  ex
35
36
  end
data/lib/ramaze/global.rb CHANGED
@@ -8,9 +8,6 @@ module Ramaze
8
8
  o "Set the adapter Ramaze will run on.",
9
9
  :adapter => :webrick, :cli => [:webrick, :mongrel, :thin]
10
10
 
11
- o "All running threads of Adapter will be collected here.",
12
- :adapters => Set.new
13
-
14
11
  o "Set the size of Backtrace shown.",
15
12
  :backtrace_size => 10, :cli => 10
16
13
 
@@ -85,6 +82,9 @@ module Ramaze
85
82
  o "Don't wait until all adapter-threads are finished or killed.",
86
83
  :run_loose => false, :cli => false
87
84
 
85
+ o "The running server will be put here from Adapter.",
86
+ :server => nil
87
+
88
88
  o "Turn on session for all requests.",
89
89
  :sessions => true, :cli => true
90
90
 
@@ -97,9 +97,6 @@ module Ramaze
97
97
  o "Interval in seconds of the background SourceReload",
98
98
  :sourcereload => 3, :cli => 3
99
99
 
100
- o "How many adapters Ramaze should spawn.",
101
- :spawn => 1, :cli => 1, :short => :s
102
-
103
100
  o "Test before start if adapters will be able to connect",
104
101
  :test_connections => true, :cli => true
105
102
 
@@ -89,11 +89,6 @@ module Ramaze
89
89
  cache = Ramaze.const_get(class_name)
90
90
  end
91
91
 
92
- # a range built from port and the number of spawns.
93
- def ports
94
- (port.to_i..(port.to_i + (spawn.to_i - 1)))
95
- end
96
-
97
92
  # reassigns the interval in the instance of SourceReload that is running or
98
93
  # just waiting.
99
94
  def sourcereload=(interval)
@@ -51,13 +51,24 @@ module Ramaze
51
51
  options = { :controller => current.controller,
52
52
  :instance => current.instance.dup }
53
53
 
54
- roots = [options[:controller].template_paths].flatten
55
-
56
- if (files = Dir["{#{roots.join(',')}}"/"{#{file},#{file}.*}"]).any?
57
- options[:template] = files.first.squeeze '/'
54
+ file = file.to_s
55
+
56
+ if Pathname(file).absolute?
57
+ file = file.squeeze '/'
58
+ unless File.exist?(file)
59
+ Log.warn "render_template: #{file} does not exist."
60
+ return ''
61
+ end
62
+ options[:template] = file
58
63
  else
59
- Log.warn "render_template: #{file} does not exist in the following directories: #{roots.join(',')}."
60
- return ''
64
+ roots = [options[:controller].template_paths].flatten
65
+
66
+ if (files = Dir["{#{roots.join(',')}}"/"{#{file},#{file}.*}"]).any?
67
+ options[:template] = files.first.squeeze '/'
68
+ else
69
+ Log.warn "render_template: #{file} does not exist in the following directories: #{roots.join(',')}."
70
+ return ''
71
+ end
61
72
  end
62
73
 
63
74
  binding = options[:instance].scope
@@ -4,12 +4,11 @@
4
4
  class Browser
5
5
  attr_reader :cookie, :http
6
6
 
7
- def initialize(url = '/', base = '/', &block)
7
+ def initialize(base = '/', &block)
8
8
  @base = base
9
9
  @history = []
10
- @http = SimpleHttp.new(url2uri(url))
11
-
12
- get url
10
+ @uri = URI("http://localhost:#{Ramaze::Global.port}")
11
+ @http = SimpleHttp.new(@uri)
13
12
 
14
13
  story(&block) if block_given?
15
14
  end
@@ -18,28 +17,28 @@ class Browser
18
17
  instance_eval(&block) if block_given?
19
18
  end
20
19
 
21
- def get url = '/', hash = {}
22
- request(:get, url, hash)
20
+ def get path = '/', hash = {}
21
+ request(:get, path, hash)
23
22
  end
24
23
 
25
- def post url = '/', hash = {}
26
- request(:post, url, hash)
24
+ def post path = '/', hash = {}
25
+ request(:post, path, hash)
27
26
  end
28
27
 
29
- def erequest method, url, hash = {}
30
- response = request(method, url, hash)
28
+ def erequest method, path, hash = {}
29
+ response = request(method, path, hash)
31
30
  eval(response)
32
31
  rescue Object => ex
33
32
  p :response => response
34
33
  ex.message
35
34
  end
36
35
 
37
- def epost url = '/', hash = {}
38
- erequest(:post, url, hash)
36
+ def epost path = '/', hash = {}
37
+ erequest(:post, path, hash)
39
38
  end
40
39
 
41
- def eget url = '/', hash = {}
42
- erequest(:get, url, hash)
40
+ def eget path = '/', hash = {}
41
+ erequest(:get, path, hash)
43
42
  end
44
43
 
45
44
  def hget(*args)
@@ -66,8 +65,8 @@ class Browser
66
65
  end
67
66
  end
68
67
 
69
- def request method, url, hash = {}
70
- @http.uri = url2uri(url)
68
+ def request method, path, hash = {}
69
+ @http.uri = @uri + "/#{@base}/#{path}".squeeze('/')
71
70
  @http.request_headers['referer'] = @history.last.path rescue '/'
72
71
 
73
72
  if method == :get and not hash.empty?
@@ -86,13 +85,4 @@ class Browser
86
85
  @cookie = @http.response_headers['set-cookie']
87
86
  @http.request_headers['cookie'] = @cookie
88
87
  end
89
-
90
- def url2uri url
91
- uri = URI.parse(url)
92
- uri.scheme = 'http'
93
- uri.host = 'localhost'
94
- uri.port = Ramaze::Global.port
95
- uri.path = "/#{@base}/#{url}".squeeze('/')
96
- uri
97
- end
98
88
  end
@@ -5,7 +5,7 @@ module Ramaze #:nodoc:
5
5
  module Version #:nodoc:
6
6
  MAJOR = 0
7
7
  MINOR = 3
8
- TINY = 9
8
+ TINY = 9.1
9
9
 
10
10
  STRING = [MAJOR, MINOR, TINY].join('.')
11
11
  end
@@ -0,0 +1,32 @@
1
+ require 'spec/helper'
2
+ require 'examples/simple_auth'
3
+
4
+ describe "SimpleAuth" do
5
+ behaves_like 'browser'
6
+ ramaze(:adapter => :webrick)
7
+
8
+ it 'should show start page' do
9
+ Browser.new do
10
+ http.basic_authentication "username", "password"
11
+ get('/').should == "Secret Info"
12
+ end
13
+
14
+ Browser.new do
15
+ http.basic_authentication "admin", "secret"
16
+ get('/').should == "Secret Info"
17
+ end
18
+ end
19
+
20
+ it 'should not show start page' do
21
+ Browser.new do
22
+ lambda{ get('/') }.should.raise
23
+ end
24
+
25
+ Browser.new do
26
+ lambda do
27
+ http.basic_authentication "hello", "world"
28
+ lambda{ get('/') }.should.raise
29
+ end
30
+ end
31
+ end
32
+ end
@@ -54,7 +54,7 @@ describe "StackHelper" do
54
54
  ].each do |controller|
55
55
 
56
56
  it controller.to_s do
57
- Browser.new('/', Ramaze::Global.mapping.invert[controller]) do
57
+ Browser.new(Ramaze::Global.mapping.invert[controller]) do
58
58
  get('/secured').should == ''
59
59
  post('/login', 'username' => 'manveru', 'password' => 'password')
60
60
  get('/secured').should == 'Secret content'
@@ -73,7 +73,7 @@ describe Ramaze::Helper::Flash do
73
73
  ramaze :adapter => :webrick
74
74
 
75
75
  should "set and forget flash twice" do
76
- Browser.new('/') do
76
+ Browser.new do
77
77
  get('/first_here')
78
78
  get('/then_here').should == 'hey'
79
79
  2.times{ get('/then_here').should.be.empty }
@@ -56,6 +56,7 @@ describe "StackHelper" do
56
56
 
57
57
  it "indirect login" do
58
58
  Browser.new do
59
+ get('/')
59
60
  get('/foo').should == 'logged in'
60
61
  get('/secure').should == 'secret content'
61
62
  eget('/').should == {:logged_in => true}
@@ -64,6 +65,7 @@ describe "StackHelper" do
64
65
 
65
66
  it "indirect login with params" do
66
67
  Browser.new do
68
+ get('/')
67
69
  eget('/bar', 'x' => 'y').should == {'x' => 'y'}
68
70
  eget('/').should == {:logged_in => true}
69
71
  end
@@ -36,7 +36,7 @@ describe Ramaze::Helper::User do
36
36
  ramaze :adapter => :webrick
37
37
 
38
38
  should 'login' do
39
- Browser.new '/status' do
39
+ Browser.new do
40
40
  get('/status').should == 'false'
41
41
  get('/login', 'name' => 'arthur', 'password' => '42')
42
42
  get('/status').should == 'true'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ramaze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.3.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - manveru
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-16 00:00:00 -07:00
12
+ date: 2008-03-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -301,6 +301,7 @@ files:
301
301
  - spec/examples/hello.rb
302
302
  - spec/examples/linking.rb
303
303
  - spec/examples/simple.rb
304
+ - spec/examples/simple_auth.rb
304
305
  - spec/examples/templates
305
306
  - spec/examples/templates/template_amrita2.rb
306
307
  - spec/examples/templates/template_erubis.rb