rack-http-enforcer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.rdoc +18 -0
  3. data/lib/rack-http-enforcer.rb +57 -0
  4. metadata +166 -0
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Rudolf Schmidt
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,18 @@
1
+ = rack-http-enforcer
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rack-http-enforcer
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Rudolf Schmidt. See LICENSE.txt for further details.
18
+
@@ -0,0 +1,57 @@
1
+ module Rack
2
+ class HttpEnforcer
3
+
4
+ def initialize( app, options = {} )
5
+ @app, @options = app, options
6
+ end
7
+
8
+ def call( env )
9
+ scheme = 'http' unless http_request?( env )
10
+
11
+ if scheme
12
+ location = redirect_to || request_url( env, scheme )
13
+ body = "<html><body>You are being <a href=\"#{location}\">redirected</a>.</body></html>"
14
+
15
+ [ 301, { 'Content-Type' => 'text/html', 'Location' => location }, [body] ]
16
+ else
17
+ @app.call( env )
18
+ end
19
+ end
20
+
21
+
22
+ private
23
+
24
+ def http_request?( env )
25
+ scheme( env ) == 'http'
26
+ end
27
+
28
+ # Fixed in rack >= 1.3
29
+ def scheme( env )
30
+ if env['HTTPS'] == 'on'
31
+ 'https'
32
+ elsif env['HTTP_X_FORWARDED_PROTO']
33
+ env['HTTP_X_FORWARDED_PROTO'].split(',')[0]
34
+ else
35
+ env['rack.url_scheme']
36
+ end
37
+ end
38
+
39
+ def request_url( env, scheme )
40
+ Rack::Request.new(
41
+ env.merge(
42
+ 'rack.url_scheme' => scheme,
43
+ 'SERVER_PORT' => scheme == 'https' ? 443 : 80
44
+ )
45
+ ).url
46
+ end
47
+
48
+ def redirect_to
49
+ if @options[:redirect_to].is_a?( Proc )
50
+ @options[:redirect_to].call
51
+ else
52
+ @options[:redirect_to]
53
+ end
54
+ end
55
+
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-http-enforcer
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Rudolf Schmidt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-07 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 7
34
+ version: 1.0.7
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 1
46
+ segments:
47
+ - 1
48
+ - 5
49
+ - 1
50
+ version: 1.5.1
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: test-unit
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 15
62
+ segments:
63
+ - 2
64
+ - 1
65
+ - 2
66
+ version: 2.1.2
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: shoulda
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 37
78
+ segments:
79
+ - 2
80
+ - 11
81
+ - 3
82
+ version: 2.11.3
83
+ type: :development
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: rack
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ hash: 29
94
+ segments:
95
+ - 1
96
+ - 2
97
+ - 1
98
+ version: 1.2.1
99
+ type: :development
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ name: rack-test
103
+ prerelease: false
104
+ requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ hash: 7
110
+ segments:
111
+ - 0
112
+ - 5
113
+ - 6
114
+ version: 0.5.6
115
+ type: :development
116
+ version_requirements: *id006
117
+ description: Rack::HttpEnforcer is a simple Rack middleware to enforce http connections
118
+ email:
119
+ executables: []
120
+
121
+ extensions: []
122
+
123
+ extra_rdoc_files: []
124
+
125
+ files:
126
+ - lib/rack-http-enforcer.rb
127
+ - LICENSE.txt
128
+ - README.rdoc
129
+ has_rdoc: true
130
+ homepage: http://github.com/tobmatth/rack-ssl-enforcer
131
+ licenses: []
132
+
133
+ post_install_message:
134
+ rdoc_options: []
135
+
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ hash: 3
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ hash: 23
153
+ segments:
154
+ - 1
155
+ - 3
156
+ - 6
157
+ version: 1.3.6
158
+ requirements: []
159
+
160
+ rubyforge_project: rack-http-enforcer
161
+ rubygems_version: 1.3.7
162
+ signing_key:
163
+ specification_version: 3
164
+ summary: A simple Rack middleware to enforce HTTP
165
+ test_files: []
166
+