edgar-rack 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. data/COPYING +18 -0
  2. data/KNOWN-ISSUES +21 -0
  3. data/README +401 -0
  4. data/Rakefile +101 -0
  5. data/SPEC +171 -0
  6. data/bin/rackup +4 -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 +81 -0
  12. data/lib/rack/auth/abstract/handler.rb +37 -0
  13. data/lib/rack/auth/abstract/request.rb +43 -0
  14. data/lib/rack/auth/basic.rb +58 -0
  15. data/lib/rack/auth/digest/md5.rb +124 -0
  16. data/lib/rack/auth/digest/nonce.rb +51 -0
  17. data/lib/rack/auth/digest/params.rb +53 -0
  18. data/lib/rack/auth/digest/request.rb +40 -0
  19. data/lib/rack/builder.rb +80 -0
  20. data/lib/rack/cascade.rb +41 -0
  21. data/lib/rack/chunked.rb +52 -0
  22. data/lib/rack/commonlogger.rb +49 -0
  23. data/lib/rack/conditionalget.rb +63 -0
  24. data/lib/rack/config.rb +15 -0
  25. data/lib/rack/content_length.rb +29 -0
  26. data/lib/rack/content_type.rb +23 -0
  27. data/lib/rack/deflater.rb +96 -0
  28. data/lib/rack/directory.rb +157 -0
  29. data/lib/rack/etag.rb +59 -0
  30. data/lib/rack/file.rb +118 -0
  31. data/lib/rack/handler.rb +88 -0
  32. data/lib/rack/handler/cgi.rb +61 -0
  33. data/lib/rack/handler/evented_mongrel.rb +8 -0
  34. data/lib/rack/handler/fastcgi.rb +90 -0
  35. data/lib/rack/handler/lsws.rb +61 -0
  36. data/lib/rack/handler/mongrel.rb +90 -0
  37. data/lib/rack/handler/scgi.rb +59 -0
  38. data/lib/rack/handler/swiftiplied_mongrel.rb +8 -0
  39. data/lib/rack/handler/thin.rb +17 -0
  40. data/lib/rack/handler/webrick.rb +73 -0
  41. data/lib/rack/head.rb +19 -0
  42. data/lib/rack/lint.rb +567 -0
  43. data/lib/rack/lobster.rb +65 -0
  44. data/lib/rack/lock.rb +44 -0
  45. data/lib/rack/logger.rb +18 -0
  46. data/lib/rack/methodoverride.rb +27 -0
  47. data/lib/rack/mime.rb +210 -0
  48. data/lib/rack/mock.rb +185 -0
  49. data/lib/rack/nulllogger.rb +18 -0
  50. data/lib/rack/recursive.rb +61 -0
  51. data/lib/rack/reloader.rb +109 -0
  52. data/lib/rack/request.rb +307 -0
  53. data/lib/rack/response.rb +151 -0
  54. data/lib/rack/rewindable_input.rb +104 -0
  55. data/lib/rack/runtime.rb +27 -0
  56. data/lib/rack/sendfile.rb +139 -0
  57. data/lib/rack/server.rb +289 -0
  58. data/lib/rack/session/abstract/id.rb +348 -0
  59. data/lib/rack/session/cookie.rb +152 -0
  60. data/lib/rack/session/memcache.rb +93 -0
  61. data/lib/rack/session/pool.rb +79 -0
  62. data/lib/rack/showexceptions.rb +378 -0
  63. data/lib/rack/showstatus.rb +113 -0
  64. data/lib/rack/static.rb +53 -0
  65. data/lib/rack/urlmap.rb +55 -0
  66. data/lib/rack/utils.rb +698 -0
  67. data/rack.gemspec +39 -0
  68. data/test/cgi/lighttpd.conf +25 -0
  69. data/test/cgi/rackup_stub.rb +6 -0
  70. data/test/cgi/sample_rackup.ru +5 -0
  71. data/test/cgi/test +9 -0
  72. data/test/cgi/test.fcgi +8 -0
  73. data/test/cgi/test.ru +5 -0
  74. data/test/gemloader.rb +6 -0
  75. data/test/multipart/bad_robots +259 -0
  76. data/test/multipart/binary +0 -0
  77. data/test/multipart/empty +10 -0
  78. data/test/multipart/fail_16384_nofile +814 -0
  79. data/test/multipart/file1.txt +1 -0
  80. data/test/multipart/filename_and_modification_param +7 -0
  81. data/test/multipart/filename_with_escaped_quotes +6 -0
  82. data/test/multipart/filename_with_escaped_quotes_and_modification_param +7 -0
  83. data/test/multipart/filename_with_percent_escaped_quotes +6 -0
  84. data/test/multipart/filename_with_unescaped_quotes +6 -0
  85. data/test/multipart/ie +6 -0
  86. data/test/multipart/nested +10 -0
  87. data/test/multipart/none +9 -0
  88. data/test/multipart/semicolon +6 -0
  89. data/test/multipart/text +15 -0
  90. data/test/rackup/config.ru +31 -0
  91. data/test/spec_auth_basic.rb +70 -0
  92. data/test/spec_auth_digest.rb +241 -0
  93. data/test/spec_builder.rb +123 -0
  94. data/test/spec_cascade.rb +45 -0
  95. data/test/spec_cgi.rb +102 -0
  96. data/test/spec_chunked.rb +60 -0
  97. data/test/spec_commonlogger.rb +56 -0
  98. data/test/spec_conditionalget.rb +86 -0
  99. data/test/spec_config.rb +23 -0
  100. data/test/spec_content_length.rb +36 -0
  101. data/test/spec_content_type.rb +29 -0
  102. data/test/spec_deflater.rb +125 -0
  103. data/test/spec_directory.rb +57 -0
  104. data/test/spec_etag.rb +75 -0
  105. data/test/spec_fastcgi.rb +107 -0
  106. data/test/spec_file.rb +92 -0
  107. data/test/spec_handler.rb +49 -0
  108. data/test/spec_head.rb +30 -0
  109. data/test/spec_lint.rb +515 -0
  110. data/test/spec_lobster.rb +43 -0
  111. data/test/spec_lock.rb +142 -0
  112. data/test/spec_logger.rb +28 -0
  113. data/test/spec_methodoverride.rb +58 -0
  114. data/test/spec_mock.rb +241 -0
  115. data/test/spec_mongrel.rb +182 -0
  116. data/test/spec_nulllogger.rb +12 -0
  117. data/test/spec_recursive.rb +69 -0
  118. data/test/spec_request.rb +774 -0
  119. data/test/spec_response.rb +245 -0
  120. data/test/spec_rewindable_input.rb +118 -0
  121. data/test/spec_runtime.rb +39 -0
  122. data/test/spec_sendfile.rb +83 -0
  123. data/test/spec_server.rb +8 -0
  124. data/test/spec_session_abstract_id.rb +43 -0
  125. data/test/spec_session_cookie.rb +171 -0
  126. data/test/spec_session_memcache.rb +289 -0
  127. data/test/spec_session_pool.rb +200 -0
  128. data/test/spec_showexceptions.rb +87 -0
  129. data/test/spec_showstatus.rb +79 -0
  130. data/test/spec_static.rb +48 -0
  131. data/test/spec_thin.rb +86 -0
  132. data/test/spec_urlmap.rb +213 -0
  133. data/test/spec_utils.rb +678 -0
  134. data/test/spec_webrick.rb +141 -0
  135. data/test/testrequest.rb +78 -0
  136. data/test/unregistered_handler/rack/handler/unregistered.rb +7 -0
  137. data/test/unregistered_handler/rack/handler/unregistered_long_one.rb +7 -0
  138. metadata +329 -0
