Search in sources :

Example 66 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestChunkCoding method testCorruptChunkedInputStreamTruncatedCRLF.

// Truncated stream (missing closing CRLF)
@Test
public void testCorruptChunkedInputStreamTruncatedCRLF() throws IOException {
    final String s = "5\r\n01234";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    final byte[] tmp = new byte[5];
    Assertions.assertEquals(5, in.read(tmp));
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> in.read());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 67 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class AbstractMessageParser method parseHeaders.

/**
 * Parses HTTP headers from the data receiver stream according to the generic
 * format as specified by the HTTP/1.1 protocol specification.
 *
 * @param inBuffer Session input buffer
 * @param inputStream Input stream
 * @param maxHeaderCount maximum number of headers allowed. If the number
 *  of headers received from the data stream exceeds maxCount value, an
 *  IOException will be thrown. Setting this parameter to a negative value
 *  or zero will disable the check.
 * @param maxLineLen maximum number of characters for a header line,
 *  including the continuation lines. Setting this parameter to a negative
 *  value or zero will disable the check.
 * @param parser line parser to use.
 * @param headerLines List of header lines. This list will be used to store
 *   intermediate results. This makes it possible to resume parsing of
 *   headers in case of a {@link java.io.InterruptedIOException}.
 *
 * @return array of HTTP headers
 *
 * @throws IOException in case of an I/O error
 * @throws HttpException in case of HTTP protocol violation
 *
 * @since 4.1
 */
public static Header[] parseHeaders(final SessionInputBuffer inBuffer, final InputStream inputStream, final int maxHeaderCount, final int maxLineLen, final LineParser parser, final List<CharArrayBuffer> headerLines) throws HttpException, IOException {
    Args.notNull(inBuffer, "Session input buffer");
    Args.notNull(inputStream, "Input stream");
    Args.notNull(parser, "Line parser");
    Args.notNull(headerLines, "Header line list");
    CharArrayBuffer current = null;
    CharArrayBuffer previous = null;
    for (; ; ) {
        if (current == null) {
            current = new CharArrayBuffer(64);
        } else {
            current.clear();
        }
        final int readLen = inBuffer.readLine(current, inputStream);
        if (readLen == -1 || current.length() < 1) {
            break;
        }
        // discussion on folded headers
        if ((current.charAt(0) == ' ' || current.charAt(0) == '\t') && previous != null) {
            // we have continuation folded header
            // so append value
            int i = 0;
            while (i < current.length()) {
                final char ch = current.charAt(i);
                if (ch != ' ' && ch != '\t') {
                    break;
                }
                i++;
            }
            if (maxLineLen > 0 && previous.length() + 1 + current.length() - i > maxLineLen) {
                throw new MessageConstraintException("Maximum line length limit exceeded");
            }
            previous.append(' ');
            previous.append(current, i, current.length() - i);
        } else {
            headerLines.add(current);
            previous = current;
            current = null;
        }
        if (maxHeaderCount > 0 && headerLines.size() >= maxHeaderCount) {
            throw new MessageConstraintException("Maximum header count exceeded");
        }
    }
    final Header[] headers = new Header[headerLines.size()];
    for (int i = 0; i < headerLines.size(); i++) {
        final CharArrayBuffer buffer = headerLines.get(i);
        headers[i] = parser.parseHeader(buffer);
    }
    return headers;
}
Also used : Header(org.apache.hc.core5.http.Header) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) MessageConstraintException(org.apache.hc.core5.http.MessageConstraintException)

Example 68 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestResponseParser method testConnectionClosedException.

@Test
public void testConnectionClosedException() throws Exception {
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[] {});
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
    final ClassicHttpResponse response = parser.parse(inBuffer, inputStream);
    Assertions.assertNull(response);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 69 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestResponseParser method testBasicMessageParsingLeadingEmptyLines.

@Test
public void testBasicMessageParsingLeadingEmptyLines() throws Exception {
    final String s = "\r\n" + "\r\n" + "HTTP/1.1 200 OK\r\n" + "Server: whatever\r\n" + "\r\n";
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder());
    final DefaultHttpResponseParser parser = new DefaultHttpResponseParser(Http1Config.custom().setMaxEmptyLineCount(3).build());
    final ClassicHttpResponse httpresponse = parser.parse(inBuffer, inputStream);
    Assertions.assertEquals(200, httpresponse.getCode());
    Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
    final Header[] headers = httpresponse.getHeaders();
    Assertions.assertEquals(1, headers.length);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 70 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.

the class TestResponseParser method testMessageParsingTimeout.

@Test
public void testMessageParsingTimeout() throws Exception {
    final String s = "HTTP\000/1.1 200\000 OK\r\n" + "Server: wha\000tever\r\n" + "Date: some date\r\n" + "Set-Coo\000kie: c1=stuff\r\n" + "\000\r\n";
    final TimeoutByteArrayInputStream inputStream = new TimeoutByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final DefaultHttpResponseParser parser = new DefaultHttpResponseParser();
    int timeoutCount = 0;
    ClassicHttpResponse httpresponse = null;
    for (int i = 0; i < 10; i++) {
        try {
            httpresponse = parser.parse(inBuffer, inputStream);
            break;
        } catch (final InterruptedIOException ex) {
            timeoutCount++;
        }
    }
    Assertions.assertNotNull(httpresponse);
    Assertions.assertEquals(5, timeoutCount);
    Assertions.assertEquals(200, httpresponse.getCode());
    Assertions.assertEquals("OK", httpresponse.getReasonPhrase());
    final Header[] headers = httpresponse.getHeaders();
    Assertions.assertEquals(3, headers.length);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) InterruptedIOException(java.io.InterruptedIOException) SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)123 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)62 ReadableByteChannel (java.nio.channels.ReadableByteChannel)61 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)61 ByteArrayInputStream (java.io.ByteArrayInputStream)58 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)47 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 ByteBuffer (java.nio.ByteBuffer)30 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)25 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Header (org.apache.hc.core5.http.Header)13 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)10 InputStream (java.io.InputStream)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 InterruptedIOException (java.io.InterruptedIOException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4