io.netty.handler.codec.http.LastHttpContent Java Examples

The following examples show how to use io.netty.handler.codec.http.LastHttpContent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NettyClient.java    From ambry with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject in) {
  // Make sure that we increase refCnt because we are going to process it async. The other end has to release
  // after processing.
  responseParts.offer(ReferenceCountUtil.retain(in));
  if (in instanceof HttpResponse && in.decoderResult().isSuccess()) {
    isKeepAlive = HttpUtil.isKeepAlive((HttpResponse) in);
  } else if (in.decoderResult().isFailure()) {
    Throwable cause = in.decoderResult().cause();
    if (cause instanceof Exception) {
      exception = (Exception) cause;
    } else {
      exception =
          new Exception("Encountered Throwable when trying to decode response. Message: " + cause.getMessage());
    }
    invokeFutureAndCallback("CommunicationHandler::channelRead0 - decoder failure");
  }
  if (in instanceof LastHttpContent) {
    if (isKeepAlive) {
      invokeFutureAndCallback("CommunicationHandler::channelRead0 - last content");
    } else {
      // if not, the future will be invoked when the channel is closed.
      ctx.close();
    }
  }
}
 
Example #2
Source File: ServerResponseCaptureFilter.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    if (httpObject instanceof HttpResponse) {
        httpResponse = (HttpResponse) httpObject;
        captureContentEncoding(httpResponse);
    }

    if (httpObject instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) httpObject;

        storeResponseContent(httpContent);

        if (httpContent instanceof LastHttpContent) {
            LastHttpContent lastContent = (LastHttpContent) httpContent;
            captureTrailingHeaders(lastContent);

            captureFullResponseContents();
        }
    }

    return super.serverToProxyResponse(httpObject);
}
 
Example #3
Source File: FrontendIntegrationTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
 * @param contents the content of the response.
 * @param expectedContentLength the length of the contents in bytes.
 * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
 */
private ByteBuffer getContent(Queue<HttpObject> contents, long expectedContentLength) {
  ByteBuffer buffer = ByteBuffer.allocate((int) expectedContentLength);
  boolean endMarkerFound = false;
  for (HttpObject object : contents) {
    assertFalse("There should have been no more data after the end marker was found", endMarkerFound);
    HttpContent content = (HttpContent) object;
    buffer.put(content.content().nioBuffer());
    endMarkerFound = object instanceof LastHttpContent;
    content.release();
  }
  assertEquals("Content length did not match expected", expectedContentLength, buffer.position());
  assertTrue("End marker was not found", endMarkerFound);
  buffer.flip();
  return buffer;
}
 
