Skip to content
Browse files

move rack response construction to server class

  • Loading branch information...
1 parent 7707d79 commit 200205dbaebd6d6600b9fc8793cf47d4084dc3ab @mmb committed Oct 4, 2011
View
6 config.ru
@@ -38,17 +38,17 @@ if !config['cache_metastore'].to_s.empty? and
end
use Rack::ConditionalGet
-use Murlsh::EtagAddEncoding
+use Murlsh::Rack::EtagAddEncoding
use Rack::Deflater
use Rack::Head
use Rack::ETag
-use Murlsh::FarFutureExpires, :patterns => [
+use Murlsh::Rack::FarFutureExpires, :patterns => [
%r{[\da-z]{32}\.(?:gif|jpe?g|png)$}i,
%r{\.gen\.(css|js)$}
]
feed_url = URI.join(config.fetch('root_url'), config.fetch('feed_file'))
-use Murlsh::MustRevalidate, :patterns => [
+use Murlsh::Rack::MustRevalidate, :patterns => [
%r{^#{Regexp.escape(feed_url.path)}$},
%r{^/(url)?$},
%r{^/json\.json$},
View
4 lib/murlsh.rb
@@ -6,10 +6,8 @@
require 'murlsh/build_query'
require 'murlsh/dispatch'
require 'murlsh/doc'
-require 'murlsh/etag_add_encoding'
require 'murlsh/enclosure'
require 'murlsh/failproof'
-require 'murlsh/far_future_expires'
require 'murlsh/feed_body'
require 'murlsh/image_list'
require 'murlsh/img_store'
@@ -18,11 +16,11 @@
require 'murlsh/m3u_body'
require 'murlsh/m3u_server'
require 'murlsh/markup'
-require 'murlsh/must_revalidate'
require 'murlsh/openlock'
require 'murlsh/podcast_server'
require 'murlsh/plugin'
require 'murlsh/pop_server'
+require 'murlsh/rack'
require 'murlsh/random_server'
require 'murlsh/rss_body'
require 'murlsh/rss_server'
View
5 lib/murlsh/atom_server.rb
@@ -1,7 +1,5 @@
require 'uri'
-require 'rack'
-
require 'murlsh'
module Murlsh
@@ -19,8 +17,7 @@ def get(req)
feed_url = URI.join(config.fetch('root_url'), config.fetch('feed_file'))
body = Murlsh::AtomBody.new(config, req, feed_url, result_set.results)
- resp = Rack::Response.new(body, 200,
- 'Content-Type' => 'application/atom+xml')
+ resp = build_response(body, 200, 'Content-Type' => 'application/atom+xml')
if u = body.updated
resp['Last-Modified'] = u.httpdate
end
View
6 lib/murlsh/dispatch.rb
@@ -60,16 +60,16 @@ def dispatch(req)
# Rack call.
def call(env)
- req = Rack::Request.new(env)
+ req = ::Rack::Request.new(env)
dispatch(req).call(req).finish
end
# Called if the request is not found.
def not_found(req)
if req.head?
- Rack::Response.new([], 404)
+ ::Rack::Response.new([], 404)
else
- Rack::Response.new("<p>#{req.url} not found</p>
+ ::Rack::Response.new("<p>#{req.url} not found</p>
<p><a href=\"#{config.fetch('root_url')}\">root<a></p>
",
View
25 lib/murlsh/etag_add_encoding.rb
@@ -1,25 +0,0 @@
-require 'rack/utils'
-
-module Murlsh
-
- # Rack middleware to add the content encoding to the end of the ETag because
- # ETag must be different for different representations.
- class EtagAddEncoding
-
- def initialize(app); @app = app; end
-
- def call(env)
- status, headers, body = @app.call(env)
-
- headers = Rack::Utils::HeaderHash.new(headers)
-
- if headers['ETag']
- headers['ETag'].sub! /(")?$/, "#{headers['Content-Encoding']}\\1"
- end
-
- [status, headers, body]
- end
-
- end
-
-end
View
39 lib/murlsh/far_future_expires.rb
@@ -1,39 +0,0 @@
-require 'time'
-
-require 'rack'
-require 'rack/utils'
-
-module Murlsh
-
- # Rack middleware to set a far future expires header for urls that match
- # patterns.
- class FarFutureExpires
-
- def initialize(app, options={})
- @app = app
- @patterns = options[:patterns] ? [*options[:patterns]] : []
- # rfc2616 HTTP/1.1 servers SHOULD NOT send Expires dates more than one
- # year in the future.
- @future = options[:future] || (Time.now + 31536000).httpdate
- end
-
- def call(env)
- status, headers, body = @app.call(env)
-
- req = Rack::Request.new(env)
-
- headers = Rack::Utils::HeaderHash.new(headers)
-
- @patterns.each do |pattern|
- if pattern.match(req.path)
- headers['Expires'] = @future
- break
- end
- end
-
- [status, headers, body]
- end
-
- end
-
-end
View
4 lib/murlsh/json_server.rb
@@ -1,5 +1,3 @@
-require 'rack'
-
module Murlsh
# Serve most recent urls in json.
@@ -14,7 +12,7 @@ def get(req)
body = Murlsh::JsonBody.new(config, req, result_set)
- Rack::Response.new body, 200, 'Content-Type' => 'application/json'
+ build_response body, 200, 'Content-Type' => 'application/json'
end
end
View
4 lib/murlsh/m3u_server.rb
@@ -1,7 +1,5 @@
require 'uri'
-require 'rack'
-
require 'murlsh'
module Murlsh
@@ -26,7 +24,7 @@ def get(req)
feed_url = URI.join(config.fetch('root_url'), 'm3u.m3u')
body = Murlsh::M3uBody.new(config, req, feed_url, result_set.results)
- resp = Rack::Response.new(body, 200, 'Content-Type' => 'audio/x-mpegurl')
+ resp = build_response(body, 200, 'Content-Type' => 'audio/x-mpegurl')
if u = body.updated
resp['Last-Modified'] = u.httpdate
end
View
34 lib/murlsh/must_revalidate.rb
@@ -1,34 +0,0 @@
-require 'rack'
-require 'rack/utils'
-
-module Murlsh
-
- # Rack middleware to force caches to always revalidate for urls that match
- # patterns.
- class MustRevalidate
-
- def initialize(app, options={})
- @app = app
- @patterns = Array(options[:patterns])
- end
-
- def call(env)
- status, headers, body = @app.call(env)
-
- req = Rack::Request.new(env)
-
- headers = Rack::Utils::HeaderHash.new(headers)
-
- @patterns.each do |pattern|
- if pattern.match(req.path)
- headers['Cache-Control'] = 'must-revalidate, max-age=0'
- break
- end
- end
-
- [status, headers, body]
- end
-
- end
-
-end
View
5 lib/murlsh/podcast_server.rb
@@ -1,7 +1,5 @@
require 'uri'
-require 'rack'
-
require 'murlsh'
module Murlsh
@@ -20,8 +18,7 @@ def get(req)
feed_url = URI.join(config.fetch('root_url'), 'podcast.rss')
body = Murlsh::RssBody.new(config, req, feed_url, result_set.results)
- resp = Rack::Response.new(body, 200,
- 'Content-Type' => 'application/rss+xml')
+ resp = build_response(body, 200, 'Content-Type' => 'application/rss+xml')
if u = body.updated
resp['Last-Modified'] = u.httpdate
end
View
3 lib/murlsh/pop_server.rb
@@ -2,7 +2,6 @@
require 'active_record'
require 'postrank-uri'
-require 'rack'
require 'rmail'
module Murlsh
@@ -29,7 +28,7 @@ def post(req)
end
end
- Rack::Response.new response_body.to_json, 200,
+ build_response response_body.to_json, 200,
'Content-Type' => 'application/json'
end
View
3 lib/murlsh/rack.rb
@@ -0,0 +1,3 @@
+require 'murlsh/rack/etag_add_encoding'
+require 'murlsh/rack/far_future_expires'
+require 'murlsh/rack/must_revalidate'
View
29 lib/murlsh/rack/etag_add_encoding.rb
@@ -0,0 +1,29 @@
+require 'rack/utils'
+
+module Murlsh
+
+ module Rack
+
+ # Rack middleware to add the content encoding to the end of the ETag because
+ # ETag must be different for different representations.
+ class EtagAddEncoding
+
+ def initialize(app); @app = app; end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ headers = ::Rack::Utils::HeaderHash.new(headers)
+
+ if headers['ETag']
+ headers['ETag'].sub! /(")?$/, "#{headers['Content-Encoding']}\\1"
+ end
+
+ [status, headers, body]
+ end
+
+ end
+
+ end
+
+end
View
43 lib/murlsh/rack/far_future_expires.rb
@@ -0,0 +1,43 @@
+require 'time'
+
+require 'rack'
+require 'rack/utils'
+
+module Murlsh
+
+ module Rack
+
+ # Rack middleware to set a far future expires header for urls that match
+ # patterns.
+ class FarFutureExpires
+
+ def initialize(app, options={})
+ @app = app
+ @patterns = options[:patterns] ? [*options[:patterns]] : []
+ # rfc2616 HTTP/1.1 servers SHOULD NOT send Expires dates more than one
+ # year in the future.
+ @future = options[:future] || (Time.now + 31536000).httpdate
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ req = ::Rack::Request.new(env)
+
+ headers = ::Rack::Utils::HeaderHash.new(headers)
+
+ @patterns.each do |pattern|
+ if pattern.match(req.path)
+ headers['Expires'] = @future
+ break
+ end
+ end
+
+ [status, headers, body]
+ end
+
+ end
+
+ end
+
+end
View
38 lib/murlsh/rack/must_revalidate.rb
@@ -0,0 +1,38 @@
+require 'rack'
+require 'rack/utils'
+
+module Murlsh
+
+ module Rack
+
+ # Rack middleware to force caches to always revalidate for urls that match
+ # patterns.
+ class MustRevalidate
+
+ def initialize(app, options={})
+ @app = app
+ @patterns = Array(options[:patterns])
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+
+ req = ::Rack::Request.new(env)
+
+ headers = ::Rack::Utils::HeaderHash.new(headers)
+
+ @patterns.each do |pattern|
+ if pattern.match(req.path)
+ headers['Cache-Control'] = 'must-revalidate, max-age=0'
+ break
+ end
+ end
+
+ [status, headers, body]
+ end
+
+ end
+
+ end
+
+end
View
4 lib/murlsh/random_server.rb
@@ -1,5 +1,3 @@
-require 'rack'
-
require 'murlsh'
module Murlsh
@@ -20,7 +18,7 @@ def get(req)
config.fetch('root_url')
end
- resp = Rack::Response.new("<a href=\"#{url}\">#{url}</a>")
+ resp = build_response("<a href=\"#{url}\">#{url}</a>")
resp.redirect(url)
resp
View
5 lib/murlsh/rss_server.rb
@@ -1,7 +1,5 @@
require 'uri'
-require 'rack'
-
require 'murlsh'
module Murlsh
@@ -19,8 +17,7 @@ def get(req)
feed_url = URI.join(config.fetch('root_url'), 'rss.rss')
body = Murlsh::RssBody.new(config, req, feed_url, result_set.results)
- resp = Rack::Response.new(body, 200,
- 'Content-Type' => 'application/rss+xml')
+ resp = build_response(body, 200, 'Content-Type' => 'application/rss+xml')
if u = body.updated
resp['Last-Modified'] = u.httpdate
end
View
4 lib/murlsh/server.rb
@@ -1,3 +1,5 @@
+require 'rack'
+
module Murlsh
# Superclass for servers.
@@ -8,4 +10,6 @@ def initialize(config); @config = config; end
attr_reader :config
end
+ def build_response(*args &block); ::Rack::Response.new *args &block; end
+
end
View
9 lib/murlsh/url_server.rb
@@ -1,5 +1,4 @@
require 'active_record'
-require 'rack'
module Murlsh
@@ -18,7 +17,7 @@ def get(req)
body = Murlsh::UrlBody.new(config, req, result_set, content_type)
- resp = Rack::Response.new
+ resp = build_response
resp.write(body.build)
resp['Content-Type'] = content_type
@@ -70,8 +69,8 @@ def post(req)
response_body, response_code = '', 403
end
- Rack::Response.new(response_body.to_json, response_code, {
- 'Content-Type' => 'application/json' })
+ build_response response_body.to_json, response_code,
+ 'Content-Type' => 'application/json'
end
# Delete a url.
@@ -91,7 +90,7 @@ def delete(req)
response_code = 404
end
- Rack::Response.new response_body, response_code,
+ build_response response_body, response_code,
'Content-Type' => 'application/json'
end

0 comments on commit 200205d

Please sign in to comment.
Something went wrong with that request. Please try again.