File tree Expand file tree Collapse file tree 3 files changed +55
-6
lines changed Expand file tree Collapse file tree 3 files changed +55
-6
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,12 @@ export interface AttachOptions {
3131 * @default 1000
3232 */
3333 destroyUpgradeTimeout ?: number ;
34+
35+ /**
36+ * Whether we should add a trailing slash to the request path.
37+ * @default true
38+ */
39+ addTrailingSlash ?: boolean ;
3440}
3541
3642export interface ServerOptions {
@@ -181,6 +187,22 @@ export abstract class BaseServer extends EventEmitter {
181187
182188 protected abstract init ( ) ;
183189
190+ /**
191+ * Compute the pathname of the requests that are handled by the server
192+ * @param options
193+ * @protected
194+ */
195+ protected _computePath ( options : AttachOptions ) {
196+ let path = ( options . path || "/engine.io" ) . replace ( / \/ $ / , "" ) ;
197+
198+ if ( options . addTrailingSlash !== false ) {
199+ // normalize path
200+ path += "/" ;
201+ }
202+
203+ return path ;
204+ }
205+
184206 /**
185207 * Returns a list of available transports for upgrade given a certain transport.
186208 *
@@ -635,14 +657,11 @@ export class Server extends BaseServer {
635657 * @api public
636658 */
637659 public attach ( server : HttpServer , options : AttachOptions = { } ) {
638- let path = ( options . path || "/engine.io" ) . replace ( / \/ $ / , "" ) ;
639-
660+ const path = this . _computePath ( options ) ;
640661 const destroyUpgradeTimeout = options . destroyUpgradeTimeout || 1000 ;
641662
642- // normalize path
643- path += "/" ;
644-
645663 function check ( req ) {
664+ // TODO use `path === new URL(...).pathname` in the next major release (ref: https://nodejs.org/api/url.html)
646665 return path === req . url . slice ( 0 , path . length ) ;
647666 }
648667
Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ export class uServer extends BaseServer {
6565 app /* : TemplatedApp */ ,
6666 options : AttachOptions & uOptions = { }
6767 ) {
68- const path = ( options . path || "/engine.io" ) . replace ( / \/ $ / , "" ) + "/" ;
68+ const path = this . _computePath ( options ) ;
6969 ( app as TemplatedApp )
7070 . any ( path , this . handleRequest . bind ( this ) )
7171 //
Original file line number Diff line number Diff line change @@ -745,6 +745,36 @@ describe("server", () => {
745745 } ) ;
746746 } ) ;
747747 } ) ;
748+
749+ it ( "should support requests without trailing slash" , done => {
750+ listen ( { addTrailingSlash : false } , port => {
751+ const partialDone = createPartialDone ( done , 2 ) ;
752+
753+ request
754+ . get ( `http://localhost:${ port } /engine.io` )
755+ . query ( { transport : "polling" } )
756+ . end ( ( err , res ) => {
757+ expect ( err ) . to . be ( null ) ;
758+ expect ( res . status ) . to . be ( 200 ) ;
759+ partialDone ( ) ;
760+ } ) ;
761+
762+ request
763+ . get ( `http://localhost:${ port } /engine.io/foo/bar/` )
764+ . query ( { transport : "polling" } )
765+ . end ( ( err , res ) => {
766+ if ( process . env . EIO_WS_ENGINE === "uws" ) {
767+ expect ( err ) . to . not . be ( null ) ;
768+ expect ( err . message ) . to . be ( "socket hang up" ) ;
769+ } else {
770+ expect ( err ) . to . be ( null ) ;
771+ // this should not work, but it is kept for backward-compatibility
772+ expect ( res . status ) . to . be ( 200 ) ;
773+ }
774+ partialDone ( ) ;
775+ } ) ;
776+ } ) ;
777+ } ) ;
748778 } ) ;
749779
750780 describe ( "close" , ( ) => {
You can’t perform that action at this time.
0 commit comments