Example #4
Source File: HttpDownloader.java    From netty-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
	try {
		if (msg instanceof HttpRequest) {
			initFileChannel();
		} else if (msg instanceof HttpContent) {
			if (fileChnl == null) {
				initFileChannel();
			}
			ByteBuf byteBuf = ((HttpContent) msg).content();
			writeBytesToFile(byteBuf);
		} else if (msg instanceof LastHttpContent) {
			if (fileChnl != null && outStream != null) {
				fileChnl.close();
				outStream.close();
			}
			ctx.close();
		}
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #5
Source File: OnvifCodec.java    From IpCamera with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void channelRead(@Nullable ChannelHandlerContext ctx, @Nullable Object msg) throws Exception {
    if (msg == null) {
        return;
    }
    try {
        if (msg instanceof HttpContent) {
            HttpContent content = (HttpContent) msg;
            incomingMessage += content.content().toString(CharsetUtil.UTF_8);
        }
        if (msg instanceof LastHttpContent) {
            onvifConnection.processReply(incomingMessage);
            incomingMessage = "";
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
 
Example #6
Source File: ServerResponseCaptureFilter.java    From Dream-Catcher with MIT License 6 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    if (httpObject instanceof HttpResponse) {
        httpResponse = (HttpResponse) httpObject;
        captureContentEncoding(httpResponse);
    }

    if (httpObject instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) httpObject;

        storeResponseContent(httpContent);

        if (httpContent instanceof LastHttpContent) {
            LastHttpContent lastContent = (LastHttpContent) httpContent;
            captureTrailingHeaders(lastContent);

            captureFullResponseContents();
        }
    }

    return super.serverToProxyResponse(httpObject);
}
 
Example #7
Source File: FallbackResponder.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
@Override
public void sendResponse(FullHttpRequest req, ChannelHandlerContext ctx) throws Exception {
   counter.inc();
   byte[] content = null;

   try(InputStream is = FallbackResponder.class.getClassLoader().getResourceAsStream(resource)) {
      content = IOUtils.toByteArray(is);
   }

   FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
   HttpHeaders.setContentLength(response, content.length);
   response.headers().set(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_XML_UTF_8.toString());
   response.content().writeBytes(content);
   ctx.write(response);
   ChannelFuture future = ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
   future.addListener(ChannelFutureListener.CLOSE);
}
 
Example #8
Source File: RequestContentDeserializerHandler.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Override
public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof LastHttpContent) {
        HttpProcessingState state = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        Endpoint<?> endpoint = state.getEndpointForExecution();
        RequestInfo reqInfo = state.getRequestInfo();
        // Don't bother trying to deserialize until we have an endpoint and the request content has fully arrived
        if (endpoint != null && reqInfo.isCompleteRequestWithAllChunks()) {
            // Setup the content deserializer if desired
            TypeReference<?> contentTypeRef = endpoint.requestContentType();
            if (contentTypeRef != null) {
                // A non-null TypeReference is available, so deserialization is possible. Retrieve the appropriate
                //      deserializer and setup the RequestInfo so that it can lazily deserialize when requested.
                ObjectMapper deserializer = endpoint.customRequestContentDeserializer(reqInfo);
                if (deserializer == null)
                    deserializer = defaultRequestContentDeserializer;

                //noinspection unchecked
                reqInfo.setupContentDeserializer(deserializer, contentTypeRef);
            }
        }
    }

    return PipelineContinuationBehavior.CONTINUE;
}
 
Example #9
Source File: HttpPostStandardRequestDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Initialized the internals from a new chunk
 *
 * @param content
 *            the new received chunk
 * @throws ErrorDataDecoderException
 *             if there is a problem with the charset decoding or other
 *             errors
 */
@Override
public HttpPostStandardRequestDecoder offer(HttpContent content) {
    checkDestroyed();

    // Maybe we should better not copy here for performance reasons but this will need
    // more care by the caller to release the content in a correct manner later
    // So maybe something to optimize on a later stage
    ByteBuf buf = content.content();
    if (undecodedChunk == null) {
        undecodedChunk = buf.copy();
    } else {
        undecodedChunk.writeBytes(buf);
    }
    if (content instanceof LastHttpContent) {
        isLastChunk = true;
    }
    parseBody();
    if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
        undecodedChunk.discardReadBytes();
    }
    return this;
}
 
Example #10
Source File: NettyResponseChannelTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a {@link HttpContent}. Checks state and echoes back the content.
 * @param httpContent the {@link HttpContent} that needs to be handled.
 * @throws Exception
 */
private void handleContent(HttpContent httpContent) throws Exception {
  if (request != null) {
    boolean isLast = httpContent instanceof LastHttpContent;
    ChannelWriteCallback callback = new ChannelWriteCallback(httpContent);
    // Retain it here since SimpleChannelInboundHandler would auto release it after the channelRead0.
    // And release it in the callback.
    callback.setFuture(restResponseChannel.write(httpContent.content().retain(), callback));
    writeCallbacksToVerify.add(callback);
    if (isLast) {
      restResponseChannel.onResponseComplete(null);
      assertFalse("Request channel is not closed", request.isOpen());
    }
  } else {
    throw new RestServiceException("Received data without a request", RestServiceErrorCode.InvalidRequestState);
  }
}
 
