Skip to content
Merged
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
40 changes: 34 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, IServerlessRequest, IServerlessResponse, 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;
}
Comment thread
w666 marked this conversation as resolved.

export type ServerType = http.Server | IExpressApp;

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

Expand All @@ -35,6 +35,32 @@ 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 (address === null) {
return `http://localhost:8080`;
}

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

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

return `http://${address.address}:${address.port}`;
Comment thread
w666 marked this conversation as resolved.
}

//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 @@ -78,6 +104,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 | null, path: string | RegExp, services: IServices, wsdl: WSDL, options?: IServerOptions) {
super();
Expand All @@ -94,6 +121,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);
Comment thread
w666 marked this conversation as resolved.
if (typeof path === 'string' && path[path.length - 1] !== '/') {
path += '/';
} else if (path instanceof RegExp && path.source[path.source.length - 1] !== '/') {
Expand Down Expand Up @@ -127,7 +155,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 @@ -285,7 +313,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 @@ -343,7 +371,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 ? obj.Body : obj;
const headers = obj.Header;
Expand Down Expand Up @@ -387,7 +415,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, 'http://localhost').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 @@ -11,7 +11,6 @@ import * as fs from 'fs';
import { isPlainObject, mergeWith } from '../utils';
import * as path from 'path';
import * as sax from 'sax';
import * as url from 'url';
import { HttpClient } from '../http';
import { NamespaceContext } from '../nscontext';
import { IOptions } from '../types';
Expand Down Expand Up @@ -1230,7 +1229,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(include.location, this.uri || '').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;
Comment thread
w666 marked this conversation as resolved.
}
Comment thread
w666 marked this conversation as resolved.
},
null,
{ 'test-header': 'test' },
Expand Down