Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/http/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class HTTP::Client
#
# This constructor will raise an exception if any scheme but HTTP or HTTPS
# is used.
def self.new(uri : URI, tls : TLSContext = nil)
def self.new(uri : URI, tls : TLSContext = nil) : self
tls = tls_flag(uri, tls)
host = validate_host(uri)
new(host, uri.port, tls)
Expand Down Expand Up @@ -715,7 +715,7 @@ class HTTP::Client
# response = client.exec "GET", "/"
# response.body # => "..."
# ```
def exec(method : String, path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response
def exec(method : String, path : String, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response
exec new_request method, path, headers, body
end

Expand Down Expand Up @@ -745,7 +745,7 @@ class HTTP::Client
# response = HTTP::Client.exec "GET", "http://www.example.com"
# response.body # => "..."
# ```
def self.exec(method, url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls : TLSContext = nil) : HTTP::Client::Response
def self.exec(method : String, url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls : TLSContext = nil) : HTTP::Client::Response
headers = default_one_shot_headers(headers)
exec(url, tls) do |client, path|
client.exec method, path, headers, body
Expand Down
12 changes: 6 additions & 6 deletions src/http/client/response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HTTP::Client
getter! body_io : IO
@cookies : Cookies?

def initialize(@status : HTTP::Status, @body : String? = nil, @headers : Headers = Headers.new, status_message = nil, @version = "HTTP/1.1", @body_io = nil)
def initialize(@status : HTTP::Status, @body : String? = nil, @headers : Headers = Headers.new, status_message : String? = nil, @version = "HTTP/1.1", @body_io : IO? = nil)
@status_message = status_message || @status.description

if Response.mandatory_body?(@status)
Expand All @@ -22,7 +22,7 @@ class HTTP::Client
end
end

def self.new(status_code : Int32, body : String? = nil, headers : Headers = Headers.new, status_message = nil, version = "HTTP/1.1", body_io = nil)
def self.new(status_code : Int32, body : String? = nil, headers : Headers = Headers.new, status_message : String? = nil, version : String = "HTTP/1.1", body_io : IO? = nil) : self
new(HTTP::Status.new(status_code), body, headers, status_message, version, body_io)
end

Expand Down Expand Up @@ -68,7 +68,7 @@ class HTTP::Client
end
end

def to_io(io)
def to_io(io : IO) : Nil
io << @version << ' ' << @status.code << ' ' << @status_message << "\r\n"
cookies = @cookies
headers = cookies ? cookies.add_response_headers(@headers) : @headers
Expand All @@ -87,19 +87,19 @@ class HTTP::Client
!(status.informational? || status.no_content? || status.not_modified?)
end

def self.supports_chunked?(version) : Bool
def self.supports_chunked?(version : String) : Bool
version == "HTTP/1.1"
end

def self.from_io(io, ignore_body = false, decompress = true)
def self.from_io(io : IO, ignore_body : Bool = false, decompress : Bool = true) : self
from_io?(io, ignore_body, decompress) ||
raise("Unexpected end of http request")
end

# 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) : self?
def self.from_io?(io : IO, ignore_body : Bool = false, decompress : Bool = true) : self?
from_io?(io, ignore_body: ignore_body, decompress: decompress) do |response|
if response
response.consume_body_io
Expand Down
16 changes: 8 additions & 8 deletions src/http/common.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ module HTTP
end

# :nodoc:
def self.serialize_headers_and_body(io, headers, body, body_io, version)
def self.serialize_headers_and_body(io : IO, headers : HTTP::Headers, body : String?, body_io : IO?, version : String) : Nil
if body
serialize_headers_and_string_body(io, headers, body)
elsif body_io
Expand All @@ -282,7 +282,7 @@ module HTTP
end
end

def self.serialize_headers_and_string_body(io, headers, body)
def self.serialize_headers_and_string_body(io : IO, headers : HTTP::Headers, body : String) : Nil
headers["Content-Length"] = body.bytesize.to_s
headers.serialize(io)
io << "\r\n"
Expand All @@ -295,7 +295,7 @@ module HTTP
io << "\r\n"
end

def self.serialize_chunked_body(io, body)
def self.serialize_chunked_body(io : IO, body : IO) : Nil
buf = uninitialized UInt8[8192]
while (buf_length = body.read(buf.to_slice)) > 0
buf_length.to_s(io, 16)
Expand All @@ -307,7 +307,7 @@ module HTTP
end

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

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

Expand Down Expand Up @@ -380,7 +380,7 @@ module HTTP
# quoted = %q(\"foo\\bar\")
# HTTP.dequote_string(quoted) # => %q("foo\bar")
# ```
def self.dequote_string(str) : String
def self.dequote_string(str : String) : String
data = str.to_slice
quoted_pair_index = data.index('\\'.ord)
return str unless quoted_pair_index
Expand Down Expand Up @@ -410,7 +410,7 @@ module HTTP
# io.rewind
# io.gets_to_end # => %q(\"foo\\\ bar\")
# ```
def self.quote_string(string, io) : Nil
def self.quote_string(string : String, io : IO) : Nil
# Escaping rules: https://evolvis.org/pipermail/evolvis-platfrm-discuss/2014-November/000675.html

string.each_char do |char|
Expand All @@ -435,7 +435,7 @@ module HTTP
# string = %q("foo\ bar")
# HTTP.quote_string(string) # => %q(\"foo\\\ bar\")
# ```
def self.quote_string(string) : String
def self.quote_string(string : String) : String
String.build do |io|
quote_string(string, io)
end
Expand Down
6 changes: 3 additions & 3 deletions src/http/content.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module HTTP
@continue_sent = false
setter expects_continue : Bool = false

def close
def close : Nil
@expects_continue = false
super
end
Expand Down Expand Up @@ -73,7 +73,7 @@ module HTTP
@io.peek
end

def skip(bytes_count) : Nil
def skip(bytes_count : Int) : Nil
ensure_send_continue
@io.skip(bytes_count)
end
Expand Down Expand Up @@ -164,7 +164,7 @@ module HTTP
peek
end

def skip(bytes_count) : Nil
def skip(bytes_count : Int) : Nil
ensure_send_continue
if bytes_count <= @chunk_remaining
@io.skip(bytes_count)
Expand Down
14 changes: 7 additions & 7 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module HTTP
# Sets the name of this cookie.
#
# Raises `IO::Error` if the value is invalid as per [RFC 6265 §4.1.1](https://tools.ietf.org/html/rfc6265#section-4.1.1).
def name=(name : String)
def name=(name : String) : Nil
validate_name(name)
@name = name

Expand All @@ -87,7 +87,7 @@ module HTTP
# Sets the value of this cookie.
#
# Raises `IO::Error` if the value is invalid as per [RFC 6265 §4.1.1](https://tools.ietf.org/html/rfc6265#section-4.1.1).
def value=(value : String)
def value=(value : String) : String
validate_value(value)
@value = value
end
Expand Down Expand Up @@ -178,7 +178,7 @@ module HTTP
end

# :ditto:
def to_cookie_header(io) : Nil
def to_cookie_header(io : IO) : Nil
io << @name
io << '='
io << @value
Expand All @@ -197,7 +197,7 @@ module HTTP
#
# *time_reference* can be passed to use a different reference time for
# comparison. Default is the current time (`Time.utc`).
def expired?(time_reference = Time.utc) : Bool
def expired?(time_reference : Time = Time.utc) : Bool
if @max_age.try &.zero?
true
elsif expiration_time = self.expiration_time
Expand Down Expand Up @@ -253,7 +253,7 @@ module HTTP
# cookie.value # => ""
# cookie.expired? # => true
# ```
def expire
def expire : Nil
self.value = ""
self.expires = Time::UNIX_EPOCH
self.max_age = Time::Span.zero
Expand Down Expand Up @@ -307,13 +307,13 @@ module HTTP
end
end

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

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

Expand Down
30 changes: 15 additions & 15 deletions src/http/cookies.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ module HTTP
# Creates a new instance by parsing the `Cookie` headers in the given `HTTP::Headers`.
#
# See `HTTP::Client::Response#cookies`.
def self.from_client_headers(headers) : self
def self.from_client_headers(headers : HTTP::Headers) : self
new.tap(&.fill_from_client_headers(headers))
end

# Filling cookies by parsing the `Cookie` headers in the given `HTTP::Headers`.
def fill_from_client_headers(headers) : self
def fill_from_client_headers(headers : HTTP::Headers) : self
if values = headers.get?("Cookie")
values.each do |header|
Cookie::Parser.parse_cookies(header) { |cookie| self << cookie }
Expand All @@ -46,12 +46,12 @@ module HTTP
# Creates a new instance by parsing the `Set-Cookie` headers in the given `HTTP::Headers`.
#
# See `HTTP::Request#cookies`.
def self.from_server_headers(headers) : self
def self.from_server_headers(headers : HTTP::Headers) : self
new.tap(&.fill_from_server_headers(headers))
end

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

Expand All @@ -91,7 +91,7 @@ module HTTP
# response = HTTP::Client::Response.new(200)
# response.cookies["foo"] = HTTP::Cookie.new("foo", "bar", "/admin", Time.utc + 12.hours, secure: true)
# ```
def []=(key, value : Cookie)
def []=(key : String, value : Cookie) : Cookie
unless key == value.name
raise ArgumentError.new("Cookie name must match the given key")
end
Expand All @@ -104,7 +104,7 @@ module HTTP
# ```
# request.cookies["foo"].value # => "bar"
# ```
def [](key) : Cookie
def [](key : String) : Cookie
@cookies[key]
end

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

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

Expand All @@ -137,19 +137,19 @@ module HTTP
# ```
# response.cookies << HTTP::Cookie.new("foo", "bar", http_only: true)
# ```
def <<(cookie : Cookie)
def <<(cookie : Cookie) : Cookie
@cookies[cookie.name] = cookie
end

# Clears the collection, removing all cookies.
def clear : Hash(String, HTTP::Cookie)
def clear : Hash(String, 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) : Cookie?
def delete(key : String) : Cookie?
@cookies.delete(key)
end

Expand All @@ -161,7 +161,7 @@ module HTTP
end

# Returns an iterator over the cookies of this collection.
def each
def each : Iterator(Cookie)
@cookies.each_value
end

Expand All @@ -178,7 +178,7 @@ module HTTP
# Adds `Cookie` headers for the cookies in this collection to the
# given `HTTP::Headers` instance and returns it. Removes any existing
# `Cookie` headers in it.
def add_request_headers(headers)
def add_request_headers(headers : HTTP::Headers) : HTTP::Headers
if empty?
headers.delete("Cookie")
else
Expand All @@ -195,7 +195,7 @@ module HTTP
# Adds `Set-Cookie` headers for the cookies in this collection to the
# given `HTTP::Headers` instance and returns it. Removes any existing
# `Set-Cookie` headers in it.
def add_response_headers(headers)
def add_response_headers(headers : HTTP::Headers) : HTTP::Headers
headers.delete("Set-Cookie")
each do |cookie|
headers.add("Set-Cookie", cookie.to_set_cookie_header)
Expand Down
2 changes: 1 addition & 1 deletion src/http/formdata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module HTTP::FormData
# `multipart/form-data` is not compatible with the original definition in
# [RFC 2183](https://tools.ietf.org/html/rfc2183), but are instead specified
# in [RFC 2388](https://tools.ietf.org/html/rfc2388).
def self.parse_content_disposition(content_disposition) : {String, FileMetadata}
def self.parse_content_disposition(content_disposition : String) : {String, FileMetadata}
filename = nil
creation_time = nil
modification_time = nil
Expand Down
4 changes: 2 additions & 2 deletions src/http/formdata/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ module HTTP::FormData

# Adds a form part with the given *name* and *value*. *Headers* can
# optionally be provided for the form part.
def field(name : String, value, headers : HTTP::Headers = HTTP::Headers.new) : Nil
def field(name : String, value : _, headers : HTTP::Headers = HTTP::Headers.new) : Nil
file(name, IO::Memory.new(value.to_s), headers: headers)
end

# Adds a form part called *name*, with data from *io* as the value.
# *Metadata* can be provided to add extra metadata about the file to the
# Content-Disposition header for the form part. Other headers can be added
# using *headers*.
def file(name : String, io, metadata : FileMetadata = FileMetadata.new, headers : HTTP::Headers = HTTP::Headers.new)
def file(name : String, io : IO, metadata : FileMetadata = FileMetadata.new, headers : HTTP::Headers = HTTP::Headers.new) : Nil
fail "Cannot add form part: already finished" if @state.finished?

headers["Content-Disposition"] = generate_content_disposition(name, metadata)
Expand Down
Loading