| 1 | package com.mojang.realmsclient.client;
|
| 2 |
|
| 3 | import com.mojang.realmsclient.exception.RealmsHttpException;
|
| 4 | import java.io.IOException;
|
| 5 | import java.io.InputStream;
|
| 6 | import java.io.InputStreamReader;
|
| 7 | import java.io.OutputStream;
|
| 8 | import java.io.OutputStreamWriter;
|
| 9 | import java.net.HttpURLConnection;
|
| 10 | import java.net.MalformedURLException;
|
| 11 | import java.net.Proxy;
|
| 12 | import java.net.URL;
|
| 13 | import java.nio.charset.StandardCharsets;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public abstract class Request<T extends Request<T>> {
|
| 20 | protected HttpURLConnection connection;
|
| 21 | private boolean connected;
|
| 22 | protected String url;
|
| 23 | private static final int DEFAULT_READ_TIMEOUT = 60000;
|
| 24 | private static final int DEFAULT_CONNECT_TIMEOUT = 5000;
|
| 25 | private static final String NETWORK_PROTOCOL_KEY = "X-NetworkProtocolVersion";
|
| 26 | private static final String CLIENT_REF_KEY = "Client-Ref";
|
| 27 | private static final String IS_SNAPSHOT_KEY = "Is-Prerelease";
|
| 28 | private static final String COOKIE_KEY = "Cookie";
|
| 29 |
|
| 30 | public Request(final String url, final int connectTimeout, final int readTimeout) {
|
| 31 | try {
|
| 32 | this.url = url;
|
| 33 | Proxy proxy = RealmsClientConfig.getProxy();
|
| 34 | if (proxy != null) {
|
| 35 | this.connection = (HttpURLConnection)new URL(url).openConnection(proxy);
|
| 36 | } else {
|
| 37 | this.connection = (HttpURLConnection)new URL(url).openConnection();
|
| 38 | }
|
| 39 |
|
| 40 | this.connection.setConnectTimeout(connectTimeout);
|
| 41 | this.connection.setReadTimeout(readTimeout);
|
| 42 | } catch (MalformedURLException e) {
|
| 43 | throw new RealmsHttpException(e.getMessage(), e);
|
| 44 | } catch (IOException e) {
|
| 45 | throw new RealmsHttpException(e.getMessage(), e);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | public void cookie(final String key, final String value) {
|
| 50 | cookie(this.connection, key, value);
|
| 51 | }
|
| 52 |
|
| 53 | public static void cookie(final HttpURLConnection connection, final String key, final String value) {
|
| 54 | String cookie = connection.getRequestProperty("Cookie");
|
| 55 | if (cookie == null) {
|
| 56 | connection.setRequestProperty("Cookie", key + "=" + value);
|
| 57 | } else {
|
| 58 | connection.setRequestProperty("Cookie", cookie + ";" + key + "=" + value);
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | public void addVersionHeaders(final String id, final int networkProtocol, final boolean isSnapshot) {
|
| 63 | this.connection.addRequestProperty("Client-Ref", id);
|
| 64 | this.connection.addRequestProperty("X-NetworkProtocolVersion", String.valueOf(networkProtocol));
|
| 65 | this.connection.addRequestProperty("Is-Prerelease", String.valueOf(isSnapshot));
|
| 66 | }
|
| 67 |
|
| 68 | public int getRetryAfterHeader() {
|
| 69 | return getRetryAfterHeader(this.connection);
|
| 70 | }
|
| 71 |
|
| 72 | public static int getRetryAfterHeader(final HttpURLConnection connection) {
|
| 73 | String pauseTime = connection.getHeaderField("Retry-After");
|
| 74 |
|
| 75 | try {
|
| 76 | return Integer.valueOf(pauseTime);
|
| 77 | } catch (Exception ignored) {
|
| 78 | return 5;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | public int responseCode() {
|
| 83 | try {
|
| 84 | this.connect();
|
| 85 | return this.connection.getResponseCode();
|
| 86 | } catch (Exception e) {
|
| 87 | throw new RealmsHttpException(e.getMessage(), e);
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | public String text() {
|
| 92 | try {
|
| 93 | this.connect();
|
| 94 | String result;
|
| 95 | if (this.responseCode() >= 400) {
|
| 96 | result = this.read(this.connection.getErrorStream());
|
| 97 | } else {
|
| 98 | result = this.read(this.connection.getInputStream());
|
| 99 | }
|
| 100 |
|
| 101 | this.dispose();
|
| 102 | return result;
|
| 103 | } catch (IOException e) {
|
| 104 | throw new RealmsHttpException(e.getMessage(), e);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | private String read(final @Nullable InputStream in) throws IOException {
|
| 109 | if (in == null) {
|
| 110 | return "";
|
| 111 | }
|
| 112 |
|
| 113 | InputStreamReader streamReader = new InputStreamReader(in, StandardCharsets.UTF_8);
|
| 114 | StringBuilder sb = new StringBuilder();
|
| 115 |
|
| 116 | for (int x = streamReader.read(); x != -1; x = streamReader.read()) {
|
| 117 | sb.append((char)x);
|
| 118 | }
|
| 119 |
|
| 120 | return sb.toString();
|
| 121 | }
|
| 122 |
|
| 123 | private void dispose() {
|
| 124 | byte[] bytes = new byte[1024];
|
| 125 |
|
| 126 | try {
|
| 127 | InputStream in = this.connection.getInputStream();
|
| 128 |
|
| 129 | while (in.read(bytes) > 0) {
|
| 130 | }
|
| 131 |
|
| 132 | in.close();
|
| 133 | return;
|
| 134 | } catch (Exception ignore) {
|
| 135 | try {
|
| 136 | InputStream errorStream = this.connection.getErrorStream();
|
| 137 | if (errorStream != null) {
|
| 138 | while (errorStream.read(bytes) > 0) {
|
| 139 | }
|
| 140 |
|
| 141 | errorStream.close();
|
| 142 | return;
|
| 143 | }
|
| 144 | } catch (IOException var8) {
|
| 145 | return;
|
| 146 | }
|
| 147 | } finally {
|
| 148 | if (this.connection != null) {
|
| 149 | this.connection.disconnect();
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | protected T connect() {
|
| 155 | if (this.connected) {
|
| 156 | return (T)this;
|
| 157 | }
|
| 158 |
|
| 159 | T t = this.doConnect();
|
| 160 | this.connected = true;
|
| 161 | return t;
|
| 162 | }
|
| 163 |
|
| 164 | protected abstract T doConnect();
|
| 165 |
|
| 166 | public static Request<?> get(final String url) {
|
| 167 | return new Request.Get(url, 5000, 60000);
|
| 168 | }
|
| 169 |
|
| 170 | public static Request<?> get(final String url, final int connectTimeoutMillis, final int readTimeoutMillis) {
|
| 171 | return new Request.Get(url, connectTimeoutMillis, readTimeoutMillis);
|
| 172 | }
|
| 173 |
|
| 174 | public static Request<?> post(final String uri, final String content) {
|
| 175 | return new Request.Post(uri, content, 5000, 60000);
|
| 176 | }
|
| 177 |
|
| 178 | public static Request<?> post(final String uri, final String content, final int connectTimeoutMillis, final int readTimeoutMillis) {
|
| 179 | return new Request.Post(uri, content, connectTimeoutMillis, readTimeoutMillis);
|
| 180 | }
|
| 181 |
|
| 182 | public static Request<?> delete(final String url) {
|
| 183 | return new Request.Delete(url, 5000, 60000);
|
| 184 | }
|
| 185 |
|
| 186 | public static Request<?> put(final String url, final String content) {
|
| 187 | return new Request.Put(url, content, 5000, 60000);
|
| 188 | }
|
| 189 |
|
| 190 | public static Request<?> put(final String url, final String content, final int connectTimeoutMillis, final int readTimeoutMillis) {
|
| 191 | return new Request.Put(url, content, connectTimeoutMillis, readTimeoutMillis);
|
| 192 | }
|
| 193 |
|
| 194 | public String getHeader(final String header) {
|
| 195 | return getHeader(this.connection, header);
|
| 196 | }
|
| 197 |
|
| 198 | public static String getHeader(final HttpURLConnection connection, final String header) {
|
| 199 | try {
|
| 200 | return connection.getHeaderField(header);
|
| 201 | } catch (Exception ignored) {
|
| 202 | return "";
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | @Environment(EnvType.CLIENT)
|
| 207 | public static class Delete extends Request<Request.Delete> {
|
| 208 | public Delete(final String uri, final int connectTimeout, final int readTimeout) {
|
| 209 | super(uri, connectTimeout, readTimeout);
|
| 210 | }
|
| 211 |
|
| 212 | public Request.Delete doConnect() {
|
| 213 | try {
|
| 214 | this.connection.setDoOutput(true);
|
| 215 | this.connection.setRequestMethod("DELETE");
|
| 216 | this.connection.connect();
|
| 217 | return this;
|
| 218 | } catch (Exception e) {
|
| 219 | throw new RealmsHttpException(e.getMessage(), e);
|
| 220 | }
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | @Environment(EnvType.CLIENT)
|
| 225 | public static class Get extends Request<Request.Get> {
|
| 226 | public Get(final String uri, final int connectTimeout, final int readTimeout) {
|
| 227 | super(uri, connectTimeout, readTimeout);
|
| 228 | }
|
| 229 |
|
| 230 | public Request.Get doConnect() {
|
| 231 | try {
|
| 232 | this.connection.setDoInput(true);
|
| 233 | this.connection.setDoOutput(true);
|
| 234 | this.connection.setUseCaches(false);
|
| 235 | this.connection.setRequestMethod("GET");
|
| 236 | return this;
|
| 237 | } catch (Exception e) {
|
| 238 | throw new RealmsHttpException(e.getMessage(), e);
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | @Environment(EnvType.CLIENT)
|
| 244 | public static class Post extends Request<Request.Post> {
|
| 245 | private final String content;
|
| 246 |
|
| 247 | public Post(final String uri, final String content, final int connectTimeout, final int readTimeout) {
|
| 248 | super(uri, connectTimeout, readTimeout);
|
| 249 | this.content = content;
|
| 250 | }
|
| 251 |
|
| 252 | public Request.Post doConnect() {
|
| 253 | try {
|
| 254 | if (this.content != null) {
|
| 255 | this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
| 256 | }
|
| 257 |
|
| 258 | this.connection.setDoInput(true);
|
| 259 | this.connection.setDoOutput(true);
|
| 260 | this.connection.setUseCaches(false);
|
| 261 | this.connection.setRequestMethod("POST");
|
| 262 | OutputStream out = this.connection.getOutputStream();
|
| 263 | OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
|
| 264 | writer.write(this.content);
|
| 265 | writer.close();
|
| 266 | out.flush();
|
| 267 | return this;
|
| 268 | } catch (Exception e) {
|
| 269 | throw new RealmsHttpException(e.getMessage(), e);
|
| 270 | }
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | @Environment(EnvType.CLIENT)
|
| 275 | public static class Put extends Request<Request.Put> {
|
| 276 | private final String content;
|
| 277 |
|
| 278 | public Put(final String uri, final String content, final int connectTimeout, final int readTimeout) {
|
| 279 | super(uri, connectTimeout, readTimeout);
|
| 280 | this.content = content;
|
| 281 | }
|
| 282 |
|
| 283 | public Request.Put doConnect() {
|
| 284 | try {
|
| 285 | if (this.content != null) {
|
| 286 | this.connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
|
| 287 | }
|
| 288 |
|
| 289 | this.connection.setDoOutput(true);
|
| 290 | this.connection.setDoInput(true);
|
| 291 | this.connection.setRequestMethod("PUT");
|
| 292 | OutputStream out = this.connection.getOutputStream();
|
| 293 | OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
|
| 294 | writer.write(this.content);
|
| 295 | writer.close();
|
| 296 | out.flush();
|
| 297 | return this;
|
| 298 | } catch (Exception e) {
|
| 299 | throw new RealmsHttpException(e.getMessage(), e);
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 | }
|
| 304 |
|