forked from baalexander/node-xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
78 lines (69 loc) · 2.54 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var http = require('http')
, https = require('https')
, url = require('url')
, EventEmitter = require('events').EventEmitter
, Serializer = require('./serializer')
, Deserializer = require('./deserializer')
/**
* Creates a new Server object. Also creates an HTTP server to start listening
* for XML-RPC method calls. Will emit an event with the XML-RPC call's method
* name when receiving a method call.
*
* @constructor
* @param {Object|String} options - The HTTP server options. Either a URI string
* (e.g. 'http://localhost:9090') or an object
* with fields:
* - {String} host - (optional)
* - {Number} port
* @param {Boolean} isSecure - True if using https for making calls,
* otherwise false.
* @return {Server}
*/
function Server(options, isSecure, onListening) {
if (false === (this instanceof Server)) {
return new Server(options, isSecure)
}
onListening = onListening || function() {}
var that = this
// If a string URI is passed in, converts to URI fields
if (typeof options === 'string') {
options = url.parse(options)
options.host = options.hostname
options.path = options.pathname
}
function handleMethodCall(request, response) {
var deserializer = new Deserializer()
deserializer.deserializeMethodCall(request, function(error, methodName, params) {
if (that._events.hasOwnProperty(methodName)) {
that.emit(methodName, null, params, function(error, value) {
var xml = null
if (error !== null) {
xml = Serializer.serializeFault(error)
}
else {
xml = Serializer.serializeMethodResponse(value)
}
response.writeHead(200, {'Content-Type': 'text/xml'})
response.end(xml)
})
}
else {
that.emit('NotFound', methodName, params);
response.writeHead(404);
response.end();
}
})
}
this.httpServer = isSecure ? https.createServer(options, handleMethodCall)
: http.createServer(handleMethodCall)
process.nextTick(function() {
this.httpServer.listen(options.port, options.host, onListening)
}.bind(this))
this.close = function(callback) {
this.httpServer.once('close', callback)
this.httpServer.close()
}.bind(this)
}
// Inherit from EventEmitter to emit and listen
Server.prototype.__proto__ = EventEmitter.prototype
module.exports = Server