Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Rack::Utils and Rack::ContentLength refactor #350

Closed
wants to merge 1 commit into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2012
  1. @elcuervo
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 38 deletions.
  1. +7 −5 lib/rack/content_length.rb
  2. +21 −33 lib/rack/utils.rb
View
12 lib/rack/content_length.rb
@@ -10,15 +10,17 @@ def initialize(app)
@app = app
end
+ def no_length_header_but_with_body(status, headers, body)
+ !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) &&
+ !headers['Content-Length'] && !headers['Transfer-Encoding'] &&
+ body.respond_to?(:to_ary)
+ end
+
def call(env)
status, headers, body = @app.call(env)
headers = HeaderHash.new(headers)
- if !STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) &&
- !headers['Content-Length'] &&
- !headers['Transfer-Encoding'] &&
- body.respond_to?(:to_ary)
-
+ if no_length_header_but_with_body(status, headers, body)
obody = body
body, length = [], 0
obody.each { |part| body << part; length += bytesize(part) }
View
54 lib/rack/utils.rb
@@ -36,14 +36,8 @@ def escape_path(s)
# Unescapes a URI escaped string with +encoding+. +encoding+ will be the
# target encoding of the string returned, and it defaults to UTF-8
- if defined?(::Encoding)
- def unescape(s, encoding = Encoding::UTF_8)
- URI.decode_www_form_component(s, encoding)
- end
- else
- def unescape(s, encoding = nil)
- URI.decode_www_form_component(s, encoding)
- end
+ def unescape(s, encoding = defined?(Encoding) ? Encoding::UTF_8 : nil)
+ URI.decode_www_form_component(s, encoding)
end
module_function :unescape
@@ -234,28 +228,28 @@ def set_cookie_header!(header, key, value)
value.map { |v| escape v }.join("&") +
"#{domain}#{path}#{expires}#{secure}#{httponly}"
- case header["Set-Cookie"]
- when nil, ''
- header["Set-Cookie"] = cookie
- when String
- header["Set-Cookie"] = [header["Set-Cookie"], cookie].join("\n")
- when Array
- header["Set-Cookie"] = (header["Set-Cookie"] + [cookie]).join("\n")
- end
+ header["Set-Cookie"] = case header["Set-Cookie"]
+ when nil, ''
+ cookie
+ when String
+ [header["Set-Cookie"], cookie].join("\n")
+ when Array
+ (header["Set-Cookie"] + [cookie]).join("\n")
+ end
nil
end
module_function :set_cookie_header!
def delete_cookie_header!(header, key, value = {})
- case header["Set-Cookie"]
- when nil, ''
- cookies = []
- when String
- cookies = header["Set-Cookie"].split("\n")
- when Array
- cookies = header["Set-Cookie"]
- end
+ cookies = case header["Set-Cookie"]
+ when nil, ''
+ []
+ when String
+ header["Set-Cookie"].split("\n")
+ when Array
+ header["Set-Cookie"]
+ end
cookies.reject! { |cookie|
if value[:domain]
@@ -279,14 +273,8 @@ def delete_cookie_header!(header, key, value = {})
# Return the bytesize of String; uses String#size under Ruby 1.8 and
# String#bytesize under 1.9.
- if ''.respond_to?(:bytesize)
- def bytesize(string)
- string.bytesize
- end
- else
- def bytesize(string)
- string.size
- end
+ def bytesize(string)
+ ''.respond_to?(:bytesize) ? string.bytesize : string.size
end
module_function :bytesize
@@ -373,7 +361,7 @@ def self.new(hash={})
end
def initialize(hash={})
- super()
+ super
@names = {}
hash.each { |k, v| self[k] = v }
end
Something went wrong with that request. Please try again.