Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lol768 committed Sep 3, 2023
1 parent 2bcc343 commit 04226e5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.fragments.ShareOrderDialog;
import org.quantumbadger.redreader.fragments.UserProfileDialog;
import org.quantumbadger.redreader.http.HTTPBackend;
import org.quantumbadger.redreader.image.AlbumInfo;
import org.quantumbadger.redreader.image.DeviantArtAPI;
import org.quantumbadger.redreader.image.GetAlbumInfoListener;
Expand Down Expand Up @@ -238,9 +239,11 @@ public static void onLinkClicked(
case RedditURLParser.OPAQUE_SHARED_URL:
// kick off a thread to get the real url
new Thread(() -> {
final String realUrl = OpaqueSharedURL.resolveUsingNetwork(
final String toFetchUrl = OpaqueSharedURL.getUrlToFetch(
(OpaqueSharedURL) redditURL
);
).toString();
final String realUrl = HTTPBackend.getBackend()
.resolveRedirectUri(toFetchUrl);
if(realUrl != null) {
activity.runOnUiThread(() -> onLinkClicked(
activity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package org.quantumbadger.redreader.http;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.common.Optional;
import org.quantumbadger.redreader.http.body.HTTPRequestBody;
Expand Down Expand Up @@ -87,6 +89,8 @@ void onError(
void onSuccess(String mimetype, Long bodyBytes, InputStream body);
}

public abstract @Nullable String resolveRedirectUri(String url);

public abstract Request prepareRequest(Context context, RequestDetails details);

public abstract void recreateHttpBackend();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request.Builder;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
Expand Down Expand Up @@ -168,20 +169,34 @@ public static synchronized HTTPBackend getHttpBackend() {
return httpBackend;
}

@Deprecated
public OkHttpClient getClientDirectly() {
return mClient;
}

@Override
public synchronized void recreateHttpBackend() {
httpBackend = new OKHTTPBackend();
}

@Override
public String resolveRedirectUri(final String url) {
try {
final Builder builder = new Builder();
final okhttp3.Request headRequest = builder.url(url).head().build();
final OkHttpClient noRedirectsClient =
mClient.newBuilder().followRedirects(false).build();
try (Response response = noRedirectsClient.newCall(headRequest).execute()) {
if (response.isRedirect()) {
return response.header("Location");
}
}
} catch (final IOException e) {
Log.e(TAG, "Failed to resolve redirect URL", e);
return null;
}
return null;
}

@Override
public Request prepareRequest(final Context context, final RequestDetails details) {

final okhttp3.Request.Builder builder = new okhttp3.Request.Builder();
final Builder builder = new Builder();

builder.header("User-Agent", Constants.ua(context));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@

import androidx.annotation.Nullable;

import org.quantumbadger.redreader.http.HTTPBackend;
import org.quantumbadger.redreader.http.okhttp.OKHTTPBackend;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;

public class OpaqueSharedURL extends RedditURLParser.RedditURL {

@Nullable
Expand Down Expand Up @@ -67,23 +60,8 @@ public static OpaqueSharedURL parse(final Uri uri) {
return new OpaqueSharedURL(pathSegments.get(1), pathSegments.get(3));
}

public static String resolveUsingNetwork(final OpaqueSharedURL url) {
final Uri toFetch = Uri.parse(String.format("https://www.reddit.com/r/%s/s/%s", url.subreddit, url.shareKey));
// Send a HTTP HEAD request to toFetch, and return back the Location header
// This will be the URL of the post
OkHttpClient client = ((OKHTTPBackend) HTTPBackend.getBackend()).getClientDirectly();
client = client.newBuilder().followRedirects(false).build();
final Request request = new Request.Builder().url(toFetch.toString()).head().build();
try {
try (okhttp3.Response response = client.newCall(request).execute()) {
if (response.isRedirect()) {
return response.header("Location");
}
}
} catch (final IOException e) {
return null;
}
return null;
public static Uri getUrlToFetch(final OpaqueSharedURL url) {
return Uri.parse(String.format("https://www.reddit.com/r/%s/s/%s", url.subreddit, url.shareKey));
}

@Nullable
Expand Down

0 comments on commit 04226e5

Please sign in to comment.