Java Code Examples for io.netty.handler.codec.http.LastHttpContent#trailingHeaders()

The following examples show how to use io.netty.handler.codec.http.LastHttpContent#trailingHeaders() . 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: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpgradeDataEndWithTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    LastHttpContent trailers = new DefaultLastHttpContent(hello, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));

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

    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example 2
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDataEndWithTrailersAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    ByteBuf hello = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
    LastHttpContent trailers = new DefaultLastHttpContent(hello, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));

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

    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example 3
Source File: ClientRequestCaptureFilter.java    From CapturePacket 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 4
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 5
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 6
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 7
Source File: Http1SegmentedData.java    From xio with Apache License 2.0 5 votes vote down vote up
static Headers buildHeaders(HttpContent content) {
  if (content instanceof LastHttpContent) {
    LastHttpContent last = (LastHttpContent) content;
    if (last.trailingHeaders() != null) {
      return new Http1Headers(last.trailingHeaders());
    } else {
      return null;
    }
  } else {
    return null;
  }
}
 
Example 8
Source File: ServerResponseCaptureFilter.java    From browserup-proxy with Apache License 2.0 5 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastContent) {
    trailingHeaders = lastContent.trailingHeaders();

    // technically, the Content-Encoding header can be in a trailing header, although this is excruciatingly uncommon
    if (trailingHeaders != null) {
        String trailingContentEncoding = trailingHeaders.get(HttpHeaderNames.CONTENT_ENCODING);
        if (trailingContentEncoding != null) {
            contentEncoding = trailingContentEncoding;
        }
    }

}
 
Example 9
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpgradeTrailers() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(true));
    LastHttpContent trailers = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));

    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example 10
Source File: Http2StreamFrameToHttpObjectCodecTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncodeTrailersAsClient() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel(new Http2StreamFrameToHttpObjectCodec(false));
    LastHttpContent trailers = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, true);
    HttpHeaders headers = trailers.trailingHeaders();
    headers.set("key", "value");
    assertTrue(ch.writeOutbound(trailers));

    Http2HeadersFrame headerFrame = ch.readOutbound();
    assertThat(headerFrame.headers().get("key").toString(), is("value"));
    assertTrue(headerFrame.isEndStream());

    assertThat(ch.readOutbound(), is(nullValue()));
    assertFalse(ch.finish());
}
 
Example 11
Source File: Http1ObjectEncoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
private LastHttpContent convertTrailers(HttpHeaders inputHeaders) {
    if (inputHeaders.isEmpty()) {
        return LastHttpContent.EMPTY_LAST_CONTENT;
    }

    final LastHttpContent lastContent = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, false);
    final io.netty.handler.codec.http.HttpHeaders outputHeaders = lastContent.trailingHeaders();
    convertTrailers(inputHeaders, outputHeaders);
    return lastContent;
}
 
Example 12
Source File: ServerResponseCaptureFilter.java    From CapturePacket with MIT License 5 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastContent) {
    trailingHeaders = lastContent.trailingHeaders();

    // technically, the Content-Encoding header can be in a trailing header, although this is excruciatingly uncommon
    if (trailingHeaders != null) {
        String trailingContentEncoding = trailingHeaders.get(HttpHeaders.Names.CONTENT_ENCODING);
        if (trailingContentEncoding != null) {
            contentEncoding = trailingContentEncoding;
        }
    }

}
 
Example 13
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void addContentChunk_does_not_throw_IllegalStateException_if_requestInfo_trailingHeaders_is_already_populated_when_last_chunk_arrives_if_same_instance() {
    // given
    RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.isCompleteRequestWithAllChunks = false;
    LastHttpContent lastChunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(UUID.randomUUID().toString(), CharsetUtil.UTF_8));
    lastChunk.trailingHeaders().add("somekey", "someval");
    requestInfo.trailingHeaders = lastChunk.trailingHeaders();

    // when
    requestInfo.addContentChunk(lastChunk);

    // then
    assertThat(requestInfo.trailingHeaders, is(lastChunk.trailingHeaders()));
}
 
Example 14
Source File: ServerResponseCaptureFilter.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastContent) {
    trailingHeaders = lastContent.trailingHeaders();

    // technically, the Content-Encoding header can be in a trailing header, although this is excruciatingly uncommon
    if (trailingHeaders != null) {
        String trailingContentEncoding = trailingHeaders.get(HttpHeaders.Names.CONTENT_ENCODING);
        if (trailingContentEncoding != null) {
            contentEncoding = trailingContentEncoding;
        }
    }

}
 
