Permalink
Browse files

Rewrite the way we handle socket internally

I will probably move some code into classes, instead of using namespaced_methods.

#query_handle exists because it's the best method to be hooked if you want to instrument and measure the queries. It sits just before the socket connection, can handle the errors and exposes the final list of args.
  • Loading branch information...
1 parent 19b931b commit 66538fc285cbd577d52b875b1ba485ff7d1cb97f @weppos committed Oct 16, 2012
View
27 lib/whois/server/adapters/base.rb
@@ -135,16 +135,14 @@ def request(string)
end
- private
+ private
# Store a record part in {#buffer}.
#
# @param [String] body
# @param [String] host
# @return [void]
#
- # @api public
- #
def buffer_append(body, host)
@buffer << Whois::Record::Part.new(:body => body, :host => host)
end
@@ -157,8 +155,7 @@ def buffer_start
result
end
- # @api public
- def query_the_socket(query, host, port = nil)
+ def query_prepare(query, host, port = nil)
args = []
args.push(host)
args.push(port || options[:port] || DEFAULT_WHOIS_PORT)
@@ -178,19 +175,31 @@ def query_the_socket(query, host, port = nil)
args.push(options[:bind_port]) if options[:bind_port]
end
- ask_the_socket(query, *args)
+ query_handle(query, *args)
+ end
+
+ alias :query_the_socket :query_prepare
+ # @api private
+ def query_handle(query, *args)
+ query_socket(query, *args)
rescue *RESCUABLE_CONNECTION_ERRORS => error
raise ConnectionError, "#{error.class}: #{error.message}"
end
- # This method handles the lowest connection
- # to the WHOIS server.
+ # Executes the low-level Socket connection.
+ #
+ # It opens the socket passing given +args+,
+ # sends the +query+ and reads the response.
#
# This is for internal use only!
#
+ # @param [String] query
+ # @param [Array] args
+ # @return [String]
+ #
# @api private
- def ask_the_socket(query, *args)
+ def query_socket(query, *args)
client = TCPSocket.new(*args)
client.write("#{query}\r\n") # I could use put(foo) and forget the \n
client.read # but write/read is more symmetric than puts/read
View
8 spec/integration_spec.rb
@@ -9,7 +9,7 @@
with_definitions do
Whois::Server.define(:tld, ".it", "whois.nic.it")
Whois::Server::Adapters::Standard.any_instance \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.it", "whois.nic.it", 43) \
.returns(response)
@@ -30,7 +30,7 @@
with_definitions do
Whois::Server.define(:tld, ".it", "whois.nic.it")
Whois::Server::Adapters::Standard.any_instance \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.it", "whois.nic.it", 43, "192.168.1.1", 3000) \
.returns(response)
@@ -45,7 +45,7 @@
with_definitions do
Whois::Server.define(:tld, ".it", "whois.nic.it")
Whois::Server::Adapters::Standard.any_instance \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.it", "whois.nic.it", 43, Whois::Server::Adapters::Base::DEFAULT_BIND_HOST, 3000) \
.returns(response)
@@ -60,7 +60,7 @@
with_definitions do
Whois::Server.define(:tld, ".it", "whois.nic.it")
Whois::Server::Adapters::Standard.any_instance.
- expects(:ask_the_socket).
+ expects(:query_socket).
with("example.it", "whois.example.com", 43).
returns(response)
View
6 spec/whois/server/adapters/afilias_spec.rb
@@ -13,7 +13,7 @@
it "returns the WHOIS record" do
response = "No match for DOMAIN.TEST."
expected = response
- @server.expects(:ask_the_socket).with("domain.test", "whois.afilias-grs.info", 43).returns(response)
+ @server.expects(:query_socket).with("domain.test", "whois.afilias-grs.info", 43).returns(response)
record = @server.query("domain.test")
record.to_s.should == expected
@@ -27,8 +27,8 @@
referral = File.read(fixture("referrals/afilias.bz.txt"))
response = "Match for DOMAIN.TEST."
expected = referral + "\n" + response
- @server.expects(:ask_the_socket).with("domain.test", "whois.afilias-grs.info", 43).returns(referral)
- @server.expects(:ask_the_socket).with("domain.test", "whois.belizenic.bz", 43).returns(response)
+ @server.expects(:query_socket).with("domain.test", "whois.afilias-grs.info", 43).returns(referral)
+ @server.expects(:query_socket).with("domain.test", "whois.belizenic.bz", 43).returns(response)
record = @server.query("domain.test")
record.to_s.should == expected
View
8 spec/whois/server/adapters/base_spec.rb
@@ -114,7 +114,7 @@
describe "#query_the_socket" do
[ Errno::ECONNRESET, Errno::EHOSTUNREACH, Errno::ECONNREFUSED, SocketError ].each do |error|
it "re-raises #{error} as Whois::ConnectionError" do
- klass.any_instance.expects(:ask_the_socket).raises(error)
+ klass.any_instance.expects(:query_socket).raises(error)
expect {
klass.new(*definition).send(:query_the_socket, "example.com", "whois.test")
}.to raise_error(Whois::ConnectionError, "#{error}: #{error.new.message}")
@@ -128,7 +128,7 @@
it "does not bind the WHOIS query" do
@base \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.test", "whois.test", 43)
@base.send(:query_the_socket, "example.test", "whois.test", 43)
@@ -142,7 +142,7 @@
it "binds the WHOIS query to given host and port" do
@base \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.test", "whois.test", 43, "192.168.1.1", 3000)
@base.send(:query_the_socket, "example.test", "whois.test", 43)
@@ -156,7 +156,7 @@
it "binds the WHOIS query to given port and defaults host" do
@base \
- .expects(:ask_the_socket) \
+ .expects(:query_socket) \
.with("example.test", "whois.test", 43, klass::DEFAULT_BIND_HOST, 3000)
@base.send(:query_the_socket, "example.test", "whois.test", 43)
View
8 spec/whois/server/adapters/formatted_spec.rb
@@ -12,7 +12,7 @@
response = "Whois Response"
expected = response
server = klass.new(*@definition)
- server.expects(:ask_the_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 43).returns(response)
+ server.expects(:query_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 43).returns(response)
record = server.query("domain.de")
record.to_s.should == expected
@@ -23,7 +23,7 @@
it "raises an error" do
lambda do
server = klass.new(*[:tld, ".de", "whois.denic.de", {}])
- server.expects(:ask_the_socket).never
+ server.expects(:query_socket).never
server.query("domain.de")
end.should raise_error(Whois::ServerError)
end
@@ -33,7 +33,7 @@
it "sends the request to given port" do
response = "Whois Response"
server = klass.new(:tld, ".de", "whois.denic.de", { :format => "-T dn,ace -C US-ASCII %s", :port => 20 })
- server.expects(:ask_the_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 20).returns(response)
+ server.expects(:query_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 20).returns(response)
server.query("domain.de")
end
@@ -44,7 +44,7 @@
response = "Whois Response"
server = klass.new(:tld, ".de", "whois.denic.de", { :format => "-T dn,ace -C US-ASCII %s" })
server.configure(:bind_host => "192.168.1.1", :bind_port => 3000)
- server.expects(:ask_the_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 43, "192.168.1.1", 3000).returns(response)
+ server.expects(:query_socket).with("-T dn,ace -C US-ASCII domain.de", "whois.denic.de", 43, "192.168.1.1", 3000).returns(response)
server.query("domain.de")
end
View
6 spec/whois/server/adapters/standard_spec.rb
@@ -12,7 +12,7 @@
response = "Whois Response"
expected = response
server = klass.new(*@definition)
- server.expects(:ask_the_socket).with("domain.test", "whois.test", 43).returns(response)
+ server.expects(:query_socket).with("domain.test", "whois.test", 43).returns(response)
record = server.query("domain.test")
record.to_s.should == expected
@@ -23,7 +23,7 @@
it "sends the request to given port" do
response = "Whois Response"
server = klass.new(:tld, ".test", "whois.test", { :port => 20 })
- server.expects(:ask_the_socket).with("domain.test", "whois.test", 20).returns(response)
+ server.expects(:query_socket).with("domain.test", "whois.test", 20).returns(response)
server.query("domain.test")
end
@@ -34,7 +34,7 @@
response = "Whois Response"
server = klass.new(:tld, ".test", "whois.test", { :port => 20 })
server.configure(:bind_host => "192.168.1.100", :bind_port => 3000)
- server.expects(:ask_the_socket).with("domain.test", "whois.test", 20, "192.168.1.100", 3000).returns(response)
+ server.expects(:query_socket).with("domain.test", "whois.test", 20, "192.168.1.100", 3000).returns(response)
server.query("domain.test")
end
View
18 spec/whois/server/adapters/verisign_spec.rb
@@ -13,7 +13,7 @@
it "returns the WHOIS record" do
response = "No match for DOMAIN.TEST."
expected = response
- @server.expects(:ask_the_socket).with("=domain.test", "whois.test", 43).returns(response)
+ @server.expects(:query_socket).with("=domain.test", "whois.test", 43).returns(response)
record = @server.query("domain.test")
record.to_s.should == expected
@@ -27,8 +27,8 @@
referral = File.read(fixture("referrals/crsnic.com.txt"))
response = "Match for DOMAIN.TEST."
expected = referral + "\n" + response
- @server.expects(:ask_the_socket).with("=domain.test", "whois.test", 43).returns(referral)
- @server.expects(:ask_the_socket).with("domain.test", "whois.markmonitor.com", 43).returns(response)
+ @server.expects(:query_socket).with("=domain.test", "whois.test", 43).returns(referral)
+ @server.expects(:query_socket).with("domain.test", "whois.markmonitor.com", 43).returns(response)
record = @server.query("domain.test")
record.to_s.should == expected
@@ -38,17 +38,17 @@
it "extracts the closest referral when multiple referrals" do
referral = File.read(fixture("referrals/crsnic.com_referral_multiple.txt"))
- @server.expects(:ask_the_socket).with("=domain.test", "whois.test", 43).returns(referral)
- @server.expects(:ask_the_socket).with("domain.test", "whois.markmonitor.com", 43).returns("")
+ @server.expects(:query_socket).with("=domain.test", "whois.test", 43).returns(referral)
+ @server.expects(:query_socket).with("domain.test", "whois.markmonitor.com", 43).returns("")
record = @server.query("domain.test")
record.parts.should have(2).parts
end
it "ignores referral when is not defined" do
referral = File.read(fixture("referrals/crsnic.com_referral_not_defined.txt"))
- @server.expects(:ask_the_socket).with("=domain.test", "whois.test", 43).returns(referral)
- @server.expects(:ask_the_socket).never
+ @server.expects(:query_socket).with("=domain.test", "whois.test", 43).returns(referral)
+ @server.expects(:query_socket).never
record = @server.query("domain.test")
record.parts.should have(1).part
@@ -58,8 +58,8 @@
# This is the case of vrsn-20100925-dnssecmonitor86.net
it "gracefully ignores referral when is missing" do
referral = File.read(fixture("referrals/crsnic.com_referral_missing.txt"))
- @server.expects(:ask_the_socket).with("=domain.test", "whois.test", 43).returns(referral)
- @server.expects(:ask_the_socket).never
+ @server.expects(:query_socket).with("=domain.test", "whois.test", 43).returns(referral)
+ @server.expects(:query_socket).never
record = @server.query("domain.test")
record.parts.should have(1).part

0 comments on commit 66538fc

Please sign in to comment.