/*
 * Copyright 2011-2018 GatlingCorp (http://gatling.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.gatling.http.client.impl;

import io.gatling.http.client.HttpClientConfig;
import io.gatling.http.client.WebSocketListener;
import io.gatling.http.client.impl.request.WritableRequest;
import io.gatling.http.client.impl.request.WritableRequestBuilder;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.websocketx.*;

import java.io.IOException;

public class WebSocketHandler extends ChannelDuplexHandler {

  private static final IOException PREMATURE_CLOSE = new IOException("Premature close") {
    @Override
    public synchronized Throwable fillInStackTrace() {
      return this;
    }
  };

  private final HttpClientConfig config;
  private HttpTx tx;
  private WebSocketListener wsListener;
  private WebSocketClientHandshaker handshaker;

  WebSocketHandler(HttpClientConfig config) {
    this.config = config;
  }

  private void setActive(HttpTx tx) {
    this.tx = tx;
    wsListener = (WebSocketListener) tx.listener;
  }

  private void setInactive() {
    tx = null;
  }

  private boolean isInactive() {
    return tx == null || tx.requestTimeout.isDone();
  }

  private void crash(ChannelHandlerContext ctx, Throwable cause, boolean close) {
    if (isInactive()) {
      return;
    }

    wsListener.onThrowable(cause);
    tx.requestTimeout.cancel();
    setInactive();

    if (close) {
      ctx.close();
    }
  }

  @Override
  public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {

    if (msg instanceof HttpTx) {

      HttpTx tx = (HttpTx) msg;
      setActive(tx);

      if (tx.requestTimeout.isDone()) {
        setInactive();
        return;
      }

      try {
        WritableRequest request = WritableRequestBuilder.buildRequest(tx.request, ctx.alloc(), config);

        handshaker =
          WebSocketClientHandshakerFactory.newHandshaker(
            tx.request.getUri().toJavaNetURI(),
            WebSocketVersion.V13,
            null,
            true,
            request.getRequest().headers(),
            config.getWebSocketMaxFramePayloadLength());

        handshaker.handshake(ctx.channel());

      } catch (Exception e) {
        crash(ctx, e, true);
      }
    } else {
      // all other messages are CONNECT request and WebSocket frames
      ctx.write(msg, promise);
    }
  }

  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) {

    Channel ch = ctx.channel();
    if (!handshaker.isHandshakeComplete()) {
      if (isInactive()) {
        return;
      }

      try {
        // received 101 response
        FullHttpResponse response = (FullHttpResponse) msg;
        handshaker.finishHandshake(ch, response);
        tx.requestTimeout.cancel();

        wsListener.onHttpResponse(response.status(), response.headers());
        wsListener.openWebSocket(ch);

      } catch (WebSocketHandshakeException e) {
        System.out.println("WebSocket Client failed to connect");
        crash(ctx, e, true);
      }
      return;
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
      wsListener.onTextFrame((TextWebSocketFrame) frame);
    } else if (frame instanceof BinaryWebSocketFrame) {
      wsListener.onBinaryFrame((BinaryWebSocketFrame) frame);
    } else if (frame instanceof PongWebSocketFrame) {
      wsListener.onPongFrame((PongWebSocketFrame) frame);
    } else if (frame instanceof CloseWebSocketFrame) {
      wsListener.onCloseFrame((CloseWebSocketFrame) frame);
      ch.close();
    }
  }

  @Override
  public void channelInactive(ChannelHandlerContext ctx) {
    crash(ctx, PREMATURE_CLOSE, false);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    crash(ctx, cause, true);
  }
}

@@ -3657,74 +3657,8 @@
 ) %7B%0A
-        System.out.println(%22WebSocket Client failed to connect%22);%0A
     

Remove sysout