Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/parser/wsdl/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var WSDLElement = require('./wsdlElement');
var QName = require('../qname');
var namespaces = require('../helper').namespaces;

class Binding extends WSDLElement {
constructor(nsName, attrs, options) {
Expand All @@ -20,6 +21,11 @@ class Binding extends WSDLElement {
if (child.name === 'binding') {
this.transport = child.$transport;
this.style = child.$style;
if (child.nsURI === namespaces.soap) {
this.soapVersion = '1.1';
} else if (child.nsURI === namespaces.soap12) {
this.soapVersion = '1.2';
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/parser/xmlHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ class XMLHandler {
}
}

xmlToJson(nsContext, xml, descriptor) {
xmlToJson(nsContext, xml, descriptor, onRootElement) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this signature change to include a callback. May lead to argument bloat over time if we keep adding callbacks.

One potential alternative is a dedicated static helper to extract the root element:

// src/parser/xmlHandler.js
static parseRootQName(xml) {
  const parser = sax.parser(true, {xmlns: true});
  let root;
  parser.onopentag = function(node) {
    if (!root) root = new QName(node.uri, node.local, node.prefix);
  };
  parser.onerror = function() { parser.error = null; parser.resume(); };
  // Only feed until the root tag has been seen
  for (let i = 0; i < xml.length && !root; i += 1024) {
    parser.write(xml.slice(i, i + 1024));
  }
  return root;
}

Then it can be used as such:

// src/server.js _process
const root = XMLHandler.parseRootQName(input);
const soapVersion = root && root.name === 'Envelope' &&
  {[SOAP11_ENVELOPE]: '1.1', [SOAP12_ENVELOPE]: '1.2'}[root.nsURI];
const obj = this.xmlHandler.xmlToJson(null, input);

Not a big blocker for me, but to consider for long-term maintainability.

var self = this;
var p = sax.parser(true);
nsContext = nsContext || new NamespaceContext();
Expand Down Expand Up @@ -634,6 +634,9 @@ class XMLHandler {

var elementQName = QName.parse(nsName);
elementQName.nsURI = nsContext.getNamespaceURI(elementQName.prefix);
if (onRootElement && stack.length === 1) {
onRootElement(elementQName);
}

// SOAP href (#id)
if (attrs.href != null) {
Expand Down
88 changes: 49 additions & 39 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ try {
// Ignore error
}

const SOAP11_ENVELOPE = 'http://schemas.xmlsoap.org/soap/envelope/';
const SOAP12_ENVELOPE = 'http://www.w3.org/2003/05/soap-envelope';
Comment on lines +25 to +26

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the refactor. Can we go one step and get rid of these in favour of helper.namespaces.soap / .soap12?


class Server extends Base {

constructor(server, path, services, wsdl, options) {
Expand Down Expand Up @@ -137,15 +140,50 @@ class Server extends Base {
}
};

_findPort(pathname, soapVersion) {
const services = this.wsdl.definitions.services;
let firstPort;
let firstPathMatch;
for (const serviceName in services) {
const ports = services[serviceName].ports;
for (const portName in ports) {
const port = ports[portName];
const portPathname = url.parse(port.location).pathname.replace(/\/$/, '');
const match = {serviceName, portName, binding: port.binding};

if (typeof this.log === 'function') {
this.log('info', 'Trying ' + portName + ' from path ' + portPathname);
}

if (portPathname === pathname) {
if (!soapVersion || port.binding.soapVersion === soapVersion) {
return match;
}
firstPathMatch = firstPathMatch || match;
}
firstPort = firstPort || match;
}
}
// Generated WSDLs sometimes advertise a different path from the listener.
return firstPathMatch || firstPort;
}

_process(input, req, callback) {
let soapVersion;
const obj = this.xmlHandler.xmlToJson(null, input, null, function(root) {
if (root.name === 'Envelope') {
if (root.nsURI === SOAP11_ENVELOPE) {
soapVersion = '1.1';
} else if (root.nsURI === SOAP12_ENVELOPE) {
soapVersion = '1.2';
}
}
});
var self = this,
pathname = url.parse(req.url).pathname.replace(/\/$/, ''),
obj = this.xmlHandler.xmlToJson(null, input),
body = obj.Body,
headers = obj.Header,
bindings = this.wsdl.definitions.bindings, binding,
operation, operationName,
serviceName, portName,
operationName,
includeTimestamp = obj.Header && obj.Header.Security &&
obj.Header.Security.Timestamp;

Expand All @@ -162,39 +200,11 @@ class Server extends Base {
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;
var name;
for (name in services) {
serviceName = name;
var service = services[serviceName];
var ports = service.ports;
for (name in ports) {
portName = name;
var port = ports[portName];
var portPathname = url.parse(port.location).pathname.replace(/\/$/, '');

if (typeof self.log === 'function') {
self.log('info', 'Trying ' + portName + ' from path ' + portPathname);
}

if (portPathname === pathname)
return port.binding;

// The port path is almost always wrong for generated WSDLs
if (!firstPort) {
firstPort = port;
}
}
}
return !firstPort ? void 0 : firstPort.binding;
})(this);

if (!binding) {
const port = this._findPort(pathname, soapVersion);
if (!port) {
throw new Error(g.f('Failed to bind to {{WSDL}}'));
}
const {binding, serviceName, portName} = port;

try {
if (binding.style === 'rpc') {
Expand Down Expand Up @@ -310,11 +320,11 @@ class Server extends Base {
var outputBodyDescriptor = operationDescriptor.output.body;
debugDetail('Server handleResult. outputBodyDescriptor: %j ', outputBodyDescriptor);

var soapNsURI = 'http://schemas.xmlsoap.org/soap/envelope/';
var soapNsURI = SOAP11_ENVELOPE;
var soapNsPrefix = self.wsdl.options.envelopeKey || 'soap';

if (operation.soapVersion === '1.2') {
soapNsURI = 'http://www.w3.org/2003/05/soap-envelope';
soapNsURI = SOAP12_ENVELOPE;
}

debug('Server soapNsURI: %s soapNsPrefix: %s', soapNsURI, soapNsPrefix);
Expand Down Expand Up @@ -404,11 +414,11 @@ class Server extends Base {
var faultEnvDescriptor = operation.descriptor.faultEnvelope.elements[0];


var soapNsURI = 'http://schemas.xmlsoap.org/soap/envelope/';
var soapNsURI = SOAP11_ENVELOPE;
var soapNsPrefix = self.wsdl.options.envelopeKey || 'soap';

if (operation.soapVersion === '1.2') {
soapNsURI = 'http://www.w3.org/2003/05/soap-envelope';
soapNsURI = SOAP12_ENVELOPE;
}

var nsContext = self.createNamespaceContext(soapNsPrefix, soapNsURI);
Expand Down
57 changes: 57 additions & 0 deletions test/server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,63 @@ it('should return a valid error if the server stops responding': function(done)

});

describe('SOAP Server with shared SOAP 1.1 and SOAP 1.2 addresses', function() {
const wsdl = fs.readFileSync(__dirname + '/wsdl/strict/shared-soap-address.wsdl', 'utf8');
const service = {
StockQuoteService: {
Soap11Port: {
GetPrice: function() {
throw {Fault: {faultcode: 'soap:Server', faultstring: 'SOAP 1.1 fault'}};
}
},
Soap12Port: {
GetPrice: function() {
throw {Fault: {Code: {Value: 'soap:Receiver'}, Reason: {Text: 'SOAP 1.2 fault'}}};
}
}
}
};
let server;

before(function(done) {
server = http.createServer();
soap.listen(server, '/stockquote', service, wsdl);
server.listen(0, '127.0.0.1', done);
});

after(function(done) {
server.close(done);
});

for (const {version, contentType, namespace, fault} of [
{
version: '1.1',
contentType: 'text/xml',
namespace: 'http://schemas.xmlsoap.org/soap/envelope/',
fault: '<faultstring>SOAP 1.1 fault</faultstring>'
},
{
version: '1.2',
contentType: 'application/soap+xml',
namespace: 'http://www.w3.org/2003/05/soap-envelope',
fault: '<soap:Text>SOAP 1.2 fault</soap:Text>'
}
]) {
it('should dispatch SOAP ' + version + ' requests to their matching port', async function() {
const response = await fetch('http://127.0.0.1:' + server.address().port + '/stockquote', {
method: 'POST',
headers: {'Content-Type': contentType + '; charset=utf-8'},
body: '<Envelope xmlns="' + namespace + '"><Body>' +
'<GetPrice xmlns="urn:stockquote">ABC</GetPrice></Body></Envelope>'
});
const body = await response.text();
assert.strictEqual(response.status, 500);
assert.ok(body.includes('xmlns:soap="' + namespace + '"'), body);
assert.ok(body.includes(fault), body);
});
}
});

describe('SOAP Server Options', function() {
before(function(done) {
fs.readFile(__dirname + '/wsdl/strict/stockquote.wsdl', 'utf8', function(err, data) {
Expand Down
50 changes: 50 additions & 0 deletions test/wsdl/strict/shared-soap-address.wsdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="urn:stockquote"
targetNamespace="urn:stockquote">
<wsdl:types>
<xsd:schema targetNamespace="urn:stockquote">
<xsd:element name="GetPrice" type="xsd:string"/>
<xsd:element name="GetPriceResponse" type="xsd:float"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetPriceInput">
<wsdl:part name="body" element="tns:GetPrice"/>
</wsdl:message>
<wsdl:message name="GetPriceOutput">
<wsdl:part name="body" element="tns:GetPriceResponse"/>
</wsdl:message>
<wsdl:portType name="StockQuotePortType">
<wsdl:operation name="GetPrice">
<wsdl:input message="tns:GetPriceInput"/>
<wsdl:output message="tns:GetPriceOutput"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Soap11Binding" type="tns:StockQuotePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetPrice">
<soap:operation soapAction="urn:GetPrice"/>
<wsdl:input><soap:body use="literal"/></wsdl:input>
<wsdl:output><soap:body use="literal"/></wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="Soap12Binding" type="tns:StockQuotePortType">
<soap12:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetPrice">
<soap12:operation soapAction="urn:GetPrice"/>
<wsdl:input><soap12:body use="literal"/></wsdl:input>
<wsdl:output><soap12:body use="literal"/></wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="StockQuoteService">
<wsdl:port name="Soap11Port" binding="tns:Soap11Binding">
<soap:address location="http://localhost/stockquote"/>
</wsdl:port>
<wsdl:port name="Soap12Port" binding="tns:Soap12Binding">
<soap12:address location="http://localhost/stockquote"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>