rackful 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,71 +0,0 @@
1
- # Copyright ©2011-2012 Pieter van Beek <pieterb@sara.nl>
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- require 'rackful'
16
- require 'rack/request'
17
-
18
- =begin markdown
19
- Rack middleware, inspired by {Rack::RelativeRedirect}.
20
-
21
- This middleware allows you to return a relative or absolute path your +Location:+
22
- response header.
23
- This was common practice in HTTP/1.0, but HTTP/1.1 requires a full URI in the
24
- +Location:+ header.
25
- This middleware automatically translates your path to a full URI.
26
-
27
- Differences with {Rack::RelativeRedirect}:
28
-
29
- - uses Rack::Request::base_url for creating absolute URIs.
30
- - the `Location:` header, if present, is always rectified, independent of the
31
- HTTP status code.
32
- =end
33
- class Rackful::RelativeLocation
34
-
35
- def initialize(app)
36
- @app = app
37
- end
38
-
39
- def call_old(env)
40
- res = @app.call(env)
41
- if ( location = res[1]['Location'] ) and
42
- ! %r{\A[a-z]+://}.match(location)
43
- request = Rack::Request.new env
44
- unless '/' == location[0, 1]
45
- path = ( res[1]['Content-Location'] || request.path ).dup
46
- path[ %r{[^/]*\z} ] = ''
47
- location = File.expand_path( location, path )
48
- end
49
- if '/' == location[0, 1]
50
- location = request.base_url + location
51
- end
52
- res[1]['Location'] = location
53
- end
54
- res
55
- end
56
-
57
- def call(env)
58
- res = @app.call(env)
59
- request = nil
60
- ['Location', 'Content-Location'].each do
61
- |header|
62
- if ( location = res[1][header] ) and
63
- '/' == location[0,1]
64
- request ||= Rack::Request.new env
65
- res[1][header] = request.base_url + location
66
- end
67
- end
68
- res
69
- end
70
-
71
- end # Rackful::RelativeLocation
data/lib/rackful/path.rb DELETED
@@ -1,179 +0,0 @@
1
- # Required for parsing:
2
-
3
- # Required for running:
4
- require 'rack/utils'
5
-
6
-
7
- # A String monkeypatch
8
- # @private
9
- class String
10
-
11
- # @return [Rackful::Path]
12
- def to_path; Rackful::Path.new(self); end
13
-
14
- end
15
-
16
-
17
- module Rackful
18
-
19
-
20
- # Relative URI (a path)
21
- class Path < String
22
-
23
-
24
- # @return [self]
25
- def to_path; self; end
26
-
27
-
28
- # @return [Path] a copy of `self`, with a trailing slash.
29
- def slashify
30
- r = self.dup
31
- r << '/' if '/' != r[-1,1]
32
- r
33
- end
34
-
35
-
36
- # Adds a trailing slash to `self` if necessary.
37
- # @return [self]
38
- def slashify!
39
- if '/' != self[-1,1]
40
- self << '/'
41
- else
42
- nil
43
- end
44
- self
45
- end
46
-
47
-
48
- # @return [Path]a copy of `self` without a trailing slash.
49
- def unslashify
50
- r = self.dup
51
- r = r.chomp( '/' ) if '/' == r[-1,1]
52
- r
53
- end
54
-
55
-
56
- # Removes a trailing slash from `self`.
57
- # @return [self]
58
- def unslashify!
59
- if '/' == self[-1,1]
60
- self.chomp! '/'
61
- else
62
- nil
63
- end
64
- self
65
- end
66
-
67
-
68
- # @param [Encoding] encoding the character encoding to presume for `self`
69
- # @return [String] the unescaped version of `self`
70
- def unescape( encoding = Encoding::UTF_8 ); Rack::Utils.unescape(self, encoding); end
71
-
72
-
73
- # @return [Array<String>] Unencoded segments
74
- def segments( encoding = Encoding::UTF_8 )
75
- r = self.split('/').collect { |s| Rack::Utils.unescape( s, encoding ) }
76
- r.shift
77
- r
78
- end
79
-
80
-
81
- # Turns a relative URI (starting with `/`) into a relative path (starting with `./`)
82
- # @param path [Path]
83
- # @return [String] a relative URI
84
- def relative base_path = Request.current.base_path
85
- case self
86
- when base_path
87
- # RFC2396, Section 4.2
88
- return ''
89
- when %r{(?:\A|/)\.\.?(?:/|\z)}
90
- # self has abnormal absolute path,
91
- # like "/./", "/../", "/x/../", ...
92
- return self.dup
93
- end
94
-
95
- src_path = base_path.scan(%r{(?:\A|[^/]+)/})
96
- dst_path = self.scan(%r{(?:\A|[^/]+)/?})
97
-
98
- # discard same parts
99
- while !dst_path.empty? && dst_path.first == src_path.first
100
- src_path.shift
101
- dst_path.shift
102
- end
103
-
104
- tmp = dst_path.join
105
-
106
- # calculate
107
- if src_path.empty?
108
- if tmp.empty?
109
- return './'
110
- elsif dst_path.first.include?(':') # (see RFC2396 Section 5)
111
- return './' + tmp
112
- else
113
- return tmp
114
- end
115
- end
116
-
117
- return '../' * src_path.size + tmp
118
- end
119
-
120
-
121
- end # class Path
122
-
123
-
124
- end # module Rackful
125
-
126
-
127
- #~ # Monkeypatch to this stdlib class.
128
- #~ class URI::Generic
129
- #~
130
- #~ # @see http://www.w3.org/TR/html401/struct/links.html#adef-rel the HTML `rel` attribute.
131
- #~ attr_accessor :rel
132
- #~
133
- #~ # @see http://www.w3.org/TR/html401/struct/links.html#adef-rev the HTML `rev` attribute.
134
- #~ attr_accessor :rev
135
- #~
136
- #~ def to_xhtml base_path, encoding = Encoding::UTF_8
137
- #~ retval = "<a href=\"#{self.route_from(base_path)}\"".encode encoding
138
- #~ retval << " rel=\"#{self.rel}\"" if self.rel
139
- #~ retval << " rev=\"#{self.rev}\"" if self.rev
140
- #~ retval << '>'
141
- #~ if self.relative? && ! self.query && ! self.fragment
142
- #~ retval << Rack::Utils.escape_html(
143
- #~ Rack::Utils.unescape( self.route_from(base_path).to_s, encoding )
144
- #~ )
145
- #~ else
146
- #~ retval << self.to_s
147
- #~ end
148
- #~ retval << '</a>'
149
- #~ retval
150
- #~ end
151
- #~
152
- #~ # @return [URI::Generic]
153
- #~ def slashify
154
- #~ r = self.dup
155
- #~ r.path = r.path.unslashify
156
- #~ r
157
- #~ end
158
- #~
159
- #~ # @return [self, nil]
160
- #~ def slashify!
161
- #~ self.path.slashify! && self
162
- #~ end
163
- #~
164
- #~ # @return [URI::Generic]
165
- #~ def unslashify
166
- #~ r = self.dup
167
- #~ r.path = r.path.unslashify
168
- #~ r
169
- #~ end
170
- #~
171
- #~ # @return [self, nil]
172
- #~ def unslashify!
173
- #~ self.path.unslashify! && self
174
- #~ end
175
- #~
176
- #~ end # class ::URI::Generic
177
-
178
-
179
-