55
66import { EventEmitter } from 'events' ;
77import * as http from 'http' ;
8- import * as url from 'url' ;
98import { IOneWayOptions , IServerOptions , IServerlessRequest , IServerlessResponse , 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,32 @@ 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 ( address === null ) {
50+ return `http://localhost:8080` ;
51+ }
52+
53+ if ( typeof address === 'string' ) {
54+ return `http://${ address } ` ;
55+ }
56+
57+ if ( address . family . toLowerCase ( ) === 'ipv6' ) {
58+ return `http://localhost:${ address . port } ` ;
59+ }
60+
61+ return `http://${ address . address } :${ address . port } ` ;
62+ }
63+
3864//eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
3965export interface Server {
4066 emit ( event : 'request' , request : any , methodName : string ) : boolean ;
@@ -78,6 +104,7 @@ export class Server extends EventEmitter {
78104 private enableChunkedEncoding : boolean ;
79105 private soapHeaders : any [ ] ;
80106 private callback ?: ( err : any , res : any ) => void ;
107+ private baseUrl : string ;
81108
82109 constructor ( server : ServerType | null , path : string | RegExp , services : IServices , wsdl : WSDL , options ?: IServerOptions ) {
83110 super ( ) ;
@@ -94,6 +121,7 @@ export class Server extends EventEmitter {
94121 this . onewayOptions = ( options && options . oneWay ) || { } ;
95122 this . enableChunkedEncoding = options . enableChunkedEncoding === undefined ? true : ! ! options . enableChunkedEncoding ;
96123 this . callback = options . callback ? options . callback : ( ) => { } ;
124+ this . baseUrl = getServerBaseUrl ( server ) ;
97125 if ( typeof path === 'string' && path [ path . length - 1 ] !== '/' ) {
98126 path += '/' ;
99127 } else if ( path instanceof RegExp && path . source [ path . source . length - 1 ] !== '/' ) {
@@ -127,7 +155,7 @@ export class Server extends EventEmitter {
127155 return ;
128156 }
129157 }
130- let reqPath = url . parse ( req . url ) . pathname ;
158+ let reqPath = new URL ( req . url , this . baseUrl ) . pathname ;
131159 if ( reqPath [ reqPath . length - 1 ] !== '/' ) {
132160 reqPath += '/' ;
133161 }
@@ -285,7 +313,7 @@ export class Server extends EventEmitter {
285313 }
286314
287315 private _requestListener ( req : Request , res : Response ) {
288- const reqParse = url . parse ( req . url ) ;
316+ const reqParse = new URL ( req . url , this . baseUrl ) ;
289317 const reqQuery = reqParse . search ;
290318
291319 if ( typeof this . log === 'function' ) {
@@ -343,7 +371,7 @@ export class Server extends EventEmitter {
343371 }
344372
345373 private _process ( input , req : Request , res : Response , cb : ( result : any , statusCode ?: number ) => any ) {
346- const pathname = url . parse ( req . url ) . pathname . replace ( / \/ $ / , '' ) ;
374+ const pathname = new URL ( req . url , this . baseUrl ) . pathname . replace ( / \/ $ / , '' ) ;
347375 const obj = this . wsdl . xmlToObject ( input ) ;
348376 const body = obj . Body ? obj . Body : obj ;
349377 const headers = obj . Header ;
@@ -387,7 +415,7 @@ export class Server extends EventEmitter {
387415 for ( name in ports ) {
388416 portName = name ;
389417 const port = ports [ portName ] ;
390- const portPathname = url . parse ( port . location ) . pathname . replace ( / \/ $ / , '' ) ;
418+ const portPathname = new URL ( port . location , 'http://localhost' ) . pathname . replace ( / \/ $ / , '' ) ;
391419
392420 if ( typeof this . log === 'function' ) {
393421 this . log ( 'info' , 'Trying ' + portName + ' from path ' + portPathname , req ) ;
0 commit comments