diff --git a/src-api/chan/http/HttpClient.java b/src-api/chan/http/HttpClient.java index 6f6db94c..3a404757 100644 --- a/src-api/chan/http/HttpClient.java +++ b/src-api/chan/http/HttpClient.java @@ -558,7 +558,7 @@ private void executeInternal(HttpRequest request) throws HttpException { } } - HttpResponse read(HttpHolder holder) throws HttpException { + HttpResponse read(HttpHolder holder, boolean forceDirect) throws HttpException { try { HttpURLConnection connection = holder.getConnection(); holder.checkDisconnected(); @@ -575,7 +575,7 @@ HttpResponse read(HttpHolder holder) throws HttpException { commonInput = new GZIPInputStream(commonInput); contentLength = -1; } - OutputStream output = holder.outputStream; + OutputStream output = forceDirect ? null : holder.outputStream; ClientInputStream input = new ClientInputStream(commonInput, holder, holder.inputListener, contentLength); ByteArrayOutputStream writeTo = output == null ? new ByteArrayOutputStream() : null; if (output == null) { diff --git a/src-api/chan/http/HttpHolder.java b/src-api/chan/http/HttpHolder.java index 5acf9eee..0f0b5d42 100644 --- a/src-api/chan/http/HttpHolder.java +++ b/src-api/chan/http/HttpHolder.java @@ -1,21 +1,8 @@ -/* - * Copyright 2014-2016 Fukurou Mishiranu - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package chan.http; +import android.net.Uri; +import chan.annotation.Public; +import com.mishiranu.dashchan.util.IOUtils; import java.io.Closeable; import java.io.IOException; import java.io.OutputStream; @@ -24,12 +11,6 @@ import java.util.List; import java.util.Map; -import android.net.Uri; - -import chan.annotation.Public; - -import com.mishiranu.dashchan.util.IOUtils; - @Public public final class HttpHolder { Uri requestedUri; @@ -179,11 +160,19 @@ void disconnectAndClear() { @Public public HttpResponse read() throws HttpException { + return read(false); + } + + public HttpResponse readDirect() throws HttpException { + return read(true); + } + + private HttpResponse read(boolean direct) throws HttpException { HttpResponse response = this.response; if (response != null) { return response; } - response = HttpClient.getInstance().read(this); + response = HttpClient.getInstance().read(this, direct); this.response = response; return response; } @@ -266,4 +255,4 @@ public String getCookieValue(String name) { public HttpValidator getValidator() { return validator; } -} \ No newline at end of file +} diff --git a/src/com/mishiranu/dashchan/content/ImageLoader.java b/src/com/mishiranu/dashchan/content/ImageLoader.java index 271b5244..65fbc0cb 100644 --- a/src/com/mishiranu/dashchan/content/ImageLoader.java +++ b/src/com/mishiranu/dashchan/content/ImageLoader.java @@ -35,6 +35,9 @@ import java.util.concurrent.ThreadPoolExecutor; public class ImageLoader { + private static final int CONNECT_TIMEOUT = 10000; + private static final int READ_TIMEOUT = 5000; + private static final ImageLoader INSTANCE = new ImageLoader(); public static ImageLoader getInstance() { @@ -150,22 +153,20 @@ protected Bitmap doInBackground(HttpHolder holder, Void... params) { if (chanName == null) { chanName = this.chanName; } - int connectTimeout = 10000; - int readTimeout = 5000; if (chanName != null) { ChanPerformer performer = ChanPerformer.get(chanName); try { ChanPerformer.ReadContentResult result = performer.safe() - .onReadContent(new ChanPerformer.ReadContentData(uri, connectTimeout, - readTimeout, holder, null, null)); + .onReadContent(new ChanPerformer.ReadContentData(uri, + CONNECT_TIMEOUT, READ_TIMEOUT, holder, null, null)); bitmap = result != null && result.response != null ? result.response.getBitmap() : null; } catch (ExtensionException e) { e.getErrorItemAndHandle(); return null; } } else { - bitmap = new HttpRequest(uri, holder).setTimeouts(connectTimeout, readTimeout) - .read().getBitmap(); + bitmap = new HttpRequest(uri, holder) + .setTimeouts(CONNECT_TIMEOUT, READ_TIMEOUT).read().getBitmap(); } } if (isCancelled()) { diff --git a/src/com/mishiranu/dashchan/content/async/ReadFileTask.java b/src/com/mishiranu/dashchan/content/async/ReadFileTask.java index fc2e9739..afb42aa5 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadFileTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadFileTask.java @@ -23,6 +23,9 @@ import java.io.OutputStream; public class ReadFileTask extends HttpHolderTask { + private static final int CONNECT_TIMEOUT = 15000; + private static final int READ_TIMEOUT = 15000; + public interface Callback { void onStartDownloading(); void onFinishDownloading(boolean success, Uri uri, DataFile file, ErrorItem errorItem); @@ -113,7 +116,6 @@ protected Boolean doInBackground(HttpHolder holder, String... params) { } } else { Uri uri = fromUri; - final int connectTimeout = 15000, readTimeout = 15000; byte[] response; String chanName = this.chanName; if (chanName == null) { @@ -121,11 +123,11 @@ protected Boolean doInBackground(HttpHolder holder, String... params) { } if (chanName != null) { ChanPerformer.ReadContentResult result = ChanPerformer.get(chanName).safe() - .onReadContent(new ChanPerformer.ReadContentData (uri, connectTimeout, readTimeout, + .onReadContent(new ChanPerformer.ReadContentData(uri, CONNECT_TIMEOUT, READ_TIMEOUT, holder, progressHandler, null)); response = result.response.getBytes(); } else { - response = new HttpRequest(uri, holder).setTimeouts(connectTimeout, readTimeout) + response = new HttpRequest(uri, holder).setTimeouts(CONNECT_TIMEOUT, READ_TIMEOUT) .setInputListener(progressHandler).read().getBytes(); } ByteArrayInputStream input = new ByteArrayInputStream(response); diff --git a/src/com/mishiranu/dashchan/content/async/ReadVideoTask.java b/src/com/mishiranu/dashchan/content/async/ReadVideoTask.java index 18629cee..8b72b6ae 100644 --- a/src/com/mishiranu/dashchan/content/async/ReadVideoTask.java +++ b/src/com/mishiranu/dashchan/content/async/ReadVideoTask.java @@ -15,6 +15,9 @@ import java.io.OutputStream; public class ReadVideoTask extends HttpHolderTask { + private static final int CONNECT_TIMEOUT = 15000; + private static final int READ_TIMEOUT = 15000; + private final String chanName; private final Uri uri; private final CachingInputStream inputStream; @@ -45,9 +48,8 @@ public ReadVideoTask(String chanName, Uri uri, CachingInputStream inputStream, C @Override protected Boolean doInBackground(HttpHolder holder, Void... params) { try { - int connectTimeout = 15000, readTimeout = 15000; ChanPerformer.ReadContentResult result = ChanPerformer.get(chanName).safe() - .onReadContent(new ChanPerformer.ReadContentData(uri, connectTimeout, readTimeout, holder, + .onReadContent(new ChanPerformer.ReadContentData(uri, CONNECT_TIMEOUT, READ_TIMEOUT, holder, progressHandler, inputStream.getOutputStream())); HttpResponse response = result != null ? result.response : null; if (response != null) { diff --git a/src/com/mishiranu/dashchan/content/net/CloudFlareResolver.java b/src/com/mishiranu/dashchan/content/net/CloudFlareResolver.java index df492958..8d820c84 100644 --- a/src/com/mishiranu/dashchan/content/net/CloudFlareResolver.java +++ b/src/com/mishiranu/dashchan/content/net/CloudFlareResolver.java @@ -98,7 +98,7 @@ public RelayBlockResolver.Result checkResponse(RelayBlockResolver resolver, int responseCode = holder.getResponseCode(); if ((responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_UNAVAILABLE) && holder.getHeaderFields().containsKey("CF-RAY")) { - String responseText = holder.read().getString(); + String responseText = holder.readDirect().getString(); switch (responseCode) { case HttpURLConnection.HTTP_FORBIDDEN: case HttpURLConnection.HTTP_UNAVAILABLE: { diff --git a/src/com/mishiranu/dashchan/content/net/StormWallResolver.java b/src/com/mishiranu/dashchan/content/net/StormWallResolver.java index 1fcc62aa..d9f8a75e 100644 --- a/src/com/mishiranu/dashchan/content/net/StormWallResolver.java +++ b/src/com/mishiranu/dashchan/content/net/StormWallResolver.java @@ -72,7 +72,7 @@ public RelayBlockResolver.Result checkResponse(RelayBlockResolver resolver, String chanName, HttpHolder holder) throws HttpException { List contentType = holder.getHeaderFields().get("Content-Type"); if (contentType != null && contentType.size() == 1 && contentType.get(0).startsWith("text/html")) { - String responseText = holder.read().getString(); + String responseText = holder.readDirect().getString(); if (responseText.contains("