qoobaa-rack 1.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/COPYING +18 -0
  2. data/KNOWN-ISSUES +18 -0
  3. data/RDOX +0 -0
  4. data/README +353 -0
  5. data/Rakefile +164 -0
  6. data/SPEC +164 -0
  7. data/bin/rackup +176 -0
  8. data/contrib/rack_logo.svg +111 -0
  9. data/example/lobster.ru +4 -0
  10. data/example/protectedlobster.rb +14 -0
  11. data/example/protectedlobster.ru +8 -0
  12. data/lib/rack/adapter/camping.rb +22 -0
  13. data/lib/rack/auth/abstract/handler.rb +37 -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 +487 -0
  21. data/lib/rack/builder.rb +63 -0
  22. data/lib/rack/cascade.rb +41 -0
  23. data/lib/rack/chunked.rb +49 -0
  24. data/lib/rack/commonlogger.rb +52 -0
  25. data/lib/rack/conditionalget.rb +47 -0
  26. data/lib/rack/content_length.rb +29 -0
  27. data/lib/rack/content_type.rb +23 -0
  28. data/lib/rack/deflater.rb +96 -0
  29. data/lib/rack/directory.rb +153 -0
  30. data/lib/rack/file.rb +88 -0
  31. data/lib/rack/handler/cgi.rb +61 -0
  32. data/lib/rack/handler/evented_mongrel.rb +8 -0
  33. data/lib/rack/handler/fastcgi.rb +88 -0
  34. data/lib/rack/handler/lsws.rb +60 -0
  35. data/lib/rack/handler/mongrel.rb +87 -0
  36. data/lib/rack/handler/scgi.rb +62 -0
  37. data/lib/rack/handler/swiftiplied_mongrel.rb +8 -0
  38. data/lib/rack/handler/thin.rb +18 -0
  39. data/lib/rack/handler/webrick.rb +71 -0
  40. data/lib/rack/handler.rb +69 -0
  41. data/lib/rack/head.rb +19 -0
  42. data/lib/rack/lint.rb +546 -0
  43. data/lib/rack/lobster.rb +65 -0
  44. data/lib/rack/lock.rb +16 -0
  45. data/lib/rack/methodoverride.rb +27 -0
  46. data/lib/rack/mime.rb +204 -0
  47. data/lib/rack/mock.rb +187 -0
  48. data/lib/rack/recursive.rb +57 -0
  49. data/lib/rack/reloader.rb +107 -0
  50. data/lib/rack/request.rb +248 -0
  51. data/lib/rack/response.rb +183 -0
  52. data/lib/rack/rewindable_input.rb +100 -0
  53. data/lib/rack/session/abstract/id.rb +142 -0
  54. data/lib/rack/session/cookie.rb +91 -0
  55. data/lib/rack/session/memcache.rb +109 -0
  56. data/lib/rack/session/pool.rb +100 -0
  57. data/lib/rack/showexceptions.rb +349 -0
  58. data/lib/rack/showstatus.rb +106 -0
  59. data/lib/rack/static.rb +38 -0
  60. data/lib/rack/urlmap.rb +55 -0
  61. data/lib/rack/utils.rb +528 -0
  62. data/lib/rack.rb +90 -0
  63. data/rack.gemspec +60 -0
  64. data/test/cgi/lighttpd.conf +20 -0
  65. data/test/cgi/test +9 -0
  66. data/test/cgi/test.fcgi +8 -0
  67. data/test/cgi/test.ru +7 -0
  68. data/test/multipart/binary +0 -0
  69. data/test/multipart/empty +10 -0
  70. data/test/multipart/file1.txt +1 -0
  71. data/test/multipart/ie +6 -0
  72. data/test/multipart/nested +10 -0
  73. data/test/multipart/none +9 -0
  74. data/test/multipart/text +10 -0
  75. data/test/spec_rack_auth_basic.rb +73 -0
  76. data/test/spec_rack_auth_digest.rb +226 -0
  77. data/test/spec_rack_auth_openid.rb +84 -0
  78. data/test/spec_rack_builder.rb +84 -0
  79. data/test/spec_rack_camping.rb +51 -0
  80. data/test/spec_rack_cascade.rb +48 -0
  81. data/test/spec_rack_cgi.rb +89 -0
  82. data/test/spec_rack_chunked.rb +62 -0
  83. data/test/spec_rack_commonlogger.rb +61 -0
  84. data/test/spec_rack_conditionalget.rb +41 -0
  85. data/test/spec_rack_content_length.rb +43 -0
  86. data/test/spec_rack_content_type.rb +30 -0
  87. data/test/spec_rack_deflater.rb +127 -0
  88. data/test/spec_rack_directory.rb +61 -0
  89. data/test/spec_rack_fastcgi.rb +89 -0
  90. data/test/spec_rack_file.rb +75 -0
  91. data/test/spec_rack_handler.rb +43 -0
  92. data/test/spec_rack_head.rb +30 -0
  93. data/test/spec_rack_lint.rb +521 -0
  94. data/test/spec_rack_lobster.rb +45 -0
  95. data/test/spec_rack_lock.rb +38 -0
  96. data/test/spec_rack_methodoverride.rb +60 -0
  97. data/test/spec_rack_mock.rb +243 -0
  98. data/test/spec_rack_mongrel.rb +189 -0
  99. data/test/spec_rack_recursive.rb +77 -0
  100. data/test/spec_rack_request.rb +504 -0
  101. data/test/spec_rack_response.rb +218 -0
  102. data/test/spec_rack_rewindable_input.rb +118 -0
  103. data/test/spec_rack_session_cookie.rb +82 -0
  104. data/test/spec_rack_session_memcache.rb +250 -0
  105. data/test/spec_rack_session_pool.rb +172 -0
  106. data/test/spec_rack_showexceptions.rb +21 -0
  107. data/test/spec_rack_showstatus.rb +72 -0
  108. data/test/spec_rack_static.rb +37 -0
  109. data/test/spec_rack_thin.rb +91 -0
  110. data/test/spec_rack_urlmap.rb +185 -0
  111. data/test/spec_rack_utils.rb +467 -0
  112. data/test/spec_rack_webrick.rb +130 -0
  113. data/test/testrequest.rb +57 -0
  114. data/test/unregistered_handler/rack/handler/unregistered.rb +7 -0
  115. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +7 -0
  116. metadata +276 -0
