/
server.js
executable file
·232 lines (207 loc) · 7.22 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (c) 2011 Vinay Pulim <vinay@milewise.com>
* MIT Licensed
*/
function findKey(obj, val) {
for (var n in obj) if (obj[n] === val) return n;
}
var url = require('url'),
compress = null;
try { compress = require("compress"); } catch(e) {}
var Server = function(server, path, services, wsdl) {
var self = this;
this.path = path;
this.services = services;
this.wsdl = wsdl;
if (path[path.length-1] != '/') path += '/';
wsdl.onReady(function(err) {
var listeners = server.listeners('request').slice();
server.removeAllListeners('request');
server.addListener('request', function(req, res) {
if (typeof self.authorizeConnection === 'function') {
if (!self.authorizeConnection(req.connection.remoteAddress)) {
res.end();
return;
}
}
var reqPath = url.parse(req.url).pathname;
if (reqPath[reqPath.length-1] != '/') reqPath += '/';
if (path === reqPath) {
self._requestListener(req, res);
}
else {
for (var i = 0, len = listeners.length; i < len; i++){
listeners[i].call(this, req, res);
}
}
});
})
}
Server.prototype._requestListener = function(req, res) {
var self = this;
var reqParse = url.parse(req.url);
var reqPath = reqParse.pathname;
var reqQuery = reqParse.search;
self.log("info", "Handling "+req.method+" on "+req.url);
if (req.method === 'GET') {
if (reqQuery && reqQuery.toLowerCase() === '?wsdl') {
self.log("info", "Wants the WSDL");
res.setHeader("Content-Type", "application/xml");
res.write(self.wsdl.toXML());
}
res.end();
}
else if (req.method === 'POST') {
res.setHeader('Content-Type', req.headers['content-type']);
var chunks = [], gunzip;
if (compress && req.headers["content-encoding"] == "gzip") {
gunzip = new compress.Gunzip;
gunzip.init();
}
req.on('data', function(chunk) {
if (gunzip) chunk = gunzip.inflate(chunk, "binary");
chunks.push(chunk);
})
req.on('end', function() {
var xml = chunks.join(''), result;
if (gunzip) {
gunzip.end();
gunzip = null
}
try {
self._process(xml, req.url, function(result) {
res.write(result);
res.end();
if (typeof self.log === 'function') {
self.log("received", xml);
self.log("replied", result);
}
});
}
catch(err) {
err = err.stack || err;
res.write(err);
res.end();
if (typeof self.log === 'function') {
self.log("error", err);
}
}
});
}
else {
res.end();
}
}
Server.prototype._process = function(input, URL, callback) {
var self = this,
pathname = url.parse(URL).pathname.replace(/\/$/,''),
obj = this.wsdl.xmlToObject(input),
body = obj.Body,
bindings = this.wsdl.definitions.bindings, binding,
methods, method, methodName,
serviceName, portName;
if (typeof self.authenticate === 'function') {
if (obj.Header == null || obj.Header.Security == null) {
throw new Error('No security header');
}
if (!self.authenticate(obj.Header.Security)) {
throw new Error('Invalid username or password');
}
}
self.log("info", "Attempting to bind to "+pathname);
// use port.location and current url to find the right binding
binding = (function(self){
var services = self.wsdl.definitions.services;
var firstPort = undefined;
for(serviceName in services ) {
var service = services[serviceName];
var ports = service.ports;
for(portName in ports) {
var port = ports[portName];
var portPathname = url.parse(port.location).pathname.replace(/\/$/,'');
self.log("info", "Trying "+portName+" from path "+portPathname);
if(portPathname===pathname)
return port.binding;
// The port path is almost always wrong for genrated WSDLs
if(firstPort == undefined) {
firstPort = port;
}
}
}
return firstPort == undefined ? undefined : firstPort.binding;
})(this);
if (!binding) {
throw new Error('Failed to bind to WSDL');
}
methods = binding.methods;
if(binding.style === 'rpc') {
methodName = Object.keys(body)[0];
self._executeMethod({
serviceName: serviceName,
portName: portName,
methodName: methodName,
outputName: methodName + 'Response',
args: body[methodName],
style: 'rpc'
}, callback);
} else {
var messageElemName = Object.keys(body)[0];
var pair = binding.topElements[messageElemName];
self._executeMethod({
serviceName: serviceName,
portName: portName,
methodName: pair.methodName,
outputName: pair.outputName,
args: body[messageElemName],
style: 'document'
}, callback);
}
}
Server.prototype._executeMethod = function(options, callback) {
options = options || {};
var self = this,
method, body,
serviceName = options.serviceName,
portName = options.portName,
methodName = options.methodName,
outputName = options.outputName,
args = options.args,
style = options.style,
handled = false;
try {
method = this.services[serviceName][portName][methodName];
} catch(e) {
return callback(this._envelope(''));
}
function handleResult(result) {
if (handled) return;
handled = true;
if(style==='rpc') {
body = self.wsdl.objectToRpcXML(outputName, result, '', self.wsdl.definitions.$targetNamespace);
} else {
var element = self.wsdl.definitions.services[serviceName].ports[portName].binding.methods[methodName].output;
body = self.wsdl.objectToDocumentXML(outputName, result, element.targetNSAlias, element.targetNamespace);
}
callback(self._envelope(body));
}
var result = method(args, handleResult);
if (typeof result !== 'undefined') {
handleResult(result);
}
}
Server.prototype._envelope = function(body) {
var defs = this.wsdl.definitions,
ns = defs.$targetNamespace,
encoding = '',
alias = findKey(defs.xmlns, ns);
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +
encoding +
this.wsdl.xmlnsInEnvelope + '>' +
"<soap:Body>" +
body +
"</soap:Body>" +
"</soap:Envelope>";
return xml;
}
exports.Server = Server;