55
66import { EventEmitter } from 'events' ;
77import * as http from 'http' ;
8- import * as url from 'url' ;
98import { IOneWayOptions , IServerOptions , IServices , ISoapFault , ISoapServiceMethod } from './types' ;
109import { WSDL } from './wsdl' ;
1110import { BindingElement , IPort } from './wsdl/elements' ;
1211import zlib from 'zlib' ;
1312
14- interface IExpressApp {
13+ interface IExpressApp extends http . Server {
1514 route ;
1615 use ;
1716}
1817
1918export type ServerType = http . Server | IExpressApp ;
19+
2020type Request = http . IncomingMessage & { body ?: any } ;
2121type Response = http . ServerResponse ;
2222
@@ -35,6 +35,28 @@ function getDateString(d) {
3535 return d . getUTCFullYear ( ) + '-' + pad ( d . getUTCMonth ( ) + 1 ) + '-' + pad ( d . getUTCDate ( ) ) + 'T' + pad ( d . getUTCHours ( ) ) + ':' + pad ( d . getUTCMinutes ( ) ) + ':' + pad ( d . getUTCSeconds ( ) ) + 'Z' ;
3636}
3737
38+ function getServerBaseUrl ( server : ServerType ) : string {
39+ if ( ! server ) {
40+ return `http://localhost:8080` ;
41+ }
42+
43+ if ( typeof server . address !== 'function' ) {
44+ return `http://${ server . address } ` ;
45+ }
46+
47+ const address = server . address ( ) ;
48+
49+ if ( typeof address === 'string' ) {
50+ return `http://${ server . address } ` ;
51+ }
52+
53+ if ( address . family . toLowerCase ( ) === 'ipv6' ) {
54+ return `http://localhost:${ address . port } ` ;
55+ }
56+
57+ return `http://${ address . address } :${ address . port } ` ;
58+ }
59+
3860//eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
3961export interface Server {
4062 emit ( event : 'request' , request : any , methodName : string ) : boolean ;
@@ -74,6 +96,7 @@ export class Server extends EventEmitter {
7496 private enableChunkedEncoding : boolean ;
7597 private soapHeaders : any [ ] ;
7698 private callback ?: ( err : any , res : any ) => void ;
99+ private baseUrl : string ;
77100
78101 constructor ( server : ServerType , path : string | RegExp , services : IServices , wsdl : WSDL , options ?: IServerOptions ) {
79102 super ( ) ;
@@ -90,6 +113,7 @@ export class Server extends EventEmitter {
90113 this . onewayOptions = ( options && options . oneWay ) || { } ;
91114 this . enableChunkedEncoding = options . enableChunkedEncoding === undefined ? true : ! ! options . enableChunkedEncoding ;
92115 this . callback = options . callback ? options . callback : ( ) => { } ;
116+ this . baseUrl = getServerBaseUrl ( server ) ;
93117 if ( typeof path === 'string' && path [ path . length - 1 ] !== '/' ) {
94118 path += '/' ;
95119 } else if ( path instanceof RegExp && path . source [ path . source . length - 1 ] !== '/' ) {
@@ -118,7 +142,7 @@ export class Server extends EventEmitter {
118142 return ;
119143 }
120144 }
121- let reqPath = url . parse ( req . url ) . pathname ;
145+ let reqPath = new URL ( req . url , this . baseUrl ) . pathname ;
122146 if ( reqPath [ reqPath . length - 1 ] !== '/' ) {
123147 reqPath += '/' ;
124148 }
@@ -225,7 +249,7 @@ export class Server extends EventEmitter {
225249 }
226250
227251 private _requestListener ( req : Request , res : Response ) {
228- const reqParse = url . parse ( req . url ) ;
252+ const reqParse = new URL ( req . url , this . baseUrl ) ;
229253 const reqQuery = reqParse . search ;
230254
231255 if ( typeof this . log === 'function' ) {
@@ -283,7 +307,7 @@ export class Server extends EventEmitter {
283307 }
284308
285309 private _process ( input , req : Request , res : Response , cb : ( result : any , statusCode ?: number ) => any ) {
286- const pathname = url . parse ( req . url ) . pathname . replace ( / \/ $ / , '' ) ;
310+ const pathname = new URL ( req . url , this . baseUrl ) . pathname . replace ( / \/ $ / , '' ) ;
287311 const obj = this . wsdl . xmlToObject ( input ) ;
288312 const body = obj . Body ;
289313 const headers = obj . Header ;
@@ -327,7 +351,7 @@ export class Server extends EventEmitter {
327351 for ( name in ports ) {
328352 portName = name ;
329353 const port = ports [ portName ] ;
330- const portPathname = url . parse ( port . location ) . pathname . replace ( / \/ $ / , '' ) ;
354+ const portPathname = new URL ( port . location ) . pathname . replace ( / \/ $ / , '' ) ;
331355
332356 if ( typeof this . log === 'function' ) {
333357 this . log ( 'info' , 'Trying ' + portName + ' from path ' + portPathname , req ) ;
0 commit comments