Something went wrong. Try again.
The Minecraft: Java Edition decompiled source code.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293package 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<T extends Request<T>> { 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<Request.Delete> { 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<Request.Get> { 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<Request.Post> { 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<Request.Put> { 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); } } }}