Skip to content

Commit

Permalink
Allow relay block resolvers to read response directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishiranu committed Sep 18, 2020
1 parent 6ba076a commit d04654e
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src-api/chan/http/HttpClient.java
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
37 changes: 13 additions & 24 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -266,4 +255,4 @@ public String getCookieValue(String name) {
public HttpValidator getValidator() {
return validator;
}
}
}
13 changes: 7 additions & 6 deletions src/com/mishiranu/dashchan/content/ImageLoader.java
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand Down
8 changes: 5 additions & 3 deletions src/com/mishiranu/dashchan/content/async/ReadFileTask.java
Expand Up @@ -23,6 +23,9 @@
import java.io.OutputStream;

public class ReadFileTask extends HttpHolderTask<String, Long, Boolean> {
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);
Expand Down Expand Up @@ -113,19 +116,18 @@ 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) {
chanName = ChanManager.getInstance().getChanNameByHost(uri.getAuthority());
}
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);
Expand Down
6 changes: 4 additions & 2 deletions src/com/mishiranu/dashchan/content/async/ReadVideoTask.java
Expand Up @@ -15,6 +15,9 @@
import java.io.OutputStream;

public class ReadVideoTask extends HttpHolderTask<Void, Long, Boolean> {
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;
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -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: {
Expand Down
Expand Up @@ -72,7 +72,7 @@ public RelayBlockResolver.Result checkResponse(RelayBlockResolver resolver,
String chanName, HttpHolder holder) throws HttpException {
List<String> 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("<script src=\"https://static.stormwall.pro")) {
boolean success = resolver.runWebView(chanName, Client::new);
return new RelayBlockResolver.Result(true, success);
Expand Down

0 comments on commit d04654e

Please sign in to comment.