Doua Beri
Doua Beri

Reputation: 10949

jetty websockets only server - no http server - remove the 404 error

I've implemented a small web-sockets server using jetty, embedded in a larger java application. The server is running on 8081 port, and everything works ok(tested with a small client , in google chrome). However if I try to access the http://localhost:8081 (yes with http) I receive an 404 error, powered by jetty.

Is there a way in jetty to detect if the received format is based on websockets format and if not(in our case http) to just close the socket connection and never return the 404 error?

Thanks

Upvotes: 2

Views: 1627

Answers (2)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49462

what Tim is suggesting is what the WebSocketServlet already does. https://github.com/eclipse/jetty.project/blob/master/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketServlet.java

Just extend the WebSocketServlet and then use the various doGet, doPost, doPut methods for handling the HTTP requests. Leave the service() method alone to do what it needs to.

Upvotes: 0

Tim
Tim

Reputation: 6509

You can do this yourself, but I don't think there's anything packaged with Jetty to do it.

If you're using the WebSocketServlet, then you should override the service method, to be something like

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
  if(!"WebSocket".equals(request.getHeader("Upgrade")))
  {
     org.eclipse.jetty.server.Request jettyRequest =  org.eclipse.jetty.server.Request.getRequest(request);
     jettyRequest.getEndPoint().close();
     jettyRequest.setHandled(true);
  }
  else
  {
     super.service(request, response);
  }
}

If you've rolled your own handler, then you should be able to work out how to adapt that code to your needs.

Upvotes: 2

Related Questions