serve 0.10.2 → 0.11.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,14 @@
1
+ = Change Log
2
+
3
+ == 0.11.2 (May 25, 2010)
4
+
5
+ * Add dependency on Rack to gemspec [jlong]
6
+
7
+ == 0.11.1 (May 25, 2010)
8
+
9
+ * Added support for Ruby 1.9 [jlong]
10
+ * Refactored to use Rack internally [jlong]
11
+
1
12
  == 0.10.2 (May 13, 2010)
2
13
 
3
14
  * Changed render helper to support passing the name of the partial as the first parameter to match the new Rails syntax [jlong]
data/QUICKSTART.rdoc CHANGED
@@ -126,4 +126,6 @@ The page should now have a title and heading that both read "Login".
126
126
 
127
127
  == Content For
128
128
 
129
+
130
+
129
131
  == Partials
data/README.rdoc CHANGED
@@ -62,7 +62,7 @@ markdown :: Evaluates the document as Markdown (requires the Bluecloth gem)
62
62
  erb :: Experimental support for ERB
63
63
  haml :: Evaluates the document as Haml (requires the Haml gem)
64
64
  sass :: Evaluates the document as Sass (requires the Haml gem)
65
- scss :: Evaluates the document as Scss (requires the Haml gem)
65
+ scss :: Evaluates the document as SCSS (requires the Haml gem)
66
66
  email :: Evaluates the document as if it is an e-mail message; the format is identical to a plain/text e-mail message's source
67
67
  redirect :: Redirects to the URL contained in the document
68
68
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.2
1
+ 0.11.2
data/bin/serve CHANGED
@@ -2,7 +2,7 @@
2
2
  lib = File.dirname(__FILE__) + '/../lib'
3
3
  require 'rubygems'
4
4
 
5
- gem 'activesupport'
5
+ gem 'activesupport', '= 2.3.5'
6
6
  require 'active_support'
7
7
 
8
8
  if File.file?(lib + '/serve/version.rb')
@@ -1,8 +1,5 @@
1
- require 'webrick'
2
1
  require 'active_support'
3
- require 'serve/webrick/extensions'
4
- require 'serve/webrick/servlet'
5
- require 'serve/webrick/server'
2
+ require 'serve/rack'
6
3
 
7
4
  module Serve
8
5
  class Application
@@ -75,8 +72,8 @@ module Serve
75
72
  " http://localhost:4000/",
76
73
  " ",
77
74
  " If the haml, redcloth, or bluecloth gems are installed the command can serve ",
78
- " Haml, Sass, Textile, and Markdown for documents with haml, sass, textile, ",
79
- " and markdown file extensions.",
75
+ " Haml, Sass, SCSS, Textile, and Markdown for documents with haml, sass, scss, ",
76
+ " textile, and markdown file extensions.",
80
77
  " ",
81
78
  " If the Rails command script/server exists in the current directory the ",
82
79
  " script will start that instead with the specified environment or the ",
@@ -138,15 +135,18 @@ module Serve
138
135
  end
139
136
 
140
137
  def run_server
141
- extensions = Serve::WEBrick::Server.register_handlers
142
- server = Serve::WEBrick::Server.new(
143
- :Port => options[:port],
144
- :BindAddress => options[:address],
145
- :DocumentRoot => options[:root],
146
- :DirectoryIndex => %w(index.html index.rhtml index.txt index.text) + extensions.collect {|ext| "index.#{ext}"}
147
- )
148
- trap("INT") { server.shutdown }
149
- server.start
138
+ root = options[:root]
139
+ app = Rack::Builder.new do
140
+ use Rack::CommonLogger
141
+ use Rack::ShowStatus
142
+ use Rack::ShowExceptions
143
+ run Rack::Cascade.new([
144
+ Serve::RackAdapter.new(root),
145
+ Rack::Directory.new(root)
146
+ ])
147
+ end
148
+ webrick = Rack::Handler.get('webrick')
149
+ webrick.run app, :Port => options[:port], :BindAddress => options[:address]
150
150
  end
151
151
  end
152
152
  end
@@ -1,5 +1,3 @@
1
- require 'active_support'
2
-
3
1
  module Serve
4
2
  mattr_accessor :file_resolver
5
3
 
@@ -1,7 +1,7 @@
1
1
  module Serve #:nodoc:
2
2
  class TextileHandler < FileTypeHandler #:nodoc:
3
3
  extension 'textile'
4
-
4
+
5
5
  def parse(string)
6
6
  require 'redcloth'
7
7
  "<html><body>#{ RedCloth.new(string).to_html }</body></html>"
