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
15 changes: 11 additions & 4 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { NtlmClient } from 'axios-ntlm';
import { randomUUID } from 'crypto';
import debugBuilder from 'debug';
import { ReadStream } from 'fs';
import * as url from 'url';
import MIMEType from 'whatwg-mimetype';
import { gzipSync } from 'zlib';
import { IExOptions, IHeaders, IHttpClient, IOptions } from './types';
Expand All @@ -24,6 +23,13 @@ export interface IAttachment {
body: NodeJS.ReadableStream;
}

function getPortFromUrl(url: URL): string {
if (url.port) return url.port;
if (url.protocol.toLowerCase() === 'https:') return '443';
if (url.protocol.toLowerCase() === 'http:') return '80';
return '';
}

/**
* A class representing the http client
* @param {Object} [options] Options object. It allows the customization of
Expand All @@ -50,18 +56,19 @@ export class HttpClient implements IHttpClient {
* @returns {Object} The http request object for the `request` module
*/
public buildRequest(rurl: string, data: any, exheaders?: IHeaders, exoptions: IExOptions = {}): any {
const curl = url.parse(rurl);
const curl = new URL(rurl);
const method = data ? 'POST' : 'GET';

const host = curl.hostname;
const port = parseInt(curl.port, 10);

const port = getPortFromUrl(curl);
const headers: IHeaders = {
'User-Agent': 'node-soap/' + version,
'Accept': 'text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'none',
'Accept-Charset': 'utf-8',
...(exoptions.forever && { Connection: 'keep-alive' }),
'Host': host + (isNaN(port) ? '' : ':' + port),
'Host': host + (port ? ':' + port : ''),
};
const mergeOptions = ['headers'];

Expand Down
36 changes: 30 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

import { EventEmitter } from 'events';
import * as http from 'http';
import * as url from 'url';
import { IOneWayOptions, IServerOptions, IServices, ISoapFault, ISoapServiceMethod } from './types';
import { WSDL } from './wsdl';
import { BindingElement, IPort } from './wsdl/elements';
import zlib from 'zlib';

interface IExpressApp {
interface IExpressApp extends http.Server {
route;
use;
}

export type ServerType = http.Server | IExpressApp;

type Request = http.IncomingMessage & { body?: any };
type Response = http.ServerResponse;

Expand All @@ -35,6 +35,28 @@ function getDateString(d) {
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + 'Z';
}

function getServerBaseUrl(server: ServerType): string {
if (!server) {
return `http://localhost:8080`;
}

if (typeof server.address !== 'function') {
return `http://${server.address}`;
}

const address = server.address();

if (typeof address === 'string') {
return `http://${server.address}`;
}

if (address.family.toLowerCase() === 'ipv6') {
return `http://localhost:${address.port}`;
}

return `http://${address.address}:${address.port}`;
}

//eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface Server {
emit(event: 'request', request: any, methodName: string): boolean;
Expand Down Expand Up @@ -74,6 +96,7 @@ export class Server extends EventEmitter {
private enableChunkedEncoding: boolean;
private soapHeaders: any[];
private callback?: (err: any, res: any) => void;
private baseUrl: string;

constructor(server: ServerType, path: string | RegExp, services: IServices, wsdl: WSDL, options?: IServerOptions) {
super();
Expand All @@ -90,6 +113,7 @@ export class Server extends EventEmitter {
this.onewayOptions = (options && options.oneWay) || {};
this.enableChunkedEncoding = options.enableChunkedEncoding === undefined ? true : !!options.enableChunkedEncoding;
this.callback = options.callback ? options.callback : () => {};
this.baseUrl = getServerBaseUrl(server);
if (typeof path === 'string' && path[path.length - 1] !== '/') {
path += '/';
} else if (path instanceof RegExp && path.source[path.source.length - 1] !== '/') {
Expand Down Expand Up @@ -118,7 +142,7 @@ export class Server extends EventEmitter {
return;
}
}
let reqPath = url.parse(req.url).pathname;
let reqPath = new URL(req.url, this.baseUrl).pathname;
if (reqPath[reqPath.length - 1] !== '/') {
reqPath += '/';
}
Expand Down Expand Up @@ -225,7 +249,7 @@ export class Server extends EventEmitter {
}

private _requestListener(req: Request, res: Response) {
const reqParse = url.parse(req.url);
const reqParse = new URL(req.url, this.baseUrl);
const reqQuery = reqParse.search;

if (typeof this.log === 'function') {
Expand Down Expand Up @@ -283,7 +307,7 @@ export class Server extends EventEmitter {
}

private _process(input, req: Request, res: Response, cb: (result: any, statusCode?: number) => any) {
const pathname = url.parse(req.url).pathname.replace(/\/$/, '');
const pathname = new URL(req.url, this.baseUrl).pathname.replace(/\/$/, '');
const obj = this.wsdl.xmlToObject(input);
const body = obj.Body;
const headers = obj.Header;
Expand Down Expand Up @@ -327,7 +351,7 @@ export class Server extends EventEmitter {
for (name in ports) {
portName = name;
const port = ports[portName];
const portPathname = url.parse(port.location).pathname.replace(/\/$/, '');
const portPathname = new URL(port.location).pathname.replace(/\/$/, '');

if (typeof this.log === 'function') {
this.log('info', 'Trying ' + portName + ' from path ' + portPathname, req);
Expand Down
11 changes: 9 additions & 2 deletions src/wsdl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import * as _ from 'lodash';
import * as path from 'path';
import * as sax from 'sax';
import stripBom from 'strip-bom';
import * as url from 'url';
import { HttpClient } from '../http';
import { NamespaceContext } from '../nscontext';
import { IOptions } from '../types';
Expand Down Expand Up @@ -1225,7 +1224,15 @@ export class WSDL {
includePath = path.resolve(path.dirname(this.uri), include.location);
}
} else {
includePath = url.resolve(this.uri || '', include.location);
if (/^https?:/i.test(include.location)) {
includePath = include.location;
} else {
try {
includePath = new URL(this.uri || '', include.location).toString();
} catch {
includePath = include.location;
}
}
}

const options = Object.assign({}, this.options);
Expand Down
9 changes: 7 additions & 2 deletions test/client-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,13 @@ var fs = require('fs'),
client.MyOperation(
{},
function () {
assert.ok(client.lastRequestHeaders.Host.indexOf(':443') > -1);
done();
try {
assert.ok(client.lastRequestHeaders.Host.indexOf(':443') > -1);
done();
} catch (err) {
done(err);
throw err;
}
},
null,
{ 'test-header': 'test' },
Expand Down