refraction2 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +16 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +44 -0
- data/README.md +138 -0
- data/Rakefile +6 -0
- data/lib/refraction/version.rb +3 -0
- data/lib/refraction.rb +123 -0
- data/lib/refraction2.rb +2 -0
- data/refraction.gemspec +26 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8b9be8d806089e0ab258abdb8d39764f6b4aa9a3b0da00624e4c1a6c8807725a
|
4
|
+
data.tar.gz: 1e3721b59574a86f9bd4fc8571030fdbb965201d2d2cdd77358347f541a9979c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ce9d8cedf5f47c6fc7e8759552525acddef855446757d5a8451f577756350c25b0c1e58de9cc9bdeda5fc505ecc28ade10487af8f91902b95327e55cfc833e03
|
7
|
+
data.tar.gz: 3ed4cb5e21d061206f8fb93d25ab85ef0e63bc01a197afaba006426499b4e917fa104ba90da0630a28f7de7095c63628b0bdc4ad2448ea1108e302e438b347d0
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
## [0.2.1] - 2022-05-30 (first release as `refraction2`)
|
11
|
+
|
12
|
+
### Fixed
|
13
|
+
|
14
|
+
- fixed host and port overrides for Rack 2.2+
|
15
|
+
|
16
|
+
## [0.1.3] - 2010-02-01 (last release as `refraction` by Josh Susser)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Janosch Müller
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
23
|
+
Based on refraction by Pivotal Labs, Josh Susser, Sam Pierson, Wai Lun Mang.
|
24
|
+
|
25
|
+
Copyright (c) 2009-2010 Pivotal Labs
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
"Software"), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
41
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
42
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
43
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
44
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
# Refraction2
|
2
|
+
|
3
|
+
`refraction2` is a Rack 2.2+ compatible re-release and drop-in replacement for
|
4
|
+
[joshsusser/refraction](https://github.com/joshsusser/refraction).
|
5
|
+
|
6
|
+
## Original README
|
7
|
+
|
8
|
+
Refraction is a Rack middleware replacement for `mod_rewrite`. It can rewrite URLs before they are
|
9
|
+
processed by your web application, and can redirect using 301 and 302 status codes. Refraction is
|
10
|
+
thread-safe, so it doesn't need to be guarded by Rack::Lock.
|
11
|
+
|
12
|
+
The best thing about Refraction is that rewrite rules are written in plain old Ruby code, not some
|
13
|
+
funky web server config syntax. That means you can use Ruby regular expressions, case statements,
|
14
|
+
conditionals, and whatever else you feel like.
|
15
|
+
|
16
|
+
For example:
|
17
|
+
|
18
|
+
Refraction.configure do |req|
|
19
|
+
feedburner = "http://feeds.pivotallabs.com/pivotallabs"
|
20
|
+
|
21
|
+
if req.env['HTTP_USER_AGENT'] !~ /FeedBurner|FeedValidator/ && req.host =~ /pivotallabs\.com/
|
22
|
+
case req.path
|
23
|
+
when %r{^/(talks|blabs|blog)\.(atom|rss)$} ; req.found! "#{feedburner}/#{$1}.#{$2}"
|
24
|
+
when %r{^/users/(chris|edward)/blog\.(atom|rss)$} ; req.found! "#{feedburner}/#{$1}.#{$2}"
|
25
|
+
end
|
26
|
+
else
|
27
|
+
case req.host
|
28
|
+
when 'tweed.pivotallabs.com'
|
29
|
+
req.rewrite! "http://pivotallabs.com/tweed#{req.path}"
|
30
|
+
when /([-\w]+\.)?pivotallabs\.com/
|
31
|
+
# passthrough with no change
|
32
|
+
else # wildcard domains (e.g. pivotalabs.com)
|
33
|
+
req.permanent! :host => "pivotallabs.com"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Notice the use of regular expressions, the $1, $2, etc pseudo-variables, and string interpolation.
|
39
|
+
This is an easy way to match URL patterns and assemble the new URL based on what was matched.
|
40
|
+
|
41
|
+
## Installation (Rails)
|
42
|
+
|
43
|
+
Refraction can be installed in a Rails application as a plugin.
|
44
|
+
|
45
|
+
$ script/plugin install git://github.com/pivotal/refraction.git
|
46
|
+
|
47
|
+
It can also be used as a gem:
|
48
|
+
|
49
|
+
$ gem install refraction
|
50
|
+
|
51
|
+
In `environments/production.rb`, add Refraction at or near the top of your middleware stack.
|
52
|
+
|
53
|
+
config.middleware.insert_before(::Rack::Lock, ::Refraction, {})
|
54
|
+
|
55
|
+
You may want to occasionally turn on Refraction in the development environment for testing
|
56
|
+
purposes, but if your rules redirect to other servers that can be a problem.
|
57
|
+
|
58
|
+
Put your rules in `config/initializers/refraction_rules.rb` (see example above). The file name
|
59
|
+
doesn't actually matter, but convention is useful.
|
60
|
+
|
61
|
+
## Server Configuration
|
62
|
+
|
63
|
+
If your application is serving multiple virtual hosts, it's probably easiest to configure your web
|
64
|
+
server to handle a wildcard server name and let Refraction handle managing the virtual hosts. For
|
65
|
+
example, in nginx, that is done with a `server_name _;` directive.
|
66
|
+
|
67
|
+
## Writing Rules
|
68
|
+
|
69
|
+
Set up your rewrite/redirection rules during your app initialization using `Refraction.configure`.
|
70
|
+
The `configure` method takes a block which is run for every request to process the rules. The block
|
71
|
+
is passed a RequestContext object that contains information about the request URL and environment.
|
72
|
+
The request object also has a small API for effecting rewrites and redirects.
|
73
|
+
|
74
|
+
> Important note: don't do a `return` from within the configuration
|
75
|
+
> block. That would be bad (meaning your entire application would
|
76
|
+
> break). That's just how blocks work in Ruby.
|
77
|
+
|
78
|
+
### `RequestContext#set(options)`
|
79
|
+
|
80
|
+
The `set` method takes an options hash that sets pieces of the rewritten URL or redirect location
|
81
|
+
header.
|
82
|
+
|
83
|
+
* :scheme - Usually `http` or `https`.
|
84
|
+
* :host - The server name.
|
85
|
+
* :port - The server port. Usually not needed, as the scheme implies a default value.
|
86
|
+
* :path - The path of the URL.
|
87
|
+
* :query - Added at the end of the URL after a question mark (?)
|
88
|
+
|
89
|
+
Any URL components not explicitly set remain unchanged from the original request URL. You can use
|
90
|
+
`set` before calls to `rewrite!`, `permanent!`, or `found!` to set common values. Subsequent
|
91
|
+
methods will merge their component values into values from `set`.
|
92
|
+
|
93
|
+
### `RequestContext#rewrite!(options)`
|
94
|
+
|
95
|
+
The `rewrite!` method modifies the request URL and relevant pieces of the environment. When
|
96
|
+
Refraction rule processing results in a `rewrite!`, the request is passed on down the Rack stack
|
97
|
+
to the app or the next middleware component. `rewrite!` can take a single argument, either an
|
98
|
+
options hash that uses the same options as the `set` method, or a string that sets all components
|
99
|
+
of the URL.
|
100
|
+
|
101
|
+
### `RequestContext#permanent!(options)`
|
102
|
+
|
103
|
+
The `permanent!` method tells Refraction to return a response with a `301 Moved Permanently`
|
104
|
+
status, and sets the URL for the Location header. Like `rewrite!` it can take either a string or
|
105
|
+
hash argument to set the URL or some of its components.
|
106
|
+
|
107
|
+
### `RequestContext#found!(options)`
|
108
|
+
|
109
|
+
The `found!` method tells Refraction to return a response with a `302 Found` status, and sets the
|
110
|
+
URL for the Location header. Like `#rewrite!` it can take either a string or hash argument to set
|
111
|
+
the URL or some of its components.
|
112
|
+
|
113
|
+
### `RequestContext#respond!(status, headers, content)`
|
114
|
+
|
115
|
+
Use `respond!` to return an arbitrary response to the request. This is useful for responding with
|
116
|
+
the contents of a static file. For example:
|
117
|
+
|
118
|
+
req.respond!(503, {'Content-Type' => 'text/html'}, File.read(MAINTENANCE_PATH))
|
119
|
+
|
120
|
+
The args are largely the same as the contents of a standard Rack response array with the exceptions
|
121
|
+
that you don't need to wrap the content in an array, and the Content-Length header is generated so it
|
122
|
+
should not be supplied.
|
123
|
+
|
124
|
+
### URL components
|
125
|
+
|
126
|
+
The request object provides the following components of the URL for matching requests: `scheme`,
|
127
|
+
`host`, `port`, `path`, and `query`. It also provides a full environment hash as the `env`
|
128
|
+
attribute. For example, `req.env['HTTP_USER_AGENT']` can be used to access the request's user
|
129
|
+
agent property.
|
130
|
+
|
131
|
+
## Contributors
|
132
|
+
|
133
|
+
* Josh Susser (maintainer)
|
134
|
+
* Sam Pierson
|
135
|
+
* Wai Lun Mang
|
136
|
+
* Brian Morearty
|
137
|
+
|
138
|
+
Copyright (c) 2009-2010 Pivotal Labs. This software is licensed under the MIT License.
|
data/Rakefile
ADDED
data/lib/refraction.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require_relative 'refraction/version'
|
3
|
+
|
4
|
+
class Refraction
|
5
|
+
class Request < Rack::Request
|
6
|
+
attr_reader :action, :status, :message
|
7
|
+
|
8
|
+
# backward compatibility: support URI::HTTP component names
|
9
|
+
def method; request_method; end
|
10
|
+
def query; query_string; end
|
11
|
+
|
12
|
+
### actions
|
13
|
+
|
14
|
+
def set(options)
|
15
|
+
if options.is_a?(String)
|
16
|
+
@re_location = options
|
17
|
+
else
|
18
|
+
@re_scheme = options[:protocol] if options[:protocol] # :protocol is alias for :scheme
|
19
|
+
@re_scheme = options[:scheme] if options[:scheme]
|
20
|
+
@re_host = options[:host] if options[:host]
|
21
|
+
@re_port = options[:port] if options[:port]
|
22
|
+
@re_path = options[:path] if options[:path]
|
23
|
+
@re_query = options[:query] if options[:query]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def rewrite!(options)
|
28
|
+
@action = :rewrite
|
29
|
+
set(options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def permanent!(options)
|
33
|
+
@action = :permanent
|
34
|
+
@status = 301
|
35
|
+
set(options)
|
36
|
+
@message = "moved to #{@uri}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def found!(options)
|
40
|
+
@action = :found
|
41
|
+
@status = 302
|
42
|
+
set(options)
|
43
|
+
@message = "moved to #{@uri}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def respond!(status, headers, content)
|
47
|
+
@action = :respond
|
48
|
+
@status = status
|
49
|
+
@headers = headers
|
50
|
+
@message = content
|
51
|
+
end
|
52
|
+
|
53
|
+
### response
|
54
|
+
|
55
|
+
def response
|
56
|
+
headers = @headers || { 'Location' => location, 'Content-Type' => 'text/plain' }
|
57
|
+
headers['Content-Length'] = message.length.to_s
|
58
|
+
[status, headers, [message]]
|
59
|
+
end
|
60
|
+
|
61
|
+
def location
|
62
|
+
@re_location || url
|
63
|
+
end
|
64
|
+
|
65
|
+
# use original request's values when not set explicitly
|
66
|
+
def scheme; @re_scheme || super; end
|
67
|
+
def host; @re_host || super; end
|
68
|
+
def path; @re_path || super; end
|
69
|
+
def query_string; @re_query || super; end
|
70
|
+
|
71
|
+
# changing the scheme or host means use default port instead of port in original request
|
72
|
+
def port
|
73
|
+
@re_port || ((@re_scheme || @re_host) && DEFAULT_PORTS[scheme]) || super
|
74
|
+
end
|
75
|
+
|
76
|
+
def host_with_port
|
77
|
+
port.nil? || port == DEFAULT_PORTS[scheme] ? host : "#{host}:#{port}"
|
78
|
+
end
|
79
|
+
|
80
|
+
def http_host
|
81
|
+
port ? "#{host}:#{port}" : host
|
82
|
+
end
|
83
|
+
end ### class Request
|
84
|
+
|
85
|
+
def self.configure(&block)
|
86
|
+
@rules = block
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.rules
|
90
|
+
@rules
|
91
|
+
end
|
92
|
+
|
93
|
+
def initialize(app)
|
94
|
+
@app = app
|
95
|
+
end
|
96
|
+
|
97
|
+
def rules
|
98
|
+
self.class.rules
|
99
|
+
end
|
100
|
+
|
101
|
+
def call(env)
|
102
|
+
return @app.call(env) unless rules
|
103
|
+
|
104
|
+
request = Request.new(env)
|
105
|
+
rules.call(request)
|
106
|
+
|
107
|
+
case request.action
|
108
|
+
when :permanent, :found, :respond
|
109
|
+
request.response
|
110
|
+
when :rewrite
|
111
|
+
env["rack.url_scheme"] = request.scheme
|
112
|
+
env["HTTP_HOST"] = request.http_host
|
113
|
+
env["SERVER_NAME"] = request.host
|
114
|
+
env["HTTP_PORT"] = request.port if request.port
|
115
|
+
env["PATH_INFO"] = request.path
|
116
|
+
env["QUERY_STRING"] = request.query
|
117
|
+
env["REQUEST_URI"] = request.fullpath
|
118
|
+
@app.call(env)
|
119
|
+
else
|
120
|
+
@app.call(env)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/lib/refraction2.rb
ADDED
data/refraction.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/refraction/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'refraction2'
|
5
|
+
spec.version = Refraction::VERSION
|
6
|
+
spec.authors = ['Pivotal Labs', 'Josh Susser', 'Sam Pierson', 'Wai Lun Mang', 'Janosch Müller']
|
7
|
+
spec.email = ['janosch84@gmail.com']
|
8
|
+
|
9
|
+
spec.summary = 'Rack middleware replacement for joshsusser/refraction'
|
10
|
+
spec.homepage = 'https://github.com/jaynetics/refraction2'
|
11
|
+
spec.license = 'MIT'
|
12
|
+
spec.required_ruby_version = '>= 2.5.0'
|
13
|
+
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
15
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'rack', '>= 2.2'
|
25
|
+
end
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: refraction2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pivotal Labs
|
8
|
+
- Josh Susser
|
9
|
+
- Sam Pierson
|
10
|
+
- Wai Lun Mang
|
11
|
+
- Janosch Müller
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
date: 2022-05-30 00:00:00.000000000 Z
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: rack
|
19
|
+
requirement: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '2.2'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '2.2'
|
31
|
+
description:
|
32
|
+
email:
|
33
|
+
- janosch84@gmail.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- ".rspec"
|
39
|
+
- CHANGELOG.md
|
40
|
+
- Gemfile
|
41
|
+
- LICENSE.txt
|
42
|
+
- README.md
|
43
|
+
- Rakefile
|
44
|
+
- lib/refraction.rb
|
45
|
+
- lib/refraction/version.rb
|
46
|
+
- lib/refraction2.rb
|
47
|
+
- refraction.gemspec
|
48
|
+
homepage: https://github.com/jaynetics/refraction2
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata:
|
52
|
+
homepage_uri: https://github.com/jaynetics/refraction2
|
53
|
+
source_code_uri: https://github.com/jaynetics/refraction2
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options: []
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.5.0
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubygems_version: 3.4.0.dev
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Rack middleware replacement for joshsusser/refraction
|
73
|
+
test_files: []
|