Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unknown 404 #297

Open
wants to merge 2 commits into
base: rc-1.5.0
Choose a base branch
from
Open

Feature/unknown 404 #297

wants to merge 2 commits into from

Conversation

thanaParis
Copy link
Collaborator

From the OCPP 2.0.1 specification:

• If the CSMS does not recognize the Charging Station identifier in the URL path, it SHOULD send an HTTP response with status 404 and abort the WebSocket connection as described in [RFC6455].

Citrine previously always gave a 401, this pr gives a 404 when the charger is unknown and 500 when the server itself has an error.

/**
* See {@link IUpgradeError.terminateConnection}
**/
error?.terminateConnection?.(socket) || this._terminateConnectionInternalError(socket);
Copy link
Contributor

@elliot-sabitov elliot-sabitov Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt the left and the right of the || operator, always going to fire? I am seeing that error?.terminateConnection returns void, doesnt that mean that the right side will fire unintentionally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be cleaner to move:

terminateConnection(socket: Duplex): void {
        socket.write('HTTP/1.1 401 Unauthorized\r\n');
        socket.write(
            'WWW-Authenticate: Basic realm="Access to the WebSocket", charset="UTF-8"\r\n',
        );
        socket.write('\r\n');
        socket.end();
        socket.destroy();
    }

as a helper method along with this._terminateConnectionInternalError and instead maybe do something like:

if (error instanceOf UpgradeAuthenticationError) {
  this.terminateAuthenticationError();
} else {
  this.terminateConnectionError();
}

the two functions are more or less the same and it feels a bit odd that the socket handling is in the error

Copy link
Contributor

@elliot-sabitov elliot-sabitov Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see, there is also UpgradeUnknownError, but maybe there can be a handle helper function here like:

terminateConnectionHelper(socket: Duplex, str1 = 'HTTP/1.1 500 Internal Server Error', str2 = ''): void {
        socket.write('${str1}\r\n');
        socket.write(
            '${str2}\r\n',
        );
        socket.write('\r\n');
        socket.end();
        socket.destroy();
    }

and then just:

if (error instanceOf UpgradeAuthenticationError) {
  this.terminateConnectionHelper(socket, 'HTTP/1.1 401 Unauthorized', ''WWW-Authenticate: Basic realm="Access to the WebSocket", charset="UTF-8"');
} else if (error instanceOf UpgradeUnknownError) {
  this.terminateConnectionHelper(socket, 'HTTP/1.1 404 Not Found');
} else {
 this.terminateConnectionHelper(socket);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants