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.

  • :clTRID - 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”

  • :old_server - Set to true to read and write frames in a way that is compatible with old EPP servers. Default is false.

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



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/epp/server.rb', line 19

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"
  @old_server = attributes[:old_server] || false
  @lang       = attributes[:lang] || 'en'
end

Instance Attribute Details

#clTRIDObject

Returns the value of attribute clTRID.



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

def clTRID
  @clTRID
end

#old_serverObject

Returns the value of attribute old_server.



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

def old_server
  @old_server
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

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

Instance Method Details

#close_connectionObject

Closes the connection to the EPP server.



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/epp/server.rb', line 143

def close_connection
  if defined?(@socket) and @socket.is_a?(OpenSSL::SSL::SSLSocket)
    @socket.close
    @socket = nil
  end
  
  if defined?(@connection) and @connection.is_a?(TCPSocket)
    @connection.close
    @connection = nil
  end
  
  return true if @connection.nil? and @socket.nil?
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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/epp/server.rb', line 161

def get_frame
   if @old_server
      data = ""
      first_char = @socket.read(1)
      
      if first_char.nil? and @socket.eof?
        raise SocketError.new("Connection closed by remote server")
      elsif first_char.nil?
        raise SocketError.new("Error reading frame from remote server")
      else
         data << first_char
         
         while char = @socket.read(1)
            data << char
            
            return data if data =~ %r|<\/epp>\n$|mi # at end
         end
      end
   else
      header = @socket.read(4)

      if header.nil? and @socket.eof?
        raise SocketError.new("Connection closed by remote server")
      elsif header.nil?
        raise SocketError.new("Error reading frame from remote server")
      else
        unpacked_header = header.unpack("N")
        length = unpacked_header[0]

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

#loginObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/epp/server.rb', line 58

def 
  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"
  })
  
  command = xml.root.add_element("command")
   = command.add_element("login")
  
  .add_element("clID").text = @tag
  .add_element("pw").text = @password
  
  options = .add_element("options")
  options.add_element("version").text = "1.0"
  options.add_element("lang").text = @lang
  
  services = .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"
  
  command.add_element("clTRID").text = @clTRID

  # Receive the login response
  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
end

#logoutObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/epp/server.rb', line 98

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"
  })
  
  command = xml.root.add_element("command")
   = command.add_element("logout")
  
  # Receive the logout response
  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 == 1500
    return true
  else
    raise EppErrorResponse.new(:xml => response, :code => result_code, :message => result_message)
  end
end

#open_connectionObject

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



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/epp/server.rb', line 128

def open_connection
  @connection = TCPSocket.new(@server, @port)
  @socket     = OpenSSL::SSL::SSLSocket.new(@connection)
  
  # Synchronously close the connection & socket
  @socket.sync_close
  
  # Connect
  @socket.connect
  
  # Get the initial frame
  get_frame
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.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/epp/server.rb', line 35

def request(xml)
  open_connection
  
  begin
    
    @response = send_request(xml)
  ensure
    logout unless @old_server
    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.



202
203
204
# File 'lib/epp/server.rb', line 202

def send_frame(xml)      
   @socket.write( @old_server ? (xml + "\r\n") : ([xml.size + 4].pack("N") + xml) )
end

#send_request(xml) ⇒ Object

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



53
54
55
56
# File 'lib/epp/server.rb', line 53

def send_request(xml)
  send_frame(xml)
  response = get_frame
end