avdi-rack_base_uri 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,65 @@
1
+ = rack_base_uri
2
+
3
+ * http://avdi.org/
4
+
5
+ == DESCRIPTION:
6
+
7
+ A middleware to automatically set the base URI for [X]HTML documents. This is
8
+ useful when you want to mount a web application on a subdirectory,
9
+ e.g. http://example.org/myapp/
10
+
11
+ The effect is accomplished by munging [X]HTML responses with HPricot. So in the
12
+ above example, HTML documents will have a "<base
13
+ href='http://example.org/myapp/'>" element added to their HEAD, and XHTML
14
+ documents will have the xml:base attribute of the root element set to
15
+ 'http://example.org/myapp/'.
16
+
17
+ == FEATURES/PROBLEMS:
18
+
19
+ * It might work
20
+
21
+ == SYNOPSIS:
22
+
23
+ In your rackup file:
24
+
25
+ require 'rack_base_uri'
26
+
27
+ use Rack::CommonLogger
28
+ map "/lobster" do
29
+ use Rack::BaseUri
30
+ run Rack::Lobster.new
31
+ end
32
+
33
+ == REQUIREMENTS:
34
+
35
+ * Rack
36
+ * Hpricot
37
+
38
+ == INSTALL:
39
+
40
+ TBD
41
+
42
+ == LICENSE:
43
+
44
+ (The MIT License)
45
+
46
+ Copyright (c) 2008 FIX
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of this software and associated documentation files (the
50
+ 'Software'), to deal in the Software without restriction, including
51
+ without limitation the rights to use, copy, modify, merge, publish,
52
+ distribute, sublicense, and/or sell copies of the Software, and to
53
+ permit persons to whom the Software is furnished to do so, subject to
54
+ the following conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
63
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
65
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'rack'
6
+ require 'hpricot'
7
+
8
+ module Rack
9
+ class BaseUri
10
+ def initialize(app)
11
+ @app = app
12
+ end
13
+
14
+ def call(env)
15
+ host = env.fetch('HTTP_HOST') {
16
+ env['rack.url_scheme'] + '://' + env['SERVER_NAME'] + ':' + env['SERVER_PORT']
17
+ }
18
+ base = host + env['SCRIPT_NAME']
19
+ result = @app.call(env)
20
+ headers = result[1]
21
+ doc = Hpricot(result[2].to_s)
22
+ case headers['Content-Type']
23
+ when 'text/html'
24
+ (doc/'head').append("<base href='#{base}'>")
25
+ when 'application/xhtml+xml', 'text/xml'
26
+ root = doc.root
27
+ if root.name.downcase == 'html'
28
+ root['xml:base'] = base
29
+ end
30
+ end
31
+ return [
32
+ result[0],
33
+ result[1],
34
+ Array(doc.to_s)
35
+ ]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,9 @@
1
+ class Rack::BaseUri #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 2
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,99 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+ require 'rubygems'
3
+ require 'rack/mock'
4
+ require 'rack/urlmap'
5
+ require 'hpricot'
6
+
7
+ describe Rack::BaseUri do
8
+ before :each do
9
+ @headers = {'Content-Type' => 'text/html'}
10
+ @body = <<-HTML
11
+ <html>
12
+ <head>
13
+ <title>Test Page</title>
14
+ </head>
15
+ <body>Hello World</body>
16
+ </html>
17
+ HTML
18
+ @result = [
19
+ 200,
20
+ @headers,
21
+ [@body]
22
+ ]
23
+ @app = stub("app", :call => @result)
24
+ @it = Rack::BaseUri.new(@app)
25
+ @base = 'http://example.org/subdir'
26
+ end
27
+
28
+ def do_request
29
+ @map = Rack::URLMap.new({@base => @it})
30
+ @request = Rack::MockRequest.new(@map)
31
+ @response = @request.get("/subdir/foo", 'HTTP_HOST' => 'http://example.org')
32
+ @doc = Hpricot(@response.body)
33
+ end
34
+
35
+ describe "with HTML content" do
36
+ it "should add a base element to the HTML" do
37
+ do_request
38
+ @response.should be_ok
39
+ tag = @doc.at("head base")
40
+ tag.should_not be_nil
41
+ tag['href'].should == "http://example.org/subdir"
42
+ end
43
+ end
44
+ describe "with application/xhtml+xml content type" do
45
+ before :each do
46
+ @headers['Content-Type'] = 'application/xhtml+xml'
47
+ end
48
+
49
+ it "should add a base element to the HTML" do
50
+ do_request
51
+ @response.should be_ok
52
+ html = @doc.at("html")
53
+ html.should_not be_nil
54
+ html['xml:base'].should == "http://example.org/subdir"
55
+ end
56
+ end
57
+
58
+ describe "with text/xml content type" do
59
+ before :each do
60
+ @headers['Content-Type'] = 'text/xml'
61
+ end
62
+
63
+ it "should add a base element to the HTML" do
64
+ do_request
65
+ @response.should be_ok
66
+ html = @doc.at("html")
67
+ html.should_not be_nil
68
+ html['xml:base'].should == "http://example.org/subdir"
69
+ end
70
+ end
71
+
72
+ describe "with text/xml content type but non-HTML content" do
73
+ before :each do
74
+ @headers['Content-Type'] = 'text/xml'
75
+ @content = "<foo></foo>"
76
+ @body.replace(@content)
77
+ end
78
+
79
+ it "should leave the content alone" do
80
+ do_request
81
+ @response.should be_ok
82
+ @response.body.should == @content
83
+ end
84
+ end
85
+
86
+ describe "with text/html content type but plain text content" do
87
+ before :each do
88
+ @headers['Content-Type'] = 'text/html'
89
+ @content = "Not HTML"
90
+ @body.replace(@content)
91
+ end
92
+
93
+ it "should leave the content alone" do
94
+ do_request
95
+ @response.should be_ok
96
+ @response.body.should == @content
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'rack_base_uri'
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avdi-rack_base_uri
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Avdi Grimm
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - "="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hpricot
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - "="
30
+ - !ruby/object:Gem::Version
31
+ version: "0.6"
32
+ version:
33
+ description: A middleware to automatically set the base URI for [X]HTML documents. This is useful when you want to mount a web application on a subdirectory, e.g. http://example.org/myapp/
34
+ email: avdi@avdi.org
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - README
43
+ - lib/rack_base_uri.rb
44
+ - lib/rack_base_uri/version.rb
45
+ - spec/spec_helper.rb
46
+ - spec/rack_base_uri_spec.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/avdi/rack_base_uri/
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 1.8.0
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: A Rack plugin for automatically setting document base URIs in [X]HTML
73
+ test_files: []
74
+