Capcode 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -76,8 +76,8 @@ module Capcode::Views
76
76
  end
77
77
  end
78
78
 
79
- Capcode.map( "/file" ) do
80
- Rack::File.new( "." )
81
- end
79
+ #Capcode.map( "/file" ) do
80
+ # Rack::File.new( "." )
81
+ #end
82
82
 
83
- Capcode.run( :port => 3001, :host => "localhost" )
83
+ Capcode.run( :port => 3001, :host => "localhost", :root => "." )
@@ -0,0 +1 @@
1
+ welcome in test directory
@@ -1,7 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'rack'
3
- require 'json'
3
+ require 'json' ## DELETE THIS IN 1.0.0
4
4
  require 'logger'
5
+ require 'mime/types'
5
6
  require 'capcode/version'
6
7
  require 'capcode/core_ext'
7
8
 
@@ -21,6 +22,11 @@ module Capcode
21
22
 
22
23
  # Helpers contains methods available in your controllers
23
24
  module Helpers
25
+ @@__ROOT_DIRECTORY = ""
26
+ def self.root=(p)
27
+ @@__ROOT_DIRECTORY=p
28
+ end
29
+
24
30
  # Render a view
25
31
  def render( h )
26
32
  if h.class == Hash
@@ -46,7 +52,7 @@ module Capcode
46
52
  # end
47
53
  # end
48
54
  # end
49
- def json( d )
55
+ def json( d ) ## DELETE THIS IN 1.0.0
50
56
  warn( "json is deprecated, please use `render( :json => ... )'" )
51
57
  @response['Content-Type'] = 'application/json'
52
58
  d.to_json
@@ -113,22 +119,66 @@ module Capcode
113
119
  # end
114
120
  # end
115
121
  #
116
- # Do the same (r500 and r501) to customize 500 and 501 errors
122
+ # Do the same (r500, r501, r403) to customize 500, 501, 403 errors
117
123
  class HTTPError
118
- def initialize(app)#:nodoc:
124
+ def initialize(app, root)#:nodoc:
119
125
  @app = app
126
+ @root = root
120
127
  end
121
128
 
122
129
  def call(env) #:nodoc:
123
- status, headers, body = @app.call(env)
130
+ @env = env
131
+ @status, @headers, @body = @app.call(@env)
124
132
 
125
- if self.methods.include? "r#{status}"
126
- body = self.send( "r#{status}", env['REQUEST_PATH'] )
127
- headers['Content-Length'] = body.length.to_s
133
+ if @status == 404
134
+ if @env["PATH_INFO"].include? ".."
135
+ @status = 403
136
+ @headers = {}
137
+ @body = '403 - Invalid path'
138
+ error
139
+ end
140
+
141
+ @path = File.join(@root, @env["PATH_INFO"])
142
+ begin
143
+ if File.file?(@path) && File.readable?(@path)
144
+ serving
145
+ else
146
+ tmpPath = File.join(@root, @env["PATH_INFO"], "index.html")
147
+ if File.file?(tmpPath) && File.readable?(tmpPath) # @env["PATH_INFO"][-1].chr == "/" &&
148
+ @path = tmpPath
149
+ serving
150
+ else
151
+ raise Errno::EPERM
152
+ end
153
+ end
154
+ rescue SystemCallError
155
+ error
156
+ end
157
+ else
158
+ error
159
+ end
160
+ end
161
+
162
+ def serving #:nodoc:
163
+ body = open(@path).read
164
+ size = body.size
165
+
166
+ [200, {
167
+ "Last-Modified" => File.mtime(@path).httpdate,
168
+ "Content-Type" => (MIME::Types.type_for(@path)[0] || '/text/plain').to_s,
169
+ "Content-Length" => size.to_s
170
+ }, body]
171
+ end
172
+
173
+ def error #:nodoc:
174
+ if self.methods.include? "r#{@status}"
175
+ @body = self.send( "r#{@status}", @env['REQUEST_PATH'] )
176
+ @headers['Content-Length'] = @body.length.to_s
128
177
  end
129
178
 
130
- [status, headers, body]
179
+ [@status, @headers, @body]
131
180
  end
181
+
132
182
  end
133
183
 
134
184
  class << self
@@ -279,6 +329,8 @@ module Capcode
279
329
  # * <tt>:pid</tt> = PID file (default: $0.pid)
280
330
  # * <tt>:daemonize</tt> = Daemonize application (default: false)
281
331
  # * <tt>:db_config</tt> = database configuration file (default: database.yml)
332
+ # * <tt>:root</tt> = root directory (default: "." -- relative to the working directory)
333
+ # * <tt>:working_directory</tt> = Working directory (default: directory of the main.rb)
282
334
  def run( args = {} )
283
335
  conf = {
284
336
  :port => args[:port]||3000,
@@ -288,79 +340,87 @@ module Capcode
288
340
  :session => args[:session]||{},
289
341
  :pid => args[:pid]||"#{$0}.pid",
290
342
  :daemonize => args[:daemonize]||false,
291
- :db_config => args[:db_config]||"database.yml"
343
+ :db_config => args[:db_config]||"database.yml",
344
+ :root => args[:root]||".",
345
+ :working_directory => args[:working_directory]||File.expand_path(File.dirname($0))
292
346
  }
