rack_url_stripper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dan Pickett
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = rack_url_stripper
2
+
3
+ middleware to remove url's, actions, and script from your markup
4
+
5
+ To use:
6
+
7
+ config.gem 'rack_url_stripper'
8
+
9
+ Set a path prefix to filter what gets stripped:
10
+
11
+ Rack::UrlStripper::Middleware.path_prefix = '/templates/'
12
+
13
+ Any path that starts with '/templates/' will remove all links, action url's, and script tags
14
+
15
+ == Note on Patches/Pull Requests
16
+
17
+ * Fork the project.
18
+ * Make your feature addition or bug fix.
19
+ * Add tests for it. This is important so I don't break it in a
20
+ future version unintentionally.
21
+ * Commit, do not mess with rakefile, version, or history.
22
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
23
+ * Send me a pull request. Bonus points for topic branches.
24
+
25
+ == Copyright
26
+
27
+ Copyright (c) 2010 Second Rotation, Inc. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack_url_stripper"
8
+ gem.summary = %Q{Removes urls from anchors and forms et al}
9
+ gem.description = %Q{Strips urls from anchors and forms to essentially create an html page that doesn't like anywhere}
10
+ gem.email = "dpickett@enlightsolutions.com"
11
+ gem.homepage = "http://github.com/dpickett/rack_url_stripper"
12
+ gem.authors = ["Dan Pickett"]
13
+ gem.add_development_dependency 'rspec', '>= 1.2.9'
14
+ gem.add_development_dependency 'rack-test', '>= 0.5.3'
15
+ gem.add_dependency 'nokogiri', '>= 1.3.3'
16
+ gem.add_dependency 'rack', '>=1.0.1'
17
+ gem.add_dependency 'configatron', '2.5.1'
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
23
+ end
24
+
25
+ require 'spec/rake/spectask'
26
+ Spec::Rake::SpecTask.new(:spec) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.spec_files = FileList['spec/**/*_spec.rb']
29
+ end
30
+
31
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
32
+ spec.libs << 'lib' << 'spec'
33
+ spec.pattern = 'spec/**/*_spec.rb'
34
+ spec.rcov = true
35
+ end
36
+
37
+ task :spec => :check_dependencies
38
+
39
+ begin
40
+ require 'reek/adapters/rake_task'
41
+ Reek::RakeTask.new do |t|
42
+ t.fail_on_error = true
43
+ t.verbose = false
44
+ t.source_files = 'lib/**/*.rb'
45
+ end
46
+ rescue LoadError
47
+ task :reek do
48
+ abort "Reek is not available. In order to run reek, you must: sudo gem install reek"
49
+ end
50
+ end
51
+
52
+ begin
53
+ require 'roodi'
54
+ require 'roodi_task'
55
+ RoodiTask.new do |t|
56
+ t.verbose = false
57
+ end
58
+ rescue LoadError
59
+ task :roodi do
60
+ abort "Roodi is not available. In order to run roodi, you must: sudo gem install roodi"
61
+ end
62
+ end
63
+
64
+ task :default => :spec
65
+
66
+ require 'rake/rdoctask'
67
+ Rake::RDocTask.new do |rdoc|
68
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
69
+
70
+ rdoc.rdoc_dir = 'rdoc'
71
+ rdoc.title = "rack_url_stripper #{version}"
72
+ rdoc.rdoc_files.include('README*')
73
+ rdoc.rdoc_files.include('lib/**/*.rb')
74
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+
3
+ require 'rack'
4
+ require 'nokogiri'
5
+ require 'configatron'
6
+
7
+ require 'rack_url_stripper/document_to_strip'
8
+ require 'rack_url_stripper/middleware'
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ module UrlStripper
3
+ class DocumentToStrip
4
+ def initialize(body)
5
+ @doc = Nokogiri::HTML.parse(body)
6
+ end
7
+
8
+ def clear_uri_for(element, attribute)
9
+ @doc.xpath("//#{element}/@#{attribute}").each do |i|
10
+ i.value = '#'
11
+ end
12
+ end
13
+
14
+ def remove_tag(tag_name)
15
+ @doc.xpath("//#{tag_name}").remove()
16
+ end
17
+
18
+ def to_s
19
+ @doc.to_s
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module Rack
2
+ module UrlStripper
3
+ class Middleware
4
+ class PathPrefixNotSpecifiedError < Exception; end
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ require_path_prefix
12
+
13
+ @resp = @app.call(env)
14
+ strip_document if should_strip_document?(env)
15
+
16
+ @resp
17
+ end
18
+
19
+ def self.path_prefix=(path)
20
+ configatron.rack_url_stripper.path_prefix = path
21
+ end
22
+
23
+ def self.path_prefix
24
+ configatron.rack_url_stripper.path_prefix
25
+ end
26
+
27
+ private
28
+ def require_path_prefix
29
+ if self.class.path_prefix.nil?
30
+ raise PathPrefixNotSpecifiedError, 'No path_prefix was set for rack_url_stripper'
31
+ end
32
+ end
33
+
34
+ def should_strip_document?(env)
35
+ env['PATH_INFO'] =~ /^#{self.class.path_prefix}/
36
+ end
37
+
38
+ def strip_document
39
+ doc = Rack::UrlStripper::DocumentToStrip.new(@resp[2].body[0])
40
+
41
+ doc.clear_uri_for('a', 'href')
42
+ doc.clear_uri_for('form', 'action')
43
+ doc.remove_tag('script')
44
+
45
+ @resp[2].body[0] = doc.to_s
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <title>Test</title>
7
+ </head>
8
+
9
+ <script src="http://www.google.com">
10
+ </script>
11
+
12
+ <body>
13
+ <a href="http://www.google.com">Google</a>
14
+ <a href='http://www.yahoo.com'>Yahoo</a>
15
+ <a href="some_relative_path">Something Relative</a>
16
+ <a href="/some_absolute_path">Something Absolute</a>
17
+
18
+ <form action="/something">
19
+ <input type="hidden" name="foo" value="bar" />
20
+ <input type="hidden" name="bar" value="foo" />
21
+ </form>
22
+
23
+ <form action="http://www.google.com/fdsa">
24
+ <input type="text" name="fool" />
25
+ <input type="submit" value="submit" />
26
+ </form>
27
+
28
+ <script type="text/javascript">
29
+ alert('hello world');
30
+ </script>
31
+ </body>
32
+ </html>
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::UrlStripper::Middleware do
4
+ class TestApplication
5
+ def call(env)
6
+ req = Rack::Request.new(env)
7
+ resp = Rack::Response.new
8
+ resp.write(File.read(File.join(File.dirname(__FILE__), '/../fixtures/markup.html')))
9
+ resp.finish
10
+ end
11
+ end
12
+
13
+ def app
14
+ Rack::UrlStripper::Middleware.new(TestApplication.new)
15
+ end
16
+
17
+ describe 'when requesting w/ appropriate path prefix' do
18
+ before(:each) do
19
+ Rack::UrlStripper::Middleware.path_prefix = '/proper_prefix/'
20
+ @response = get '/proper_prefix/giggity/giggity'
21
+ end
22
+
23
+ it 'should drop urls in an href of an anchor' do
24
+ @response.body.should_not =~ /href=\"([^\#]*)\"/i
25
+ @response.body.should_not =~ /href=\'([^\#]*)\'/i
26
+ end
27
+
28
+ it 'should drop urls in the action of a form tag' do
29
+ @response.body.should_not =~ /action=\"([^\#]*)\"/i
30
+ @response.body.should_not =~ /action=\'([^\#]*)\'/i
31
+ end
32
+
33
+ it 'should remove script tags' do
34
+ @response.body.should_not =~ /<script/i
35
+ end
36
+
37
+ it 'should remove the stuff inside a script tag' do
38
+ @response.body.should_not =~ /alert/
39
+ end
40
+ end
41
+
42
+ describe 'when requesting withou the appropirate path prefix' do
43
+ before(:each) do
44
+ Rack::UrlStripper::Middleware.path_prefix = '/proper_prefix/'
45
+ @response = get '/giggity/giggity'
46
+ end
47
+
48
+ it 'should not drop urls in an href' do
49
+ @response.body.should_not =~ /href=\"\#\"/i
50
+ end
51
+
52
+ it 'should not drop action urls in a form tag' do
53
+ @response.body.should_not =~ /action=\"\#\"/i
54
+ end
55
+ end
56
+
57
+ it 'should raise an error if I have not specified a path prefix' do
58
+ Rack::UrlStripper::Middleware.path_prefix = nil
59
+ lambda { get "/" }.should raise_error
60
+ end
61
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --backtrace
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rack_url_stripper'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'rack/test'
7
+
8
+ Spec::Runner.configure do |config|
9
+ include Rack::Test::Methods
10
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_url_stripper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Pickett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-11 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack-test
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.3
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: nokogiri
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.3
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: rack
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.1
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: configatron
57
+ type: :runtime
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.5.1
64
+ version:
65
+ description: Strips urls from anchors and forms to essentially create an html page that doesn't like anywhere
66
+ email: dpickett@enlightsolutions.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - lib/rack_url_stripper.rb
82
+ - lib/rack_url_stripper/document_to_strip.rb
83
+ - lib/rack_url_stripper/middleware.rb
84
+ - spec/fixtures/markup.html
85
+ - spec/rack_url_stripper/middleware_spec.rb
86
+ - spec/spec.opts
87
+ - spec/spec_helper.rb
88
+ has_rdoc: true
89
+ homepage: http://github.com/dpickett/rack_url_stripper
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options:
94
+ - --charset=UTF-8
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ version:
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: "0"
108
+ version:
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.3.5
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Removes urls from anchors and forms et al
116
+ test_files:
117
+ - spec/rack_url_stripper/middleware_spec.rb
118
+ - spec/spec_helper.rb