Class: Epp::Server

Inherits:
Object
  • Object
show all
Includes:
RequiresParameters
Defined in:
lib/epp/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequiresParameters

#requires!

Constructor Details

#initialize(attributes = {}) ⇒ Server

Required Attrbiutes

  • :server - The EPP server to connect to

  • :tag - The tag or username used with <login> requests.

  • :password - The password used with <login> requests.

Optional Attributes

  • :port - The EPP standard port is 700. However, you can choose a different port to use.

  • :xmlns - The EPP xmlns value. Default is ‘urn:ietf:params:xml:ns:epp-1.0’.

  • :xmlns_xsi - The EPP xmlns:xsi value. Default is ‘www.w3.org/2001/XMLSchema-instance’.

  • :xsi_schema_location - The EPP xsi:schemaLocation value. Default is ‘urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd’.

  • :lang - Set custom language attribute. Default is ‘en’.

  • :services - Use custom EPP services in the <login> frame. The defaults use the EPP standard domain, contact and host 1.0 services.

  • :extensions - URLs to custom extensions to standard EPP. Use these to extend the standard EPP (e.g., Nominet uses extensions). Defaults to none.

  • :version - Set the EPP version. Defaults to “1.0”.

  • :cert - SSL Certificate.

  • :key - SSL Key.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/epp/server.rb', line 25

def initialize(attributes = {})
  requires!(attributes, :tag, :password, :server)

  @tag        = attributes[:tag]
  @password   = attributes[:password]
  @server     = attributes[:server]
  @port       = attributes[:port]       || 700
  @xmlns      = attributes[:xmlns]      || 'urn:ietf:params:xml:ns:epp-1.0'
  @xmlns_xsi  = attributes[:xmlns_xsi]  || 'http://www.w3.org/2001/XMLSchema-instance'
  @xsi_schema_location = attributes[:xsi_schema_location] || 'urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd'
  @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[:version]    || "1.0"
  @cert       = attributes[:cert]       || nil
  @key        = attributes[:key]        || nil

  @logged_in  = false
end

Instance Attribute Details

#certObject

Returns the value of attribute cert.



5
6
7
# File 'lib/epp/server.rb', line 5

def cert
  @cert
end

#extensionsObject

Returns the value of attribute extensions.



5
6
7
# File 'lib/epp/server.rb', line 5

def extensions
  @extensions
end

#keyObject

Returns the value of attribute key.



5
6
7
# File 'lib/epp/server.rb', line 5

def key
  @key
end

#langObject

Returns the value of attribute lang.



5
6
7
# File 'lib/epp/server.rb', line 5

def lang
  @lang
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/epp/server.rb', line 5

def password
  @password
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/epp/server.rb', line 5

def port
  @port
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/epp/server.rb', line 5

def server
  @server
end

#servicesObject

Returns the value of attribute services.



5
6
7
# File 'lib/epp/server.rb', line 5

def services
  @services
end

#tagObject

Returns the value of attribute tag.



5
6
7
# File 'lib/epp/server.rb', line 5

def tag
  @tag
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/epp/server.rb', line 5

def version
  @version
end

#xmlnsObject

Returns the value of attribute xmlns.



5
6
7
# File 'lib/epp/server.rb', line 5

def xmlns
  @xmlns
end

#xmlns_xsiObject

Returns the value of attribute xmlns_xsi.



5
6
7
# File 'lib/epp/server.rb', line 5

def xmlns_xsi
  @xmlns_xsi
end

#xsi_schema_locationObject

Returns the value of attribute xsi_schema_location.



5
6
7
# File 'lib/epp/server.rb', line 5

def xsi_schema_location
  @xsi_schema_location
end

Instance Method Details

#build_epp_request(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/epp/server.rb', line 45

def build_epp_request(&block)
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.epp(
      'xmlns' => xmlns,
      'xmlns:xsi' => xmlns_xsi,
      'xsi:schemaLocation' => xsi_schema_location
    ) do
      yield xml if block_given?
    end
  end
end

#close_connectionObject

Closes the connection to the EPP server.



103
104
105
106
107
108
109
110
# File 'lib/epp/server.rb', line 103

def close_connection
  @socket.close     if @socket and not @socket.closed?
  @connection.close if @connection and not @connection.closed?

  @socket = @connection = nil

  return true
end

#get_frameObject

Receive an EPP frame from the server. Since the connection is blocking, this method will wait until the connection becomes available for use. If the connection is broken, a SocketError will be raised. Otherwise, it will return a string containing the XML from the server.

Raises:

  • (SocketError)


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/epp/server.rb', line 116

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

  return @socket.read(length - 4)
end

#header_size(header) ⇒ Object

Returns size of header of response from the EPP server.



143
144
145
# File 'lib/epp/server.rb', line 143

def header_size(header)
  header.unpack("N").first
end

#open_connectionObject

Establishes the connection to the server. If the connection is established, then this method will call get_frame and return the EPP <greeting> frame which is sent by the server upon connection.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/epp/server.rb', line 88

def open_connection
  @connection = TCPSocket.new(server, port)
  @context = OpenSSL::SSL::SSLContext.new
  @context.cert = @cert
  @context.key = @key

  @socket = OpenSSL::SSL::SSLSocket.new(@connection, @context) if @connection

  @socket.sync_close = true
  @socket.connect

  get_frame
end

#packed(xml) ⇒ Object

Pack the XML as a header for the EPP server.



138
139
140
# File 'lib/epp/server.rb', line 138

def packed(xml)
  [xml.bytesize + 4].pack("N")
end

#request(xml) ⇒ Object

Sends an XML request to the EPP server, and receives an XML response. <login> and <logout> requests are also wrapped around the request, so we can close the socket immediately after the request is made.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/epp/server.rb', line 61

def request(xml)
  open_connection

  @logged_in = true if 

  begin
    @response = send_request(xml)
  ensure
    @logged_in = false if @logged_in && logout

    close_connection
  end

  return @response
end

#send_frame(xml) ⇒ Object

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.



133
134
135
# File 'lib/epp/server.rb', line 133

def send_frame(xml)
  @socket.write(packed(xml) + xml.bytes.pack('C*'))
end

#send_request(xml) ⇒ Object

Wrapper which sends an XML frame to the server, and receives the response frame in return.



79
80
81
82
# File 'lib/epp/server.rb', line 79

def send_request(xml)
  send_frame(xml)
  get_frame
end