Example 15
Source File: ServerResponseCaptureFilter.java    From Dream-Catcher with MIT License 5 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastContent) {
    trailingHeaders = lastContent.trailingHeaders();

    // technically, the Content-Encoding header can be in a trailing header, although this is excruciatingly uncommon
    if (trailingHeaders != null) {
        String trailingContentEncoding = trailingHeaders.get(HttpHeaders.Names.CONTENT_ENCODING);
        if (trailingContentEncoding != null) {
            contentEncoding = trailingContentEncoding;
        }
    }

}
 
Example 16
Source File: RequestInfoImplTest.java    From riposte with Apache License 2.0 4 votes vote down vote up
@Test
public void uber_constructor_works_for_valid_values() {
    // given
    String uri = "/some/uri/path/%24foobar%26?notused=blah";
    HttpMethod method = HttpMethod.PATCH;
    Charset contentCharset = CharsetUtil.US_ASCII;
    HttpHeaders headers = new DefaultHttpHeaders().add("header1", "val1").add(HttpHeaders.Names.CONTENT_TYPE, "text/text charset=" + contentCharset.displayName());
    QueryStringDecoder queryParams = new QueryStringDecoder(uri + "?foo=bar&secondparam=secondvalue");
    Set<Cookie> cookies = new HashSet<>(Arrays.asList(new DefaultCookie("cookie1", "val1"), new DefaultCookie("cookie2", "val2")));
    Map<String, String> pathParams = Arrays.stream(new String[][] {{"pathParam1", "val1"}, {"pathParam2", "val2"}}).collect(Collectors.toMap(pair -> pair[0], pair -> pair[1]));
    String content = UUID.randomUUID().toString();
    byte[] contentBytes = content.getBytes();
    LastHttpContent chunk = new DefaultLastHttpContent(Unpooled.copiedBuffer(contentBytes));
    chunk.trailingHeaders().add("trailingHeader1", "trailingVal1");
    List<HttpContent> contentChunks = Collections.singletonList(chunk);
    HttpVersion protocolVersion = HttpVersion.HTTP_1_1;
    boolean keepAlive = true;
    boolean fullRequest = true;
    boolean isMultipart = false;

    // when
    RequestInfoImpl<?> requestInfo = new RequestInfoImpl<>(uri, method, headers, chunk.trailingHeaders(), queryParams, cookies, pathParams, contentChunks, protocolVersion, keepAlive, fullRequest, isMultipart);

    // then
    assertThat("getUri should return passed in value", requestInfo.getUri(), is(uri));
    assertThat("getPath did not decode as expected", requestInfo.getPath(), is("/some/uri/path/$foobar&"));
    assertThat(requestInfo.getMethod(), is(method));
    assertThat(requestInfo.getHeaders(), is(headers));
    assertThat(requestInfo.getTrailingHeaders(), is(chunk.trailingHeaders()));
    assertThat(requestInfo.getQueryParams(), is(queryParams));
    assertThat(requestInfo.getCookies(), is(cookies));
    assertThat(requestInfo.pathTemplate, nullValue());
    assertThat(requestInfo.pathParams, is(pathParams));
    assertThat(requestInfo.getRawContentBytes(), is(contentBytes));
    assertThat(requestInfo.getRawContent(), is(content));
    assertThat(requestInfo.content, nullValue());
    assertThat(requestInfo.getContentCharset(), is(contentCharset));
    assertThat(requestInfo.getProtocolVersion(), is(protocolVersion));
    assertThat(requestInfo.isKeepAliveRequested(), is(keepAlive));
    assertThat(requestInfo.isCompleteRequestWithAllChunks, is(fullRequest));
    assertThat(requestInfo.isMultipart, is(isMultipart));
}
 
Example 17
Source File: HarCaptureFilter.java    From AndroidHttpCapture with MIT License 4 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastHttpContent) {
    HttpHeaders headers = lastHttpContent.trailingHeaders();

    captureHeaders(headers);
}
 
Example 18
Source File: HarCaptureFilter.java    From Dream-Catcher with MIT License 4 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastHttpContent) {
    Log.e("InnerHandle", "captureTrailingHeaders " + harEntry.getId());
    HttpHeaders headers = lastHttpContent.trailingHeaders();

    captureHeaders(headers);
}
 
Example 19
Source File: HarCaptureFilter.java    From CapturePacket with MIT License 4 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastHttpContent) {
    HttpHeaders headers = lastHttpContent.trailingHeaders();

    captureHeaders(headers);
}
 
Example 20
Source File: HarCaptureFilter.java    From browserup-proxy with Apache License 2.0 4 votes vote down vote up
protected void captureTrailingHeaders(LastHttpContent lastHttpContent) {
    HttpHeaders headers = lastHttpContent.trailingHeaders();

    captureHeaders(headers);
}