293
347
 
294
- # Check that mongrel exists
295
- if conf[:server].nil? || conf[:server] == "mongrel"
296
- begin
297
- require 'mongrel'
298
- conf[:server] = "mongrel"
299
- rescue LoadError
300
- puts "!! could not load mongrel. Falling back to webrick."
301
- conf[:server] = "webrick"
348
+ # Run in the Working directory
349
+ Dir.chdir( conf[:working_directory] ) do
350
+ # Set root directory for helpers
351
+ Capcode::Helpers.root = File.expand_path( conf[:root] )
352
+
353
+ # Check that mongrel exists
354
+ if conf[:server].nil? || conf[:server] == "mongrel"
355
+ begin
356
+ require 'mongrel'
357
+ conf[:server] = "mongrel"
358
+ rescue LoadError
359
+ puts "!! could not load mongrel. Falling back to webrick."
360
+ conf[:server] = "webrick"
361
+ end
302
362
  end
303
- end
304
-
305
- Capcode.constants.each do |k|
306
- begin
307
- if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
308
- u, m, c = eval "Capcode::#{k}.__urls__"
309
- u.keys.each do |_u|
310
- raise Capcode::RouteError, "Route `#{_u}' already define !", caller if @@__ROUTES.keys.include?(_u)
311
- @@__ROUTES[_u] = c.new
363
+
364
+ Capcode.constants.each do |k|
365
+ begin
366
+ if eval "Capcode::#{k}.public_methods(true).include?( '__urls__' )"
367
+ u, m, c = eval "Capcode::#{k}.__urls__"
368
+ u.keys.each do |_u|
369
+ raise Capcode::RouteError, "Route `#{_u}' already define !", caller if @@__ROUTES.keys.include?(_u)
370
+ @@__ROUTES[_u] = c.new
371
+ end
312
372
  end
373
+ rescue => e
374
+ raise e.message
313
375
  end
314
- rescue => e
315
- raise e.message
316
376
  end
317
- end
318
-
319
- app = Rack::URLMap.new(@@__ROUTES)
320
- app = Rack::Session::Cookie.new( app, conf[:session] )
321
- app = Capcode::HTTPError.new(app)
322
- app = Rack::ContentLength.new(app)
323
- app = Rack::Lint.new(app)
324
- app = Rack::ShowExceptions.new(app)
325
- # app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
326
- app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
327
-
328
- # From rackup !!!
329
- if conf[:daemonize]
330
- if RUBY_VERSION < "1.9"
331
- exit if fork
332
- Process.setsid
333
- exit if fork
334
- # Dir.chdir "/"
335
- File.umask 0000
336
- STDIN.reopen "/dev/null"
337
- STDOUT.reopen "/dev/null", "a"
338
- STDERR.reopen "/dev/null", "a"
339
- else
340
- Process.daemon
377
+
378
+ app = Rack::URLMap.new(@@__ROUTES)
379
+ app = Rack::Session::Cookie.new( app, conf[:session] )
380
+ app = Capcode::HTTPError.new(app, conf[:root])
381
+ app = Rack::ContentLength.new(app)
382
+ app = Rack::Lint.new(app)
383
+ app = Rack::ShowExceptions.new(app)
384
+ # app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!
385
+ app = Rack::CommonLogger.new( app, Logger.new(conf[:log]) )
386
+
387
+ # From rackup !!!
388
+ if conf[:daemonize]
389
+ if RUBY_VERSION < "1.9"
390
+ exit if fork
391
+ Process.setsid
392
+ exit if fork
393
+ # Dir.chdir "/"
394
+ File.umask 0000
395
+ STDIN.reopen "/dev/null"
396
+ STDOUT.reopen "/dev/null", "a"
397
+ STDERR.reopen "/dev/null", "a"
398
+ else
399
+ Process.daemon
400
+ end
401
+
402
+ File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") }
403
+ at_exit { File.delete(conf[:pid]) if File.exist?(conf[:pid]) }
404
+ end
405
+
406
+ # Start database
407
+ if self.methods.include? "db_connect"
408
+ db_connect( conf[:db_config], conf[:log] )
409
+ end
410
+
411
+ # Start server
412
+ case conf[:server]
413
+ when "mongrel"
414
+ puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}"
415
+ Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server|
416
+ trap "SIGINT", proc { server.stop }
417
+ }
418
+ when "webrick"
419
+ puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}"
420
+ Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server|
421
+ trap "SIGINT", proc { server.shutdown }
422
+ }
341
423
  end
