Skip to content

Commit

Permalink
Add return type restrictions to HTTP-related classes (#10586)
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin authored Jun 7, 2021
1 parent 3184e19 commit 406afba
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 96 deletions.
14 changes: 7 additions & 7 deletions src/http/client/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ class HTTP::Client::Response
new(HTTP::Status.new(status_code), body, headers, status_message, version, body_io)
end

def body
def body : String
@body || ""
end

def body?
def body? : String?
@body
end

# Returns `true` if the response status code is between 200 and 299.
def success?
def success? : Bool
@status.success?
end

# Returns a convenience wrapper around querying and setting cookie related
# headers, see `HTTP::Cookies`.
def cookies
def cookies : HTTP::Cookies
@cookies ||= Cookies.from_server_headers(headers)
end

def keep_alive?
def keep_alive? : Bool
HTTP.keep_alive?(self)
end

Expand All @@ -53,7 +53,7 @@ class HTTP::Client::Response
end

# Convenience method to retrieve the HTTP status code.
def status_code
def status_code : Int32
status.code
end

Expand Down Expand Up @@ -98,7 +98,7 @@ class HTTP::Client::Response
# Parses an `HTTP::Client::Response` from the given `IO`.
# Might return `nil` if there's no data in the `IO`,
# which probably means that the connection was closed.
def self.from_io?(io, ignore_body = false, decompress = true)
def self.from_io?(io, ignore_body = false, decompress = true) : self?
from_io?(io, ignore_body: ignore_body, decompress: decompress) do |response|
if response
response.consume_body_io
Expand Down
12 changes: 6 additions & 6 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ module HTTP
)

# :nodoc:
def self.header_name(slice : Bytes)
def self.header_name(slice : Bytes) : String
# Check if the header name is a common one.
# If so we avoid having to allocate a string for it.
if slice.size < 20
Expand Down Expand Up @@ -306,7 +306,7 @@ module HTTP
end

# :nodoc:
def self.content_length(headers)
def self.content_length(headers) : UInt64?
length_headers = headers.get? "Content-Length"
return nil unless length_headers
first_header = length_headers[0]
Expand All @@ -317,7 +317,7 @@ module HTTP
end

# :nodoc:
def self.keep_alive?(message)
def self.keep_alive?(message) : Bool
case message.headers["Connection"]?.try &.downcase
when "keep-alive"
true
Expand All @@ -333,7 +333,7 @@ module HTTP
end
end

def self.expect_continue?(headers)
def self.expect_continue?(headers) : Bool
headers["Expect"]?.try(&.downcase) == "100-continue"
end

Expand Down Expand Up @@ -379,7 +379,7 @@ module HTTP
# quoted = %q(\"foo\\bar\")
# HTTP.dequote_string(quoted) # => %q("foo\bar")
# ```
def self.dequote_string(str)
def self.dequote_string(str) : String
data = str.to_slice
quoted_pair_index = data.index('\\'.ord)
return str unless quoted_pair_index
Expand Down Expand Up @@ -434,7 +434,7 @@ module HTTP
# string = %q("foo\ bar")
# HTTP.quote_string(string) # => %q(\"foo\\\ bar\")
# ```
def self.quote_string(string)
def self.quote_string(string) : String
String.build do |io|
quote_string(string, io)
end
Expand Down
30 changes: 15 additions & 15 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module HTTP
end
end

def to_set_cookie_header
def to_set_cookie_header : String
path = @path
expires = @expires
domain = @domain
Expand All @@ -95,7 +95,7 @@ module HTTP
end
end

def to_cookie_header
def to_cookie_header : String
String.build do |io|
to_cookie_header(io)
end
Expand All @@ -107,7 +107,7 @@ module HTTP
io << @value
end

def expired?
def expired? : Bool
if e = expires
e <= Time.utc
else
Expand Down Expand Up @@ -156,13 +156,13 @@ module HTTP
end
end

def parse_cookies(header)
def parse_cookies(header) : Array(Cookie)
cookies = [] of Cookie
parse_cookies(header) { |cookie| cookies << cookie }
cookies
end

def parse_set_cookie(header)
def parse_set_cookie(header) : Cookie?
match = header.match(SetCookieString)
return unless match

Expand Down Expand Up @@ -224,7 +224,7 @@ module HTTP
end

# Filling cookies by parsing the `Cookie` headers in the given `HTTP::Headers`.
def fill_from_client_headers(headers)
def fill_from_client_headers(headers) : self
if values = headers.get?("Cookie")
values.each do |header|
Cookie::Parser.parse_cookies(header) { |cookie| self << cookie }
Expand All @@ -241,7 +241,7 @@ module HTTP
end

# Filling cookies by parsing the `Set-Cookie` headers in the given `HTTP::Headers`.
def fill_from_server_headers(headers)
def fill_from_server_headers(headers) : self
if values = headers.get?("Set-Cookie")
values.each do |header|
Cookie::Parser.parse_set_cookie(header).try { |cookie| self << cookie }
Expand Down Expand Up @@ -292,7 +292,7 @@ module HTTP
# ```
# request.cookies["foo"].value # => "bar"
# ```
def [](key)
def [](key) : Cookie
@cookies[key]
end

