Search in sources :

Example 41 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLine.

@Test
public void testMultibyteCodedReadWriteLine() throws Exception {
    final String s1 = constructString(SWISS_GERMAN_HELLO);
    final String s2 = constructString(RUSSIAN_HELLO);
    final String s3 = "Like hello and stuff";
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        chbuffer.append(s1);
        outbuffer.writeLine(chbuffer, outputStream);
        chbuffer.clear();
        chbuffer.append(s2);
        outbuffer.writeLine(chbuffer, outputStream);
        chbuffer.clear();
        chbuffer.append(s3);
        outbuffer.writeLine(chbuffer, outputStream);
    }
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    final long expected = ((s1.getBytes(StandardCharsets.UTF_8).length + 2) + (s2.getBytes(StandardCharsets.UTF_8).length + 2) + (s3.getBytes(StandardCharsets.UTF_8).length + 2)) * 10;
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s1, chbuffer.toString());
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s2, chbuffer.toString());
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s3, chbuffer.toString());
    }
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(expected, bytesRead);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 42 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testUnmappableInputActionReplace.

@Test
public void testUnmappableInputActionReplace() throws Exception {
    final String s = "This text contains a circumflex \u0302 !!!";
    final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.IGNORE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, encoder);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    chbuffer.append(s);
    outbuffer.writeLine(chbuffer, outputStream);
    outbuffer.flush(outputStream);
    final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
    Assertions.assertEquals("This text contains a circumflex ? !!!\r\n", result);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetEncoder(java.nio.charset.CharsetEncoder) Test(org.junit.jupiter.api.Test)

Example 43 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLongLine.

@Test
public void testMultibyteCodedReadWriteLongLine() throws Exception {
    final String s1 = constructString(SWISS_GERMAN_HELLO);
    final String s2 = constructString(RUSSIAN_HELLO);
    final String s3 = "Like hello and stuff";
    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 1024; i++) {
        buf.append(s1).append(s2).append(s3);
    }
    final String s = buf.toString();
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s);
    outbuffer.writeLine(chbuffer, outputStream);
    outbuffer.flush(outputStream);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(s, chbuffer.toString());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 44 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestSessionInOutBuffers method testMalformedInputActionReplace.

@Test
public void testMalformedInputActionReplace() throws Exception {
    final byte[] tmp = constructString(SWISS_GERMAN_HELLO).getBytes(StandardCharsets.ISO_8859_1);
    final CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, decoder);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(tmp);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals("Gr\ufffdezi_z\ufffdm\ufffd", chbuffer.toString());
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 45 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class EntityUtils method parse.

/**
 * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
 * The encoding is taken from the entity's Content-Encoding header.
 * <p>
 * This is typically used while parsing an HTTP POST.
 * </p>
 *
 * @param entity
 *            The entity to parse
 * @param maxStreamLength
 *            The maximum size of the stream to read; use it to guard against unreasonable or malicious processing.
 * @return a list of {@link NameValuePair} as built from the URI's query portion.
 * @throws IOException
 *             If there was an exception getting the entity's data.
 */
public static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength) throws IOException {
    Args.notNull(entity, "HttpEntity");
    final int contentLength = toContentLength((int) Args.checkContentLength(entity));
    final ContentType contentType = ContentType.parse(entity.getContentType());
    if (!ContentType.APPLICATION_FORM_URLENCODED.isSameMimeType(contentType)) {
        return Collections.emptyList();
    }
    final Charset charset = contentType.getCharset(DEFAULT_CHARSET);
    final CharArrayBuffer buf;
    try (final InputStream inStream = entity.getContent()) {
        if (inStream == null) {
            return Collections.emptyList();
        }
        buf = toCharArrayBuffer(inStream, contentLength, charset, maxStreamLength);
    }
    if (buf.isEmpty()) {
        return Collections.emptyList();
    }
    return WWWFormCodec.parse(buf, charset);
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) InputStream(java.io.InputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Charset(java.nio.charset.Charset)

Aggregations

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)91 Test (org.junit.jupiter.api.Test)74 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)13 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)11 ReadableByteChannel (java.nio.channels.ReadableByteChannel)10 Header (org.apache.hc.core5.http.Header)10 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)10 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)10 HeaderElement (org.apache.hc.core5.http.HeaderElement)9 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)8 MessageConstraintException (org.apache.hc.core5.http.MessageConstraintException)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)6 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)5