Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize URL User-Agent #5955

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.kdt.pojavlaunch.modloaders.modpacks.api;

import static net.kdt.pojavlaunch.utils.DownloadUtils.USER_AGENT;

import android.util.ArrayMap;
import android.util.Log;

Expand All @@ -22,16 +24,15 @@
@SuppressWarnings("unused")
public class ApiHandler {
public final String baseUrl;
public final Map<String, String> additionalHeaders;
public final Map<String, String> additionalHeaders = new ArrayMap<>();

public ApiHandler(String url) {
baseUrl = url;
additionalHeaders = null;
additionalHeaders.put("User-Agent", USER_AGENT);
}

public ApiHandler(String url, String apiKey) {
baseUrl = url;
additionalHeaders = new ArrayMap<>();
this(url);
additionalHeaders.put("x-api-key", apiKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

@SuppressWarnings("IOStreamConstructor")
public class DownloadUtils {
public static final String USER_AGENT = Tools.APP_NAME;
//Standardize the User-Agent format to "Launcher Name/Launcher Version" to facilitate data collection by resource platforms.
public static final String USER_AGENT = Tools.APP_NAME + "/" + BuildConfig.VERSION_NAME;

public static void download(String url, OutputStream os) throws IOException {
download(new URL(url), os);
Expand Down Expand Up @@ -67,6 +68,7 @@ public static void downloadFileMonitored(String urlInput, File outputFile, @Null
FileUtils.ensureParentDirectory(outputFile);

HttpURLConnection conn = (HttpURLConnection) new URL(urlInput).openConnection();
conn.setRequestProperty("User-Agent", USER_AGENT);
InputStream readStr = conn.getInputStream();
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
int current;
Expand Down
Loading