Expand All @@ -306,7 +306,7 @@ module HTTP
# request.cookies["foo"] = "bar"
# request.cookies["foo"]?.try &.value # > "bar"
# ```
def []?(key)
def []?(key) : Cookie?
@cookies[key]?
end

Expand All @@ -315,7 +315,7 @@ module HTTP
# ```
# request.cookies.has_key?("foo") # => true
# ```
def has_key?(key)
def has_key?(key) : Bool
@cookies.has_key?(key)
end

Expand All @@ -330,14 +330,14 @@ module HTTP
end

# Clears the collection, removing all cookies.
def clear
def clear : Hash(String, HTTP::Cookie)
@cookies.clear
end

# Deletes and returns the `HTTP::Cookie` for the specified *key*, or
# returns `nil` if *key* cannot be found in the collection. Note that
# *key* should match the name attribute of the desired `HTTP::Cookie`.
def delete(key)
def delete(key) : Cookie?
@cookies.delete(key)
end

Expand All @@ -354,12 +354,12 @@ module HTTP
end

# Returns the number of cookies contained in this collection.
def size
def size : Int32
@cookies.size
end

# Whether the collection contains any cookies.
def empty?
def empty? : Bool
@cookies.empty?
end

Expand Down Expand Up @@ -393,7 +393,7 @@ module HTTP
end

# Returns this collection as a plain `Hash`.
def to_h
def to_h : Hash(String, Cookie)
@cookies.dup
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/http/formdata/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module HTTP::FormData
# builder = HTTP::FormData::Builder.new(io, "a4VF")
# builder.content_type # => "multipart/form-data; boundary=\"a4VF\""
# ```
def content_type
def content_type : String
String.build do |str|
str << "multipart/form-data; boundary=\""
HTTP.quote_string(@boundary, str)
Expand Down
2 changes: 1 addition & 1 deletion src/http/formdata/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module HTTP::FormData
end

# True if `#next` can be called legally.
def has_next?
def has_next? : Bool
@multipart.has_next?
end
end
Expand Down
32 changes: 16 additions & 16 deletions src/http/headers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ struct HTTP::Headers
@hash[wrap(key)] = value
end

def [](key)
def [](key) : String
values = @hash[wrap(key)]
concat values
end

def []?(key)
def []?(key) : String?
fetch(key, nil)
end

Expand All @@ -85,7 +85,7 @@ struct HTTP::Headers
# headers = HTTP::Headers{"Connection" => "keep-alive, Upgrade"}
# headers.includes_word?("Connection", "Upgrade") # => true
# ```
def includes_word?(key, word)
def includes_word?(key, word) : Bool
return false if word.empty?

values = @hash[wrap(key)]?
Expand Down Expand Up @@ -118,31 +118,31 @@ struct HTTP::Headers
false
end

def add(key, value : String)
def add(key, value : String) : self
check_invalid_header_content value
unsafe_add(key, value)
self
end

def add(key, value : Array(String))
def add(key, value : Array(String)) : self
value.each { |val| check_invalid_header_content val }
unsafe_add(key, value)
self
end

def add?(key, value : String)
def add?(key, value : String) : Bool
return false unless valid_value?(value)
unsafe_add(key, value)
true
end

def add?(key, value : Array(String))
def add?(key, value : Array(String)) : Bool
value.each { |val| return false unless valid_value?(val) }
unsafe_add(key, value)
true
end

def fetch(key, default)
def fetch(key, default) : String?
fetch(wrap(key)) { default }
end

Expand All @@ -151,20 +151,20 @@ struct HTTP::Headers
values ? concat(values) : yield key
end

def has_key?(key)
def has_key?(key) : Bool
@hash.has_key? wrap(key)
end

def empty?
def empty? : Bool
@hash.empty?
end

def delete(key)
def delete(key) : String?
values = @hash.delete wrap(key)
values ? concat(values) : nil
end

def merge!(other)
def merge!(other) : self
other.each do |key, value|
self[wrap(key)] = value
end
Expand Down Expand Up @@ -233,11 +233,11 @@ struct HTTP::Headers
end
end

def get(key)
def get(key) : Array(String)
cast @hash[wrap(key)]
end

def get?(key)
def get?(key) : Array(String)?
@hash[wrap(key)]?.try { |value| cast(value) }
end

Expand All @@ -253,7 +253,7 @@ struct HTTP::Headers
dup
end

def same?(other : HTTP::Headers)
def same?(other : HTTP::Headers) : Bool
object_id == other.object_id
end

Expand Down Expand Up @@ -299,7 +299,7 @@ struct HTTP::Headers
end
end

def valid_value?(value)
def valid_value?(value) : Bool
return invalid_value_char(value).nil?
end

Expand Down
Loading

0 comments on commit 406afba

Please sign in to comment.