Exemple #1
0
  /**
   * Server-side handshake.
   *
   * @param request received handshake request.
   * @param extensionContext extension context.
   * @return created handshake.
   * @throws HandshakeException when there is problem with received {@link UpgradeRequest}.
   */
  static Handshake createServerHandshake(
      UpgradeRequest request, ExtendedExtension.ExtensionContext extensionContext)
      throws HandshakeException {
    final Handshake handshake = new Handshake();

    handshake.incomingRequest = request;
    handshake.extensionContext = extensionContext;
    checkForHeader(request.getHeader(UpgradeRequest.UPGRADE), UpgradeRequest.UPGRADE, "WebSocket");
    checkForHeader(
        request.getHeader(UpgradeRequest.CONNECTION),
        UpgradeRequest.CONNECTION,
        UpgradeRequest.UPGRADE);

    // TODO - trim?
    final String protocolHeader = request.getHeader(HandshakeRequest.SEC_WEBSOCKET_PROTOCOL);
    handshake.subProtocols =
        (protocolHeader == null
            ? Collections.<String>emptyList()
            : Arrays.asList(protocolHeader.split(",")));

    if (request.getHeader(UpgradeRequest.HOST) == null) {
      throw new HandshakeException(LocalizationMessages.HEADERS_MISSING());
    }

    //        final String queryString = request.getQueryString();
    //        if (queryString != null) {
    //            if (!queryString.isEmpty()) {
    //            }
    ////            Parameters queryParameters = new Parameters();
    ////            queryParameters.processParameters(queryString);
    ////            final Set<String> names = queryParameters.getParameterNames();
    ////            for (String name : names) {
    ////                queryParams.put(name, queryParameters.getParameterValues(name));
    ////            }
    //        }

    List<String> value = request.getHeaders().get(HandshakeRequest.SEC_WEBSOCKET_EXTENSIONS);
    if (value != null) {
      handshake.extensions = TyrusExtension.fromHeaders(value);
    }
    handshake.secKey =
        SecKey.generateServerKey(new SecKey(request.getHeader(HandshakeRequest.SEC_WEBSOCKET_KEY)));

    return handshake;
  }
Exemple #2
0
  /**
   * Client side only - validate server response.
   *
   * @param response response to be validated.
   * @throws HandshakeException when HTTP Status of received response is not 101 - Switching
   *     protocols.
   */
  public void validateServerResponse(UpgradeResponse response) throws HandshakeException {
    if (RESPONSE_CODE_VALUE != response.getStatus()) {
      throw new HandshakeException(
          response.getStatus(),
          LocalizationMessages.INVALID_RESPONSE_CODE(RESPONSE_CODE_VALUE, response.getStatus()));
    }

    checkForHeader(
        response.getFirstHeaderValue(UpgradeRequest.UPGRADE),
        UpgradeRequest.UPGRADE,
        UpgradeRequest.WEBSOCKET);
    checkForHeader(
        response.getFirstHeaderValue(UpgradeRequest.CONNECTION),
        UpgradeRequest.CONNECTION,
        UpgradeRequest.UPGRADE);

    //        if (!getSubProtocols().isEmpty()) {
    //            checkForHeader(response.getHeaders(), WebSocketEngine.SEC_WS_PROTOCOL_HEADER,
    //                           WebSocketEngine.SEC_WS_PROTOCOL_HEADER);
    //        }

    secKey.validateServerKey(response.getFirstHeaderValue(HandshakeResponse.SEC_WEBSOCKET_ACCEPT));
  }