Skip to content
Closed
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
19 changes: 17 additions & 2 deletions src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class HTTP::Request
@cookies : Cookies?
@query_params : Params?
@uri : URI?
@io : IO?

def initialize(@method : String, @resource : String, headers : Headers? = nil, body : String | Bytes | IO | Nil = nil, @version = "HTTP/1.1")
def initialize(@method : String, @resource : String, headers : Headers? = nil, body : String | Bytes | IO | Nil = nil, @version = "HTTP/1.1", @io : IO? = nil)
@headers = headers.try(&.dup) || Headers.new
self.body = body
end
Expand Down Expand Up @@ -99,13 +100,27 @@ class HTTP::Request
return BadRequest.new unless HTTP::SUPPORTED_VERSIONS.includes?(http_version)

HTTP.parse_headers_and_body(io) do |headers, body|
return new method, resource, headers, body, http_version
return new method, resource, headers, body, http_version, io
end

# Malformed or unexpectedly ended http request
BadRequest.new
end

def remote_address
if (io = @io).responds_to?(:remote_address)
io.remote_address
else
raise "#{io} doesn't have a remote address"
end
end

def remote_address?
if (io = @io).responds_to?(:remote_address)
io.remote_address
end
end

# Lazily parses and return the request's path component.
def path
uri.path || "/"
Expand Down
14 changes: 14 additions & 0 deletions src/openssl/ssl/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ abstract class OpenSSL::SSL::Socket < IO
raise IO::Error.new("Can't rewind OpenSSL::SSL::Socket::Client")
end

def remote_address
if (io = @bio.io).responds_to?(:remote_address)
io.remote_address
else
raise "#{io} doesn't have a remote address"
end
end

def remote_address?
if (io = @bio.io).responds_to?(:remote_address)
io.remote_address
end
end

# Returns the hostname provided through Server Name Indication (SNI)
def hostname : String?
if host_name = LibSSL.ssl_get_servername(@ssl, LibSSL::TLSExt::NAMETYPE_host_name)
Expand Down