MultiPartParser { public boolean parse(ByteBuffer buffer, boolean last) { boolean handle = false; while (!handle && BufferUtils.hasContent(buffer)) { switch (state) { case PREAMBLE: parsePreamble(buffer); continue; case DELIMITER: case DELIMITER_PADDING: case DELIMITER_CLOSE: parseDelimiter(buffer); continue; case BODY_PART: handle = parseMimePartHeaders(buffer); break; case FIRST_OCTETS: case OCTETS: handle = parseOctetContent(buffer); break; case EPILOGUE: BufferUtils.clear(buffer); break; case END: handle = true; break; default: throw new IllegalStateException(); } } if (last && BufferUtils.isEmpty(buffer)) { if (state == State.EPILOGUE) { state = State.END; if (LOG.isDebugEnabled()) LOG.debug("messageComplete {}", this); return handler.messageComplete(); } else { if (LOG.isDebugEnabled()) LOG.debug("earlyEOF {}", this); handler.earlyEOF(); return true; } } return handle; } MultiPartParser(Handler handler, String boundary); void reset(); Handler getHandler(); State getState(); boolean isState(State state); boolean parse(ByteBuffer buffer, boolean last); @Override String toString(); static final LazyLogger LOG; }
@Test public void testCrAsLineTermination() { TestHandler handler = new TestHandler() { @Override public boolean messageComplete() { return true; } @Override public boolean content(ByteBuffer buffer, boolean last) { super.content(buffer, last); return false; } }; MultiPartParser parser = new MultiPartParser(handler, "AaB03x"); ByteBuffer data = BufferUtils.toBuffer( "--AaB03x\r\n" + "content-disposition: form-data; name=\"field1\"\r\n" + "\r" + "Joe Blow\r\n" + "--AaB03x--\r\n"); BadMessageException x = assertThrows(BadMessageException.class, () -> parser.parse(data, true), "Invalid EOL"); assertTrue(x.getMessage().contains("Bad EOL")); }
@Test public void testBadHeaderNames() { String[] bad = new String[] { "Foo\\Bar: value\r\n", "Foo@Bar: value\r\n", "Foo,Bar: value\r\n", "Foo}Bar: value\r\n", "Foo{Bar: value\r\n", "Foo=Bar: value\r\n", "Foo>Bar: value\r\n", "Foo<Bar: value\r\n", "Foo)Bar: value\r\n", "Foo(Bar: value\r\n", "Foo?Bar: value\r\n", "Foo\"Bar: value\r\n", "Foo/Bar: value\r\n", "Foo]Bar: value\r\n", "Foo[Bar: value\r\n", "\u0192\u00f8\u00f8\u00df\u00e5\u00ae: value\r\n" }; for (String s : bad) { ByteBuffer buffer = BufferUtils.toBuffer( "--AaB03x\r\n" + s + "\r\n--AaB03x--\r\n"); MultiPartParser.Handler handler = new TestHandler(); MultiPartParser parser = new MultiPartParser(handler, "AaB03x"); try { parser.parse(buffer, true); } catch (BadMessageException e) { assertTrue(e.getMessage().contains("Illegal character")); } } }