TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.53k stars 2.58k forks source link

Set Response Headers when Server is denying the client? #1294

Open unordentlich opened 1 year ago

unordentlich commented 1 year ago

Describe what you would like to know or do I'm denying clients which aren't providing all neccessary information and would like to set a custom Response Header field right here, is there a way for that? So that I can provide an error message, what is missing :) image

Describe the solution you'd considered I tried to put a HttpStatusMessage in the Builder´, which didn't work 👀


@Override
    public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft,
                                                                       ClientHandshake request) throws InvalidDataException {
        ServerHandshakeBuilder builder = super
                .onWebsocketHandshakeReceivedAsServer(conn, draft, request);

        int success = 0;
        for (int i = 0; i <= 10; i++) {
            String key = Core.getConfiguration().config().getString("socket.authenticator." + i + ".key");
            String value = Core.getConfiguration().config().getString("socket.authenticator." + i + ".value");

            if (key == null || value == null) continue;

            if (!request.hasFieldValue(key)) continue;

            if (request.getFieldValue(key).equals(value)) {
                success++;
            }
        }
        if (success != 11) {
        // HERE I WANT TO SET A CUSTOM HTTP RESPONSE HEADER :)
            throw new InvalidDataException(CloseFrame.POLICY_VALIDATION, "Not accepted");
        }

        return builder;
    }