/** * Parses HTTP headers from the data receiver stream according to the generic * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3. * * @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. * @return array of HTTP headers * @param lineParser the line parser. If {@code null} * {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used * * @throws IOException in case of an I/O error * @throws HttpException in case of HTTP protocol violation */ public static Header[] parseHeaders( final SessionInputBuffer inBuffer, final InputStream inputStream, final int maxHeaderCount, final int maxLineLen, final LineParser lineParser) throws HttpException, IOException { final List<CharArrayBuffer> headerLines = new ArrayList<>(); return parseHeaders(inBuffer, inputStream, maxHeaderCount, maxLineLen, lineParser != null ? lineParser : LazyLineParser.INSTANCE, headerLines); }
final int i = buffer.readLine(this.headLine, inputStream); if (i == -1) { throw createConnectionClosedException(); this.message = createMessage(this.headLine); if (this.message != null) { break; final Header[] headers = AbstractMessageParser.parseHeaders( buffer, inputStream,
final int i = buffer.readLine(this.headLine, inputStream); if (i == -1) { throw createConnectionClosedException(); this.message = createMessage(this.headLine); if (this.message != null) { break; final Header[] headers = AbstractMessageParser.parseHeaders( buffer, inputStream,
/** * Parses HTTP headers from the data receiver stream according to the generic * format as given in Section 3.1 of RFC 822, RFC-2616 Section 4 and 19.3. * * @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. * @return array of HTTP headers * @param lineParser the line parser. If {@code null} * {@link org.apache.hc.core5.http.message.LazyLineParser#INSTANCE} will be used * * @throws IOException in case of an I/O error * @throws HttpException in case of HTTP protocol violation */ public static Header[] parseHeaders( final SessionInputBuffer inBuffer, final InputStream inputStream, final int maxHeaderCount, final int maxLineLen, final LineParser lineParser) throws HttpException, IOException { final List<CharArrayBuffer> headerLines = new ArrayList<>(); return parseHeaders(inBuffer, inputStream, maxHeaderCount, maxLineLen, lineParser != null ? lineParser : LazyLineParser.INSTANCE, headerLines); }
/** * Reads and stores the Trailer headers. * @throws IOException in case of an I/O error */ private void parseTrailerHeaders() throws IOException { try { this.footers = AbstractMessageParser.parseHeaders(buffer, inputStream, h1Config.getMaxHeaderCount(), h1Config.getMaxLineLength(), null); } catch (final HttpException ex) { final IOException ioe = new MalformedChunkCodingException("Invalid trailing header: " + ex.getMessage()); ioe.initCause(ex); throw ioe; } }
/** * Reads and stores the Trailer headers. * @throws IOException in case of an I/O error */ private void parseTrailerHeaders() throws IOException { try { this.footers = AbstractMessageParser.parseHeaders(buffer, inputStream, h1Config.getMaxHeaderCount(), h1Config.getMaxLineLength(), null); } catch (final HttpException ex) { final IOException ioe = new MalformedChunkCodingException("Invalid trailing header: " + ex.getMessage()); ioe.initCause(ex); throw ioe; } }
@Test public void testEmptyDataStream() throws Exception { final String s = ""; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null); Assert.assertNotNull(headers); Assert.assertEquals(0, headers.length); }
@Test public void testMaxHeaderCount() throws Exception { final String s = "header1: stuff\r\n" + "header2: stuff \r\n" + "header3: stuff\r\n" + "\r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); try { AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, -1, null); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } }
@Test public void testMaxHeaderCountForFoldedHeader() throws Exception { final String s = "header1: stuff\r\n" + " stuff \r\n" + " stuff\r\n" + "\r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); try { AbstractMessageParser.parseHeaders(inBuffer, inputStream, 2, 15, null); Assert.fail("IOException should have been thrown"); } catch (final IOException ex) { // expected } }
@Test public void testParsingHeader() throws Exception { final String s = "header1: stuff; param1 = value1; param2 = \"value 2\" \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 Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.length); Assert.assertEquals("header1: stuff; param1 = value1; param2 = \"value 2\" ", headers[0].toString()); }
@Test public void testParsingInvalidHeaders() throws Exception { final String s1 = " stuff\r\n" + "header1: stuff\r\n" + "\r\n"; final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(s1.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); try { AbstractMessageParser.parseHeaders(inBuffer1, inputStream1, -1, -1, null); Assert.fail("ProtocolException should have been thrown"); } catch (final ProtocolException ex) { // expected } final String s2 = " : stuff\r\n" + "header1: stuff\r\n" + "\r\n"; final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(s2.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); try { AbstractMessageParser.parseHeaders(inBuffer2, inputStream2, -1, -1, null); Assert.fail("ProtocolException should have been thrown"); } catch (final ProtocolException ex) { // expected } }
@Test public void testParsingMalformedFirstHeader() throws Exception { final String s = " header1: stuff\r\n" + "header2: stuff \r\n"; final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII)); final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.US_ASCII.newDecoder()); final Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null); Assert.assertNotNull(headers); Assert.assertEquals(2, headers.length); Assert.assertEquals("header1", headers[0].getName()); Assert.assertEquals("stuff", headers[0].getValue()); Assert.assertEquals("header2", headers[1].getName()); Assert.assertEquals("stuff", headers[1].getValue()); }
@Test public void testBasicHeaderParsing() throws Exception { final String s = "header1: stuff\r\n" + "header2: stuff \r\n" + "header3: stuff\r\n" + " and more stuff\r\n" + "\t and even more stuff\r\n" + " \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 Header[] headers = AbstractMessageParser.parseHeaders(inBuffer, inputStream, -1, -1, null); Assert.assertNotNull(headers); Assert.assertEquals(3, headers.length); Assert.assertEquals("header1", headers[0].getName()); Assert.assertEquals("stuff", headers[0].getValue()); Assert.assertEquals("header2", headers[1].getName()); Assert.assertEquals("stuff", headers[1].getValue()); Assert.assertEquals("header3", headers[2].getName()); Assert.assertEquals("stuff and more stuff and even more stuff", headers[2].getValue()); }