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

conn.close() in server's onOpen method causes null pointer exception #621

Closed
peace-maker opened this issue Nov 22, 2017 · 3 comments · Fixed by #622
Closed

conn.close() in server's onOpen method causes null pointer exception #621

peace-maker opened this issue Nov 22, 2017 · 3 comments · Fixed by #622

Comments

@peace-maker
Copy link

peace-maker commented Nov 22, 2017

Expected Behavior

Closing the websocket connection in onOpen should be possible.

Current Behavior

Calling conn.close() in public void WebSocketServer::onOpen(WebSocket conn, ClientHandshake handshake) leads to a NullPointerException. The socket is still closed correctly, but an error is logged.

Steps to Reproduce (for bugs)

  1. Run a server which closes new connections right away in onOpen like below.
  2. Try to connect with any client.
import java.net.InetSocketAddress;

import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

public class TestWebSocketServer extends WebSocketServer {

	public TestWebSocketServer(int port) {
		super(new InetSocketAddress(port));
	}
	
	@Override
	public void onOpen(WebSocket conn, ClientHandshake handshake) {
		conn.close(CloseFrame.POLICY_VALIDATION, "Not authorized");
	}

	@Override
	public void onClose(WebSocket conn, int code, String reason, boolean remote) {
	}

	@Override
	public void onMessage(WebSocket conn, String message) {
	}

	@Override
	public void onError(WebSocket conn, Exception ex) {
	}

	@Override
	public void onStart() {
	}
	
	public static void main(String[] args) {
		TestWebSocketServer server = new TestWebSocketServer(9999);
		server.start();
	}
}

Debug log (for bugs)

Error while reading from remote connection: java.lang.NullPointerException
java.lang.NullPointerException
	at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:214)
	at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:881)

Context

I'm trying to implement an authentication scheme for my websocket service using a token which gets passed to the websocket server in a cookie during the handshake. Rejecting the handshake in onWebsocketHandshakeReceivedAsServer isn't an option for me, because I can't destinguish between the server rejecting the connection and the server being offline in the browser.
I need to wait until the connection is open to validate the token and send a custom closecode if it's invalid.

Your Environment

  • Version used: 1.3.6
  • Java version: 1.8.0_152
  • Operating System and version: Windows 10 x64
@marci4
Copy link
Collaborator

marci4 commented Nov 22, 2017

Hello @peace-maker,

thank you for your bug report!
I will take a look at it.

Greetings
marci4

@marci4 marci4 mentioned this issue Nov 22, 2017
8 tasks
marci4 added a commit that referenced this issue Nov 22, 2017
@marci4
Copy link
Collaborator

marci4 commented Nov 22, 2017

Hello @peace-maker,

I looked into it. The stacktrace is from a caught exception. Still this exception should not happen.

Commited a fix for this. Could you please check if this is fixed for you as well?

Greetings
marci4

Edit: sorry for the automatic close... Damn you github

@peace-maker
Copy link
Author

Hi @marci4,

works good now without any more log spamming.

Thank you!

@marci4 marci4 closed this as completed Nov 25, 2017
@marci4 marci4 added this to the Release 1.3.7 milestone Nov 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants