add HttpRequester

This commit is contained in:
yushijinhun 2017-12-02 19:25:00 +08:00
parent eec4a40eec
commit 12466bbbed
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
3 changed files with 142 additions and 12 deletions

View file

@ -2,12 +2,12 @@ package org.to2mbn.authlibinjector;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.to2mbn.authlibinjector.util.HttpRequester.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.instrument.ClassFileTransformer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@ -17,7 +17,6 @@ import java.util.Optional;
import java.util.function.Consumer;
import org.to2mbn.authlibinjector.internal.org.json.JSONException;
import org.to2mbn.authlibinjector.internal.org.json.JSONObject;
import org.to2mbn.authlibinjector.internal.org.json.JSONTokener;
import org.to2mbn.authlibinjector.transform.ClassTransformer;
import org.yaml.snakeyaml.Yaml;
@ -118,8 +117,8 @@ public final class AuthlibInjector {
JSONObject remoteConfig;
try {
remoteConfig = jsonGet(url);
} catch (IOException e) {
remoteConfig = new JSONObject(http.request("GET", url));
} catch (IOException | JSONException e) {
log("unable to configure remotely: {0}", e);
return empty();
}
@ -136,12 +135,4 @@ public final class AuthlibInjector {
return of(config);
}
private static JSONObject jsonGet(String url) throws IOException {
try (Reader reader = new InputStreamReader(new URL(url).openStream(), StandardCharsets.UTF_8)) {
return new JSONObject(new JSONTokener(reader));
} catch (JSONException e) {
throw new IOException("Unresolvable JSON", e);
}
}
}

View file

@ -0,0 +1,114 @@
package org.to2mbn.authlibinjector.util;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.to2mbn.authlibinjector.util.IOUtils.asString;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class HttpRequester {
/** Common http requester */
public static final HttpRequester http = new HttpRequester();
private int timeout = 15000;
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String request(String method, String url) throws IOException {
return request(method, url, null);
}
public String request(String method, String url, Map<String, String> headers) throws IOException {
HttpURLConnection conn = createConnection(url, headers);
conn.setRequestMethod(method);
try {
conn.connect();
try (InputStream in = conn.getInputStream()) {
return asString(in);
}
} catch (IOException e) {
try (InputStream in = conn.getErrorStream()) {
return readErrorStream(in, e);
}
} finally {
conn.disconnect();
}
}
public String requestWithPayload(String method, String url, Object payload, String contentType) throws IOException {
return requestWithPayload(method, url, payload, contentType, null);
}
public String requestWithPayload(String method, String url, Object payload, String contentType, Map<String, String> headers) throws IOException {
byte[] bytePayload;
if (payload instanceof byte[]) {
bytePayload = (byte[]) payload;
} else if (payload == null) {
bytePayload = new byte[0];
} else {
bytePayload = String.valueOf(payload).getBytes(UTF_8);
}
HttpURLConnection conn = createConnection(url, headers);
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", String.valueOf(bytePayload.length));
conn.setDoOutput(true);
try {
conn.connect();
try (OutputStream out = conn.getOutputStream()) {
out.write(bytePayload);
}
try (InputStream in = conn.getInputStream()) {
return asString(in);
}
} catch (IOException e) {
try (InputStream in = conn.getErrorStream()) {
return readErrorStream(in, e);
}
} finally {
conn.disconnect();
}
}
private String readErrorStream(InputStream in, IOException e) throws IOException {
if (in == null)
throw e;
try {
return asString(in);
} catch (IOException e1) {
if (e != e1)
e1.addSuppressed(e);
throw e1;
}
}
private HttpURLConnection createConnection(String url, Map<String, String> headers) throws IOException {
HttpURLConnection conn = createConnection(new URL(url));
if (headers != null)
headers.forEach((key, value) -> conn.setRequestProperty(key, value));
return conn;
}
private HttpURLConnection createConnection(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(timeout);
conn.setReadTimeout(timeout);
conn.setUseCaches(false);
return conn;
}
}

View file

@ -0,0 +1,25 @@
package org.to2mbn.authlibinjector.util;
import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public final class IOUtils {
public static String asString(InputStream in) throws IOException {
CharArrayWriter w = new CharArrayWriter();
Reader reader = new InputStreamReader(in, UTF_8);
char[] buf = new char[4096]; // 8192 bytes
int read;
while ((read = reader.read(buf)) != -1) {
w.write(buf, 0, read);
}
return new String(w.toCharArray());
}
private IOUtils() {}
}