Example #11
Source File: HttpPostRequestEncoderTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataIsMultipleOfChunkSize2() throws Exception {
    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
            HttpMethod.POST, "http://localhost");
    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, true);
    int length = 7943;
    char[] array = new char[length];
    Arrays.fill(array, 'a');
    String longText = new String(array);
    encoder.addBodyAttribute("foo", longText);

    assertNotNull(encoder.finalizeRequest());

    checkNextChunkSize(encoder, 8080);

    HttpContent httpContent = encoder.readChunk((ByteBufAllocator) null);
    assertTrue("Expected LastHttpContent is not received", httpContent instanceof LastHttpContent);
    httpContent.release();

    assertTrue("Expected end of input is not receive", encoder.isEndOfInput());
}
 
Example #12
Source File: HttpProxyHandler.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean handleResponse(ChannelHandlerContext ctx, Object response) throws Exception {
    if (response instanceof HttpResponse) {
        if (status != null) {
            throw new ProxyConnectException(exceptionMessage("too many responses"));
        }
        status = ((HttpResponse) response).status();
    }

    boolean finished = response instanceof LastHttpContent;
    if (finished) {
        if (status == null) {
            throw new ProxyConnectException(exceptionMessage("missing response"));
        }
        if (status.code() != 200) {
            throw new ProxyConnectException(exceptionMessage("status: " + status));
        }
    }

    return finished;
}
 
Example #13
Source File: NettyMessageProcessorTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the provided {@code httpRequest} and verifies that the response is an echo of the {@code restMethod}.
 * @param channel the {@link EmbeddedChannel} to send the request over.
 * @param httpMethod the {@link HttpMethod} for the request.
 * @param restMethod the equivalent {@link RestMethod} for {@code httpMethod}. Used to check for correctness of
 *                   response.
 * @param isKeepAlive if the request needs to be keep-alive.
 * @throws IOException
 */
private void sendRequestCheckResponse(EmbeddedChannel channel, HttpMethod httpMethod, RestMethod restMethod,
    boolean isKeepAlive) throws IOException {
  long requestId = REQUEST_ID_GENERATOR.getAndIncrement();
  String uri = MockRestRequestService.ECHO_REST_METHOD + requestId;
  HttpRequest httpRequest = RestTestUtils.createRequest(httpMethod, uri, null);
  HttpUtil.setKeepAlive(httpRequest, isKeepAlive);
  channel.writeInbound(httpRequest);
  channel.writeInbound(new DefaultLastHttpContent());
  HttpResponse response = (HttpResponse) channel.readOutbound();
  assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
  // MockRestRequestService echoes the RestMethod + request id.
  String expectedResponse = restMethod.toString() + requestId;
  assertEquals("Unexpected content", expectedResponse,
      RestTestUtils.getContentString((HttpContent) channel.readOutbound()));
  assertTrue("End marker was expected", channel.readOutbound() instanceof LastHttpContent);
}
 
Example #14
Source File: DFHttpSvrRspWrap.java    From dfactor with MIT License 6 votes vote down vote up
@Override
public void send(){
	if(hasSend){
		return ;
	}
	hasSend = true;
	if(channel != null){
		DFTcpChannelWrap chWrap = (DFTcpChannelWrap) channel;
		chWrap.writeNoFlush(this);
		if(_bufContent != null){
			response.headers().add(HttpHeaderNames.CONTENT_LENGTH, _bufContent.readableBytes());
			chWrap.writeNoFlush(_bufContent);
		}
		channel.write(LastHttpContent.EMPTY_LAST_CONTENT);
	}
}
 
Example #15
Source File: HttpDownloadHandlerTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** Test that the handler correctly supports http error codes i.e. 404 (NOT FOUND). */
@Test
public void httpErrorsAreSupported() throws IOException {
  EmbeddedChannel ch = new EmbeddedChannel(new HttpDownloadHandler(null, ImmutableList.of()));
  ByteArrayOutputStream out = Mockito.spy(new ByteArrayOutputStream());
  DownloadCommand cmd = new DownloadCommand(CACHE_URI, true, DIGEST, out);
  ChannelPromise writePromise = ch.newPromise();
  ch.writeOneOutbound(cmd, writePromise);

  HttpResponse response =
      new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
  response.headers().set(HttpHeaders.HOST, "localhost");
  response.headers().set(HttpHeaders.CONTENT_LENGTH, 0);
  response.headers().set(HttpHeaders.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
  ch.writeInbound(response);
  ch.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT);
  assertThat(writePromise.isDone()).isTrue();
  assertThat(writePromise.cause()).isInstanceOf(HttpException.class);
  assertThat(((HttpException) writePromise.cause()).response().status())
      .isEqualTo(HttpResponseStatus.NOT_FOUND);
  // No data should have been written to the OutputStream and it should have been closed.
  assertThat(out.size()).isEqualTo(0);
  // The caller is responsible for closing the stream.
  verify(out, never()).close();
  assertThat(ch.isOpen()).isTrue();
}
 
