BatchParserCommon { public static Header consumeHeaders(final List<Line> remainingMessage) { final int headerLineNumber = !remainingMessage.isEmpty() ? remainingMessage.get(0).getLineNumber() : 0; final Header headers = new Header(headerLineNumber); final Iterator<Line> iter = remainingMessage.iterator(); Line currentLine; boolean isHeader = true; while (iter.hasNext() && isHeader) { currentLine = iter.next(); final Matcher headerMatcher = PATTERN_HEADER_LINE.matcher(currentLine.toString()); if (headerMatcher.matches() && headerMatcher.groupCount() == 2) { iter.remove(); String headerName = headerMatcher.group(1).trim(); String headerValue = headerMatcher.group(2).trim(); headers.addHeader(headerName, Header.splitValuesByComma(headerValue), currentLine.getLineNumber()); } else { isHeader = false; } } return headers; } private  BatchParserCommon(); static String getBoundary(final String contentType, final int line); static ContentType parseContentType(final String contentType, final ContentType expected, final int line); static List<List<Line>> splitMessageByBoundary(final List<Line> message, final String boundary); static Line removeEndingCRLF(final Line line); static Header consumeHeaders(final List<Line> remainingMessage); static void consumeBlankLine(final List<Line> remainingMessage, final boolean isStrict); static InputStream convertLineListToInputStream(final List<Line> messageList, final Charset charset); static String trimLineListToLength(final List<Line> list, final int length); static InputStream convertLineListToInputStream(final List<Line> list, final Charset charset,
      final int length); static final String CONTENT_TRANSFER_ENCODING; static final String BINARY_ENCODING; }
@Test public void multipleHeaders() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Content-Id: 1" + CRLF, "Content-Id: 2" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> contentIdHeaders = header.getHeaders(HttpHeader.CONTENT_ID); assertNotNull(contentIdHeaders); assertEquals(2, contentIdHeaders.size()); assertEquals("1", contentIdHeaders.get(0)); assertEquals("2", contentIdHeaders.get(1)); }
@Test public void multipleHeadersSameValue() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Content-Id: 1" + CRLF, "Content-Id: 1" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> contentIdHeaders = header.getHeaders(HttpHeader.CONTENT_ID); assertNotNull(contentIdHeaders); assertEquals(1, contentIdHeaders.size()); assertEquals("1", contentIdHeaders.get(0)); }
@Test public void headersSeparatedByComma() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Content-Id: 1" + CRLF, "Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> upgradeHeader = header.getHeaders("upgrade"); assertNotNull(upgradeHeader); assertEquals(4, upgradeHeader.size()); assertEquals("HTTP/2.0", upgradeHeader.get(0)); assertEquals("SHTTP/1.3", upgradeHeader.get(1)); assertEquals("IRC/6.9", upgradeHeader.get(2)); assertEquals("RTA/x11", upgradeHeader.get(3)); }
@Test public void multipleAcceptHeaders() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1" + CRLF, "Accept: text/plain;q=0.3" + CRLF, "Accept-Language:en-US,en;q=0.7,en-UK;q=0.9" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> acceptHeader = header.getHeaders(HttpHeader.ACCEPT); assertNotNull(acceptHeader); assertEquals(4, acceptHeader.size()); }
@Test public void multipleAcceptHeadersSameValue() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Accept: application/atomsvc+xml;q=0.8, application/json;odata=verbose;q=0.5, */*;q=0.1" + CRLF, "Accept: application/atomsvc+xml;q=0.8" + CRLF, "Accept-Language:en-US,en;q=0.7,en-UK;q=0.9" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> acceptHeader = header.getHeaders(HttpHeader.ACCEPT); assertNotNull(acceptHeader); assertEquals(3, acceptHeader.size()); }
@Test public void multipleAcceptLanguageHeaders() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Accept-Language:en-US,en;q=0.7,en-UK;q=0.9" + CRLF, "Accept-Language: de-DE;q=0.3" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> acceptLanguageHeader = header.getHeaders(HttpHeader.ACCEPT_LANGUAGE); assertNotNull(acceptLanguageHeader); assertEquals(4, acceptLanguageHeader.size()); }
@Test public void multipleAcceptLanguageHeadersSameValue() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Accept-Language:en-US,en;q=0.7,en-UK;q=0.9" + CRLF, "Accept-Language:en-US,en;q=0.7" + CRLF, "content-type: Application/http" + CRLF, "content-transfer-encoding: Binary" + CRLF)); assertNotNull(header); final List<String> acceptLanguageHeader = header.getHeaders(HttpHeader.ACCEPT_LANGUAGE); assertNotNull(acceptLanguageHeader); assertEquals(3, acceptLanguageHeader.size()); }
@Test public void headersWithSpecialNames() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList( "Test0123456789: 42" + CRLF, "a_b: c/d" + CRLF, "!#$%&'*+-.^_`|~: weird" + CRLF)); assertNotNull(header); assertTrue(header.exists("Test0123456789")); assertTrue(header.exists("a_b")); assertTrue(header.exists("!#$%&'*+-.^_`|~")); assertEquals("weird", header.getHeader("!#$%&'*+-.^_`|~")); }
@Test public void headerWithWrongName() throws Exception { final Header header = BatchParserCommon.consumeHeaders(toLineList("a,b: c/d" + CRLF)); assertNotNull(header); assertFalse(header.iterator().hasNext()); }