Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIKA-2328] Improved error handling for HtmlParser errors with unbalanced quotes #1310

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ metadata, getEncodingDetector(context))) {
parser.setContentHandler(new XHTMLDowngradeHandler(
new HtmlHandler(mapper, handler, metadata, context, extractScripts)));

parser.parse(reader.asInputSource());
try {
parser.parse(reader.asInputSource());
} catch (StringIndexOutOfBoundsException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tballison - any thoughts on Tika's general strategy for what errors get returned by parsers? E.g. is the trend towards always throwing a TikaException, versus more specific errors?

throw new TikaException(e.getMessage(), e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -1269,6 +1270,19 @@ public void testPreferenceForTitleElement() throws Exception {
assertEquals("OldMetaTitle", m.get("title"));
}

@Test
public void testUnbalancedQuotes() throws Exception {
//this tests handling of unbalanced quotes (see TIKA-2328)
String testData = "<!DOCTYPE HTML PUBLIC \">";
assertThrows(TikaException.class, () -> {
new HtmlParser().parse(new ByteArrayInputStream(testData.getBytes()),
new BodyContentHandler(),
new Metadata(),
new ParseContext());

});
}

private class EncodingDetectorRunner implements Callable<String> {

final static String DONE = "done";
Expand Down