Example #16
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDataEndAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    LastHttpContent end = new DefaultLastHttpContent(hello, true);
    assertTrue(ch.writeOutbound(end));

    Http2DataFrame dataFrame = ch.readOutbound();
    try {
        assertThat(dataFrame.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertTrue(dataFrame.isEndStream());
    } finally {
        dataFrame.release();
    }

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example #17
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeEmptyEndAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    LastHttpContent end = LastHttpContent.EMPTY_LAST_CONTENT;
    assertTrue(ch.writeOutbound(end));

    Http2DataFrame emptyFrame = ch.readOutbound();
    try {
        assertThat(emptyFrame.content().readableBytes(), is(0));
        assertTrue(emptyFrame.isEndStream());
    } finally {
        emptyFrame.release();
    }

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example #18
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDowngradeEndData() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    assertTrue(ch.writeInbound(new DefaultHttp2DataFrame(hello, true)));

    LastHttpContent content = ch.readInbound();
    try {
        assertThat(content.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertTrue(content.trailingHeaders().isEmpty());
    } finally {
        content.release();
    }

    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example #19
Source File: ConfigHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldDecodeAuthFailureBucketConfigResponse() throws Exception {
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
        new HttpResponseStatus(401, "Unauthorized"));
    HttpContent responseChunk = LastHttpContent.EMPTY_LAST_CONTENT;

    BucketConfigRequest requestMock = mock(BucketConfigRequest.class);
    requestQueue.add(requestMock);
    channel.writeInbound(responseHeader, responseChunk);

    assertEquals(1, eventSink.responseEvents().size());
    BucketConfigResponse event = (BucketConfigResponse) eventSink.responseEvents().get(0).getMessage();

    assertEquals(ResponseStatus.ACCESS_ERROR, event.status());
    assertEquals("Unauthorized", event.config());
    assertTrue(requestQueue.isEmpty());
}
 
Example #20
Source File: RequestFilterHandlerTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Before
public void beforeMethod() {
    channelMock = mock(Channel.class);
    ctxMock = mock(ChannelHandlerContext.class);
    stateAttributeMock = mock(Attribute.class);
    state = new HttpProcessingState();

    doReturn(channelMock).when(ctxMock).channel();
    doReturn(stateAttributeMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(state).when(stateAttributeMock).get();

    firstChunkMsgMock = mock(HttpRequest.class);
    lastChunkMsgMock = mock(LastHttpContent.class);

    filter1Mock = mock(RequestAndResponseFilter.class);
    filter2Mock = mock(RequestAndResponseFilter.class);
    filtersList = Arrays.asList(filter1Mock, filter2Mock);

    handlerSpy = spy(new RequestFilterHandler(filtersList));

    requestInfoMock = mock(RequestInfo.class);

    state.setRequestInfo(requestInfoMock);
}
 
Example #21
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void addContentChunk_adds_last_chunk_trailing_headers() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
    String headerKey = UUID.randomUUID().toString();
    List<String> headerVal = Arrays.asList(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    lastChunk.trailingHeaders().add(headerKey, headerVal);

    // when
    requestInfo.addContentChunk(lastChunk);

    // then
    assertThat(requestInfo.trailingHeaders.names().size(), is(1));
    assertThat(requestInfo.trailingHeaders.getAll(headerKey), is(headerVal));
}
 
Example #22
Source File: HttpWsServer.java    From zbus-server with MIT License 6 votes vote down vote up
private void handleUploadMessage(io.netty.handler.codec.http.HttpMessage httpMsg, Message uploadMessage) throws IOException{
	if (httpMsg instanceof HttpContent) { 
           HttpContent chunk = (HttpContent) httpMsg;
           decoder.offer(chunk); 
           try {
               while (decoder.hasNext()) {
                   InterfaceHttpData data = decoder.next();
                   if (data != null) {
                       try { 
                       	handleUploadFile(data, uploadMessage);
                       } finally {
                           data.release();
                       }
                   }
               }
           } catch (EndOfDataDecoderException e1) { 
           	//ignore
           }
           
           if (chunk instanceof LastHttpContent) {  
           	resetUpload();
           }
       }
}
 
Example #23
Source File: CleartextHttp2ServerUpgradeHandlerTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void downgrade() throws Exception {
    setUpServerChannel();

    String requestString = "GET / HTTP/1.1\r\n" +
                     "Host: example.com\r\n\r\n";
    ByteBuf inbound = Unpooled.buffer().writeBytes(requestString.getBytes(CharsetUtil.US_ASCII));

    assertTrue(channel.writeInbound(inbound));

    Object firstInbound = channel.readInbound();
    assertTrue(firstInbound instanceof HttpRequest);
    HttpRequest request = (HttpRequest) firstInbound;
    assertEquals(HttpMethod.GET, request.method());
    assertEquals("/", request.uri());
    assertEquals(HttpVersion.HTTP_1_1, request.protocolVersion());
    assertEquals(new DefaultHttpHeaders().add("Host", "example.com"), request.headers());

    ((LastHttpContent) channel.readInbound()).release();

    assertNull(channel.readInbound());
}
 
Example #24
Source File: RequestsToOriginMetricsCollector.java    From styx with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    //
    // Break out LiveHttpResponse and and LastHttpContent handling in separate
    // blocks. This way it doesn't require an HttpObjectAggregator in the
    // pipeline.
    //
    if (msg instanceof HttpResponse) {
        HttpResponse resp = (HttpResponse) msg;

        int code = resp.getStatus().code();
        updateHttpResponseCounters(code);
    }

    if (msg instanceof HttpContent && !firstContentChunkReceived) {
        stopAndRecordTimeToFirstByte();
        firstContentChunkReceived = true;
    }

    if (msg instanceof LastHttpContent) {
        stopAndRecordLatency();
    }

    super.channelRead(ctx, msg);
}
 
Example #25
Source File: GZipResponseFilter.java    From zuul with Apache License 2.0 5 votes vote down vote up
@Override
public HttpContent processContentChunk(ZuulMessage resp, HttpContent chunk) {
    final Gzipper gzipper = (Gzipper) resp.getContext().get(CommonContextKeys.GZIPPER);
    gzipper.write(chunk);
    if (chunk instanceof LastHttpContent) {
        gzipper.finish();
        return new DefaultLastHttpContent(gzipper.getByteBuf());
    } else {
        return new DefaultHttpContent(gzipper.getByteBuf());
    }
}
 
Example #26
Source File: BadClientSilencer.java    From redant with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) {
    // This handler is the last inbound handler.
    // This means msg has not been handled by any previous handler.
    ctx.close();

    if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
        onUnknownMessage(msg);
    }
}
 
Example #27
Source File: RequestStateCleanerHandlerTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeMethod() {
    stateMock = mock(HttpProcessingState.class);
    proxyRouterProcessingStateMock = mock(ProxyRouterProcessingState.class);
    ctxMock = mock(ChannelHandlerContext.class);
    channelMock = mock(Channel.class);
    pipelineMock = mock(ChannelPipeline.class);
    stateAttrMock = mock(Attribute.class);
    proxyRouterProcessingStateAttrMock = mock(Attribute.class);
    metricsListenerMock = mock(MetricsListener.class);
    distributedTracingConfigMock = mock(DistributedTracingConfig.class);
    msgMockFirstChunkOnly = mock(HttpRequest.class);
    msgMockFullRequest = mock(FullHttpRequest.class);
    msgMockLastChunkOnly = mock(LastHttpContent.class);
    idleChannelTimeoutHandlerMock = mock(IdleChannelTimeoutHandler.class);

    doReturn(channelMock).when(ctxMock).channel();
    doReturn(pipelineMock).when(ctxMock).pipeline();
    doReturn(idleChannelTimeoutHandlerMock).when(pipelineMock).get(IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
    doReturn(stateAttrMock).when(channelMock).attr(ChannelAttributes.HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(stateMock).when(stateAttrMock).get();
    doReturn(proxyRouterProcessingStateAttrMock).when(channelMock).attr(ChannelAttributes.PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY);
    doReturn(proxyRouterProcessingStateMock).when(proxyRouterProcessingStateAttrMock).get();

    handler = new RequestStateCleanerHandler(
        metricsListenerMock, incompleteHttpCallTimeoutMillis, distributedTracingConfigMock
    );
}
 
Example #28
Source File: HTTPServer.java    From tchannel-java with MIT License 5 votes vote down vote up
@Override
protected void channelRead0(
        ChannelHandlerContext channelHandlerContext,
        FullHttpRequest fullHttpRequest
) throws Exception {

    logger.warn("Received {}", fullHttpRequest);

    if (HttpUtil.is100ContinueExpected(fullHttpRequest)) {
        channelHandlerContext.writeAndFlush(
                new DefaultFullHttpResponse(
                        HttpVersion.HTTP_1_1,
                        HttpResponseStatus.CONTINUE));
    }

    HttpResponse response = new DefaultHttpResponse(
            fullHttpRequest.protocolVersion(),
            HttpResponseStatus.OK);
    HttpHeaders headers = response.headers();
    headers.set(CONTENT_TYPE, "text/html; charset=UTF-8");

    ByteBuf responseContent = Unpooled.copiedBuffer("OK", CharsetUtil.UTF_8);

    boolean keepAlive = HttpUtil.isKeepAlive(fullHttpRequest);
    if (keepAlive) {
        headers.set(CONTENT_LENGTH, responseContent.readableBytes());
        headers.set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }

    // write response
    channelHandlerContext.write(response);
    channelHandlerContext.write(responseContent);
    ChannelFuture future = channelHandlerContext.writeAndFlush(
            LastHttpContent.EMPTY_LAST_CONTENT);

    // Decide whether to close the connection or not.
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
 
Example #29
Source File: HttpUploadClientHandler.java    From tools-journey with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());

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

        if (response.status().code() == 200 && HttpUtil.isTransferEncodingChunked(response)) {
            readingChunks = true;
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }
    if (msg instanceof HttpContent) {
        HttpContent chunk = (HttpContent) msg;
        System.err.println(chunk.content().toString(CharsetUtil.UTF_8));

        if (chunk instanceof LastHttpContent) {
            if (readingChunks) {
                System.err.println("} END OF CHUNKED CONTENT");
            } else {
                System.err.println("} END OF CONTENT");
            }
            readingChunks = false;
        } else {
            System.err.println(chunk.content().toString(CharsetUtil.UTF_8));
        }
    }
}
 
Example #30
Source File: NettyInvocationBuilder.java    From docker-java with Apache License 2.0 5 votes vote down vote up
@Override
public void put(InputStream body, com.github.dockerjava.core.MediaType mediaType) {
    HttpRequestProvider requestProvider = httpPutRequestProvider(null);

    Channel channel = getChannel();

    try (ResponseCallback<Void> resultCallback = new ResponseCallback<>()) {
        HttpResponseHandler responseHandler = new HttpResponseHandler(requestProvider, resultCallback);

        channel.pipeline().addLast(new ChunkedWriteHandler());
        channel.pipeline().addLast(responseHandler);

        HttpRequest request = requestProvider.getHttpRequest(resource);

        // don't accept FullHttpRequest here
        if (request instanceof FullHttpRequest) {
            throw new DockerClientException("fatal: request is instance of FullHttpRequest");
        }

        request.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
        request.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
        request.headers().set(HttpHeaderNames.CONTENT_TYPE, mediaType.getMediaType());

        channel.write(request);
        channel.write(new ChunkedStream(new BufferedInputStream(body, 1024 * 1024)));
        channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);

        resultCallback.awaitResult();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}