@@ -0,0 +1,52 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Middleware that applies chunked transfer encoding to response bodies
6
+ # when the response does not include a Content-Length header.
7
+ class Chunked
8
+ include Rack::Utils
9
+
10
+ TERM = "\r\n"
11
+ TAIL = "0#{TERM}#{TERM}"
12
+
13
+ def initialize(app)
14
+ @app = app
15
+ end
16
+
17
+ def call(env)
18
+ status, headers, body = @app.call(env)
19
+ headers = HeaderHash.new(headers)
20
+
21
+ if env['HTTP_VERSION'] == 'HTTP/1.0' ||
22
+ STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
23
+ headers['Content-Length'] ||
24
+ headers['Transfer-Encoding']
25
+ [status, headers, body]
26
+ else
27
+ dup.chunk(status, headers, body)
28
+ end
29
+ end
30
+
31
+ def chunk(status, headers, body)
32
+ @body = body
33
+ headers.delete('Content-Length')
34
+ headers['Transfer-Encoding'] = 'chunked'
35
+ [status, headers, self]
36
+ end
37
+
38
+ def each
39
+ term = TERM
40
+ @body.each do |chunk|
41
+ size = bytesize(chunk)
42
+ next if size == 0
43
+ yield [size.to_s(16), term, chunk, term].join
44
+ end
45
+ yield TAIL
46
+ end
47
+
48
+ def close
49
+ @body.close if @body.respond_to?(:close)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,49 @@
1
+ module Rack
2
+ # Rack::CommonLogger forwards every request to an +app+ given, and
3
+ # logs a line in the Apache common log format to the +logger+, or
4
+ # rack.errors by default.
5
+ class CommonLogger
6
+ # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
7
+ # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
8
+ # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
9
+ FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
10
+
11
+ def initialize(app, logger=nil)
12
+ @app = app
13
+ @logger = logger
14
+ end
15
+
16
+ def call(env)
17
+ began_at = Time.now
18
+ status, header, body = @app.call(env)
19
+ header = Utils::HeaderHash.new(header)
20
+ log(env, status, header, began_at)
21
+ [status, header, body]
22
+ end
23
+
24
+ private
25
+
26
+ def log(env, status, header, began_at)
27
+ now = Time.now
28
+ length = extract_content_length(header)
29
+
30
+ logger = @logger || env['rack.errors']
31
+ logger.write FORMAT % [
32
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
33
+ env["REMOTE_USER"] || "-",
34
+ now.strftime("%d/%b/%Y %H:%M:%S"),
35
+ env["REQUEST_METHOD"],
36
+ env["PATH_INFO"],
37
+ env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
38
+ env["HTTP_VERSION"],
39
+ status.to_s[0..3],
40
+ length,
41
+ now - began_at ]
42
+ end
43
+
44
+ def extract_content_length(headers)
45
+ value = headers['Content-Length'] or return '-'
46
+ value.to_s == '0' ? '-' : value
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,63 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Middleware that enables conditional GET using If-None-Match and
6
+ # If-Modified-Since. The application should set either or both of the
7
+ # Last-Modified or Etag response headers according to RFC 2616. When
8
+ # either of the conditions is met, the response body is set to be zero
9
+ # length and the response status is set to 304 Not Modified.
10
+ #
11
+ # Applications that defer response body generation until the body's each
12
+ # message is received will avoid response body generation completely when
13
+ # a conditional GET matches.
14
+ #
15
+ # Adapted from Michael Klishin's Merb implementation:
16
+ # http://github.com/wycats/merb-core/tree/master/lib/merb-core/rack/middleware/conditional_get.rb
17
+ class ConditionalGet
18
+ def initialize(app)
19
+ @app = app
20
+ end
21
+
22
+ def call(env)
23
+ return @app.call(env) unless %w[GET HEAD].include?(env['REQUEST_METHOD'])
24
+
25
+ status, headers, body = @app.call(env)
26
+ headers = Utils::HeaderHash.new(headers)
27
+ if status == 200 && fresh?(env, headers)
28
+ status = 304
29
+ headers.delete('Content-Type')
30
+ headers.delete('Content-Length')
31
+ body = []
32
+ end
33
+ [status, headers, body]
34
+ end
35
+
36
+ private
37
+
38
+ def fresh?(env, headers)
39
+ modified_since = env['HTTP_IF_MODIFIED_SINCE']
40
+ none_match = env['HTTP_IF_NONE_MATCH']
41
+
42
+ return false unless modified_since || none_match
43
+
44
+ success = true
45
+ success &&= modified_since?(to_rfc2822(modified_since), headers) if modified_since
46
+ success &&= etag_matches?(none_match, headers) if none_match
47
+ success
48
+ end
49
+
50
+ def etag_matches?(none_match, headers)
51
+ etag = headers['Etag'] and etag == none_match
52
+ end
53
+
54
+ def modified_since?(modified_since, headers)
55
+ last_modified = to_rfc2822(headers['Last-Modified']) and
56
+ modified_since >= last_modified
57
+ end
58
+
59
+ def to_rfc2822(since)
60
+ Time.rfc2822(since) rescue nil
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,15 @@
1
+ module Rack
2
+ # Rack::Config modifies the environment using the block given during
3
+ # initialization.
4
+ class Config
5
+ def initialize(app, &block)
6
+ @app = app
7
+ @block = block
8
+ end
9
+
10
+ def call(env)
11
+ @block.call(env)
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+ # Sets the Content-Length header on responses with fixed-length bodies.
5
+ class ContentLength
6
+ include Rack::Utils
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, body = @app.call(env)
14
+ headers = HeaderHash.new(headers)
15
+
16
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) &&
17
+ !headers['Content-Length'] &&
18
+ !headers['Transfer-Encoding'] &&
19
+ body.respond_to?(:to_ary)
20
+
21
+ length = 0
22
+ body.each { |part| length += bytesize(part) }
23
+ headers['Content-Length'] = length.to_s
24
+ end
25
+
26
+ [status, headers, body]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'rack/utils'
2
+
3
+ module Rack
4
+
5
+ # Sets the Content-Type header on responses which don't have one.
6
+ #
7
+ # Builder Usage:
8
+ # use Rack::ContentType, "text/plain"
9
+ #
10
+ # When no content type argument is provided, "text/html" is assumed.
11
+ class ContentType
12
+ def initialize(app, content_type = "text/html")
13
+ @app, @content_type = app, content_type
14
+ end
15
+
16
+ def call(env)
17
+ status, headers, body = @app.call(env)
18
+ headers = Utils::HeaderHash.new(headers)
19
+ headers['Content-Type'] ||= @content_type
20
+ [status, headers, body]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,96 @@
1
+ require "zlib"
2
+ require "stringio"
3
+ require "time" # for Time.httpdate
4
+ require 'rack/utils'
5
+
6
+ module Rack
7
+ class Deflater
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ status, headers, body = @app.call(env)
14
+ headers = Utils::HeaderHash.new(headers)
15
+
16
+ # Skip compressing empty entity body responses and responses with
17
+ # no-transform set.
18
+ if Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
19
+ headers['Cache-Control'].to_s =~ /\bno-transform\b/
20
+ return [status, headers, body]
21
+ end
22
+
23
+ request = Request.new(env)
24
+
25
+ encoding = Utils.select_best_encoding(%w(gzip deflate identity),
26
+ request.accept_encoding)
27
+
28
+ # Set the Vary HTTP header.
29
+ vary = headers["Vary"].to_s.split(",").map { |v| v.strip }
30
+ unless vary.include?("*") || vary.include?("Accept-Encoding")
31
+ headers["Vary"] = vary.push("Accept-Encoding").join(",")
32
+ end
33
+
34
+ case encoding
35
+ when "gzip"
36
+ headers['Content-Encoding'] = "gzip"
37
+ headers.delete('Content-Length')
38
+ mtime = headers.key?("Last-Modified") ?
39
+ Time.httpdate(headers["Last-Modified"]) : Time.now
40
+ [status, headers, GzipStream.new(body, mtime)]
41
+ when "deflate"
42
+ headers['Content-Encoding'] = "deflate"
43
+ headers.delete('Content-Length')
44
+ [status, headers, DeflateStream.new(body)]
45
+ when "identity"
46
+ [status, headers, body]
47
+ when nil
48
+ message = "An acceptable encoding for the requested resource #{request.fullpath} could not be found."
49
+ [406, {"Content-Type" => "text/plain", "Content-Length" => message.length.to_s}, [message]]
50
+ end
51
+ end
52
+
53
+ class GzipStream
54
+ def initialize(body, mtime)
55
+ @body = body
56
+ @mtime = mtime
57
+ end
58
+
59
+ def each(&block)
60
+ @writer = block
61
+ gzip =::Zlib::GzipWriter.new(self)
62
+ gzip.mtime = @mtime
63
+ @body.each { |part| gzip.write(part) }
64
+ @body.close if @body.respond_to?(:close)
65
+ gzip.close
66
+ @writer = nil
67
+ end
68
+
69
+ def write(data)
70
+ @writer.call(data)
71
+ end
72
+ end
73
+
74
+ class DeflateStream
75
+ DEFLATE_ARGS = [
76
+ Zlib::DEFAULT_COMPRESSION,
77
+ # drop the zlib header which causes both Safari and IE to choke
78
+ -Zlib::MAX_WBITS,
79
+ Zlib::DEF_MEM_LEVEL,
80
+ Zlib::DEFAULT_STRATEGY
81
+ ]
82
+
83
+ def initialize(body)
84
+ @body = body
85
+ end
86
+
87
+ def each
88
+ deflater = ::Zlib::Deflate.new(*DEFLATE_ARGS)
89
+ @body.each { |part| yield deflater.deflate(part) }
90
+ @body.close if @body.respond_to?(:close)
91
+ yield deflater.finish
92
+ nil
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,157 @@
1
+ require 'time'
2
+ require 'rack/utils'
3
+ require 'rack/mime'
4
+
5
+ module Rack
6
+ # Rack::Directory serves entries below the +root+ given, according to the
7
+ # path info of the Rack request. If a directory is found, the file's contents
8
+ # will be presented in an html based index. If a file is found, the env will
9
+ # be passed to the specified +app+.
10
+ #
11
+ # If +app+ is not specified, a Rack::File of the same +root+ will be used.
12
+
13
+ class Directory
14
+ DIR_FILE = "<tr><td class='name'><a href='%s'>%s</a></td><td class='size'>%s</td><td class='type'>%s</td><td class='mtime'>%s</td></tr>"
15
+ DIR_PAGE = <<-PAGE
16
+ <html><head>
17
+ <title>%s</title>
18
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
19
+ <style type='text/css'>
20
+ table { width:100%%; }
21
+ .name { text-align:left; }
22
+ .size, .mtime { text-align:right; }
23
+ .type { width:11em; }
24
+ .mtime { width:15em; }
25
+ </style>
26
+ </head><body>
27
+ <h1>%s</h1>
28
+ <hr />
29
+ <table>
30
+ <tr>
31
+ <th class='name'>Name</th>
32
+ <th class='size'>Size</th>
33
+ <th class='type'>Type</th>
34
+ <th class='mtime'>Last Modified</th>
35
+ </tr>
36
+ %s
37
+ </table>
38
+ <hr />
39
+ </body></html>
40
+ PAGE
41
+
42
+ attr_reader :files
43
+ attr_accessor :root, :path
44
+
45
+ def initialize(root, app=nil)
46
+ @root = F.expand_path(root)
47
+ @app = app || Rack::File.new(@root)
48
+ end
49
+
50
+ def call(env)
51
+ dup._call(env)
52
+ end
53
+
54
+ F = ::File
55
+
56
+ def _call(env)
57
+ @env = env
58
+ @script_name = env['SCRIPT_NAME']
59
+ @path_info = Utils.unescape(env['PATH_INFO'])
60
+
61
+ if forbidden = check_forbidden
62
+ forbidden
63
+ else
64
+ @path = F.join(@root, @path_info)
65
+ list_path
66
+ end
67
+ end
68
+
69
+ def check_forbidden
70
+ return unless @path_info.include? ".."
71
+
72
+ body = "Forbidden\n"
73
+ size = Rack::Utils.bytesize(body)
74
+ return [403, {"Content-Type" => "text/plain",
75
+ "Content-Length" => size.to_s,
76
+ "X-Cascade" => "pass"}, [body]]
77
+ end
78
+
79
+ def list_directory
80
+ @files = [['../','Parent Directory','','','']]
81
+ glob = F.join(@path, '*')
82
+
83
+ Dir[glob].sort.each do |node|
84
+ stat = stat(node)
85
+ next unless stat
86
+ basename = F.basename(node)
87
+ ext = F.extname(node)
88
+
89
+ url = F.join(@script_name, @path_info, basename)
90
+ size = stat.size
91
+ type = stat.directory? ? 'directory' : Mime.mime_type(ext)
92
+ size = stat.directory? ? '-' : filesize_format(size)
93
+ mtime = stat.mtime.httpdate
94
+ url << '/' if stat.directory?
95
+ basename << '/' if stat.directory?
96
+
97
+ @files << [ url, basename, size, type, mtime ]
98
+ end
99
+
100
+ return [ 200, {'Content-Type'=>'text/html; charset=utf-8'}, self ]
101
+ end
102
+
103
+ def stat(node, max = 10)
104
+ F.stat(node)
105
+ rescue Errno::ENOENT, Errno::ELOOP
106
+ return nil
107
+ end
108
+
109
+ # TODO: add correct response if not readable, not sure if 404 is the best
110
+ # option
111
+ def list_path
112
+ @stat = F.stat(@path)
113
+
114
+ if @stat.readable?
115
+ return @app.call(@env) if @stat.file?
116
+ return list_directory if @stat.directory?
117
+ else
118
+ raise Errno::ENOENT, 'No such file or directory'
119
+ end
120
+
121
+ rescue Errno::ENOENT, Errno::ELOOP
122
+ return entity_not_found
123
+ end
124
+
125
+ def entity_not_found
126
+ body = "Entity not found: #{@path_info}\n"
127
+ size = Rack::Utils.bytesize(body)
128
+ return [404, {"Content-Type" => "text/plain",
129
+ "Content-Length" => size.to_s,
130
+ "X-Cascade" => "pass"}, [body]]
131
+ end
132
+
133
+ def each
134
+ show_path = @path.sub(/^#{@root}/,'')
135
+ files = @files.map{|f| DIR_FILE % f }*"\n"
136
+ page = DIR_PAGE % [ show_path, show_path , files ]
137
+ page.each_line{|l| yield l }
138
+ end
139
+
140
+ # Stolen from Ramaze
141
+
142
+ FILESIZE_FORMAT = [
143
+ ['%.1fT', 1 << 40],
144
+ ['%.1fG', 1 << 30],
145
+ ['%.1fM', 1 << 20],
146
+ ['%.1fK', 1 << 10],
147
+ ]
148
+
149
+ def filesize_format(int)
150
+ FILESIZE_FORMAT.each do |format, size|
151
+ return format % (int.to_f / size) if int >= size
152
+ end
153
+
154
+ int.to_s + 'B'
155
+ end
156
+ end
157
+ end