package com.mojang.realmsclient.client; import com.mojang.realmsclient.exception.RealmsHttpException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.Proxy; import java.net.URL; import java.nio.charset.StandardCharsets; import org.jspecify.annotations.Nullable; public abstract class Request> { protected HttpURLConnection connection; private boolean connected; protected String url; private static final int DEFAULT_READ_TIMEOUT = 60000; private static final int DEFAULT_CONNECT_TIMEOUT = 5000; private static final String IS_SNAPSHOT_KEY = "Is-Prerelease"; private static final String COOKIE_KEY = "Cookie"; public Request(String string, int i, int j) { try { this.url = string; Proxy proxy = RealmsClientConfig.getProxy(); if (proxy != null) { this.connection = (HttpURLConnection)new URL(string).openConnection(proxy); } else { this.connection = (HttpURLConnection)new URL(string).openConnection(); } this.connection.setConnectTimeout(i); this.connection.setReadTimeout(j); } catch (MalformedURLException malformedURLException) { throw new RealmsHttpException(malformedURLException.getMessage(), malformedURLException); } catch (IOException iOException) { throw new RealmsHttpException(iOException.getMessage(), iOException); } } public void cookie(String string, String string1) { cookie(this.connection, string, string1); } public static void cookie(HttpURLConnection httpURLConnection, String string, String string1) { String string2 = httpURLConnection.getRequestProperty("Cookie"); if (string2 == null) { httpURLConnection.setRequestProperty("Cookie", string + "=" + string1); } else { httpURLConnection.setRequestProperty("Cookie", string2 + ";" + string + "=" + string1); } } public void addSnapshotHeader(boolean bl) { this.connection.addRequestProperty("Is-Prerelease", String.valueOf(bl)); } public int getRetryAfterHeader() { return getRetryAfterHeader(this.connection); } public static int getRetryAfterHeader(HttpURLConnection httpURLConnection) { String string = httpURLConnection.getHeaderField("Retry-After"); try { return Integer.valueOf(string); } catch (Exception exception) { return 5; } } public int responseCode() { try { this.connect(); return this.connection.getResponseCode(); } catch (Exception exception) { throw new RealmsHttpException(exception.getMessage(), exception); } } public String text() { try { this.connect(); String string; if (this.responseCode() >= 400) { string = this.read(this.connection.getErrorStream()); } else { string = this.read(this.connection.getInputStream()); } this.dispose(); return string; } catch (IOException iOException) { throw new RealmsHttpException(iOException.getMessage(), iOException); } } private String read(@Nullable InputStream inputStream) throws IOException { if (inputStream == null) { return ""; } else { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8); StringBuilder stringBuilder = new StringBuilder(); for (int i = inputStreamReader.read(); i != -1; i = inputStreamReader.read()) { stringBuilder.append((char)i); } return stringBuilder.toString(); } } private void dispose() { byte[] b = new byte[1024]; try { InputStream inputStream = this.connection.getInputStream(); while (inputStream.read(b) > 0) { } inputStream.close(); return; } catch (Exception exception) { try { InputStream inputStream1 = this.connection.getErrorStream(); if (inputStream1 != null) { while (inputStream1.read(b) > 0) { } inputStream1.close(); return; } } catch (IOException iOException) { return; } } finally { if (this.connection != null) { this.connection.disconnect(); } } } protected T connect() { if (this.connected) { return (T)this; } else { Request request1 = this.doConnect(); this.connected = true; return (T)request1; } } protected abstract T doConnect(); public static Request get(String string) { return new Request.Get(string, 5000, 60000); } public static Request get(String string, int i, int j) { return new Request.Get(string, i, j); } public static Request post(String string, String string1) { return new Request.Post(string, string1, 5000, 60000); } public static Request post(String string, String string1, int i, int j) { return new Request.Post(string, string1, i, j); } public static Request delete(String string) { return new Request.Delete(string, 5000, 60000); } public static Request put(String string, String string1) { return new Request.Put(string, string1, 5000, 60000); } public static Request put(String string, String string1, int i, int j) { return new Request.Put(string, string1, i, j); } public String getHeader(String string) { return getHeader(this.connection, string); } public static String getHeader(HttpURLConnection httpURLConnection, String string) { try { return httpURLConnection.getHeaderField(string); } catch (Exception exception) { return ""; } } public static class Delete extends Request { public Delete(String string, int i, int j) { super(string, i, j); } public Request.Delete doConnect() { try { this.connection.setDoOutput(true); this.connection.setRequestMethod("DELETE"); this.connection.connect(); return this; } catch (Exception exception) { throw new RealmsHttpException(exception.getMessage(), exception); } } } public static class Get extends Request { public Get(String string, int i, int j) { super(string, i, j); } public Request.Get doConnect() { try { this.connection.setDoInput(true); this.connection.setDoOutput(true); this.connection.setUseCaches(false); this.connection.setRequestMethod("GET"); return this; } catch (Exception exception) { throw new RealmsHttpException(exception.getMessage(), exception); } } } public static class Post extends Request { private final String content; public Post(String string, String string1, int i, int j) { super(string, i, j); this.content = string1; } public Request.Post doConnect() { try { if (this.content != null) { this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); } this.connection.setDoInput(true); this.connection.setDoOutput(true); this.connection.setUseCaches(false); this.connection.setRequestMethod("POST"); OutputStream outputStream = this.connection.getOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); outputStreamWriter.write(this.content); outputStreamWriter.close(); outputStream.flush(); return this; } catch (Exception exception) { throw new RealmsHttpException(exception.getMessage(), exception); } } } public static class Put extends Request { private final String content; public Put(String string, String string1, int i, int j) { super(string, i, j); this.content = string1; } public Request.Put doConnect() { try { if (this.content != null) { this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8"); } this.connection.setDoOutput(true); this.connection.setDoInput(true); this.connection.setRequestMethod("PUT"); OutputStream outputStream = this.connection.getOutputStream(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); outputStreamWriter.write(this.content); outputStreamWriter.close(); outputStream.flush(); return this; } catch (Exception exception) { throw new RealmsHttpException(exception.getMessage(), exception); } } } }