File tree 3 files changed +55
-6
lines changed
3 files changed +55
-6
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,12 @@ export interface AttachOptions {
31
31
* @default 1000
32
32
*/
33
33
destroyUpgradeTimeout ?: number ;
34
+
35
+ /**
36
+ * Whether we should add a trailing slash to the request path.
37
+ * @default true
38
+ */
39
+ addTrailingSlash ?: boolean ;
34
40
}
35
41
36
42
export interface ServerOptions {
@@ -181,6 +187,22 @@ export abstract class BaseServer extends EventEmitter {
181
187
182
188
protected abstract init ( ) ;
183
189
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
+
184
206
/**
185
207
* Returns a list of available transports for upgrade given a certain transport.
186
208
*
@@ -635,14 +657,11 @@ export class Server extends BaseServer {
635
657
* @api public
636
658
*/
637
659
public attach ( server : HttpServer , options : AttachOptions = { } ) {
638
- let path = ( options . path || "/engine.io" ) . replace ( / \/ $ / , "" ) ;
639
-
660
+ const path = this . _computePath ( options ) ;
640
661
const destroyUpgradeTimeout = options . destroyUpgradeTimeout || 1000 ;
641
662
642
- // normalize path
643
- path += "/" ;
644
-
645
663
function check ( req ) {
664
+ // TODO use `path === new URL(...).pathname` in the next major release (ref: https://nodejs.org/api/url.html)
646
665
return path === req . url . slice ( 0 , path . length ) ;
647
666
}
648
667
Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ export class uServer extends BaseServer {
65
65
app /* : TemplatedApp */ ,
66
66
options : AttachOptions & uOptions = { }
67
67
) {
68
- const path = ( options . path || "/engine.io" ) . replace ( / \/ $ / , "" ) + "/" ;
68
+ const path = this . _computePath ( options ) ;
69
69
( app as TemplatedApp )
70
70
. any ( path , this . handleRequest . bind ( this ) )
71
71
//
Original file line number Diff line number Diff line change @@ -745,6 +745,36 @@ describe("server", () => {
745
745
} ) ;
746
746
} ) ;
747
747
} ) ;
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
+ } ) ;
748
778
} ) ;
749
779
750
780
describe ( "close" , ( ) => {
You can’t perform that action at this time.
0 commit comments