Created
June 27, 2021 11:44
-
-
Save nkalra0123/875e49bc966a769857359a721ed4071e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Response handleWebSocket(final IHTTPSession session) { | |
Map<String, String> headers = session.getHeaders(); | |
if (isWebsocketRequested(session)) { | |
if (!NanoWSD.HEADER_WEBSOCKET_VERSION_VALUE. | |
equalsIgnoreCase(headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION))) { | |
return Response. | |
newFixedLengthResponse(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, | |
"Invalid Websocket-Version " + headers.get(NanoWSD.HEADER_WEBSOCKET_VERSION)); | |
} | |
if (!headers.containsKey(NanoWSD.HEADER_WEBSOCKET_KEY)) { | |
return Response. | |
newFixedLengthResponse | |
(Status.BAD_REQUEST, NanoHTTPD.MIME_PLAINTEXT, "Missing Websocket-Key"); | |
} | |
WebSocket webSocket = openWebSocket(session); | |
Response handshakeResponse = webSocket.getHandshakeResponse(); | |
try { | |
handshakeResponse. | |
addHeader(NanoWSD.HEADER_WEBSOCKET_ACCEPT, | |
makeAcceptKey(headers.get(NanoWSD.HEADER_WEBSOCKET_KEY))); | |
} catch (NoSuchAlgorithmException e) { | |
return Response.newFixedLengthResponse(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, | |
"The SHA-1 Algorithm required for websockets is not available on the server."); | |
} | |
if (headers.containsKey(NanoWSD.HEADER_WEBSOCKET_PROTOCOL)) { | |
handshakeResponse. | |
addHeader(NanoWSD.HEADER_WEBSOCKET_PROTOCOL, | |
headers.get(NanoWSD.HEADER_WEBSOCKET_PROTOCOL).split(",")[0]); | |
} | |
return handshakeResponse; | |
} else { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment