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

The following examples show how to use io.netty.handler.codec.http.HttpContent. 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: ServerResponseCaptureFilter.java    From CapturePacket 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 #2
Source File: ProcessFinalResponseOutputHandlerTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void write_adds_to_finalContentLength_if_msg_is_HttpContent_and_finalContentLength_is_not_null() throws Exception {
    // given
    HttpContent msgMock = mock(HttpContent.class);
    ByteBuf contentMock = mock(ByteBuf.class);
    int contentBytes = (int)(Math.random() * 10000);

    doReturn(contentMock).when(msgMock).content();
    doReturn(contentBytes).when(contentMock).readableBytes();

    int initialFinalContentLengthValue = (int)(Math.random() * 10000);
    responseInfo.setFinalContentLength((long)initialFinalContentLengthValue);
    assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue);

    // when
    handler.write(ctxMock, msgMock, promiseMock);

    // then
    assertThat(responseInfo.getFinalContentLength()).isEqualTo(initialFinalContentLengthValue + contentBytes);
}
 
Example #3
Source File: ClientRequestCaptureFilter.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (httpObject instanceof HttpRequest) {
        this.httpRequest = (HttpRequest) httpObject;
    }

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

        storeRequestContent(httpContent);

        if (httpContent instanceof LastHttpContent) {
            LastHttpContent lastHttpContent = (LastHttpContent) httpContent;
            trailingHeaders = lastHttpContent .trailingHeaders();
        }
    }

    return null;
}
 
Example #4
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void addContentChunk_adds_chunk_content_length_to_rawContentLengthInBytes() throws IOException {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    String chunk1String = UUID.randomUUID().toString();
    String lastChunkString = UUID.randomUUID().toString();
    byte[] chunk1Bytes = chunk1String.getBytes();
    byte[] lastChunkBytes = lastChunkString.getBytes();
    HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer(chunk1Bytes));
    HttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastChunkBytes));

    // when
    requestInfo.addContentChunk(chunk1);
    requestInfo.addContentChunk(lastChunk);

    // then
    assertThat(requestInfo.contentChunks.size(), is(2));
    assertThat(requestInfo.isCompleteRequestWithAllChunks(), is(true));
    assertThat(requestInfo.getRawContentLengthInBytes(), is(chunk1Bytes.length + lastChunkBytes.length));
}
 
Example #5
Source File: HttpRequestHandler.java    From panama with MIT License 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (null == stringBuilder) {
        stringBuilder = new StringBuilder();
    }

    if (msg instanceof io.netty.handler.codec.http.HttpRequest) {
        io.netty.handler.codec.http.HttpRequest request = (io.netty.handler.codec.http.HttpRequest)msg;
        if (null == httpRequest) {
            httpRequest = new NettyHttpRequest(ctx, request);
        }

    } else if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent)msg;
        ByteBuf byteBuf = content.content();
        byte []data = new byte[byteBuf.capacity()];
        byteBuf.readBytes(data);

        stringBuilder.append(new String(data));
    }

    super.channelRead(ctx, msg);
}
 
Example #6
Source File: NettyRequestTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Tests {@link NettyRequest#addContent(HttpContent)} and
 * {@link NettyRequest#readInto(AsyncWritableChannel, Callback)} by creating a {@link NettyRequest}, adding a few
 * pieces of content to it and then reading from it to match the stream with the added content.
 * <p/>
 * The read happens at different points of time w.r.t content addition (before, during, after).
 * @param digestAlgorithm the digest algorithm to use. Can be empty or {@code null} if digest checking is not
 *                        required.
 * @param useCopyForcingByteBuf if {@code true}, uses {@link CopyForcingByteBuf} instead of the default
 *                              {@link ByteBuf}.
 * @param method Http method
 * @throws Exception
 */
private void contentAddAndReadTest(String digestAlgorithm, boolean useCopyForcingByteBuf, HttpMethod method)
    throws Exception {
  // non composite content
  // start reading before addition of content
  List<HttpContent> httpContents = new ArrayList<>();
  ByteBuffer content = generateContent(httpContents, useCopyForcingByteBuf);
  doContentAddAndReadTest(digestAlgorithm, content, httpContents, 0, method);

  // start reading in the middle of content add
  httpContents.clear();
  content = generateContent(httpContents, useCopyForcingByteBuf);
  doContentAddAndReadTest(digestAlgorithm, content, httpContents, httpContents.size() / 2, method);

  // start reading after all content added
  httpContents.clear();
  content = generateContent(httpContents, useCopyForcingByteBuf);
  doContentAddAndReadTest(digestAlgorithm, content, httpContents, httpContents.size(), method);

  // composite content
  httpContents.clear();
  content = generateCompositeContent(httpContents);
  doContentAddAndReadTest(digestAlgorithm, content, httpContents, 0, method);
}
 
Example #7
Source File: HttpPostMultipartRequestDecoder.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 HttpPostMultipartRequestDecoder 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 #8
Source File: ClientRequestCaptureFilter.java    From Dream-Catcher with MIT License 6 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (httpObject instanceof HttpRequest) {
        this.httpRequest = (HttpRequest) httpObject;
    }

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

        storeRequestContent(httpContent);

        if (httpContent instanceof LastHttpContent) {
            LastHttpContent lastHttpContent = (LastHttpContent) httpContent;
            trailingHeaders = lastHttpContent .trailingHeaders();
        }
    }

    return null;
}
 
Example #9
Source File: ClientRequestCaptureFilter.java    From AndroidHttpCapture with MIT License 6 votes vote down vote up
@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
    if (httpObject instanceof HttpRequest) {
        this.httpRequest = (HttpRequest) httpObject;
    }

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

        storeRequestContent(httpContent);

        if (httpContent instanceof LastHttpContent) {
            LastHttpContent lastHttpContent = (LastHttpContent) httpContent;
            trailingHeaders = lastHttpContent .trailingHeaders();
        }
    }

    return null;
}
 
Example #10
Source File: ZuulMessageImpl.java    From zuul with Apache License 2.0 6 votes vote down vote up
@Override
public void runBufferedBodyContentThroughFilter(ZuulFilter<?, ?> filter) {
    //Loop optimized for the common case: Most filters' processContentChunk() return
    // original chunk passed in as is without any processing
    for (int i=0; i < bodyChunks.size(); i++) {
        final HttpContent origChunk = bodyChunks.get(i);
        final HttpContent filteredChunk = filter.processContentChunk(this, origChunk);
        if ((filteredChunk != null) && (filteredChunk != origChunk)) {
            //filter actually did some processing, set the new chunk in and release the old chunk.
            bodyChunks.set(i, filteredChunk);
            final int refCnt = origChunk.refCnt();
            if (refCnt > 0) {
                origChunk.release(refCnt);
            }
        }
    }
}
 
Example #11
Source File: LogbookClientHandler.java    From logbook with MIT License 6 votes vote down vote up
@Override
public void channelRead(
        final ChannelHandlerContext context,
        final Object message) {

    runIf(message, HttpResponse.class, httpResponse -> {
        this.response = new Response(REMOTE, httpResponse);
        this.responseStage = requestStage.process(response);
    });

    runIf(message, HttpContent.class, response::buffer);

    runIf(message, LastHttpContent.class, content ->
            sequence.set(1, throwingRunnable(responseStage::write)));

    context.fireChannelRead(message);
}
 
Example #12
Source File: JBinaryHttpSparknginClient.java    From Sparkngin with GNU Affero General Public License v3.0 6 votes vote down vote up
protected Ack toAck(HttpContent content) {
  ByteBuf byteBuf = content.content() ;
  byte[] data = new byte[byteBuf.readableBytes()] ;
  byteBuf.readBytes(data) ;
  //byteBuf.release() ;
  Ack ack = null;
  try {
    ack = (Ack)IOUtil.deserialize(data);
  } catch (Exception e) {
    e.printStackTrace();
    ack = new Ack() ;
    ack.setStatus(Ack.Status.ERROR);
    ack.setMessage(e.getMessage());
  }
  return ack ;
}
 
Example #13
Source File: ViewHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldParseErrorWithEmptyRows() throws Exception {
    String response = Resources.read("error_empty_rows.json", this.getClass());
    HttpResponse responseHeader = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "OK"));
    HttpContent responseChunk1 = new DefaultLastHttpContent(Unpooled.copiedBuffer(response, CharsetUtil.UTF_8));

    ViewQueryRequest requestMock = mock(ViewQueryRequest.class);
    queue.add(requestMock);
    channel.writeInbound(responseHeader, responseChunk1);
    latch.await(1, TimeUnit.SECONDS);
    assertEquals(1, firedEvents.size());
    ViewQueryResponse inbound = (ViewQueryResponse) firedEvents.get(0);
    assertTrue(inbound.status().isSuccess());

    assertEquals(0, countAndRelease(inbound.rows()));

    String error = inbound.error().toBlocking().single();
    Map<String, Object> parsed = DefaultObjectMapper.readValueAsMap(error);
    assertEquals(1, parsed.size());
    assertNotNull(parsed.get("errors"));
}
 
Example #14
Source File: HttpPostMultipartRequestDecoder.java    From dorado with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param factory the factory used to create InterfaceHttpData
 * @param request the request to decode
 * @param charset the charset to use as default
 * @throws NullPointerException      for request or charset or factory
 * @throws ErrorDataDecoderException if the default charset was wrong when
 *                                   decoding or other errors
 */
public HttpPostMultipartRequestDecoder(HttpDataFactory factory, HttpRequest request, Charset charset) {
	this.request = checkNotNull(request, "request");
	this.charset = checkNotNull(charset, "charset");
	this.factory = checkNotNull(factory, "factory");
	// Fill default values

	setMultipart(this.request.headers().get(HttpHeaderNames.CONTENT_TYPE));
	if (request instanceof HttpContent) {
		// Offer automatically if the given request is als type of HttpContent
		// See #1089
		offer((HttpContent) request);
	} else {
		undecodedChunk = buffer();
		parseBody();
	}
}
 
Example #15
Source File: PassportStateHttpClientHandler.java    From zuul with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
    try {
        CurrentPassport passport = passport(ctx);

        if (msg instanceof HttpResponse) {
            passport.add(PassportState.IN_RESP_HEADERS_RECEIVED);
        }

        if (msg instanceof LastHttpContent) {
            passport.add(PassportState.IN_RESP_LAST_CONTENT_RECEIVED);
        }
        else if (msg instanceof HttpContent) {
            passport.add(PassportState.IN_RESP_CONTENT_RECEIVED);
        }
    }
    finally {
        super.channelRead(ctx, msg);
    }
}
 
Example #16
Source File: RecordedHttpRequestBuilderTest.java    From flashback with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void testBuildContent()
    throws Exception {
  HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com");
  RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest);

  String charset = "UTF-8";
  String str1 = "first content";
  HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset)));
  recordedHttpRequestBuilder.appendHttpContent(httpContent1);
  String str2 = "second content";
  HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset)));
  recordedHttpRequestBuilder.appendHttpContent(httpContent2);

  String lastStr = "Last chunk";
  HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset)));
  recordedHttpRequestBuilder.appendHttpContent(lastContent);

  RecordedHttpRequest recordedHttpRequest = recordedHttpRequestBuilder.build();
  Assert
      .assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpRequest.getHttpBody().getContent(charset));
}
 
Example #17
Source File: HttpPostMultipartRequestDecoder.java    From dorado 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 HttpPostMultipartRequestDecoder 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 #18
Source File: HttpBodySizeRecordingChannelHandler.java    From zuul with Apache License 2.0 6 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
{
    State state = null;
    
    // Reset the state as each new inbound request comes in.
    if (msg instanceof HttpRequest) {
        state = createNewState(ctx.channel());
    }
    
    // Update the inbound body size with this chunk.
    if (msg instanceof HttpContent) {
        if (state == null) {
            state = getOrCreateCurrentState(ctx.channel());
        }
        state.inboundBodySize += ((HttpContent) msg).content().readableBytes();
    }

    super.channelRead(ctx, msg);
}
 
Example #19
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void releaseContentChunks_clear_on_chunk_list_but_does_not_release_chunks_if_contentChunksWillBeReleasedExternally_is_true() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.contentChunksWillBeReleasedExternally();
    List<HttpContent> contentChunkList = Arrays.asList(mock(HttpContent.class), mock(HttpContent.class));
    requestInfo.contentChunks.addAll(contentChunkList);
    assertThat(requestInfo.contentChunks.size(), is(contentChunkList.size()));

    // when
    requestInfo.releaseContentChunks();

    // then
    for (HttpContent chunkMock : contentChunkList) {
        verify(chunkMock, never()).release();
    }
    assertThat(requestInfo.contentChunks.isEmpty(), is(true));
}
 
Example #20
Source File: NettyResponseChannelTest.java    From ambry with Apache License 2.0 6 votes vote down vote up
/**
 * Send the curated request and verify that the response and satisfaction are expected.
 * @param httpRequest the request to be handled by {@link MockNettyMessageProcessor}
 * @param requestContent the content needed for POST request
 * @param expectedStatus the expected {@link HttpResponseStatus}
 * @param shouldBeSatisfied whether the request should be satisfied or not
 * @throws IOException
 */
private void sendRequestAndEvaluateResponsePerformance(HttpRequest httpRequest, String requestContent,
    HttpResponseStatus expectedStatus, boolean shouldBeSatisfied) throws IOException {
  EmbeddedChannel channel = createEmbeddedChannel();
  channel.pipeline().get(MockNettyMessageProcessor.class);
  channel.writeInbound(httpRequest);
  if (requestContent != null) {
    channel.writeInbound(createContent(requestContent, true));
  }
  HttpResponse response = channel.readOutbound();
  assertEquals("Unexpected response status", expectedStatus, response.status());
  if (requestContent != null) {
    HttpContent responseContent = channel.readOutbound();
    String returnedContent = RestTestUtils.getContentString(responseContent);
    responseContent.release();
    assertEquals("Content does not match with expected content", requestContent, returnedContent);
  }
  if (shouldBeSatisfied) {
    assertTrue(httpRequest.method() + " request should be satisfied", MockNettyRequest.mockTracker.isSatisfied());
  } else {
    assertFalse(httpRequest.method() + " request should be unsatisfied", MockNettyRequest.mockTracker.isSatisfied());
  }
}
 
Example #21
Source File: HttpPostMultipartRequestDecoder.java    From netty-4.1.22 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 HttpPostMultipartRequestDecoder 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 #22
Source File: HttpBodySizeRecordingChannelHandler.java    From zuul with Apache License 2.0 6 votes vote down vote up
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
{
    State state = null;

    // Reset the state as each new outbound request goes out.
    if (msg instanceof HttpRequest) {
        state = createNewState(ctx.channel());
    }

    // Update the outbound body size with this chunk.
    if (msg instanceof HttpContent) {
        if (state == null) {
            state = getOrCreateCurrentState(ctx.channel());
        }
        state.outboundBodySize += ((HttpContent) msg).content().readableBytes();
    }

    super.write(ctx, msg, promise);
}
 
Example #23
Source File: NettyRequest.java    From ambry with Apache License 2.0 6 votes vote down vote up
@Override
public void close() {
  if (channelOpen.compareAndSet(true, false)) {
    setAutoRead(true);
    contentLock.lock();
    try {
      logger.trace("Closing NettyRequest {} with {} content chunks unread", getUri(), requestContents.size());
      // For non-POST we usually have one content chunk unread - this the LastHttpContent chunk. This is OK.
      HttpContent content = requestContents.poll();
      while (content != null) {
        ReferenceCountUtil.release(content);
        content = requestContents.poll();
      }
    } finally {
      contentLock.unlock();
      restRequestMetricsTracker.nioMetricsTracker.markRequestCompleted();
      if (digestCalculationTimeInMs >= 0) {
        nettyMetrics.digestCalculationTimeInMs.update(digestCalculationTimeInMs);
      }
      if (callbackWrapper != null) {
        callbackWrapper.invokeCallback(channelException);
      }
    }
  }
}
 
Example #24
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecodeDataAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    assertTrue(ch.writeInbound(new DefaultHttp2DataFrame(hello)));

    HttpContent content = ch.readInbound();
    try {
        assertThat(content.content().toString(CharsetUtil.UTF_8), is("hello world"));
        assertFalse(content instanceof LastHttpContent);
    } finally {
        content.release();
    }

    assertThat(ch.readInbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example #25
Source File: HttpUtils.java    From riposte with Apache License 2.0 5 votes vote down vote up
public static @Nullable String convertContentChunksToRawString(
    @NotNull Charset contentCharset,
    @Nullable Collection<HttpContent> contentChunks
) {
    byte[] rawBytes = convertContentChunksToRawBytes(contentChunks);
    if (rawBytes == null) {
        return null;
    }

    return convertRawBytesToString(contentCharset, rawBytes);
}
 
Example #26
Source File: BrpcHttpObjectEncoder.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
private static Object encodeAndRetain(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).retain();
    }
    if (msg instanceof HttpContent) {
        return ((HttpContent) msg).content().retain();
    }
    if (msg instanceof FileRegion) {
        return ((FileRegion) msg).retain();
    }
    throw new IllegalStateException("unexpected message type: " + StringUtil.simpleClassName(msg));
}
 
Example #27
Source File: HttpDownHandleInterceptFactory.java    From proxyee-down with Apache License 2.0 5 votes vote down vote up
@Override
public HttpProxyIntercept create() {
  return new HttpProxyIntercept() {

    @Override
    public void afterResponse(Channel clientChannel, Channel proxyChannel,
        HttpResponse httpResponse,
        HttpProxyInterceptPipeline pipeline) throws Exception {
      HttpRequest httpRequest = pipeline.getHttpRequest();
      ProxyConfig proxyConfig = ContentManager.CONFIG.get().getSecProxyConfig();
      TaskInfo taskInfo = HttpDownUtil.getTaskInfo(httpRequest,
          httpResponse.headers(),
          proxyConfig,
          HttpDownConstant.clientSslContext,
          HttpDownConstant.clientLoopGroup);
      HttpDownInfo httpDownInfo = new HttpDownInfo(taskInfo, httpRequest, proxyConfig);
      ContentManager.DOWN.putBoot(httpDownInfo);
      httpResponse.setStatus(HttpResponseStatus.OK);
      httpResponse.headers().clear();
      httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=utf-8");
      byte[] content = ("<html></html>")
          .getBytes("utf-8");
      httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
      clientChannel.writeAndFlush(httpResponse);
      HttpContent httpContent = new DefaultLastHttpContent();
      httpContent.content().writeBytes(content);
      clientChannel.writeAndFlush(httpContent);
      clientChannel.close();
      httpDownDispatch.dispatch(httpDownInfo);
    }
  };
}
 
Example #28
Source File: Http1ClientCodec.java    From xio with Apache License 2.0 5 votes vote down vote up
Response wrapResponse(ChannelHandlerContext ctx, HttpObject msg) {
  log.debug("wrapResponse msg={}", msg);
  final Response response;
  if (msg instanceof FullHttpResponse) {
    response = new FullHttp1Response((FullHttpResponse) msg);
  } else if (msg instanceof HttpResponse) {
    response = new SegmentedHttp1Response((HttpResponse) msg);
  } else if (msg instanceof HttpContent) {
    response =
        getProxyRequestQueue(ctx)
            .currentResponse()
            .map(r -> new SegmentedResponseData(r, new Http1SegmentedData((HttpContent) msg)))
            .orElse(null);
  } else {
    // TODO(CK): throw an exception if response is null?
    response = null;
  }

  return getProxyRequestQueue(ctx)
      .currentProxiedH2StreamId()
      .<Response>map(
          streamId -> {
            if (response instanceof SegmentedResponseData) {
              return new ProxySegmentedResponseData((SegmentedResponseData) response, streamId);
            } else {
              return new ProxyResponse(response, streamId);
            }
          })
      .orElse(response);
}
 
Example #29
Source File: NettyToStyxResponsePropagatorTest.java    From styx with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReleaseAlreadyReadBufferInCaseOfChannelGetsInactive() throws Exception {
    FluxSink subscriber = mock(FluxSink.class);
    EmbeddedChannel channel = new EmbeddedChannel(new NettyToStyxResponsePropagator(subscriber, SOME_ORIGIN));
    channel.writeInbound(new DefaultHttpResponse(HTTP_1_1, OK));

    HttpContent httpContentOne = newHttpContent("first chunk");
    channel.writeInbound(httpContentOne);

    channel.pipeline().fireChannelInactive();

    assertThat(httpContentOne.refCnt(), is(0));
}
 
Example #30
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@Override
public HttpObject serverToProxyResponse(HttpObject httpObject) {
    // if a ServerResponseCaptureFilter is configured, delegate to it to collect the server's response. if it is not
    // configured, we still need to capture basic information (timings, HTTP status, etc.), just not content.
    if (responseCaptureFilter != null) {
        responseCaptureFilter.serverToProxyResponse(httpObject);
    }

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

        captureResponse(httpResponse);
    }

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

        captureResponseSize(httpContent);
    }

    if (httpObject instanceof LastHttpContent) {
        if (dataToCapture.contains(CaptureType.RESPONSE_CONTENT)) {
            captureResponseContent(responseCaptureFilter.getHttpResponse(), responseCaptureFilter.getFullResponseContents());
        }

        harEntry.getResponse().setBodySize(responseBodySize.get());
    }

    return super.serverToProxyResponse(httpObject);
}