コード例 #1
0
ファイル: SpdyHttpEncoder.java プロジェクト: huangweili/netty
  private SpdySynReplyFrame createSynReplyFrame(HttpResponse httpResponse) throws Exception {
    boolean chunked = httpResponse.isChunked();

    // Get the Stream-ID from the headers
    int streamID = SpdyHttpHeaders.getStreamID(httpResponse);
    SpdyHttpHeaders.removeStreamID(httpResponse);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-ENcoding
    // headers are not valid and MUST not be sent.
    httpResponse.removeHeader(HttpHeaders.Names.CONNECTION);
    httpResponse.removeHeader("Keep-Alive");
    httpResponse.removeHeader("Proxy-Connection");
    httpResponse.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);

    SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamID);

    // Unfold the first line of the response into name/value pairs
    SpdyHeaders.setStatus(spdyVersion, spdySynReplyFrame, httpResponse.getStatus());
    SpdyHeaders.setVersion(spdyVersion, spdySynReplyFrame, httpResponse.getProtocolVersion());

    // Transfer the remaining HTTP headers
    for (Map.Entry<String, String> entry : httpResponse.getHeaders()) {
      spdySynReplyFrame.addHeader(entry.getKey(), entry.getValue());
    }

    if (chunked) {
      currentStreamID = streamID;
      spdySynReplyFrame.setLast(false);
    } else {
      spdySynReplyFrame.setLast(httpResponse.getContent().readableBytes() == 0);
    }

    return spdySynReplyFrame;
  }
コード例 #2
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("Channel Read");
    if (msg instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) msg;

      System.err.println("STATUS: " + response.getStatus());
      System.err.println("VERSION: " + response.getProtocolVersion());
      System.err.println();

      if (!response.headers().isEmpty()) {
        for (String name : response.headers().names()) {
          for (String value : response.headers().getAll(name)) {
            System.err.println("HEADER: " + name + " = " + value);
          }
        }
        System.err.println();
      }
    }
    if (msg instanceof HttpContent) {
      HttpContent content = (HttpContent) msg;

      System.err.print(content.content().toString(CharsetUtil.UTF_8));
      System.err.flush();

      if (content instanceof LastHttpContent) {
        System.err.println("} END OF CONTENT");
        ctx.close();
      }
    }
  }
コード例 #3
0
  public void channelRead(final ChannelHandlerContext remoteChannelCtx, final Object msg)
      throws Exception {
    LoggerUtil.debug(
        logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Remote msg", msg);

    remainMsgCount++;

    if (remainMsgCount <= 5) {
      remoteChannelCtx.read();
    }

    HttpObject ho = (HttpObject) msg;

    if (ho instanceof HttpResponse) {
      HttpResponse httpResponse = (HttpResponse) ho;

      LoggerUtil.info(
          forwardRestLogger,
          uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
          httpResponse.getStatus(),
          httpResponse.getProtocolVersion());

      httpResponse.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
      httpResponse.headers().set("Proxy-Connection", HttpHeaders.Values.KEEP_ALIVE);
    }

    if (uaChannel.isActive()) {
      uaChannel
          .writeAndFlush(ho)
          .addListener(
              new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                  LoggerUtil.debug(
                      logger,
                      uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
                      "Write to UA finished: " + future.isSuccess());
                  if (future.isSuccess()) {
                    remainMsgCount--;
                    remoteChannelCtx.read();
                    LoggerUtil.debug(
                        logger,
                        uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
                        "Fire read again");
                  } else {
                    remoteChannelCtx.close();
                  }
                }
              });
    } else {
      remoteChannelCtx.close();
    }
  }
コード例 #4
0
  public NettyHttpResponse build() {
    if (isBuild.getAndSet(true)) {
      return content;
    }
    NettyHttpResponse response = new NettyHttpResponse();
    content = response;

    if (success) {
      response.setSuccess(true);
      response.setVersion(pendingResponse.getProtocolVersion());
      response.setStatus(pendingResponse.getStatus());
      response.setHeaders(pendingResponse.headers());
      response.setContents(pendingContents);
    } else {
      response.setCause(cause);
    }
    return content;
  }
コード例 #5
0
  @Override
  public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) msg;

      System.out.println("STATUS: " + response.getStatus());
      System.out.println("VERSION: " + response.getProtocolVersion());
      System.out.println();

      if (!response.headers().isEmpty()) {
        for (String name : response.headers().names()) {
          for (String value : response.headers().getAll(name)) {
            System.out.println("HEADER: " + name + " = " + value);
          }
        }
        System.out.println();
      }

      if (HttpHeaders.isTransferEncodingChunked(response)) {
        System.out.println("CHUNKED CONTENT {");
      } else {
        System.out.println("CONTENT {");
      }
    }
    if (msg instanceof HttpContent) {
      HttpContent content = (HttpContent) msg;

      System.out.print(content.content().toString(CharsetUtil.UTF_8));
      System.out.flush();

      if (content instanceof LastHttpContent) {
        System.out.println("} END OF CONTENT");
        queue.add(ctx.channel().newSucceededFuture());
      }
    }
  }
コード例 #6
0
ファイル: Fetcher.java プロジェクト: HyoungjunKim/tajo
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

      messageReceiveCount++;
      if (msg instanceof HttpResponse) {
        try {
          HttpResponse response = (HttpResponse) msg;

          StringBuilder sb = new StringBuilder();
          if (LOG.isDebugEnabled()) {
            sb.append("STATUS: ")
                .append(response.getStatus())
                .append(", VERSION: ")
                .append(response.getProtocolVersion())
                .append(", HEADER: ");
          }
          if (!response.headers().names().isEmpty()) {
            for (String name : response.headers().names()) {
              for (String value : response.headers().getAll(name)) {
                if (LOG.isDebugEnabled()) {
                  sb.append(name).append(" = ").append(value);
                }
                if (this.length == -1 && name.equals("Content-Length")) {
                  this.length = Long.parseLong(value);
                }
              }
            }
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug(sb.toString());
          }

          if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) {
            LOG.warn("There are no data corresponding to the request");
            length = 0;
            return;
          } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) {
            LOG.error(response.getStatus().reasonPhrase());
            state = TajoProtos.FetcherState.FETCH_FAILED;
            return;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }

      if (msg instanceof HttpContent) {
        try {
          HttpContent httpContent = (HttpContent) msg;
          ByteBuf content = httpContent.content();
          if (content.isReadable()) {
            content.readBytes(fc, content.readableBytes());
          }

          if (msg instanceof LastHttpContent) {
            if (raf != null) {
              fileLen = file.length();
            }

            finishTime = System.currentTimeMillis();
            if (state != TajoProtos.FetcherState.FETCH_FAILED) {
              state = TajoProtos.FetcherState.FETCH_FINISHED;
            }

            IOUtils.cleanup(LOG, fc, raf);
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }
    }
コード例 #7
0
 @Override
 public HttpVersion getProtocolVersion() {
   return response.getProtocolVersion();
 }
コード例 #8
0
 private static boolean isResponseWithCode(HttpResponse response, HttpResponseStatus status) {
   return HttpVersion.HTTP_1_1.equals(response.getProtocolVersion())
       && status.equals(response.getStatus());
 }