public static HttpRequest createSendDataRequest(String host, String cookie, ChannelBuffer data) {
    HttpRequest request = createRequestTemplate(host, cookie, CLIENT_SEND_REQUEST_URI);
    request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Long.toString(data.readableBytes()));
    request.setContent(data);

    return request;
  }
  /**
   * Sends the opening request to the server:
   *
   * <pre>
   * GET /demo HTTP/1.1
   * Upgrade: WebSocket
   * Connection: Upgrade
   * Host: example.com
   * Origin: http://example.com
   * Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5
   * Sec-WebSocket-Key2: 12998 5 Y3 1  .P00
   *
   * ^n:ds[4U
   * </pre>
   *
   * @param ctx Channel context
   * @param channel Channel into which we can write our request
   */
  @Override
  public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) {
    // Make keys
    int spaces1 = createRandomNumber(1, 12);
    int spaces2 = createRandomNumber(1, 12);

    int max1 = Integer.MAX_VALUE / spaces1;
    int max2 = Integer.MAX_VALUE / spaces2;

    int number1 = createRandomNumber(0, max1);
    int number2 = createRandomNumber(0, max2);

    int product1 = number1 * spaces1;
    int product2 = number2 * spaces2;

    String key1 = Integer.toString(product1);
    String key2 = Integer.toString(product2);

    key1 = insertRandomCharacters(key1);
    key2 = insertRandomCharacters(key2);

    key1 = insertSpaces(key1, spaces1);
    key2 = insertSpaces(key2, spaces2);

    byte[] key3 = createRandomBytes(8);

    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(number1);
    byte[] number1Array = buffer.array();
    buffer = ByteBuffer.allocate(4);
    buffer.putInt(number2);
    byte[] number2Array = buffer.array();

    byte[] challenge = new byte[16];
    System.arraycopy(number1Array, 0, challenge, 0, 4);
    System.arraycopy(number2Array, 0, challenge, 4, 4);
    System.arraycopy(key3, 0, challenge, 8, 8);
    this.expectedChallengeResponseBytes = md5(challenge);

    // Get path
    URI wsURL = this.getWebSocketURL();
    String path = wsURL.getPath();
    if (wsURL.getQuery() != null && wsURL.getQuery().length() > 0) {
      path = wsURL.getPath() + "?" + wsURL.getQuery();
    }

    // Format request
    HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
    request.addHeader(Names.UPGRADE, Values.WEBSOCKET);
    request.addHeader(Names.CONNECTION, Values.UPGRADE);
    request.addHeader(Names.HOST, wsURL.getHost());
    request.addHeader(Names.ORIGIN, "http://" + wsURL.getHost());
    request.addHeader(Names.SEC_WEBSOCKET_KEY1, key1);
    request.addHeader(Names.SEC_WEBSOCKET_KEY2, key2);
    if (this.getSubProtocolRequest() != null && !this.getSubProtocolRequest().equals("")) {
      request.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.getSubProtocolRequest());
    }
    request.setContent(ChannelBuffers.copiedBuffer(key3));

    channel.write(request);

    ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket00FrameEncoder());
  }
 private static void setNoData(HttpRequest request) {
   request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0");
   request.setContent(null);
 }