public void runInternal() { connection = null; try { setProgressMessage(url.toString(), -1); long startTimeStamp = System.currentTimeMillis(); delayedProgress = coolReader.getEngine().showProgressDelayed(0, progressMessage, PROGRESS_DELAY_MILLIS); URLConnection conn = url.openConnection(); if (conn instanceof HttpsURLConnection) { onError("HTTPs is not supported yet"); return; } if (!(conn instanceof HttpURLConnection)) { onError("Only HTTP supported"); return; } connection = (HttpURLConnection) conn; connection.setRequestProperty("User-Agent", "CoolReader/3(Android)"); if (referer != null) connection.setRequestProperty("Referer", referer); connection.setInstanceFollowRedirects(true); connection.setAllowUserInteraction(false); connection.setConnectTimeout(20000); connection.setReadTimeout(40000); connection.setDoInput(true); String fileName = null; String disp = connection.getHeaderField("Content-Disposition"); if (disp != null) { int p = disp.indexOf("filename="); if (p > 0) { fileName = disp.substring(p + 9); } } // connection.setDoOutput(true); // connection.set int response = -1; response = connection.getResponseCode(); L.d("Response: " + response); if (response != 200) { onError("Error " + response); return; } String contentType = connection.getContentType(); String contentEncoding = connection.getContentEncoding(); int contentLen = connection.getContentLength(); // connection.getC L.d("Entity content length: " + contentLen); L.d("Entity content type: " + contentType); L.d("Entity content encoding: " + contentEncoding); setProgressMessage(url.toString(), contentLen); InputStream is = connection.getInputStream(); delayedProgress.cancel(); is = new ProgressInputStream(is, startTimeStamp, progressMessage, contentLen, 80); final int MAX_CONTENT_LEN_TO_BUFFER = 256 * 1024; boolean isZip = contentType != null && contentType.equals("application/zip"); if (expectedType != null) contentType = expectedType; else if (contentLen > 0 && contentLen < MAX_CONTENT_LEN_TO_BUFFER) { // autodetect type byte[] buf = new byte[contentLen]; if (is.read(buf) != contentLen) { onError("Wrong content length"); return; } is.close(); is = null; is = new ByteArrayInputStream(buf); if (findSubstring(buf, "<?xml version=") >= 0 && findSubstring(buf, "<feed") >= 0) contentType = "application/atom+xml"; // override type } if (contentType.startsWith("application/atom+xml")) { L.d("Parsing feed"); parseFeed(is); } else { if (fileName == null) fileName = defaultFileName; L.d("Downloading book: " + contentEncoding); downloadBook(contentType, url.toString(), is, contentLen, fileName, isZip); } } catch (Exception e) { L.e("Exception while trying to open URI " + url.toString(), e); onError("Error occured while reading OPDS catalog"); } finally { if (connection != null) try { connection.disconnect(); } catch (Exception e) { // ignore } } }