Skip to content

Commit

Permalink
decrease complexity in public_ip
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg committed Jul 24, 2024
1 parent 77dd516 commit f395e3f
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions lib/sparoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def write_cache
def public_ip(host = "checkip.amazonaws.com", port = 80)
Socket.tcp(host, port, connect_timeout: 3) do |sock|
sock.sync = true
sock.print "GET / HTTP/1.1\r\nHost: #{host}:#{port}\r\nConnection: close\r\n\r\n"
sock.print "GET / HTTP/1.1\r\nHost: #{host}\r\nConnection: close\r\n\r\n"
status = sock.readline(chomp: true)
raise(ResolvError, "#{host}:#{port} response: #{status}") unless status.start_with? "HTTP/1.1 200 "

Expand All @@ -162,11 +162,7 @@ def public_ip(host = "checkip.amazonaws.com", port = 80)
content_length = m[1].to_i
end
end
ip = if content_length.zero?
sock.readline(chomp: true)
else
sock.read(content_length).chomp
end
ip = sock.read(content_length).chomp
Resolv::IPv4.create ip
end
end
Expand Down

0 comments on commit f395e3f

Please sign in to comment.