data/lib/rack/lint.rb ADDED
@@ -0,0 +1,546 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+ # Rack::Lint validates your application and the requests and
5
+ # responses according to the Rack spec.
6
+
7
+ class Lint
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. This value may be
92
+ ## percent-encoded when I originating from
93
+ ## a URL.
94
+
95
+ ## <tt>QUERY_STRING</tt>:: The portion of the request URL that
96
+ ## follows the <tt>?</tt>, if any. May be
97
+ ## empty, but is always required!
98
+
99
+ ## <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.
100
+
101
+ ## <tt>HTTP_</tt> Variables:: Variables corresponding to the
102
+ ## client-supplied HTTP request
103
+ ## headers (i.e., variables whose
104
+ ## names begin with <tt>HTTP_</tt>). The
105
+ ## presence or absence of these
106
+ ## variables should correspond with
107
+ ## the presence or absence of the
108
+ ## appropriate HTTP header in the
109
+ ## request.
110
+
111
+ ## In addition to this, the Rack environment must include these
112
+ ## Rack-specific variables:
113
+
114
+ ## <tt>rack.version</tt>:: The Array [1,0], representing this version of Rack.
115
+ ## <tt>rack.url_scheme</tt>:: +http+ or +https+, depending on the request URL.
116
+ ## <tt>rack.input</tt>:: See below, the input stream.
117
+ ## <tt>rack.errors</tt>:: See below, the error stream.
118
+ ## <tt>rack.multithread</tt>:: true if the application object may be simultaneously invoked by another thread in the same process, false otherwise.
119
+ ## <tt>rack.multiprocess</tt>:: true if an equivalent application object may be simultaneously invoked by another process, false otherwise.
120
+ ## <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).
121
+ ##
122
+
123
+ ## Additional environment specifications have approved to
124
+ ## standardized middleware APIs. None of these are required to
125
+ ## be implemented by the server.
126
+
127
+ ## <tt>rack.session</tt>:: A hash like interface for storing request session data.
128
+ ## The store must implement:
129
+ if session = env['rack.session']
130
+ ## store(key, value) (aliased as []=);
131
+ assert("session #{session.inspect} must respond to store and []=") {
132
+ session.respond_to?(:store) && session.respond_to?(:[]=)
133
+ }
134
+
135
+ ## fetch(key, default = nil) (aliased as []);
136
+ assert("session #{session.inspect} must respond to fetch and []") {
137
+ session.respond_to?(:fetch) && session.respond_to?(:[])
138
+ }
139
+
140
+ ## delete(key);
141
+ assert("session #{session.inspect} must respond to delete") {
142
+ session.respond_to?(:delete)
143
+ }
144
+
145
+ ## clear;
146
+ assert("session #{session.inspect} must respond to clear") {
147
+ session.respond_to?(:clear)
148
+ }
149
+ end
150
+
151
+ ## The server or the application can store their own data in the
152
+ ## environment, too. The keys must contain at least one dot,
153
+ ## and should be prefixed uniquely. The prefix <tt>rack.</tt>
154
+ ## is reserved for use with the Rack core distribution and other
155
+ ## accepted specifications and must not be used otherwise.
156
+ ##
157
+
158
+ %w[REQUEST_METHOD SERVER_NAME SERVER_PORT
159
+ QUERY_STRING
160
+ rack.version rack.input rack.errors
161
+ rack.multithread rack.multiprocess rack.run_once].each { |header|
162
+ assert("env missing required key #{header}") { env.include? header }
163
+ }
164
+
165
+ ## The environment must not contain the keys
166
+ ## <tt>HTTP_CONTENT_TYPE</tt> or <tt>HTTP_CONTENT_LENGTH</tt>
167
+ ## (use the versions without <tt>HTTP_</tt>).
168
+ %w[HTTP_CONTENT_TYPE HTTP_CONTENT_LENGTH].each { |header|
169
+ assert("env contains #{header}, must use #{header[5,-1]}") {
170
+ not env.include? header
171
+ }
172
+ }
173
+
174
+ ## The CGI keys (named without a period) must have String values.
175
+ env.each { |key, value|
176
+ next if key.include? "." # Skip extensions
177
+ assert("env variable #{key} has non-string value #{value.inspect}") {
178
+ value.instance_of? String
179
+ }
180
+ }
181
+
182
+ ##
183
+ ## There are the following restrictions:
184
+
185
+ ## * <tt>rack.version</tt> must be an array of Integers.
186
+ assert("rack.version must be an Array, was #{env["rack.version"].class}") {
187
+ env["rack.version"].instance_of? Array
188
+ }
189
+ ## * <tt>rack.url_scheme</tt> must either be +http+ or +https+.
190
+ assert("rack.url_scheme unknown: #{env["rack.url_scheme"].inspect}") {
191
+ %w[http https].include? env["rack.url_scheme"]
192
+ }
193
+
194
+ ## * There must be a valid input stream in <tt>rack.input</tt>.
195
+ check_input env["rack.input"]
196
+ ## * There must be a valid error stream in <tt>rack.errors</tt>.
197
+ check_error env["rack.errors"]
198
+
199
+ ## * The <tt>REQUEST_METHOD</tt> must be a valid token.
200
+ assert("REQUEST_METHOD unknown: #{env["REQUEST_METHOD"]}") {
201
+ env["REQUEST_METHOD"] =~ /\A[0-9A-Za-z!\#$%&'*+.^_`|~-]+\z/
202
+ }
203
+
204
+ ## * The <tt>SCRIPT_NAME</tt>, if non-empty, must start with <tt>/</tt>
205
+ assert("SCRIPT_NAME must start with /") {
206
+ !env.include?("SCRIPT_NAME") ||
207
+ env["SCRIPT_NAME"] == "" ||
208
+ env["SCRIPT_NAME"] =~ /\A\//
209
+ }
210
+ ## * The <tt>PATH_INFO</tt>, if non-empty, must start with <tt>/</tt>
211
+ assert("PATH_INFO must start with /") {
212
+ !env.include?("PATH_INFO") ||
213
+ env["PATH_INFO"] == "" ||
214
+ env["PATH_INFO"] =~ /\A\//
215
+ }
216
+ ## * The <tt>CONTENT_LENGTH</tt>, if given, must consist of digits only.
217
+ assert("Invalid CONTENT_LENGTH: #{env["CONTENT_LENGTH"]}") {
218
+ !env.include?("CONTENT_LENGTH") || env["CONTENT_LENGTH"] =~ /\A\d+\z/
219
+ }
220
+
221
+ ## * One of <tt>SCRIPT_NAME</tt> or <tt>PATH_INFO</tt> must be
222
+ ## set. <tt>PATH_INFO</tt> should be <tt>/</tt> if
223
+ ## <tt>SCRIPT_NAME</tt> is empty.
224
+ assert("One of SCRIPT_NAME or PATH_INFO must be set (make PATH_INFO '/' if SCRIPT_NAME is empty)") {
225
+ env["SCRIPT_NAME"] || env["PATH_INFO"]
226
+ }
227
+ ## <tt>SCRIPT_NAME</tt> never should be <tt>/</tt>, but instead be empty.
228
+ assert("SCRIPT_NAME cannot be '/', make it '' and PATH_INFO '/'") {
229
+ env["SCRIPT_NAME"] != "/"
230
+ }
231
+ end
232
+
233
+ ## === The Input Stream
234
+ ##
235
+ ## The input stream is an IO-like object which contains the raw HTTP
236
+ ## POST data.
237
+ def check_input(input)
238
+ ## When applicable, its external encoding must be "ASCII-8BIT" and it
239
+ ## must be opened in binary mode, for Ruby 1.9 compatibility.
240
+ assert("rack.input #{input} does not have ASCII-8BIT as its external encoding") {
241
+ input.external_encoding.name == "ASCII-8BIT"
242
+ } if input.respond_to?(:external_encoding)
243
+ assert("rack.input #{input} is not opened in binary mode") {
244
+ input.binmode?
245
+ } if input.respond_to?(:binmode?)
246
+
247
+ ## The input stream must respond to +gets+, +each+, +read+ and +rewind+.
248
+ [:gets, :each, :read, :rewind].each { |method|
249
+ assert("rack.input #{input} does not respond to ##{method}") {
250
+ input.respond_to? method
251
+ }
252
+ }
253
+ end
254
+
255
+ class InputWrapper
256
+ include Assertion
257
+
258
+ def initialize(input)
259
+ @input = input
260
+ end
261
+
262
+ def size
263
+ @input.size
264
+ end
265
+
266
+ ## * +gets+ must be called without arguments and return a string,
267
+ ## or +nil+ on EOF.
268
+ def gets(*args)
269
+ assert("rack.input#gets called with arguments") { args.size == 0 }
270
+ v = @input.gets
271
+ assert("rack.input#gets didn't return a String") {
272
+ v.nil? or v.instance_of? String
273
+ }
274
+ v
275
+ end
276
+
277
+ ## * +read+ behaves like IO#read. Its signature is <tt>read([length, [buffer]])</tt>.
278
+ ## If given, +length+ must be an non-negative Integer (>= 0) or +nil+, and +buffer+ must
279
+ ## be a String and may not be nil. If +length+ is given and not nil, then this method
280
+ ## reads at most +length+ bytes from the input stream. If +length+ is not given or nil,
281
+ ## then this method reads all data until EOF.
282
+ ## When EOF is reached, this method returns nil if +length+ is given and not nil, or ""
283
+ ## if +length+ is not given or is nil.
284
+ ## If +buffer+ is given, then the read data will be placed into +buffer+ instead of a
285
+ ## newly created String object.
286
+ def read(*args)
287
+ assert("rack.input#read called with too many arguments") {
288
+ args.size <= 2
289
+ }
290
+ if args.size >= 1
291
+ assert("rack.input#read called with non-integer and non-nil length") {
292
+ args.first.kind_of?(Integer) || args.first.nil?
293
+ }
294
+ assert("rack.input#read called with a negative length") {
295
+ args.first.nil? || args.first >= 0
296
+ }
297
+ end
298
+ if args.size >= 2
299
+ assert("rack.input#read called with non-String buffer") {
300
+ args[1].kind_of?(String)
301
+ }
302
+ end
303
+
304
+ v = @input.read(*args)
305
+
306
+ assert("rack.input#read didn't return nil or a String") {
307
+ v.nil? or v.instance_of? String
308
+ }
309
+ if args[0].nil?
310
+ assert("rack.input#read(nil) returned nil on EOF") {
311
+ !v.nil?
312
+ }
313
+ end
314
+
315
+ v
316
+ end
317
+
318
+ ## * +each+ must be called without arguments and only yield Strings.
319
+ def each(*args)
320
+ assert("rack.input#each called with arguments") { args.size == 0 }
321
+ @input.each { |line|
322
+ assert("rack.input#each didn't yield a String") {
323
+ line.instance_of? String
324
+ }
325
+ yield line
326
+ }
327
+ end
328
+
329
+ ## * +rewind+ must be called without arguments. It rewinds the input
330
+ ## stream back to the beginning. It must not raise Errno::ESPIPE:
331
+ ## that is, it may not be a pipe or a socket. Therefore, handler
332
+ ## developers must buffer the input data into some rewindable object
333
+ ## if the underlying input stream is not rewindable.
334
+ def rewind(*args)
335
+ assert("rack.input#rewind called with arguments") { args.size == 0 }
336
+ assert("rack.input#rewind raised Errno::ESPIPE") {
337
+ begin
338
+ @input.rewind
339
+ true
340
+ rescue Errno::ESPIPE
341
+ false
342
+ end
343
+ }
344
+ end
345
+
346
+ ## * +close+ must never be called on the input stream.
347
+ def close(*args)
348
+ assert("rack.input#close must not be called") { false }
349
+ end
350
+ end
351
+
352
+ ## === The Error Stream
353
+ def check_error(error)
354
+ ## The error stream must respond to +puts+, +write+ and +flush+.
355
+ [:puts, :write, :flush].each { |method|
356
+ assert("rack.error #{error} does not respond to ##{method}") {
357
+ error.respond_to? method
358
+ }
359
+ }
360
+ end
361
+
362
+ class ErrorWrapper
363
+ include Assertion
364
+
365
+ def initialize(error)
366
+ @error = error
367
+ end
368
+
369
+ ## * +puts+ must be called with a single argument that responds to +to_s+.
370
+ def puts(str)
371
+ @error.puts str
372
+ end
373
+
374
+ ## * +write+ must be called with a single argument that is a String.
375
+ def write(str)
376
+ assert("rack.errors#write not called with a String") { str.instance_of? String }
377
+ @error.write str
378
+ end
379
+
380
+ ## * +flush+ must be called without arguments and must be called
381
+ ## in order to make the error appear for sure.
382
+ def flush
383
+ @error.flush
384
+ end
385
+
386
+ ## * +close+ must never be called on the error stream.
387
+ def close(*args)
388
+ assert("rack.errors#close must not be called") { false }
389
+ end
390
+ end
391
+
392
+ ## == The Response
393
+
394
+ ## === The Status
395
+ def check_status(status)
396
+ ## This is an HTTP status. When parsed as integer (+to_i+), it must be
397
+ ## greater than or equal to 100.
398
+ assert("Status must be >=100 seen as integer") { status.to_i >= 100 }
399
+ end
400
+
401
+ ## === The Headers
402
+ def check_headers(header)
403
+ ## The header must respond to +each+, and yield values of key and value.
404
+ assert("headers object should respond to #each, but doesn't (got #{header.class} as headers)") {
405
+ header.respond_to? :each
406
+ }
407
+ header.each { |key, value|
408
+ ## The header keys must be Strings.
409
+ assert("header key must be a string, was #{key.class}") {
410
+ key.instance_of? String
411
+ }
412
+ ## The header must not contain a +Status+ key,
413
+ assert("header must not contain Status") { key.downcase != "status" }
414
+ ## contain keys with <tt>:</tt> or newlines in their name,
415
+ assert("header names must not contain : or \\n") { key !~ /[:\n]/ }
416
+ ## contain keys names that end in <tt>-</tt> or <tt>_</tt>,
417
+ assert("header names must not end in - or _") { key !~ /[-_]\z/ }
418
+ ## but only contain keys that consist of
419
+ ## letters, digits, <tt>_</tt> or <tt>-</tt> and start with a letter.
420
+ assert("invalid header name: #{key}") { key =~ /\A[a-zA-Z][a-zA-Z0-9_-]*\z/ }
421
+
422
+ ## The values of the header must be Strings,
423
+ assert("a header value must be a String, but the value of " +
424
+ "'#{key}' is a #{value.class}") { value.kind_of? String }
425
+ ## consisting of lines (for multiple header values, e.g. multiple
426
+ ## <tt>Set-Cookie</tt> values) seperated by "\n".
427
+ value.split("\n").each { |item|
428
+ ## The lines must not contain characters below 037.
429
+ assert("invalid header value #{key}: #{item.inspect}") {
430
+ item !~ /[\000-\037]/
431
+ }
432
+ }
433
+ }
434
+ end
435
+
436
+ ## === The Content-Type
437
+ def check_content_type(status, headers)
438
+ headers.each { |key, value|
439
+ ## There must be a <tt>Content-Type</tt>, except when the
440
+ ## +Status+ is 1xx, 204 or 304, in which case there must be none
441
+ ## given.
442
+ if key.downcase == "content-type"
443
+ assert("Content-Type header found in #{status} response, not allowed") {
444
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
445
+ }
446
+ return
447
+ end
448
+ }
449
+ assert("No Content-Type header found") {
450
+ Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
451
+ }
452
+ end
453
+
454
+ ## === The Content-Length
455
+ def check_content_length(status, headers, env)
456
+ headers.each { |key, value|
457
+ if key.downcase == 'content-length'
458
+ ## There must not be a <tt>Content-Length</tt> header when the
459
+ ## +Status+ is 1xx, 204 or 304.
460
+ assert("Content-Length header found in #{status} response, not allowed") {
461
+ not Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status.to_i
462
+ }
463
+
464
+ bytes = 0
465
+ string_body = true
466
+
467
+ if @body.respond_to?(:to_ary)
468
+ @body.each { |part|
469
+ unless part.kind_of?(String)
470
+ string_body = false
471
+ break
472
+ end
473
+
474
+ bytes += Rack::Utils.bytesize(part)
475
+ }
476
+
477
+ if env["REQUEST_METHOD"] == "HEAD"
478
+ assert("Response body was given for HEAD request, but should be empty") {
479
+ bytes == 0
480
+ }
481
+ else
482
+ if string_body
483
+ assert("Content-Length header was #{value}, but should be #{bytes}") {
484
+ value == bytes.to_s
485
+ }
486
+ end
487
+ end
488
+ end
489
+
490
+ return
491
+ end
492
+ }
493
+ end
494
+
495
+ ## === The Body
496
+ def each
497
+ @closed = false
498
+ ## The Body must respond to +each+
499
+ @body.each { |part|
500
+ ## and must only yield String values.
501
+ assert("Body yielded non-string value #{part.inspect}") {
502
+ part.instance_of? String
503
+ }
504
+ yield part
505
+ }
506
+ ##
507
+ ## The Body itself should not be an instance of String, as this will
508
+ ## break in Ruby 1.9.
509
+ ##
510
+ ## If the Body responds to +close+, it will be called after iteration.
511
+ # XXX howto: assert("Body has not been closed") { @closed }
512
+
513
+
514
+ ##
515
+ ## If the Body responds to +to_path+, it must return a String
516
+ ## identifying the location of a file whose contents are identical
517
+ ## to that produced by calling +each+; this may be used by the
518
+ ## server as an alternative, possibly more efficient way to
519
+ ## transport the response.
520
+
521
+ if @body.respond_to?(:to_path)
522
+ assert("The file identified by body.to_path does not exist") {
523
+ ::File.exist? @body.to_path
524
+ }
525
+ end
526
+
527
+ ##
528
+ ## The Body commonly is an Array of Strings, the application
529
+ ## instance itself, or a File-like object.
530
+ end
531
+
532
+ def close
533
+ @closed = true
534
+ @body.close if @body.respond_to?(:close)
535
+ end
536
+
537
+ # :startdoc:
538
+
539
+ end
540
+ end
541
+
542
+ ## == Thanks
543
+ ## Some parts of this specification are adopted from PEP333: Python
544
+ ## Web Server Gateway Interface
545
+ ## v1.0 (http://www.python.org/dev/peps/pep-0333/). I'd like to thank
546
+ ## everyone involved in that effort.
@@ -0,0 +1,65 @@
1
+ require 'zlib'
2
+
3
+ require 'rack/request'
4
+ require 'rack/response'
5
+
6
+ module Rack
7
+ # Paste has a Pony, Rack has a Lobster!
8
+ class Lobster
9
+ LobsterString = Zlib::Inflate.inflate("eJx9kEEOwyAMBO99xd7MAcytUhPlJyj2
10
+ P6jy9i4k9EQyGAnBarEXeCBqSkntNXsi/ZCvC48zGQoZKikGrFMZvgS5ZHd+aGWVuWwhVF0
11
+ t1drVmiR42HcWNz5w3QanT+2gIvTVCiE1lm1Y0eU4JGmIIbaKwextKn8rvW+p5PIwFl8ZWJ
12
+ I8jyiTlhTcYXkekJAzTyYN6E08A+dk8voBkAVTJQ==".delete("\n ").unpack("m*")[0])
13
+
14
+ LambdaLobster = lambda { |env|
15
+ if env["QUERY_STRING"].include?("flip")
16
+ lobster = LobsterString.split("\n").
17
+ map { |line| line.ljust(42).reverse }.
18
+ join("\n")
19
+ href = "?"
20
+ else
21
+ lobster = LobsterString
22
+ href = "?flip"
23
+ end
24
+
25
+ content = ["<title>Lobstericious!</title>",
26
+ "<pre>", lobster, "</pre>",
27
+ "<a href='#{href}'>flip!</a>"]
28
+ length = content.inject(0) { |a,e| a+e.size }.to_s
29
+ [200, {"Content-Type" => "text/html", "Content-Length" => length}, content]
30
+ }
31
+
32
+ def call(env)
33
+ req = Request.new(env)
34
+ if req.GET["flip"] == "left"
35
+ lobster = LobsterString.split("\n").
36
+ map { |line| line.ljust(42).reverse }.
37
+ join("\n")
38
+ href = "?flip=right"
39
+ elsif req.GET["flip"] == "crash"
40
+ raise "Lobster crashed"
41
+ else
42
+ lobster = LobsterString
43
+ href = "?flip=left"
44
+ end
45
+
46
+ res = Response.new
47
+ res.write "<title>Lobstericious!</title>"
48
+ res.write "<pre>"
49
+ res.write lobster
50
+ res.write "</pre>"
51
+ res.write "<p><a href='#{href}'>flip!</a></p>"
52
+ res.write "<p><a href='?flip=crash'>crash!</a></p>"
53
+ res.finish
54
+ end
55
+
56
+ end
57
+ end
58
+
59
+ if $0 == __FILE__
60
+ require 'rack'
61
+ require 'rack/showexceptions'
62
+ Rack::Handler::WEBrick.run \
63
+ Rack::ShowExceptions.new(Rack::Lint.new(Rack::Lobster.new)),
64
+ :Port => 9292
65
+ end
data/lib/rack/lock.rb ADDED
@@ -0,0 +1,16 @@
1
+ module Rack
2
+ class Lock
3
+ FLAG = 'rack.multithread'.freeze
4
+
5
+ def initialize(app, lock = Mutex.new)
6
+ @app, @lock = app, lock
7
+ end
8
+
9
+ def call(env)
10
+ old, env[FLAG] = env[FLAG], false
11
+ @lock.synchronize { @app.call(env) }
12
+ ensure
13
+ env[FLAG] = old
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,27 @@
1
+ module Rack
2
+ class MethodOverride
3
+ HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
4
+
5
+ METHOD_OVERRIDE_PARAM_KEY = "_method".freeze
6
+ HTTP_METHOD_OVERRIDE_HEADER = "HTTP_X_HTTP_METHOD_OVERRIDE".freeze
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ if env["REQUEST_METHOD"] == "POST"
14
+ req = Request.new(env)
15
+ method = req.POST[METHOD_OVERRIDE_PARAM_KEY] ||
16
+ env[HTTP_METHOD_OVERRIDE_HEADER]
17
+ method = method.to_s.upcase
18
+ if HTTP_METHODS.include?(method)
19
+ env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
20
+ env["REQUEST_METHOD"] = method
21
+ end
22
+ end
23
+
24
+ @app.call(env)
25
+ end
26
+ end
27
+ end