|
| 1 | +package protocol |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/domainr/epp2/protocol/dataunit" |
| 5 | + "github.com/domainr/epp2/schema" |
| 6 | + "github.com/domainr/epp2/schema/epp" |
| 7 | +) |
| 8 | + |
| 9 | +// Writer is the interface implemented by any type that can write an EPP message body. |
| 10 | +type Writer interface { |
| 11 | + WriteEPP(epp.Body) error |
| 12 | +} |
| 13 | + |
| 14 | +type writerFunc func(body epp.Body) error |
| 15 | + |
| 16 | +func (f writerFunc) WriteEPP(body epp.Body) error { |
| 17 | + return f(body) |
| 18 | +} |
| 19 | + |
| 20 | +// Server is a low-level server for the Extensible Provisioning Protocol (EPP) |
| 21 | +// as defined in [RFC 5730]. A Server is safe to use from multiple goroutines. |
| 22 | +// |
| 23 | +// [RFC 5730]: https://datatracker.ietf.org/doc/rfc5730/ |
| 24 | +type Server interface { |
| 25 | + // ServeEPP provides an client EPP request and a mechanism to respond to the request. |
| 26 | + // It blocks until a response is received or the underlying connection is closed. |
| 27 | + // The returned [Writer] should only be used once. The returned Writer will always |
| 28 | + // be non-nil, so the caller can respond to a malformed client request. |
| 29 | + ServeEPP() (epp.Body, Writer, error) |
| 30 | + |
| 31 | + // Close closes the connection. |
| 32 | + Close() error |
| 33 | +} |
| 34 | + |
| 35 | +type server struct { |
| 36 | + server dataunit.Server |
| 37 | + coder coder |
| 38 | +} |
| 39 | + |
| 40 | +// Serve services conn as an EPP server, sending greeting as the initial <greeting> |
| 41 | +// message to the client. |
| 42 | +// EPP requests from the client will be decoded using [schemas.Schema] schemas. |
| 43 | +// If no schemas are provided, a set of reasonable defaults will be used. |
| 44 | +func Serve(conn dataunit.Conn, greeting epp.Body, schemas ...schema.Schema) (Server, error) { |
| 45 | + s := newServer(conn, schemas) |
| 46 | + // Send the initial <greeting> to the client. |
| 47 | + data, err := s.coder.marshalXML(greeting) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return s, conn.WriteDataUnit(data) |
| 52 | +} |
| 53 | + |
| 54 | +func newServer(conn dataunit.Conn, schemas schema.Schemas) *server { |
| 55 | + if len(schemas) == 0 { |
| 56 | + schemas = DefaultSchemas() |
| 57 | + } |
| 58 | + return &server{ |
| 59 | + server: dataunit.Server{Conn: conn}, |
| 60 | + coder: coder{schemas}, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Close closes the connection, interrupting any in-flight requests. |
| 65 | +func (s *server) Close() error { |
| 66 | + return s.server.Conn.Close() |
| 67 | +} |
| 68 | + |
| 69 | +func (s *server) ServeEPP() (epp.Body, Writer, error) { |
| 70 | + data, w, err := s.server.ServeDataUnit() |
| 71 | + f := writerFunc(func(body epp.Body) error { |
| 72 | + data, err := s.coder.marshalXML(body) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + return w.WriteDataUnit(data) |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return nil, f, err |
| 80 | + } |
| 81 | + body, err := s.coder.umarshalXML(data) |
| 82 | + return body, f, err |
| 83 | +} |
0 commit comments