Skip to content

Commit

Permalink
Added ability to set custom services and extensions instead of using …
Browse files Browse the repository at this point in the history
…standard set of EPP services; added ability to set custom EPP version
  • Loading branch information
Josh Delsman committed Oct 19, 2009
1 parent 0d9454c commit 86b508f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
1 change: 0 additions & 1 deletion lib/epp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
require File.dirname(__FILE__) + '/epp/exceptions.rb'

module Epp #:nodoc:
VERSION = '1.0.8'
end
87 changes: 53 additions & 34 deletions lib/epp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Epp #:nodoc:
class Server
include RequiresParameters

attr_accessor :tag, :password, :server, :port, :clTRID, :old_server
attr_accessor :tag, :password, :server, :port, :old_server, :lang, :extensions, :version, :logged_in

# ==== Required Attrbiutes
#
Expand All @@ -14,18 +14,25 @@ class Server
#
# * <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>:old_server</tt> - Set to true to read and write frames in a way that is compatible with old EPP servers. Default is false.
# * <tt>:old_server</tt> - Set to true to read and write frames in a way that is compatible with older EPP servers. Default is false.
# * <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.
# * <tt>:version</tt> - Set the EPP version. Defaults to "1.0".
def initialize(attributes = {})
requires!(attributes, :tag, :password, :server)

@tag = attributes[:tag]
@password = attributes[:password]
@server = attributes[:server]
@port = attributes[:port] || 700
@clTRID = attributes[:clTRID] || "ABC-12345"
@port = attributes[:port] || 700
@old_server = attributes[:old_server] || false
@lang = attributes[:lang] || 'en'
@lang = attributes[:lang] || 'en'
@services = attributes[:services] || ["urn:ietf:params:xml:ns:domain-1.0", "urn:ietf:params:xml:ns:contact-1.0", "urn:ietf:params:xml:ns:host-1.0"]
@extensions = attributes[:extensions] || []
@version = attributes[:verison] || "1.0"

@logged_in = false
end

# Sends an XML request to the EPP server, and receives an XML response.
Expand All @@ -35,35 +42,26 @@ def initialize(attributes = {})
def request(xml)
open_connection

logged_in = true if login

begin
login
@response = send_request(xml)
ensure
logout unless @old_server
if @logged_in && !@old_server
logged_in = false if logout
end

close_connection
end

return @response
end

# private

# Wrapper which sends an XML frame to the server, and receives
# the response frame in return.
def send_request(xml)
send_frame(xml)
response = get_frame
end
private

# Sends a standard login request to the EPP server.
def login
xml = REXML::Document.new
xml << REXML::XMLDecl.new("1.0", "UTF-8", "no")

xml.add_element("epp", {
"xmlns" => "urn:ietf:params:xml:ns:epp-1.0",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" => "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
})
xml = new_epp_request

command = xml.root.add_element("command")
login = command.add_element("login")
Expand All @@ -72,14 +70,21 @@ def login
login.add_element("pw").text = @password

options = login.add_element("options")
options.add_element("version").text = "1.0"
options.add_element("version").text = @version
options.add_element("lang").text = @lang

services = login.add_element("svcs")
services.add_element("objURI").text = "urn:ietf:params:xml:ns:domain-1.0"
services.add_element("objURI").text = "urn:ietf:params:xml:ns:contact-1.0"
services.add_element("objURI").text = "urn:ietf:params:xml:ns:host-1.0"

# Include schema extensions for registrars which require it
extensions_container = services.add_element("svcExtension") unless @extensions.empty?

for uri in @extensions
extensions_container.add_element("extURI").text = uri
end

command.add_element("clTRID").text = @clTRID

# Receive the login response
Expand All @@ -95,15 +100,9 @@ def login
end
end

# Sends a standard logout request to the EPP server.
def logout
xml = REXML::Document.new
xml << REXML::XMLDecl.new("1.0", "UTF-8", "no")

xml.add_element('epp', {
'xmlns' => "urn:ietf:params:xml:ns:epp-1.0",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
})
xml = new_epp_request

command = xml.root.add_element("command")
login = command.add_element("logout")
Expand All @@ -121,6 +120,26 @@ def logout
end
end

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

xml.add_element("epp", {
"xmlns" => "urn:ietf:params:xml:ns:epp-1.0",
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"xsi:schemaLocation" => "urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
})

return xml
end

# Wrapper which sends an XML frame to the server, and receives
# the response frame in return.
def send_request(xml)
send_frame(xml)
get_frame
end

# Establishes the connection to the server. If the connection is
# established, then this method will call get_frame and return
# the EPP <tt><greeting></tt> frame which is sent by the
Expand Down Expand Up @@ -199,8 +218,8 @@ def get_frame
# Send an XML frame to the server. Should return the total byte
# size of the frame sent to the server. If the socket returns EOF,
# the connection has closed and a SocketError is raised.
def send_frame(xml)
@socket.write( @old_server ? (xml + "\r\n") : ([xml.size + 4].pack("N") + xml) )
def send_frame(xml)
@socket.write(@old_server ? (xml + "\r\n") : ([xml.size + 4].pack("N") + xml))
end
end
end

0 comments on commit 86b508f

Please sign in to comment.