Java Code Examples for com.linecorp.armeria.common.HttpData#length()

The following examples show how to use com.linecorp.armeria.common.HttpData#length() . 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: ContentCompressionTest.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Test
void http() throws Exception {
    final WebClient client =
            WebClient.builder("http://127.0.0.1:" + dogma.serverAddress().getPort())
                     .setHttpHeader(HttpHeaderNames.AUTHORIZATION, "Bearer " + CsrfToken.ANONYMOUS)
                     .setHttpHeader(HttpHeaderNames.ACCEPT_ENCODING, "deflate")
                     .build();

    final String contentPath = HttpApiV1Constants.PROJECTS_PREFIX + '/' + PROJ +
                               HttpApiV1Constants.REPOS + '/' + REPO +
                               "/contents" + PATH;

    final AggregatedHttpResponse compressedResponse = client.get(contentPath).aggregate().join();
    assertThat(compressedResponse.status()).isEqualTo(HttpStatus.OK);

    final HttpData content = compressedResponse.content();
    try (Reader in = new InputStreamReader(new InflaterInputStream(new ByteArrayInputStream(
            content.array(), 0, content.length())), StandardCharsets.UTF_8)) {

        assertThat(CharStreams.toString(in)).contains(CONTENT);
    }
}
 
Example 2
Source File: HttpMessageAggregator.java    From armeria with Apache License 2.0 6 votes vote down vote up
protected void onData(HttpData data) {
    boolean added = false;
    try {
        if (future.isDone()) {
            return;
        }

        final int dataLength = data.length();
        if (dataLength > 0) {
            final int allowedMaxDataLength = Integer.MAX_VALUE - contentLength;
            if (dataLength > allowedMaxDataLength) {
                subscription.cancel();
                fail(new IllegalStateException("content length greater than Integer.MAX_VALUE"));
                return;
            }

            contentList.add(data);
            contentLength += dataLength;
            added = true;
        }
    } finally {
        if (!added) {
            ReferenceCountUtil.safeRelease(data);
        }
    }
}
 
Example 3
Source File: Http1ObjectEncoder.java    From armeria with Apache License 2.0 6 votes vote down vote up
private ChannelFuture doWriteSplitData(int id, HttpData data, boolean endStream) {
    try {
        int offset = 0;
        int remaining = data.length();
        ChannelFuture lastFuture;
        for (;;) {
            // Ensure an HttpContent does not exceed the maximum length of a cleartext TLS record.
            final int chunkSize = Math.min(MAX_TLS_DATA_LENGTH, remaining);
            lastFuture = write(id, new DefaultHttpContent(dataChunk(data, offset, chunkSize)), false);
            remaining -= chunkSize;
            if (remaining == 0) {
                break;
            }
            offset += chunkSize;
        }

        if (endStream) {
            lastFuture = write(id, LastHttpContent.EMPTY_LAST_CONTENT, true);
        }

        ch.flush();
        return lastFuture;
    } finally {
        ReferenceCountUtil.safeRelease(data);
    }
}
 
Example 4
Source File: HttpDataFile.java    From armeria with Apache License 2.0 5 votes vote down vote up
HttpDataFile(HttpData content,
             Clock clock,
             long lastModifiedMillis,
             boolean dateEnabled,
             boolean lastModifiedEnabled,
             @Nullable BiFunction<String, HttpFileAttributes, String> entityTagFunction,
             HttpHeaders headers) {
    this(content, null, clock, new HttpFileAttributes(content.length(), lastModifiedMillis),
         dateEnabled, lastModifiedEnabled, entityTagFunction, headers);
}
 
Example 5
Source File: Http1ObjectEncoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public final ChannelFuture doWriteData(int id, int streamId, HttpData data, boolean endStream) {
    if (!isWritable(id)) {
        ReferenceCountUtil.safeRelease(data);
        return newClosedSessionFuture();
    }

    final int length = data.length();
    if (length == 0) {
        ReferenceCountUtil.safeRelease(data);
        final HttpContent content = endStream ? LastHttpContent.EMPTY_LAST_CONTENT : EMPTY_CONTENT;
        final ChannelFuture future = write(id, content, endStream);
        ch.flush();
        return future;
    }

    try {
        if (!protocol.isTls() || length <= MAX_TLS_DATA_LENGTH) {
            // Cleartext connection or data.length() <= MAX_TLS_DATA_LENGTH
            return doWriteUnsplitData(id, data, endStream);
        } else {
            // TLS or data.length() > MAX_TLS_DATA_LENGTH
            return doWriteSplitData(id, data, endStream);
        }
    } catch (Throwable t) {
        return newFailedFuture(t);
    }
}
 
Example 6
Source File: ArmeriaHttpUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if the content of the response with the given {@link HttpStatus} is one of
 * {@link HttpStatus#NO_CONTENT}, {@link HttpStatus#RESET_CONTENT} and {@link HttpStatus#NOT_MODIFIED}.
 *
 * @throws IllegalArgumentException if the specified {@code content} is not empty when the specified
 *                                  {@link HttpStatus} is one of {@link HttpStatus#NO_CONTENT},
 *                                  {@link HttpStatus#RESET_CONTENT} and {@link HttpStatus#NOT_MODIFIED}.
 */
public static boolean isContentAlwaysEmptyWithValidation(HttpStatus status, HttpData content) {
    if (!status.isContentAlwaysEmpty()) {
        return false;
    }

    if (!content.isEmpty()) {
        throw new IllegalArgumentException(
                "A " + status + " response must have empty content: " + content.length() + " byte(s)");
    }

    return true;
}