kastner-rack 0.3.171

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.
Files changed (94) hide show
  1. data/AUTHORS +8 -0
  2. data/COPYING +18 -0
  3. data/KNOWN-ISSUES +18 -0
  4. data/README +273 -0
  5. data/Rakefile +185 -0
  6. data/bin/rackup +172 -0
  7. data/contrib/rack_logo.svg +111 -0
  8. data/example/lobster.ru +4 -0
  9. data/example/protectedlobster.rb +14 -0
  10. data/example/protectedlobster.ru +8 -0
  11. data/lib/rack.rb +85 -0
  12. data/lib/rack/adapter/camping.rb +22 -0
  13. data/lib/rack/auth/abstract/handler.rb +28 -0
  14. data/lib/rack/auth/abstract/request.rb +37 -0
  15. data/lib/rack/auth/basic.rb +58 -0
  16. data/lib/rack/auth/digest/md5.rb +124 -0
  17. data/lib/rack/auth/digest/nonce.rb +51 -0
  18. data/lib/rack/auth/digest/params.rb +55 -0
  19. data/lib/rack/auth/digest/request.rb +40 -0
  20. data/lib/rack/auth/openid.rb +437 -0
  21. data/lib/rack/builder.rb +67 -0
  22. data/lib/rack/cascade.rb +36 -0
  23. data/lib/rack/commonlogger.rb +61 -0
  24. data/lib/rack/conditionalget.rb +42 -0
  25. data/lib/rack/deflater.rb +63 -0
  26. data/lib/rack/directory.rb +149 -0
  27. data/lib/rack/file.rb +84 -0
  28. data/lib/rack/handler.rb +46 -0
  29. data/lib/rack/handler/cgi.rb +57 -0
  30. data/lib/rack/handler/evented_mongrel.rb +8 -0
  31. data/lib/rack/handler/fastcgi.rb +86 -0
  32. data/lib/rack/handler/lsws.rb +52 -0
  33. data/lib/rack/handler/mongrel.rb +78 -0
  34. data/lib/rack/handler/scgi.rb +57 -0
  35. data/lib/rack/handler/swiftiplied_mongrel.rb +8 -0
  36. data/lib/rack/handler/webrick.rb +61 -0
  37. data/lib/rack/head.rb +19 -0
  38. data/lib/rack/lint.rb +463 -0
  39. data/lib/rack/lobster.rb +65 -0
  40. data/lib/rack/methodoverride.rb +21 -0
  41. data/lib/rack/mime.rb +204 -0
  42. data/lib/rack/mock.rb +160 -0
  43. data/lib/rack/recursive.rb +57 -0
  44. data/lib/rack/reloader.rb +64 -0
  45. data/lib/rack/request.rb +217 -0
  46. data/lib/rack/response.rb +171 -0
  47. data/lib/rack/session/abstract/id.rb +140 -0
  48. data/lib/rack/session/cookie.rb +89 -0
  49. data/lib/rack/session/memcache.rb +97 -0
  50. data/lib/rack/session/pool.rb +73 -0
  51. data/lib/rack/showexceptions.rb +348 -0
  52. data/lib/rack/showstatus.rb +105 -0
  53. data/lib/rack/static.rb +38 -0
  54. data/lib/rack/urlmap.rb +48 -0
  55. data/lib/rack/utils.rb +318 -0
  56. data/rack.gemspec +31 -0
  57. data/test/cgi/lighttpd.conf +20 -0
  58. data/test/cgi/test +9 -0
  59. data/test/cgi/test.fcgi +8 -0
  60. data/test/cgi/test.ru +7 -0
  61. data/test/spec_rack_auth_basic.rb +69 -0
  62. data/test/spec_rack_auth_digest.rb +169 -0
  63. data/test/spec_rack_auth_openid.rb +137 -0
  64. data/test/spec_rack_builder.rb +84 -0
  65. data/test/spec_rack_camping.rb +51 -0
  66. data/test/spec_rack_cascade.rb +50 -0
  67. data/test/spec_rack_cgi.rb +89 -0
  68. data/test/spec_rack_commonlogger.rb +32 -0
  69. data/test/spec_rack_conditionalget.rb +41 -0
  70. data/test/spec_rack_deflater.rb +70 -0
  71. data/test/spec_rack_directory.rb +56 -0
  72. data/test/spec_rack_fastcgi.rb +89 -0
  73. data/test/spec_rack_file.rb +57 -0
  74. data/test/spec_rack_handler.rb +24 -0
  75. data/test/spec_rack_head.rb +30 -0
  76. data/test/spec_rack_lint.rb +371 -0
  77. data/test/spec_rack_lobster.rb +45 -0
  78. data/test/spec_rack_methodoverride.rb +31 -0
  79. data/test/spec_rack_mock.rb +152 -0
  80. data/test/spec_rack_mongrel.rb +170 -0
  81. data/test/spec_rack_recursive.rb +77 -0
  82. data/test/spec_rack_request.rb +426 -0
  83. data/test/spec_rack_response.rb +173 -0
  84. data/test/spec_rack_session_cookie.rb +78 -0
  85. data/test/spec_rack_session_memcache.rb +132 -0
  86. data/test/spec_rack_session_pool.rb +84 -0
  87. data/test/spec_rack_showexceptions.rb +21 -0
  88. data/test/spec_rack_showstatus.rb +72 -0
  89. data/test/spec_rack_static.rb +37 -0
  90. data/test/spec_rack_urlmap.rb +175 -0
  91. data/test/spec_rack_utils.rb +174 -0
  92. data/test/spec_rack_webrick.rb +123 -0
  93. data/test/testrequest.rb +45 -0
  94. metadata +177 -0
