Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP::Request#remote_address #7610

Merged
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
45 changes: 45 additions & 0 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,51 @@ module HTTP
end
end

describe "#remote_address" do
it "for http server" do
remote_address = nil

server = Server.new do |context|
remote_address = context.request.remote_address
end

tcp_server = TCPServer.new("127.0.0.1", 0)
server.bind tcp_server
address1 = tcp_server.local_address

run_server(server) do
HTTP::Client.new(URI.parse("http://#{address1}/")) do |client|
client.get("/")

remote_address.should eq(client.@socket.as(IPSocket).local_address.to_s)
end
end
end

it "for https server" do
remote_address = nil

server = Server.new do |context|
remote_address = context.request.remote_address
end

server_context, client_context = ssl_context_pair

socket = OpenSSL::SSL::Server.new(TCPServer.new("127.0.0.1", 0), server_context)
server.bind socket
ip_address1 = server.bind_tls "127.0.0.1", 0, server_context

run_server(server) do
HTTP::Client.new(
uri: URI.parse("https://#{ip_address1}"),
tls: client_context) do |client|
client.get("/")
remote_address.should eq(client.@socket.as(OpenSSL::SSL::Socket).local_address.to_s)
end
end
end
end

it "handles Errno" do
processor = HTTP::Server::RequestProcessor.new { }
input = RaiseErrno.new(Errno::ECONNRESET)
Expand Down
1 change: 1 addition & 0 deletions spec/std/http/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec"
require "../spec_helper"

private def wait_for(timeout = 5.seconds)
now = Time.monotonic
Expand Down
17 changes: 16 additions & 1 deletion src/http/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class HTTP::Request
@query_params : Params?
@uri : URI?

# The network address that sent the request to an HTTP server.
#
# `HTTP::Server` will try to fill this property, and its value
# will have a format like "IP:port", but this format is not guaranteed.
# Middlewares can overwrite this value.
#
# This property is not used by `HTTP::Client`.
property remote_address : String?

def initialize(@method : String, @resource : String, headers : Headers? = nil, body : String | Bytes | IO | Nil = nil, @version = "HTTP/1.1")
@headers = headers.try(&.dup) || Headers.new
self.body = body
Expand Down Expand Up @@ -99,7 +108,13 @@ 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
request = new method, resource, headers, body, http_version

if io.responds_to?(:remote_address)
request.remote_address = io.remote_address.try &.to_s
end

return request
end

# Malformed or unexpectedly ended http request
Expand Down
10 changes: 10 additions & 0 deletions src/openssl/ssl/socket.cr
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ abstract class OpenSSL::SSL::Socket < IO
String.new(host_name)
end
end

def local_address
io = @bio.io
io.responds_to?(:local_address) ? io.local_address : nil
end

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