Skip to content

Commit

Permalink
feat: add the "addTrailingSlash" option (#655)
Browse files Browse the repository at this point in the history
The "addTrailingSlash" option allows to control whether a trailing
slash is added to the path of the HTTP requests:

- true (default): "/engine.io/"
- false: "/engine.io"

Related: socketio/engine.io-client@21a6e12

Signed-off-by: iifawzi <iifawzie@gmail.com>
  • Loading branch information
iifawzi authored and darrachequesne committed Jan 10, 2023
1 parent 5e34722 commit d0fd474
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
29 changes: 24 additions & 5 deletions lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface AttachOptions {
* @default 1000
*/
destroyUpgradeTimeout?: number;

/**
* Whether we should add a trailing slash to the request path.
* @default true
*/
addTrailingSlash?: boolean;
}

export interface ServerOptions {
Expand Down Expand Up @@ -181,6 +187,22 @@ export abstract class BaseServer extends EventEmitter {

protected abstract init();

/**
* Compute the pathname of the requests that are handled by the server
* @param options
* @protected
*/
protected _computePath(options: AttachOptions) {
let path = (options.path || "/engine.io").replace(/\/$/, "");

if (options.addTrailingSlash !== false) {
// normalize path
path += "/";
}

return path;
}

/**
* Returns a list of available transports for upgrade given a certain transport.
*
Expand Down Expand Up @@ -635,14 +657,11 @@ export class Server extends BaseServer {
* @api public
*/
public attach(server: HttpServer, options: AttachOptions = {}) {
let path = (options.path || "/engine.io").replace(/\/$/, "");

const path = this._computePath(options);
const destroyUpgradeTimeout = options.destroyUpgradeTimeout || 1000;

// normalize path
path += "/";

function check(req) {
// TODO use `path === new URL(...).pathname` in the next major release (ref: https://nodejs.org/api/url.html)
return path === req.url.slice(0, path.length);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/userver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class uServer extends BaseServer {
app /* : TemplatedApp */,
options: AttachOptions & uOptions = {}
) {
const path = (options.path || "/engine.io").replace(/\/$/, "") + "/";
const path = this._computePath(options);
(app as TemplatedApp)
.any(path, this.handleRequest.bind(this))
//
Expand Down
30 changes: 30 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,36 @@ describe("server", () => {
});
});
});

it("should support requests without trailing slash", done => {
listen({ addTrailingSlash: false }, port => {
const partialDone = createPartialDone(done, 2);

request
.get(`http://localhost:${port}/engine.io`)
.query({ transport: "polling" })
.end((err, res) => {
expect(err).to.be(null);
expect(res.status).to.be(200);
partialDone();
});

request
.get(`http://localhost:${port}/engine.io/foo/bar/`)
.query({ transport: "polling" })
.end((err, res) => {
if (process.env.EIO_WS_ENGINE === "uws") {
expect(err).to.not.be(null);
expect(err.message).to.be("socket hang up");
} else {
expect(err).to.be(null);
// this should not work, but it is kept for backward-compatibility
expect(res.status).to.be(200);
}
partialDone();
});
});
});
});

describe("close", () => {
Expand Down

0 comments on commit d0fd474

Please sign in to comment.