Skip to content

Commit

Permalink
Removed dependency on active_support (not needed) and socket (include…
Browse files Browse the repository at this point in the history
…d in Ruby); Added random creation of transaction ID for <login> and <logout> transactions;
  • Loading branch information
Joshua Delsman authored and Joshua Delsman committed Jul 15, 2010
1 parent 5cc8a47 commit a8a2628
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ begin
# Dependencies
gem.add_development_dependency "shoulda"
gem.add_development_dependency "mocha"
gem.add_dependency "activesupport"
gem.add_dependency "hpricot"
end
rescue LoadError
Expand Down
3 changes: 1 addition & 2 deletions lib/epp.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Gem and other dependencies
require 'rubygems'
require 'openssl'
require 'socket'
require 'active_support'
require 'rexml/document'
require 'hpricot'
require 'uuidtools'

# Package files
require File.dirname(__FILE__) + '/require_parameters.rb'
Expand Down
55 changes: 26 additions & 29 deletions lib/epp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Server
include REXML
include RequiresParameters

attr_accessor :tag, :password, :server, :port, :services, :lang, :extensions, :version
attr_accessor :tag, :password, :server, :port, :lang, :services, :extensions, :version

# ==== Required Attrbiutes
#
Expand All @@ -14,7 +14,6 @@ class Server
# ==== Optional Attributes
#
# * <tt>:port</tt> - The EPP standard port is 700. However, you can choose a different port to use.
# * <tt>:clTRID</tt> - The client transaction identifier is an element that EPP specifies MAY be used to uniquely identify the command to the server. You are responsible for maintaining your own transaction identifier space to ensure uniqueness. Defaults to "ABC-12345"
# * <tt>:lang</tt> - Set custom language attribute. Default is 'en'.
# * <tt>:services</tt> - Use custom EPP services in the <login> frame. The defaults use the EPP standard domain, contact and host 1.0 services.
# * <tt>:extensions</tt> - URLs to custom extensions to standard EPP. Use these to extend the standard EPP (e.g., Nominet uses extensions). Defaults to none.
Expand All @@ -35,7 +34,7 @@ def initialize(attributes = {})
end

def new_epp_request
xml = Document.new
xml = Document.new
xml << XMLDecl.new("1.0", "UTF-8", "no")

xml.add_element("epp", {
Expand All @@ -59,10 +58,8 @@ def request(xml)
begin
@response = send_request(xml)
ensure
if @logged_in
@logged_in = false if logout
end

@logged_in = false if @logged_in && logout

close_connection
end

Expand Down Expand Up @@ -92,8 +89,9 @@ def open_connection

# Closes the connection to the EPP server.
def close_connection
@socket.close if @socket and not @socket.closed?
@socket.close if @socket and not @socket.closed?
@connection.close if @connection and not @connection.closed?

@socket = @connection = nil

return true
Expand All @@ -107,14 +105,14 @@ def get_frame
raise SocketError.new("Connection closed by remote server") if !@socket or @socket.eof?

header = @socket.read(4)

raise SocketError.new("Error reading frame from remote server") if header.nil?

length = header_size(header)

raise SocketError.new("Got bad frame header length of #{length} bytes from the server") if length < 5
response = @socket.read(length - 4)

return @socket.read(length - 4)
end

# Send an XML frame to the server. Should return the total byte
Expand All @@ -131,8 +129,7 @@ def packed(xml)

# Returns size of header of response from the EPP server.
def header_size(header)
unpacked_header = header.unpack("N")
unpacked_header[0]
header.unpack("N").first
end

private
Expand Down Expand Up @@ -164,37 +161,37 @@ def login
extensions_container.add_element("extURI").text = uri
end

command.add_element("clTRID").text = "ABC-12345"
command.add_element("clTRID").text = UUIDTools::UUID.timestamp_create.to_s

response = Hpricot.XML(send_request(xml.to_s))

result_message = (response/"epp"/"response"/"result"/"msg").text.strip
result_code = (response/"epp"/"response"/"result").attr("code").to_i

if result_code == 1000
return true
else
raise EppErrorResponse.new(:xml => response, :code => result_code, :message => result_message)
end
handle_response(response)
end

# Sends a standard logout request to the EPP server.
def logout
def logout
raise SocketError, "Socket must be opened before logging out" if !@socket or @socket.closed?

xml = new_epp_request

command = xml.root.add_element("command")
login = command.add_element("logout")

command.add_element("logout")
command.add_element("clTRID").text = UUIDTools::UUID.timestamp_create.to_s

response = Hpricot.XML(send_request(xml.to_s))

result_message = (response/"epp"/"response"/"result"/"msg").text.strip
result_code = (response/"epp"/"response"/"result").attr("code").to_i
handle_response(response, 1500)
end

def handle_response(response, acceptable_response = 1000)
result_code = (response/"epp"/"response"/"result").attr("code").to_i

if result_code == 1500
if result_code == acceptable_response
return true
else
result_message = (response/"epp"/"response"/"result"/"msg").text.strip

raise EppErrorResponse.new(:xml => response, :code => result_code, :message => result_message)
end
end
Expand Down
3 changes: 1 addition & 2 deletions test/test_epp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class EppTest < Test::Unit::TestCase
private

def prepare_socket!
@response = xml_file("test_request.xml")
@response = xml_file("test_response.xml")

TCPSocket.expects(:new).returns(@tcp_sock)
OpenSSL::SSL::SSLSocket.expects(:new).returns(@ssl_sock)
Expand All @@ -221,7 +221,6 @@ def prepare_socket!
@ssl_sock.expects(:read).with(4).returns("\000\000\003\r")
@ssl_sock.expects(:read).with(777).returns(@response)
@ssl_sock.stubs(:eof?)

end

def check_socket!
Expand Down

0 comments on commit a8a2628

Please sign in to comment.