data/lib/serve/rack.rb CHANGED
@@ -1,57 +1,92 @@
1
1
  require 'active_support'
2
- require 'serve'
3
2
  require 'rack'
4
3
 
5
4
  module Serve
6
- class Rack
7
- def call(env)
8
- path = Serve.resolve_file(Dir.pwd, env["PATH_INFO"])
9
- return not_found unless path
10
-
11
- ext = File.extname(path)[1..-1]
12
- handler = Serve::FileTypeHandler.handlers[ext]
13
- return no_handler(ext) unless handler
14
-
15
- res = Response.new
16
- handler.new(Dir.pwd, path).process(nil, res)
17
- [200, res.headers, res.body]
18
- rescue Exception => e
19
- return html_response(500, %(
20
- <h1>Error!</h1>
21
- <h2>#{h(e.message)}</h2>
22
- <pre>
23
- #{h(e.backtrace.join("\n"))}
24
- </pre>))
25
- end
26
-
27
- def no_handler(ext)
28
- html_response(501, %(
29
- <h1>No handler</h1>
30
-
31
- <p>Don't know how to handle resources of type "#{h(ext)}".</p>))
5
+ class Request < Rack::Request
6
+ def query
7
+ @query ||= Rack::Utils.parse_nested_query(query_string)
32
8
  end
33
9
 
34
- def not_found
35
- html_response(404, %(
36
- <h1>Not Found</h1>
37
-
38
- <p>The requested resource was not found.</p>))
10
+ def protocol
11
+ @scheme ||= scheme + "://"
12
+ end
13
+
14
+ def headers
15
+ @headers ||= Headers.new(@env)
16
+ end
17
+ end
18
+
19
+ class Response < Rack::Response
20
+ def body=(value)
21
+ # needed for Ruby 1.9
22
+ if value.respond_to? :each
23
+ super(value)
24
+ else
25
+ super([value])
26
+ end
27
+ end
28
+ end
29
+
30
+ # Borrowed from ActionDispatch in Rails
31
+ class Headers < Hash
32
+ extend ActiveSupport::Memoizable
33
+
34
+ def initialize(*args)
35
+ if args.size == 1 && args[0].is_a?(Hash)
36
+ super()
37
+ update(args[0])
38
+ else
39
+ super
40
+ end
39
41
  end
40
42
 
41
- def html_response(code, body)
42
- [code, {"Content-Type" => "text/html"}, %(<html><head></head><body>#{body}</body></html>)]
43
+ def [](header_name)
44
+ if include?(header_name)
45
+ super
46
+ else
47
+ super(env_name(header_name))
48
+ end
43
49
  end
44
50
 
45
- def h(input)
46
- CGI.escapeHTML(input)
51
+ private
52
+ # Converts a HTTP header name to an environment variable name.
53
+ def env_name(header_name)
54
+ "HTTP_#{header_name.upcase.gsub(/-/, '_')}"
55
+ end
56
+ memoize :env_name
57
+ end
58
+
59
+ class RackAdapter
60
+ def initialize(root)
61
+ @root = root
47
62
  end
48
63
 
49
- class Response
50
- attr_reader :headers
51
- attr_accessor :body
52
- def initialize
53
- @headers = {}
64
+ def call(env)
65
+ request = Request.new(env)
66
+ response = Response.new()
67
+ process(request, response).to_a
68
+ end
69
+
70
+ def process(request, response)
71
+ path = Serve.resolve_file(@root, request.path)
72
+ if path
73
+ ext = File.extname(path)[1..-1]
74
+ handler = Serve::FileTypeHandler.handlers[ext]
75
+ if handler
76
+ handler.new(@root, path).process(request, response)
77
+ response
78
+ else
79
+ default(request, response)
80
+ end
81
+ else
82
+ default(request, response)
54
83
  end
55
84
  end
85
+
86
+ def default(request, response)
87
+ response.status = 404
88
+ response.body = "Not found!"
89
+ response
90
+ end
56
91
  end
57
92
  end
@@ -11,12 +11,12 @@ module Serve
11
11
  :use_x_sendfile => false
12
12
  }
13
13
  cattr_accessor :defaults
14
-
14
+
15
15
  attr_accessor :directory, :expire_time, :default_extension, :perform_caching, :logger, :use_x_sendfile
16
16
  alias :page_cache_directory :directory
17
17
  alias :page_cache_extension :default_extension
18
18
  private :page_cache_directory, :page_cache_extension
19
-
19
+
20
20
  # Creates a ResponseCache object with the specified options.
21
21
  #
22
22
  # Options are as follows:
@@ -74,20 +74,20 @@ module Serve
74
74
  def response_cached?(path)
75
75
  perform_caching && !!read_metadata(path)
76
76
  end
77
-
77
+
78
78
  # Expires the cached response for the specified path.
79
79
  def expire_response(path)
80
80
  path = clean(path)
81
81
  expire_page(path)
82
82
  end
83
-
83
+
84
84
  # Expires the entire cache.
85
85
  def clear
86
86
  Dir["#{directory}/*"].each do |f|
87
87
  FileUtils.rm_rf f
88
88
  end
89
89
  end
90
-
90
+
91
91
  private
92
92
  # Ensures that path begins with a slash and remove extra slashes.
93
93
  def clean(path)
@@ -95,7 +95,7 @@ module Serve
95
95
  %r{^/?(.*?)/?$}.match(path)
96
96
  "/#{$1}"
97
97
  end
98
-
98
+
99
99
  # Reads a cached response from disk and updates a response object.
100
100
  def read_response(path, response, request)
101
101
  file_path = page_cache_path(path)
@@ -111,14 +111,14 @@ module Serve
111
111
  end
112
112
  response
113
113
  end
114
-
114
+
115
115
  def client_has_cache?(metadata, request)
116
116
  return false unless request
117
117
  request_time = Time.httpdate(request.env["HTTP_IF_MODIFIED_SINCE"]) rescue nil
118
118
  response_time = Time.httpdate(metadata['headers']['Last-Modified']) rescue nil
119
119
  return request_time && response_time && response_time <= request_time
120
120
  end
121
-
121
+
122
122
  # Writes a response to disk.
123
123
  def write_response(path, response)
124
124
  if response.cache_timeout
@@ -137,26 +137,26 @@ module Serve
137
137
  }.to_yaml
138
138
  cache_page(metadata, response.body, path)
139
139
  end
140
-
140
+
141
141
  def page_cache_path(path)
142
142
  path = (path.empty? || path == "/") ? "/_site-root" : URI.unescape(path)
143
143
  root_dir = File.expand_path(page_cache_directory)
144
144
  cache_path = File.expand_path(File.join(root_dir, path), root_dir)
145
145
  cache_path if cache_path.index(root_dir) == 0
146
146
  end
147
-
147
+
148
148
  def expire_page(path)
149
149
  return unless perform_caching
150
-
150
+
151
151
  if path = page_cache_path(path)
152
152
  File.delete("#{path}.yml") if File.exists?("#{path}.yml")
153
153
  File.delete("#{path}.data") if File.exists?("#{path}.data")
154
154
  end
155
155
  end
156
-
156
+
157
157
  def cache_page(metadata, content, path)
158
158
  return unless perform_caching
159
-
159
+
160
160
  if path = page_cache_path(path)
161
161
  FileUtils.makedirs(File.dirname(path))
162
162
  #dont want yml without data
@@ -94,8 +94,8 @@ module Serve #:nodoc:
94
94
  value = params[key]
95
95
  return default if value.blank?
96
96
  case value.strip.downcase
97
- when 'true', '1' : true
98
- when 'false', '0' : false
97
+ when 'true', '1' then true
98
+ when 'false', '0' then false
99
99
  else raise 'Invalid value'
100
100
  end
101
101
  end
data/lib/serve.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+
1
3
  require 'serve/version'
2
4
  require 'serve/file_resolver'
3
5
  require 'serve/handlers/file_type_handler'
@@ -7,4 +9,5 @@ require 'serve/handlers/dynamic_handler'
7
9
  require 'serve/handlers/sass_handler'
8
10
  require 'serve/handlers/email_handler'
9
11
  require 'serve/handlers/redirect_handler'
10
- require 'serve/response_cache'
12
+ require 'serve/response_cache'
13
+ require 'serve/rack'
@@ -62,7 +62,7 @@ describe Serve::ResponseCache do
62
62
  @cache.cache_response(url, response)
63
63
  name = "#{@dir}/test/me.yml"
64
64
  File.exists?(name).should == true
65
- file(name).should == "--- \nexpires: 2007-02-08 17:37:09 Z\nheaders: \n Last-Modified: Tue, 27 Feb 2007 06:13:43 GMT\n"
65
+ file(name).should match("expires: 2007-02-08 17:37:09 Z")
66
66
  data_name = "#{@dir}/test/me.data"
67
67
  file(data_name).should == "content"
68
68
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
3
  require 'rubygems'
4
- gem 'activesupport'
4
+ gem 'activesupport', '= 2.3.5'
5
5
 
6
6
  require 'serve'
7
7
  require 'spec'
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serve
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ hash: 55
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 11
9
+ - 2
10
+ version: 0.11.2
5
11
  platform: ruby
6
12
  authors:
7
13
  - John W. Long
@@ -10,30 +16,58 @@ autorequire:
10
16
  bindir: bin
11
17
  cert_chain: []
12
18
 
13
- date: 2010-05-13 00:00:00 -04:00
19
+ date: 2010-05-25 00:00:00 -04:00
14
20
  default_executable: serve
15
21
  dependencies:
16
22
  - !ruby/object:Gem::Dependency
17
- name: activesupport
23
+ name: rack
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - "="
29
+ - !ruby/object:Gem::Version
30
+ hash: 19
31
+ segments:
32
+ - 1
33
+ - 1
34
+ - 0
35
+ version: 1.1.0
18
36
  type: :runtime
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: activesupport
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
21
43
  requirements:
22
- - - ">="
44
+ - - "="
23
45
  - !ruby/object:Gem::Version
24
- version: 2.0.2
25
- version:
46
+ hash: 9
47
+ segments:
48
+ - 2
49
+ - 3
50
+ - 5
51
+ version: 2.3.5
52
+ type: :runtime
53
+ version_requirements: *id002
26
54
  - !ruby/object:Gem::Dependency
27
55
  name: rspec
28
- type: :development
29
- version_requirement:
30
- version_requirements: !ruby/object:Gem::Requirement
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
31
59
  requirements:
32
60
  - - ">="
33
61
  - !ruby/object:Gem::Version
62
+ hash: 13
63
+ segments:
64
+ - 1
65
+ - 2
66
+ - 9
34
67
  version: 1.2.9
35
- version:
36
- description: Serve is a small web server (based on WEBrick) that makes it easy to serve ERB or HAML from any directory. Serve is an ideal tool for building HTML prototypes of Rails applications. Serve can also handle SASS, Textile, and Markdown if the appropriate gems are installed.
68
+ type: :development
69
+ version_requirements: *id003
70
+ description: Serve is a small Rack-based web server that makes it easy to serve ERB or HAML from any directory. Serve is an ideal tool for building HTML prototypes of Rails applications. Serve can also handle SASS, Textile, and Markdown if the appropriate gems are installed.
37
71
  email: me@johnwlong.com
38
72
  executables:
39
73
  - serve
@@ -69,13 +103,9 @@ files:
69
103
  - lib/serve/response_cache.rb
70
104
  - lib/serve/version.rb
71
105
  - lib/serve/view_helpers.rb
72
- - lib/serve/webrick/extensions.rb
73
- - lib/serve/webrick/server.rb
74
- - lib/serve/webrick/servlet.rb
75
106
  - rails/init.rb
76
107
  - spec/application_spec.rb
77
108
  - spec/response_cache_spec.rb
78
- - spec/serve_spec.rb
79
109
  - spec/spec.opts
80
110
  - spec/spec_helper.rb
81
111
  has_rdoc: true
@@ -88,26 +118,31 @@ rdoc_options:
88
118
  require_paths:
89
119
  - lib
90
120
  required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
91
122
  requirements:
92
123
  - - ">="
93
124
  - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
94
128
  version: "0"
95
- version:
96
129
  required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
97
131
  requirements:
98
132
  - - ">="
99
133
  - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
100
137
  version: "0"
101
- version:
102
138
  requirements: []
103
139
 
104
140
  rubyforge_project:
105
- rubygems_version: 1.3.5
141
+ rubygems_version: 1.3.7
106
142
  signing_key:
107
143
  specification_version: 3
108
144
  summary: Serve is a small web server that makes it easy to serve ERB or HAML from any directory.
109
145
  test_files:
110
146
  - spec/application_spec.rb
111
147
  - spec/response_cache_spec.rb
112
- - spec/serve_spec.rb
113
148
  - spec/spec_helper.rb
@@ -1,98 +0,0 @@
1
- # Portions from Rails, Copyright (c) 2004-2008 David Heinemeier Hansson
2
- require 'active_support/memoizable'
3
-
4
- module Serve #:nodoc:
5
- module WEBrick #:nodoc:
6
-
7
- module FileHandlerExtensions #:nodoc:
8
- def self.included(base)
9
- base.extend(self)
10
- base.class_eval do
11
- alias :search_file_without_auto_appending :search_file
12
- alias :search_file :search_file_with_auto_appending
13
- end
14
- end
15
-
16
- def search_file_with_auto_appending(req, res, basename)
17
- full_path = File.join(res.filename, basename)
18
- return basename if File.file?(full_path)
19
- return nil if File.directory?(full_path)
20
- Serve.resolve_file(Dir.pwd, req.path)
21
- end
22
- end
23
-
24
- end
25
- end
26
-
27
- WEBrick::HTTPRequest.module_eval do
28
- extend ActiveSupport::Memoizable
29
-
30
- alias headers header
31
-
32
- # Returns the \host for this request, such as "example.com".
33
- def raw_host_with_port
34
- @host + ':' + @port.to_s
35
- end
36
-
37
- # Returns 'https://' if this is an SSL request and 'http://' otherwise.
38
- def protocol
39
- ssl? ? 'https://' : 'http://'
40
- end
41
- memoize :protocol
42
-
43
- # Is this an SSL request?
44
- def ssl?
45
- meta_vars['HTTPS'] == 'on'
46
- end
47
-
48
- def params
49
- query.inject({}) {|m, (k,v)| m[k.to_s.to_sym] = v; m}
50
- end
51
-
52
- # Returns the host for this request, such as example.com.
53
- def host
54
- raw_host_with_port.sub(/:\d+$/, '')
55
- end
56
- memoize :host
57
-
58
- # Returns a \host:\port string for this request, such as "example.com" or
59
- # "example.com:8080".
60
- def host_with_port
61
- "#{host}#{port_string}"
62
- end
63
- memoize :host_with_port
64
-
65
- # Returns the port number of this request as an integer.
66
- def port
67
- if raw_host_with_port =~ /:(\d+)$/
68
- $1.to_i
69
- else
70
- standard_port
71
- end
72
- end
73
- memoize :port
74
-
75
- # Returns the standard \port number for this request's protocol.
76
- def standard_port
77
- case protocol
78
- when 'https://' then 443
79
- else 80
80
- end
81
- end
82
-
83
- # Returns a \port suffix like ":8080" if the \port number of this request
84
- # is not the default HTTP \port 80 or HTTPS \port 443.
85
- def port_string
86
- port == standard_port ? '' : ":#{port}"
87
- end
88
- end
89
-
90
- WEBrick::HTTPResponse.module_eval do
91
- alias headers header
92
-
93
- def redirect(url, status)
94
- set_redirect(::WEBrick::HTTPStatus[status.to_i], url)
95
- end
96
- end
97
-
98
- WEBrick::HTTPServlet::FileHandler.class_eval { include Serve::WEBrick::FileHandlerExtensions }
@@ -1,17 +0,0 @@
1
- module Serve #:nodoc:
2
- module WEBrick
3
- class Server < ::WEBrick::HTTPServer #:nodoc:
4
- def self.register_handlers
5
- extensions = []
6
- Serve::FileTypeHandler.handlers.each do |ext, handler|
7
- extensions << ext
8
- handler_servlet = Class.new(Serve::WEBrick::Servlet) do
9
- define_method(:handler) { handler }
10
- end
11
- ::WEBrick::HTTPServlet::FileHandler.add_handler(ext, handler_servlet)
12
- end
13
- extensions
14
- end
15
- end
16
- end
17
- end
@@ -1,19 +0,0 @@
1
- module Serve #:nodoc:
2
- module WEBrick
3
- class Servlet < ::WEBrick::HTTPServlet::AbstractServlet #:nodoc:
4
- def do_GET(req, res)
5
- begin
6
- path = Serve.resolve_file(Dir.pwd, req.path)
7
- handler.new(Dir.pwd, path).process(req, res)
8
- rescue StandardError => ex
9
- raise
10
- rescue Exception => ex
11
- @logger.error(ex)
12
- raise ::WEBrick::HTTPStatus::InternalServerError, ex.message
13
- end
14
- end
15
-
16
- alias do_POST do_GET
17
- end
18
- end
19
- end
data/spec/serve_spec.rb DELETED
@@ -1,12 +0,0 @@
1
- require File.dirname(__FILE__) + '/spec_helper.rb'
2
-
3
- describe "Serve" do
4
-
5
- it "should register all of the file type handlers" do
6
- Serve::WEBrick::Server.register_handlers
7
- handlers = ["cgi", "email", "erb", "haml", "html.erb", "html.haml", "markdown", "redirect", "rhtml", "sass", "scss", "textile"]
8
- table = WEBrick::HTTPServlet::FileHandler::HandlerTable
9
- table.keys.sort.should == handlers
10
- end
11
-
12
- end