jhy/jsoup — Issue #153: Validate content-type before getting data from remote http server
Author: hoverruan
Created: 2011-12-06 03:03:17+00:00
State: open

While getting a useless document, for example, a pdf file, read all data from remote http server is a waste of time, we should stop the process after the wrong content-type is determined:

diff --git a/src/main/java/org/jsoup/helper/HttpConnection.java b/src/main/java/
index 76c905c..eb3e70d 100644
--- a/src/main/java/org/jsoup/helper/HttpConnection.java
+++ b/src/main/java/org/jsoup/helper/HttpConnection.java
@@ -407,6 +407,11 @@ public class HttpConnection implements Connection {
}
res.req = req;

+ String contentType = res.contentType();
+ if (!req.ignoreContentType() && (contentType == null || !(contentTy
+ throw new IOException(String.format("Unhandled content type \"%
+ contentType, req.url().toString()));
+
InputStream bodyStream = null;
InputStream dataStream = null;
try {
@@ -444,9 +449,6 @@ public class HttpConnection implements Connection {

public Document parse() throws IOException {
Validate.isTrue(executed, "Request must be executed (with .execute(
- if (!req.ignoreContentType() && (contentType == null || !(contentTy
- throw new IOException(String.format("Unhandled content type \"%
- contentType, url.toString()));
Document doc = DataUtil.parseByteData(byteData, charset, url.toExte
byteData.rewind();
charset = doc.outputSettings().charset().name(); // update charset