data/lib/rack/head.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Rack
2
+
3
+ class Head
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, body = @app.call(env)
10
+
11
+ if env["REQUEST_METHOD"] == "HEAD"
12
+ [status, headers, []]
13
+ else
14
+ [status, headers, body]
15
+ end
16
+ end
17
+ end
18
+
19
+ end
data/lib/rack/lint.rb ADDED
@@ -0,0 +1,463 @@
1
+ module Rack
2
+ # Rack::Lint validates your application and the requests and
3
+ # responses according to the Rack spec.
4
+
5
+ class Lint
6
+ STATUS_WITH_NO_ENTITY_BODY = (100..199).to_a << 204 << 304
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ # :stopdoc:
13
+
14
+ class LintError < RuntimeError; end
15
+ module Assertion
16
+ def assert(message, &block)
17
+ unless block.call
18
+ raise LintError, message
19
+ end
20
+ end
21
+ end
22
+ include Assertion
23
+
24
+ ## This specification aims to formalize the Rack protocol. You
25
+ ## can (and should) use Rack::Lint to enforce it.
26
+ ##
27
+ ## When you develop middleware, be sure to add a Lint before and
28
+ ## after to catch all mistakes.
29
+
30
+ ## = Rack applications
31
+
32
+ ## A Rack application is an Ruby object (not a class) that
33
+ ## responds to +call+.
34
+ def call(env=nil)
35
+ dup._call(env)
36
+ end
37
+
38
+ def _call(env)
39
+ ## It takes exactly one argument, the *environment*
40
+ assert("No env given") { env }
41
+ check_env env
42
+
43
+ env['rack.input'] = InputWrapper.new(env['rack.input'])
44
+ env['rack.errors'] = ErrorWrapper.new(env['rack.errors'])
45
+
46
+ ## and returns an Array of exactly three values:
47
+ status, headers, @body = @app.call(env)
48
+ ## The *status*,
49
+ check_status status
50
+ ## the *headers*,
51
+ check_headers headers
52
+ ## and the *body*.
53
+ check_content_type status, headers
54
+ check_content_length status, headers, env
55
+ [status, headers, self]
56
+ end
57
+
58
+ ## == The Environment
59
+ def check_env(env)
60
+ ## The environment must be an true instance of Hash (no
61
+ ## subclassing allowed) that includes CGI-like headers.
62
+ ## The application is free to modify the environment.
63
+ assert("env #{env.inspect} is not a Hash, but #{env.class}") {
64
+ env.instance_of? Hash
65
+ }
66
+
67
+ ##
68
+ ## The environment is required to include these variables
69
+ ## (adopted from PEP333), except when they'd be empty, but see
70
+ ## below.
71
+
72
+ ## <tt>REQUEST_METHOD</tt>:: The HTTP request method, such as
73
+ ## "GET" or "POST". This cannot ever
74
+ ## be an empty string, and so is
75
+ ## always required.
76
+
77
+ ## <tt>SCRIPT_NAME</tt>:: The initial portion of the request
78
+ ## URL's "path" that corresponds to the
79
+ ## application object, so that the
80
+ ## application knows its virtual
81
+ ## "location". This may be an empty
82
+ ## string, if the application corresponds
83
+ ## to the "root" of the server.
84
+
85
+ ## <tt>PATH_INFO</tt>:: The remainder of the request URL's
86
+ ## "path", designating the virtual
87
+ ## "location" of the request's target
88
+ ## within the application. This may be an
89
+ ## empty string, if the request URL targets
90
+ ## the application root and does not have a
91
+ ## trailing slash.
92
+
93
+ ## <tt>QUERY_STRING</tt>:: The portion of the request URL that
94
+ ## follows the <tt>?</tt>, if any. May be
95
+ ## empty, but is always required!
96
+
97
+ ## <tt>SERVER_NAME</tt>, <tt>SERVER_PORT</tt>:: When combined with <tt>SCRIPT_NAME</tt> and <tt>PATH_INFO</tt>, these variables can be used to complete the URL. Note, however, that <tt>HTTP_HOST</tt>, if present, should be used in preference to <tt>SERVER_NAME</tt> for reconstructing the request URL. <tt>SERVER_NAME</tt> and <tt>SERVER_PORT</tt> can never be empty strings, and so are always required.
98
+
99
+ ## <tt>HTTP_</tt> Variables:: Variables corresponding to the
100
+ ## client-supplied HTTP request
101
+ ## headers (i.e., variables whose
102
+ ## names begin with <tt>HTTP_</tt>). The
103
+ ## presence or absence of these
104
+ ## variables should correspond with
105
+ ## the presence or absence of the
106
+ ## appropriate HTTP header in the
107
+ ## request.
108
+
109
+ ## In addition to this, the Rack environment must include these
110
+ ## Rack-specific variables:
111
+
112
+ ## <tt>rack.version</tt>:: The Array [0,1], representing this version of Rack.
113
+ ## <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the request URL.
114
+ ## <tt>rack.input</tt>:: See below, the input stream.
115
+ ## <tt>rack.errors</tt>:: See below, the error stream.
116
+ ## <tt>rack.multithread</tt>:: true if the application object may be simultaneously invoked by another thread in the same process, false otherwise.
117
+ ## <tt>rack.multiprocess</tt>:: true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
118
+ ## <tt>rack.run_once</tt>:: true if the server expects (but does not guarantee!) that the application will only be invoked this one time during the life of its containing process. Normally, this will only be true for a server based on CGI (or something similar).
119
+
120
+ ## The server or the application can store their own data in the
121
+ ## environment, too. The keys must contain at least one dot,
122
+ ## and should be prefixed uniquely. The prefix <tt>rack.</tt>
123
+ ## is reserved for use with the Rack core distribution and must
124
+ ## not be used otherwise.
125
+ ##
126
+
127
+ %w[REQUEST_METHOD SERVER_NAME SERVER_PORT
128
+ QUERY_STRING
129
+ rack.version rack.input rack.errors
130
+ rack.multithread rack.multiprocess rack.run_once].each { |header|
131
+ assert("env missing required key #{header}") { env.include? header }
132
+ }
133
+
134
+ ## The environment must not contain the keys
135
+ ## <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
136
+ ## (use the versions without <tt>HTTP_</tt>).
137
+ %w[HTTP_CONTENT_TYPE HTTP_CONTENT_LENGTH].each { |header|
138
+ assert("env contains #{header}, must use #{header[5,-1]}") {
139
+ not env.include? header
140
+ }
141
+ }
142
+
143
+ ## The CGI keys (named without a period) must have String values.
144
+ env.each { |key, value|
145
+ next if key.include? "." # Skip extensions
146
+ assert("env variable #{key} has non-string value #{value.inspect}") {
147
+ value.instance_of? String
148
+ }
149
+ }
150
+
151
+ ##
152
+ ## There are the following restrictions:
153
+
154
+ ## * <tt>rack.version</tt> must be an array of Integers.
155
+ assert("rack.version must be an Array, was #{env["rack.version"].class}") {
156
+ env["rack.version"].instance_of? Array
157
+ }
158
+ ## * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
159
+ assert("rack.url_scheme unknown: #{env["rack.url_scheme"].inspect}") {
160
+ %w[http https].include? env["rack.url_scheme"]
161
+ }
162
+
163
+ ## * There must be a valid input stream in <tt>rack.input</tt>.
164
+ check_input env["rack.input"]
165
+ ## * There must be a valid error stream in <tt>rack.errors</tt>.
166
+ check_error env["rack.errors"]
167
+
168
+ ## * The <tt>REQUEST_METHOD</tt> must be a valid token.
169
+ assert("REQUEST_METHOD unknown: #{env["REQUEST_METHOD"]}") {
170
+ env["REQUEST_METHOD"] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
171
+ }
172
+
173
+ ## * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
174
+ assert("SCRIPT_NAME must start with /") {
175
+ !env.include?("SCRIPT_NAME") ||
176
+ env["SCRIPT_NAME"] == "" ||
177
+ env["SCRIPT_NAME"] =~ /\A\//
178
+ }
179
+ ## * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
180
+ assert("PATH_INFO must start with /") {
181
+ !env.include?("PATH_INFO") ||
182
+ env["PATH_INFO"] == "" ||
183
+ env["PATH_INFO"] =~ /\A\//
184
+ }
185
+ ## * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
186
+ assert("Invalid CONTENT_LENGTH: #{env["CONTENT_LENGTH"]}") {
187
+ !env.include?("CONTENT_LENGTH") || env["CONTENT_LENGTH"] =~ /\A\d+\z/
188
+ }
189
+
190
+ ## * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
191
+ ## set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
192
+ ## <tt>SCRIPT_NAME</tt> is empty.
193
+ assert("One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)") {
194
+ env["SCRIPT_NAME"] || env["PATH_INFO"]
195
+ }
196
+ ## <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
197
+ assert("SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'") {
198
+ env["SCRIPT_NAME"] != "/"
199
+ }
200
+ end
201
+
202
+ ## === The Input Stream
203
+ def check_input(input)
204
+ ## The input stream must respond to +gets+, +each+ and +read+.
205
+ [:gets, :each, :read].each { |method|
206
+ assert("rack.input #{input} does not respond to ##{method}") {
207
+ input.respond_to? method
208
+ }
209
+ }
210
+ end
211
+
212
+ class InputWrapper
213
+ include Assertion
214
+
215
+ def initialize(input)
216
+ @input = input
217
+ end
218
+
219
+ def size
220
+ @input.size
221
+ end
222
+
223
+ ## * +gets+ must be called without arguments and return a string,
224
+ ## or +nil+ on EOF.
225
+ def gets(*args)
226
+ assert("rack.input#gets called with arguments") { args.size == 0 }
227
+ v = @input.gets
228
+ assert("rack.input#gets didn't return a String") {
229
+ v.nil? or v.instance_of? String
230
+ }
231
+ v
232
+ end
233
+
234
+ ## * +read+ must be called without or with one integer argument
235
+ ## and return a string, or +nil+ on EOF.
236
+ def read(*args)
237
+ assert("rack.input#read called with too many arguments") {
238
+ args.size <= 1
239
+ }
240
+ if args.size == 1
241
+ assert("rack.input#read called with non-integer argument") {
242
+ args.first.kind_of? Integer
243
+ }
244
+ end
245
+ v = @input.read(*args)
246
+ assert("rack.input#read didn't return a String") {
247
+ v.nil? or v.instance_of? String
248
+ }
249
+ v
250
+ end
251
+
252
+ ## * +each+ must be called without arguments and only yield Strings.
253
+ def each(*args)
254
+ assert("rack.input#each called with arguments") { args.size == 0 }
255
+ @input.each { |line|
256
+ assert("rack.input#each didn't yield a String") {
257
+ line.instance_of? String
258
+ }
259
+ yield line
260
+ }
261
+ end
262
+
263
+ ## * +close+ must never be called on the input stream.
264
+ def close(*args)
265
+ assert("rack.input#close must not be called") { false }
266
+ end
267
+ end
268
+
269
+ ## === The Error Stream
270
+ def check_error(error)
271
+ ## The error stream must respond to +puts+, +write+ and +flush+.
272
+ [:puts, :write, :flush].each { |method|
273
+ assert("rack.error #{error} does not respond to ##{method}") {
274
+ error.respond_to? method
275
+ }
276
+ }
277
+ end
278
+
279
+ class ErrorWrapper
280
+ include Assertion
281
+
282
+ def initialize(error)
283
+ @error = error
284
+ end
285
+
286
+ ## * +puts+ must be called with a single argument that responds to +to_s+.
287
+ def puts(str)
288
+ @error.puts str
289
+ end
290
+
291
+ ## * +write+ must be called with a single argument that is a String.
292
+ def write(str)
293
+ assert("rack.errors#write not called with a String") { str.instance_of? String }
294
+ @error.write str
295
+ end
296
+
297
+ ## * +flush+ must be called without arguments and must be called
298
+ ## in order to make the error appear for sure.
299
+ def flush
300
+ @error.flush
301
+ end
302
+
303
+ ## * +close+ must never be called on the error stream.
304
+ def close(*args)
305
+ assert("rack.errors#close must not be called") { false }
306
+ end
307
+ end
308
+
309
+ ## == The Response
310
+
311
+ ## === The Status
312
+ def check_status(status)
313
+ ## The status, if parsed as integer (+to_i+), must be greater than or equal to 100.
314
+ assert("Status must be >=100 seen as integer") { status.to_i >= 100 }
315
+ end
316
+
317
+ ## === The Headers
318
+ def check_headers(header)
319
+ ## The header must respond to each, and yield values of key and value.
320
+ assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
321
+ header.respond_to? :each
322
+ }
323
+ header.each { |key, value|
324
+ ## The header keys must be Strings.
325
+ assert("header key must be a string, was #{key.class}") {
326
+ key.instance_of? String
327
+ }
328
+ ## The header must not contain a +Status+ key,
329
+ assert("header must not contain Status") { key.downcase != "status" }
330
+ ## contain keys with <tt>:</tt> or newlines in their name,
331
+ assert("header names must not contain : or \\n") { key !~ /[:\n]/ }
332
+ ## contain keys names that end in <tt>-</tt> or <tt>_</tt>,
333
+ assert("header names must not end in - or _") { key !~ /[-_]\z/ }
334
+ ## but only contain keys that consist of
335
+ ## letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
336
+ assert("invalid header name: #{key}") { key =~ /\A[a-zA-Z][a-zA-Z0-9_-]*\z/ }
337
+ ##
338
+ ## The values of the header must respond to #each.
339
+ assert("header values must respond to #each, but the value of " +
340
+ "'#{key}' doesn't (is #{value.class})") { value.respond_to? :each }
341
+ value.each { |item|
342
+ ## The values passed on #each must be Strings
343
+ assert("header values must consist of Strings, but '#{key}' also contains a #{item.class}") {
344
+ item.instance_of?(String)
345
+ }
346
+ ## and not contain characters below 037.
347
+ assert("invalid header value #{key}: #{item.inspect}") {
348
+ item !~ /[\000-\037]/
349
+ }
350
+ }
351
+ }
352
+ end
353
+
354
+ ## === The Content-Type
355
+ def check_content_type(status, headers)
356
+ headers.each { |key, value|
357
+ ## There must be a <tt>Content-Type</tt>, except when the
358
+ ## +Status+ is 1xx, 204 or 304, in which case there must be none
359
+ ## given.
360
+ if key.downcase == "content-type"
361
+ assert("Content-Type header found in #{status} response, not allowed") {
362
+ not STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
363
+ }
364
+ return
365
+ end
366
+ }
367
+ assert("No Content-Type header found") {
368
+ STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
369
+ }
370
+ end
371
+
372
+ ## === The Content-Length
373
+ def check_content_length(status, headers, env)
374
+ chunked_response = false
375
+ headers.each { |key, value|
376
+ if key.downcase == 'transfer-encoding'
377
+ chunked_response = value.downcase != 'identity'
378
+ end
379
+ }
380
+
381
+ headers.each { |key, value|
382
+ if key.downcase == 'content-length'
383
+ ## There must be a <tt>Content-Length</tt>, except when the
384
+ ## +Status+ is 1xx, 204 or 304, in which case there must be none
385
+ ## given.
386
+ assert("Content-Length header found in #{status} response, not allowed") {
387
+ not STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
388
+ }
389
+
390
+ assert('Content-Length header should not be used if body is chunked') {
391
+ not chunked_response
392
+ }
393
+
394
+ bytes = 0
395
+ string_body = true
396
+
397
+ @body.each { |part|
398
+ unless part.kind_of?(String)
399
+ string_body = false
400
+ break
401
+ end
402
+
403
+ bytes += (part.respond_to?(:bytesize) ? part.bytesize : part.size)
404
+ }
405
+
406
+ if env["REQUEST_METHOD"] == "HEAD"
407
+ assert("Response body was given for HEAD request, but should be empty") {
408
+ bytes == 0
409
+ }
410
+ else
411
+ if string_body
412
+ assert("Content-Length header was #{value}, but should be #{bytes}") {
413
+ value == bytes.to_s
414
+ }
415
+ end
416
+ end
417
+
418
+ return
419
+ end
420
+ }
421
+
422
+ if [ String, Array ].include?(@body.class) && !chunked_response
423
+ assert('No Content-Length header found') {
424
+ STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
425
+ }
426
+ end
427
+ end
428
+
429
+ ## === The Body
430
+ def each
431
+ @closed = false
432
+ ## The Body must respond to #each
433
+ @body.each { |part|
434
+ ## and must only yield String values.
435
+ assert("Body yielded non-string value #{part.inspect}") {
436
+ part.instance_of? String
437
+ }
438
+ yield part
439
+ }
440
+ ##
441
+ ## If the Body responds to #close, it will be called after iteration.
442
+ # XXX howto: assert("Body has not been closed") { @closed }
443
+
444
+ ##
445
+ ## The Body commonly is an Array of Strings, the application
446
+ ## instance itself, or a File-like object.
447
+ end
448
+
449
+ def close
450
+ @closed = true
451
+ @body.close if @body.respond_to?(:close)
452
+ end
453
+
454
+ # :startdoc:
455
+
456
+ end
457
+ end
458
+
459
+ ## == Thanks
460
+ ## Some parts of this specification are adopted from PEP333: Python
461
+ ## Web Server Gateway Interface
462
+ ## v1.0 (http://www.python.org/dev/peps/pep-0333/). I'd like to thank
463
+ ## everyone involved in that effort.