diff --git a/src/http/client/client.cr b/src/http/client/client.cr index 605dc66..608ee81 100644 --- a/src/http/client/client.cr +++ b/src/http/client/client.cr @@ -34,12 +34,12 @@ class HTTP::Client end {% for method in %w(get post put head delete patch) %} - def {{method.id}}(path, headers = nil, body = nil) - exec {{method.upcase}}, path, headers, body + def {{method.id}}(path, headers = nil, body = nil, src = nil, dst = nil + exec {{method.upcase}}, path, headers, body, src, dst end - def self.{{method.id}}(url, headers = nil, body = nil) - exec {{method.upcase}}, url, headers, body + def self.{{method.id}}(url, headers = nil, body = nil, src = nil, dst = nil + exec {{method.upcase}}, url, headers, body, src, dst end {% end %} @@ -64,8 +64,8 @@ class HTTP::Client HTTP::Response.from_io(socket) end - def exec(method : String, path, headers = nil, body = nil) - exec new_request method, path, headers, body + def exec(method : String, path, headers = nil, body = nil, src = nil, dst = nil + exec new_request method, path, headers, body, src, dst end def close @@ -76,10 +76,10 @@ class HTTP::Client @socket = nil end - private def new_request(method, path, headers, body) + private def new_request(method, path, headers, body, src, dst) headers ||= HTTP::Headers.new headers["Host"] ||= host_header - request = HTTP::Request.new method, path, headers, body + request = HTTP::Request.new method, path, headers, body, src, dst @before_request.try &.call(request) request end @@ -107,9 +107,9 @@ class HTTP::Client end end - def self.exec(method, url, headers = nil, body = nil) + def self.exec(method, url, headers = nil, body = nil, src = nil, dst = nil exec(url) do |client, path| - client.exec method, path, headers, body + client.exec method, path, headers, body, src, dst end end diff --git a/src/http/request.cr b/src/http/request.cr index 2fb7a04..0a25c43 100644 --- a/src/http/request.cr +++ b/src/http/request.cr @@ -7,8 +7,11 @@ class HTTP::Request getter headers getter body getter version + getter src + getter dst - def initialize(@method : String, @path, @headers = Headers.new : Headers, @body = nil, @version = "HTTP/1.1") + def initialize(@method : String, @path, @headers = Headers.new : Headers, @body = nil, @version = "HTTP/1.1", + @src = nil, @dst = nil) if body = @body @headers["Content-Length"] = body.bytesize.to_s elsif @method == "POST" || @method == "PUT" @@ -42,12 +45,14 @@ class HTTP::Request end def self.from_io(io) + src = io.peeraddr + dst = io.addr request_line = io.gets.not_nil! request_line =~ /\A(\w+)\s([^\s]+)\s(HTTP\/\d\.\d)\r?\n\Z/ method, path, http_version = $1, $2, $3 HTTP.parse_headers_and_body(io) do |headers, body| - return new method, path, headers, body, http_version + return new method, path, headers, body, http_version, src, dst end raise "unexpected end of http request"