342
-
343
- File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") }
344
- at_exit { File.delete(conf[:pid]) if File.exist?(conf[:pid]) }
345
- end
346
-
347
- # Start database
348
- if self.methods.include? "db_connect"
349
- db_connect( conf[:db_config], conf[:log] )
350
- end
351
-
352
- # Start server
353
- case conf[:server]
354
- when "mongrel"
355
- puts "** Starting Mongrel on #{conf[:host]}:#{conf[:port]}"
356
- Rack::Handler::Mongrel.run( app, {:Port => conf[:port], :Host => conf[:host]} ) { |server|
357
- trap "SIGINT", proc { server.stop }
358
- }
359
- when "webrick"
360
- puts "** Starting WEBrick on #{conf[:host]}:#{conf[:port]}"
361
- Rack::Handler::WEBrick.run( app, {:Port => conf[:port], :BindAddress => conf[:host]} ) { |server|
362
- trap "SIGINT", proc { server.shutdown }
363
- }
364
424
  end
365
425
  end
366
426
 
@@ -2,9 +2,9 @@ require 'erb'
2
2
 
3
3
  module Capcode
4
4
  module Helpers
5
- @@__ERB_PATH__ = File.expand_path( "." )
5
+ @@__ERB_PATH__ = "."
6
6
  def self.erb_path=( p )
7
- @@__ERB_PATH__ = File.expand_path( p )
7
+ @@__ERB_PATH__ = p
8
8
  end
9
9
 
10
10
  def render_erb( f )
@@ -12,7 +12,11 @@ module Capcode
12
12
  if f.include? '..'
13
13
  return [403, {}, '403 - Invalid path']
14
14
  end
15
-
15
+
16
+ unless( @@__ERB_PATH__[0].chr == "/" )
17
+ @@__ERB_PATH__ = File.expand_path( File.join(@@__ROOT_DIRECTORY, @@__ERB_PATH__) )
18
+ end
19
+
16
20
  f = f + ".rhtml" if File.extname( f ) != ".rhtml"
17
21
  file = File.join( @@__ERB_PATH__, f )
18
22
  ERB.new(open(file).read).result(binding)
@@ -2,9 +2,9 @@ require "haml"
2
2
 
3
3
  module Capcode
4
4
  module Helpers
5
- @@__HAML_PATH__ = File.expand_path( "." )
5
+ @@__HAML_PATH__ = "."
6
6
  def self.haml_path=( p )
7
- @@__HAML_PATH__ = File.expand_path( p )
7
+ @@__HAML_PATH__ = p
8
8
  end
9
9
 
10
10
  def render_haml( f ) #:nodoc:
@@ -12,7 +12,11 @@ module Capcode
12
12
  if f.include? '..'
13
13
  return [403, {}, '403 - Invalid path']
14
14
  end
15
-
15
+
16
+ unless( @@__HAML_PATH__[0].chr == "/" )
17
+ @@__HAML_PATH__ = File.expand_path( File.join(@@__ROOT_DIRECTORY, @@__HAML_PATH__) )
18
+ end
19
+
16
20
  f = f + ".haml" if File.extname( f ) != ".haml"
17
21
  file = File.join( @@__HAML_PATH__, f )
18
22
  Haml::Engine.new( open( file ).read ).to_html( self )
@@ -1,3 +1,3 @@
1
1
  module Capcode
2
- CAPCOD_VERION="0.5.0"
2
+ CAPCOD_VERION="0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Capcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Gr\xC3\xA9goire Lejeune"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-07 00:00:00 +02:00
12
+ date: 2009-06-09 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mime-types
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
25
35
  description: Capcode is a web microframework
26
36
  email: gregoire.lejeune@free.fr
27
37
  executables: []
@@ -40,6 +50,7 @@ files:
40
50
  - setup.rb
41
51
  - doc/rdoc/classes/Capcode/Helpers.html
42
52
  - doc/rdoc/classes/Capcode/HTTPError.html
53
+ - doc/rdoc/classes/Capcode/RenderError.html
43
54
  - doc/rdoc/classes/Capcode/Views.html
44
55
  - doc/rdoc/classes/Capcode.html
45
56
  - doc/rdoc/created.rid
@@ -70,13 +81,19 @@ files:
70
81
  - examples/my_blog.db
71
82
  - examples/sample.rb
72
83
  - examples/session.rb
84
+ - examples/test/index.html
73
85
  has_rdoc: true
74
86
  homepage: http://algorithmique.net
75
87
  licenses: []
76
88
 
77
- post_install_message: |-
78
- For more information about Capcode,
79
- see http://capcode.rubyforge.org
89
+ post_install_message: |+
90
+
91
+ If you want to use Markaby renderer, you must install Markaby.
92
+ If you want to use HAML renderer, you must install haml.
93
+
94
+ If For more information about Capcode, see
95
+ http://capcode.rubyforge.org"
96
+
80
97
  rdoc_options:
81
98
  - --quiet
82
99
  - --title
@@ -106,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
123
  requirements: []
107
124
 
108
125
  rubyforge_project: capcode
109
- rubygems_version: 1.3.3
126
+ rubygems_version: 1.3.4
110
127
  signing_key:
111
128
  specification_version: 3
112
129
  summary: Capcode is a web microframework