@@ -55,7 +55,8 @@ final class MojangProfileResolver implements ProfileResolver {
5555
5656 public static final MojangProfileResolver INSTANCE = new MojangProfileResolver ();
5757
58- private static final int DEFAULT_TIMEOUT = (int ) TimeUnit .SECONDS .toMillis (10 );
58+ private static final int DEFAULT_TIMEOUT = (int ) TimeUnit .SECONDS .toMillis (5 );
59+ private static final int HTTP_TIMEOUT = Integer .getInteger ("npc_lib.http.timeout" , DEFAULT_TIMEOUT );
5960
6061 private static final Gson GSON = new GsonBuilder ()
6162 .disableHtmlEscaping ()
@@ -71,44 +72,16 @@ final class MojangProfileResolver implements ProfileResolver {
7172
7273 private static @ NotNull JsonObject makeRequest (@ NotNull String endpoint ) throws IOException {
7374 HttpURLConnection connection = createBaseConnection (endpoint );
75+ connection .connect ();
7476
75- // little hack - we cannot just follow redirects as some endpoints (for example CF workers)
76- // are setting a cookie and redirect us, we need to keep that cookie for the next request
77- // so we re-request the site when we were redirected
78- int redirectCount = 0 ;
79- do {
80- connection .connect ();
81-
82- // check for a redirect
83- int status = connection .getResponseCode ();
84- boolean redirect = status == HttpURLConnection .HTTP_MOVED_TEMP
85- || status == HttpURLConnection .HTTP_MOVED_PERM
86- || status == HttpURLConnection .HTTP_SEE_OTHER ;
87-
88- if (redirect ) {
89- // get the cookies and the target endpoint
90- String cookies = connection .getHeaderField ("Set-Cookie" );
91- String redirectTarget = connection .getHeaderField ("Location" );
92-
93- // retry the request
94- connection = createBaseConnection (redirectTarget );
95- connection .setRequestProperty ("Cookie" , cookies );
96- } else {
97- // we are connected successfully
98- if (status == HttpURLConnection .HTTP_OK ) {
99- // parse the incoming data
100- try (Reader reader = new InputStreamReader (connection .getInputStream (), StandardCharsets .UTF_8 )) {
101- return GSON .fromJson (reader , JsonElement .class ).getAsJsonObject ();
102- }
103- } else {
104- // rate limit, invalid name/uuid etc.
105- throw new IllegalArgumentException ("Unable to fetch data, server responded with " + status );
106- }
77+ int status = connection .getResponseCode ();
78+ if (status == HttpURLConnection .HTTP_OK ) {
79+ try (Reader reader = new InputStreamReader (connection .getInputStream (), StandardCharsets .UTF_8 )) {
80+ return GSON .fromJson (reader , JsonElement .class ).getAsJsonObject ();
10781 }
108- } while ( redirectCount ++ < 10 );
82+ }
10983
110- // too many redirects
111- throw new IllegalStateException ("Endpoint request redirected more than 10 times!" );
84+ throw new IllegalArgumentException ("Unable to fetch data, server responded with " + status );
11285 }
11386
11487 private static @ NotNull HttpURLConnection createBaseConnection (@ NotNull String endpoint ) throws IOException {
@@ -121,11 +94,12 @@ final class MojangProfileResolver implements ProfileResolver {
12194 connection .setRequestProperty ("User-Agent" , "juliarn/npc-lib2" );
12295
12396 // ensure that the request will not take forever
124- connection .setReadTimeout (DEFAULT_TIMEOUT );
125- connection .setConnectTimeout (DEFAULT_TIMEOUT );
97+ connection .setReadTimeout (HTTP_TIMEOUT );
98+ connection .setConnectTimeout (HTTP_TIMEOUT );
12699
127100 // ensure that these are 'true' even if the defaults changed
128101 connection .setUseCaches (true );
102+ connection .setAllowUserInteraction (false );
129103 connection .setInstanceFollowRedirects (true );
130104
131105 return connection ;
@@ -134,7 +108,6 @@ final class MojangProfileResolver implements ProfileResolver {
134108 @ Override
135109 public @ NotNull CompletableFuture <Profile .Resolved > resolveProfile (@ NotNull Profile profile ) {
136110 return CompletableFuture .supplyAsync (Util .callableToSupplier (() -> {
137- // check if we need to resolve the uuid of the profile
138111 UUID uniqueId = profile .uniqueId ();
139112 if (uniqueId == null ) {
140113 // this will give us either a valid object or throw an exception
@@ -176,23 +149,18 @@ public void write(@NotNull JsonWriter out, @Nullable ProfileProperty property) t
176149
177150 @ Override
178151 public @ Nullable ProfileProperty read (@ NotNull JsonReader in ) throws IOException {
179- // early break if the value is null
180152 if (in .peek () == JsonToken .NULL ) {
181153 in .nextNull ();
182154 return null ;
183155 }
184156
185- // the values we might find
186157 String name = null ;
187158 String value = null ;
188159 String signature = null ;
189160
190- // begin the next object and read it until it's over
191161 in .beginObject ();
192162 while (in .peek () != JsonToken .END_OBJECT ) {
193163 String fieldName = in .nextName ();
194-
195- // check if we know the field
196164 switch (fieldName .toLowerCase ()) {
197165 case "name" :
198166 name = in .nextString ();
@@ -201,7 +169,6 @@ public void write(@NotNull JsonWriter out, @Nullable ProfileProperty property) t
201169 value = in .nextString ();
202170 break ;
203171 case "signature" :
204- // normally should not be included, just to be sure
205172 if (in .peek () == JsonToken .NULL ) {
206173 in .nextNull ();
207174 } else {
@@ -215,10 +182,7 @@ public void write(@NotNull JsonWriter out, @Nullable ProfileProperty property) t
215182 }
216183 }
217184
218- // finish reading
219185 in .endObject ();
220-
221- // ensure that all values are present to create the property object
222186 return name != null && value != null ? ProfileProperty .property (name , value , signature ) : null ;
223187 }
224188 }
0 commit comments