Spaces:
Build error
Build error
File size: 10,309 Bytes
d46f4a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
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 javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@OnlyIn(Dist.CLIENT)
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 p_87310_, int p_87311_, int p_87312_) {
try {
this.url = p_87310_;
Proxy proxy = RealmsClientConfig.getProxy();
if (proxy != null) {
this.connection = (HttpURLConnection)new URL(p_87310_).openConnection(proxy);
} else {
this.connection = (HttpURLConnection)new URL(p_87310_).openConnection();
}
this.connection.setConnectTimeout(p_87311_);
this.connection.setReadTimeout(p_87312_);
} catch (MalformedURLException malformedurlexception) {
throw new RealmsHttpException(malformedurlexception.getMessage(), malformedurlexception);
} catch (IOException ioexception) {
throw new RealmsHttpException(ioexception.getMessage(), ioexception);
}
}
public void cookie(String p_87323_, String p_87324_) {
cookie(this.connection, p_87323_, p_87324_);
}
public static void cookie(HttpURLConnection p_87336_, String p_87337_, String p_87338_) {
String s = p_87336_.getRequestProperty("Cookie");
if (s == null) {
p_87336_.setRequestProperty("Cookie", p_87337_ + "=" + p_87338_);
} else {
p_87336_.setRequestProperty("Cookie", s + ";" + p_87337_ + "=" + p_87338_);
}
}
public void addSnapshotHeader(boolean p_309559_) {
this.connection.addRequestProperty("Is-Prerelease", String.valueOf(p_309559_));
}
public int getRetryAfterHeader() {
return getRetryAfterHeader(this.connection);
}
public static int getRetryAfterHeader(HttpURLConnection p_87331_) {
String s = p_87331_.getHeaderField("Retry-After");
try {
return Integer.valueOf(s);
} 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 s;
if (this.responseCode() >= 400) {
s = this.read(this.connection.getErrorStream());
} else {
s = this.read(this.connection.getInputStream());
}
this.dispose();
return s;
} catch (IOException ioexception) {
throw new RealmsHttpException(ioexception.getMessage(), ioexception);
}
}
private String read(@Nullable InputStream p_87315_) throws IOException {
if (p_87315_ == null) {
return "";
} else {
InputStreamReader inputstreamreader = new InputStreamReader(p_87315_, 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[] abyte = new byte[1024];
try {
InputStream inputstream = this.connection.getInputStream();
while (inputstream.read(abyte) > 0) {
}
inputstream.close();
return;
} catch (Exception exception) {
try {
InputStream inputstream1 = this.connection.getErrorStream();
if (inputstream1 != null) {
while (inputstream1.read(abyte) > 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 {
T t = this.doConnect();
this.connected = true;
return t;
}
}
protected abstract T doConnect();
public static Request<?> get(String p_87317_) {
return new Request.Get(p_87317_, 5000, 60000);
}
public static Request<?> get(String p_87319_, int p_87320_, int p_87321_) {
return new Request.Get(p_87319_, p_87320_, p_87321_);
}
public static Request<?> post(String p_87343_, String p_87344_) {
return new Request.Post(p_87343_, p_87344_, 5000, 60000);
}
public static Request<?> post(String p_87326_, String p_87327_, int p_87328_, int p_87329_) {
return new Request.Post(p_87326_, p_87327_, p_87328_, p_87329_);
}
public static Request<?> delete(String p_87341_) {
return new Request.Delete(p_87341_, 5000, 60000);
}
public static Request<?> put(String p_87354_, String p_87355_) {
return new Request.Put(p_87354_, p_87355_, 5000, 60000);
}
public static Request<?> put(String p_87346_, String p_87347_, int p_87348_, int p_87349_) {
return new Request.Put(p_87346_, p_87347_, p_87348_, p_87349_);
}
public String getHeader(String p_87352_) {
return getHeader(this.connection, p_87352_);
}
public static String getHeader(HttpURLConnection p_87333_, String p_87334_) {
try {
return p_87333_.getHeaderField(p_87334_);
} catch (Exception exception) {
return "";
}
}
@OnlyIn(Dist.CLIENT)
public static class Delete extends Request<Request.Delete> {
public Delete(String p_87359_, int p_87360_, int p_87361_) {
super(p_87359_, p_87360_, p_87361_);
}
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);
}
}
}
@OnlyIn(Dist.CLIENT)
public static class Get extends Request<Request.Get> {
public Get(String p_87365_, int p_87366_, int p_87367_) {
super(p_87365_, p_87366_, p_87367_);
}
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);
}
}
}
@OnlyIn(Dist.CLIENT)
public static class Post extends Request<Request.Post> {
private final String content;
public Post(String p_87372_, String p_87373_, int p_87374_, int p_87375_) {
super(p_87372_, p_87374_, p_87375_);
this.content = p_87373_;
}
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);
}
}
}
@OnlyIn(Dist.CLIENT)
public static class Put extends Request<Request.Put> {
private final String content;
public Put(String p_87380_, String p_87381_, int p_87382_, int p_87383_) {
super(p_87380_, p_87382_, p_87383_);
this.content = p_87381_;
}
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);
}
}
}
} |