rackful 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE.md ADDED
@@ -0,0 +1,230 @@
1
+ <big>Apache License</big>
2
+
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+ ============================================================
8
+
9
+ Definitions
10
+ --------------
11
+
12
+ "License" shall mean the terms and conditions for use, reproduction,
13
+ and distribution as defined by Sections 1 through 9 of this document.
14
+
15
+ "Licensor" shall mean the copyright owner or entity authorized by
16
+ the copyright owner that is granting the License.
17
+
18
+ "Legal Entity" shall mean the union of the acting entity and all
19
+ other entities that control, are controlled by, or are under common
20
+ control with that entity. For the purposes of this definition,
21
+ "control" means
22
+
23
+ 1. the power, direct or indirect, to cause the
24
+ direction or management of such entity, whether by contract or
25
+ otherwise, or
26
+ 2. ownership of fifty percent (50%) or more of the
27
+ outstanding shares, or
28
+ 3. beneficial ownership of such entity.
29
+
30
+ "You" (or "Your") shall mean an individual or Legal Entity
31
+ exercising permissions granted by this License.
32
+
33
+ "Source" form shall mean the preferred form for making modifications,
34
+ including but not limited to software source code, documentation
35
+ source, and configuration files.
36
+
37
+ "Object" form shall mean any form resulting from mechanical
38
+ transformation or translation of a Source form, including but
39
+ not limited to compiled object code, generated documentation,
40
+ and conversions to other media types.
41
+
42
+ "Work" shall mean the work of authorship, whether in Source or
43
+ Object form, made available under the License, as indicated by a
44
+ copyright notice that is included in or attached to the work
45
+ (an example is provided in the Appendix below).
46
+
47
+ "Derivative Works" shall mean any work, whether in Source or Object
48
+ form, that is based on (or derived from) the Work and for which the
49
+ editorial revisions, annotations, elaborations, or other modifications
50
+ represent, as a whole, an original work of authorship. For the purposes
51
+ of this License, Derivative Works shall not include works that remain
52
+ separable from, or merely link (or bind by name) to the interfaces of,
53
+ the Work and Derivative Works thereof.
54
+
55
+ "Contribution" shall mean any work of authorship, including
56
+ the original version of the Work and any modifications or additions
57
+ to that Work or Derivative Works thereof, that is intentionally
58
+ submitted to Licensor for inclusion in the Work by the copyright owner
59
+ or by an individual or Legal Entity authorized to submit on behalf of
60
+ the copyright owner. For the purposes of this definition, "submitted"
61
+ means any form of electronic, verbal, or written communication sent
62
+ to the Licensor or its representatives, including but not limited to
63
+ communication on electronic mailing lists, source code control systems,
64
+ and issue tracking systems that are managed by, or on behalf of, the
65
+ Licensor for the purpose of discussing and improving the Work, but
66
+ excluding communication that is conspicuously marked or otherwise
67
+ designated in writing by the copyright owner as "Not a Contribution."
68
+
69
+ "Contributor" shall mean Licensor and any individual or Legal Entity
70
+ on behalf of whom a Contribution has been received by Licensor and
71
+ subsequently incorporated within the Work.
72
+
73
+ Grant of Copyright License
74
+ -----------------------------
75
+
76
+ Subject to the terms and conditions of
77
+ this License, each Contributor hereby grants to You a perpetual,
78
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
79
+ copyright license to reproduce, prepare Derivative Works of,
80
+ publicly display, publicly perform, sublicense, and distribute the
81
+ Work and such Derivative Works in Source or Object form.
82
+
83
+ Grant of Patent License
84
+ --------------------------
85
+
86
+ Subject to the terms and conditions of
87
+ this License, each Contributor hereby grants to You a perpetual,
88
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
89
+ (except as stated in this section) patent license to make, have made,
90
+ use, offer to sell, sell, import, and otherwise transfer the Work,
91
+ where such license applies only to those patent claims licensable
92
+ by such Contributor that are necessarily infringed by their
93
+ Contribution(s) alone or by combination of their Contribution(s)
94
+ with the Work to which such Contribution(s) was submitted. If You
95
+ institute patent litigation against any entity (including a
96
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
97
+ or a Contribution incorporated within the Work constitutes direct
98
+ or contributory patent infringement, then any patent licenses
99
+ granted to You under this License for that Work shall terminate
100
+ as of the date such litigation is filed.
101
+
102
+ Redistribution
103
+ -----------------
104
+
105
+ You may reproduce and distribute copies of the
106
+ Work or Derivative Works thereof in any medium, with or without
107
+ modifications, and in Source or Object form, provided that You
108
+ meet the following conditions:
109
+
110
+ 1. You must give any other recipients of the Work or
111
+ Derivative Works a copy of this License; and
112
+
113
+ 2. You must cause any modified files to carry prominent notices
114
+ stating that You changed the files; and
115
+
116
+ 3. You must retain, in the Source form of any Derivative Works
117
+ that You distribute, all copyright, patent, trademark, and
118
+ attribution notices from the Source form of the Work,
119
+ excluding those notices that do not pertain to any part of
120
+ the Derivative Works; and
121
+
122
+ 4. If the Work includes a "NOTICE" text file as part of its
123
+ distribution, then any Derivative Works that You distribute must
124
+ include a readable copy of the attribution notices contained
125
+ within such NOTICE file, excluding those notices that do not
126
+ pertain to any part of the Derivative Works, in at least one
127
+ of the following places: within a NOTICE text file distributed
128
+ as part of the Derivative Works; within the Source form or
129
+ documentation, if provided along with the Derivative Works; or,
130
+ within a display generated by the Derivative Works, if and
131
+ wherever such third-party notices normally appear. The contents
132
+ of the NOTICE file are for informational purposes only and
133
+ do not modify the License. You may add Your own attribution
134
+ notices within Derivative Works that You distribute, alongside
135
+ or as an addendum to the NOTICE text from the Work, provided
136
+ that such additional attribution notices cannot be construed
137
+ as modifying the License.
138
+
139
+ You may add Your own copyright statement to Your modifications and
140
+ may provide additional or different license terms and conditions
141
+ for use, reproduction, or distribution of Your modifications, or
142
+ for any such Derivative Works as a whole, provided Your use,
143
+ reproduction, and distribution of the Work otherwise complies with
144
+ the conditions stated in this License.
145
+
146
+ Submission of Contributions
147
+ ------------------------------
148
+
149
+ Unless You explicitly state otherwise,
150
+ any Contribution intentionally submitted for inclusion in the Work
151
+ by You to the Licensor shall be under the terms and conditions of
152
+ this License, without any additional terms or conditions.
153
+ Notwithstanding the above, nothing herein shall supersede or modify
154
+ the terms of any separate license agreement you may have executed
155
+ with Licensor regarding such Contributions.
156
+
157
+ Trademarks
158
+ -------------
159
+
160
+ This License does not grant permission to use the trade
161
+ names, trademarks, service marks, or product names of the Licensor,
162
+ except as required for reasonable and customary use in describing the
163
+ origin of the Work and reproducing the content of the NOTICE file.
164
+
165
+ Disclaimer of Warranty
166
+ -------------------------
167
+
168
+ Unless required by applicable law or
169
+ agreed to in writing, Licensor provides the Work (and each
170
+ Contributor provides its Contributions) on an "AS IS" BASIS,
171
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
172
+ implied, including, without limitation, any warranties or conditions
173
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
174
+ PARTICULAR PURPOSE. You are solely responsible for determining the
175
+ appropriateness of using or redistributing the Work and assume any
176
+ risks associated with Your exercise of permissions under this License.
177
+
178
+ Limitation of Liability
179
+ --------------------------
180
+
181
+ In no event and under no legal theory,
182
+ whether in tort (including negligence), contract, or otherwise,
183
+ unless required by applicable law (such as deliberate and grossly
184
+ negligent acts) or agreed to in writing, shall any Contributor be
185
+ liable to You for damages, including any direct, indirect, special,
186
+ incidental, or consequential damages of any character arising as a
187
+ result of this License or out of the use or inability to use the
188
+ Work (including but not limited to damages for loss of goodwill,
189
+ work stoppage, computer failure or malfunction, or any and all
190
+ other commercial damages or losses), even if such Contributor
191
+ has been advised of the possibility of such damages.
192
+
193
+ Accepting Warranty or Additional Liability
194
+ ---------------------------------------------
195
+
196
+ While redistributing
197
+ the Work or Derivative Works thereof, You may choose to offer,
198
+ and charge a fee for, acceptance of support, warranty, indemnity,
199
+ or other liability obligations and/or rights consistent with this
200
+ License. However, in accepting such obligations, You may act only
201
+ on Your own behalf and on Your sole responsibility, not on behalf
202
+ of any other Contributor, and only if You agree to indemnify,
203
+ defend, and hold each Contributor harmless for any liability
204
+ incurred by, or claims asserted against, such Contributor by reason
205
+ of your accepting any such warranty or additional liability.
206
+
207
+ APPENDIX: How to apply the Apache License to your work
208
+ ======================================================
209
+
210
+ To apply the Apache License to your work, attach the following boilerplate
211
+ notice, with the fields enclosed by brackets "[]" replaced with your own
212
+ identifying information. (Don't include the brackets!) The text should be
213
+ enclosed in the appropriate comment syntax for the file format. We also
214
+ recommend that a file or class name and description of purpose be included
215
+ on the same "printed page" as the copyright notice for easier identification
216
+ within third-party archives.
217
+
218
+ Copyright [yyyy] [name of copyright owner]
219
+
220
+ Licensed under the Apache License, Version 2.0 (the "License");
221
+ you may not use this file except in compliance with the License.
222
+ You may obtain a copy of the License at
223
+
224
+ http://www.apache.org/licenses/LICENSE-2.0
225
+
226
+ Unless required by applicable law or agreed to in writing, software
227
+ distributed under the License is distributed on an "AS IS" BASIS,
228
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
229
+ See the License for the specific language governing permissions and
230
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ All documentation is available
2
+ [here, as GitHub pages](http://pieterb.github.com/Rackful/).
data/example/config.ru ADDED
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../lib/rackful.rb'
2
+ require 'digest/md5'
3
+
4
+ # The class of the object we're going to serve:
5
+ class Root
6
+ include Rackful::Resource
7
+ def initialize *args
8
+ super
9
+ @content = 'Hello world!'
10
+ end
11
+ def do_GET request, response
12
+ response['Content-Type'] = 'text/plain'
13
+ response.write @content
14
+ end
15
+ def do_PUT request, response
16
+ @content = request.body.read
17
+ response.status = status_code :no_content
18
+ end
19
+ def etag
20
+ '"' + Digest::MD5.new.update(@content).to_s + '"'
21
+ end
22
+ end
23
+ $root_resource = Root.new '/'
24
+
25
+ # Rackful::Server needs a resource factory which can map URIs to resource objects:
26
+ class ResourceFactory
27
+ def [] uri
28
+ case uri
29
+ when '/'; $root_resource
30
+ else; nil
31
+ end
32
+ end
33
+ end
34
+
35
+ run Rackful::Server.new ResourceFactory.new
@@ -0,0 +1,66 @@
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
+ module Rackful
16
+
17
+ =begin
18
+ Rack middleware that provides header spoofing.
19
+
20
+ If you use this middleware, then clients are allowed to spoof an HTTP header
21
+ by specifying a `_http_SOME_HEADER=...` request parameter, for example:
22
+
23
+ http://example.com/some_resource?_http_DEPTH=infinity
24
+
25
+ This can be useful if you want to specify certain request headers from within
26
+ a normal web browser.
27
+ @example Using this middleware
28
+ use Rackful::HeaderSpoofing
29
+ =end
30
+ class HeaderSpoofing
31
+
32
+ def initialize app
33
+ @app = app
34
+ end
35
+
36
+ def call env
37
+ before_call env
38
+ @app.call env
39
+ # after_call env
40
+ end
41
+
42
+ def handle_header_spoofing env
43
+ original_query_string = env['QUERY_STRING']
44
+ env['QUERY_STRING'] = original_query_string.
45
+ split('&', -1).
46
+ collect { |s| s.split('=', -1) }.
47
+ select {
48
+ |p|
49
+ if /\A_http_([a-z]+(?:[\-_][a-z]+)*)\z/i === p[0]
50
+ header_name = p.shift.gsub('-', '_').upcase
51
+ env[header_name] = p.join('=')
52
+ false
53
+ else
54
+ true
55
+ end
56
+ }.
57
+ collect { |p| p.join('=') }.
58
+ join('&')
59
+ if original_query_string != env['QUERY_STRING']
60
+ env['rackful.header_spoofing.query_string'] ||= original_query_string
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,104 @@
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
+
17
+ module Rackful
18
+
19
+ =begin
20
+ Rack middleware that provides method spoofing.
21
+
22
+ If you use this middleware, then clients are allowed to spoof an HTTP method
23
+ by specifying a `_method=...` request parameter, for example:
24
+
25
+ http://example.com/some_resource?_method=DELETE
26
+
27
+ This can be useful if you want to perform `PUT` and `DELETE` requests from
28
+ within a browser, of when you want to perform a `GET` requests with (too)
29
+ many parameters, exceeding the maximum URI length in your client or server.
30
+ In that case, you can put the parameters in a `POST` body, like this:
31
+
32
+ POST /some_resource HTTP/1.1
33
+ Host: example.com
34
+ Content-Type: application/x-www-form-urlencoded
35
+ Content-Length: 123456789
36
+
37
+ param_1=hello&param_2=world&param_3=...
38
+ @example Using this middleware
39
+ use Rackful::MethodSpoofing
40
+ =end
41
+ class MethodSpoofing
42
+
43
+ def initialize app
44
+ @app = app
45
+ end
46
+
47
+ def call env
48
+ before_call env
49
+ @app.call env
50
+ after_call env
51
+ end
52
+
53
+ def before_call env
54
+ return unless ['GET', 'POST'].include? env['REQUEST_METHOD']
55
+ original_query_string = env['QUERY_STRING']
56
+ new_method = nil
57
+ env['QUERY_STRING'] = original_query_string.
58
+ split('&', -1).
59
+ collect { |s| s.split('=', -1) }.
60
+ select {
61
+ |p|
62
+ if /_method/i === p[0] &&
63
+ /\A[A-Z]+\z/ === ( method = p[1..-1].join('=').upcase ) &&
64
+ ! new_method
65
+ new_method = method
66
+ false
67
+ else
68
+ true
69
+ end
70
+ }.
71
+ collect { |p| p.join('=') }.
72
+ join('&')
73
+ if new_method
74
+ if 'GET' == new_method &&
75
+ 'POST' == env['REQUEST_METHOD'] &&
76
+ 'application/x-www-form-urlencoded' == env['CONTENT_TYPE']
77
+ unless env['QUERY_STRING'].empty
78
+ env['QUERY_STRING'] = env['QUERY_STRING'] + '&'
79
+ end
80
+ begin
81
+ env['QUERY_STRING'] = env['QUERY_STRING'] + env['rack.input'].read
82
+ env['rack.input'].rewind
83
+ end
84
+ env['rackful.method_spoofing.input'] = env['rack.input']
85
+ env.delete 'rack.input'
86
+ env.delete 'CONTENT_TYPE'
87
+ env.delete 'CONTENT_LENGTH' if env.key? 'CONTENT_LENGTH'
88
+ end
89
+ env['rackful.method_spoofing.QUERY_STRING'] ||= original_query_string
90
+ env['rackful.method_spoofing.REQUEST_METHOD'] = env['REQUEST_METHOD']
91
+ env['REQUEST_METHOD'] = new_method
92
+ end
93
+ end
94
+
95
+ def after_call env
96
+ if env.key? 'rackful.method_spoofing.input'
97
+ env['rack.input'] = env['rackful.method_spoofing.input']
98
+ env.delete 'rackful.input'
99
+ end
100
+ end
101
+
102
+ end
103
+
104
+ end
@@ -0,0 +1,58 @@
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
+
16
+ require 'rack/request'
17
+
18
+ module Rackful
19
+
20
+ =begin markdown
21
+ Rack middleware, inspired by {Rack::RelativeRedirect}.
22
+
23
+ This middleware allows you to return a relative or absolute path your +Location:+
24
+ response header.
25
+ This was common practice in HTTP/1.0, but HTTP/1.1 requires a full URI in the
26
+ +Location:+ header.
27
+ This middleware automatically translates your path to a full URI.
28
+
29
+ Differences with {Rack::RelativeRedirect}:
30
+
31
+ - uses Rack::Request::base_url for creating absolute URIs.
32
+ - the `Location:` header, if present, is always rectified, independent of the
33
+ HTTP status code.
34
+ =end
35
+ class RelativeLocation
36
+
37
+ def initialize(app)
38
+ @app = app
39
+ end
40
+
41
+ def call(env)
42
+ res = @app.call(env)
43
+ if ( location = res[1]['Location'] ) and
44
+ ! %r{\A[a-z]+://}.match(location)
45
+ request = Rack::Request.new env
46
+ unless '/' == location[0, 1]
47
+ path = request.path.dup
48
+ path[ %r{[^/]*\z} ] = ''
49
+ location = File.expand_path( location, path )
50
+ end
51
+ res[1]['Location'] = request.base_url + location
52
+ end
53
+ res
54
+ end
55
+
56
+ end # RelativeLocation
57
+
58
+ end # Rackful