diff --git a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/HTTPSession.java b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/HTTPSession.java index 98833d1..4ba8d7b 100644 --- a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/HTTPSession.java +++ b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/HTTPSession.java @@ -48,6 +48,8 @@ package moe.yushi.authlibinjector.internal.fi.iki.elonen; import static java.nio.charset.StandardCharsets.ISO_8859_1; import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_TEXT; +import static moe.yushi.authlibinjector.util.Logging.log; +import static moe.yushi.authlibinjector.util.Logging.Level.DEBUG; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; @@ -68,7 +70,6 @@ import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; import java.util.function.Function; -import java.util.logging.Level; class HTTPSession implements IHTTPSession { @@ -182,7 +183,7 @@ class HTTPSession implements IHTTPSession { this.protocolVersion = st.nextToken(); } else { this.protocolVersion = "HTTP/1.1"; - NanoHTTPD.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1."); + log(DEBUG, "no protocol version specified, strange. Assuming HTTP/1.1."); } Map headers = new LinkedHashMap<>(); @@ -363,13 +364,11 @@ class HTTPSession implements IHTTPSession { * "foo bar" */ private static String decodePercent(String str) { - String decoded = null; try { - decoded = URLDecoder.decode(str, "UTF8"); - } catch (UnsupportedEncodingException ignored) { - NanoHTTPD.LOG.log(Level.WARNING, "Encoding not supported, ignored", ignored); + return URLDecoder.decode(str, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); // never happens } - return decoded; } /** diff --git a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/NanoHTTPD.java b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/NanoHTTPD.java index aae022f..9559294 100644 --- a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/NanoHTTPD.java +++ b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/NanoHTTPD.java @@ -47,6 +47,9 @@ package moe.yushi.authlibinjector.internal.fi.iki.elonen; import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_TEXT; +import static moe.yushi.authlibinjector.util.Logging.log; +import static moe.yushi.authlibinjector.util.Logging.Level.DEBUG; +import static moe.yushi.authlibinjector.util.Logging.Level.ERROR; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; @@ -57,8 +60,6 @@ import java.net.Socket; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicLong; -import java.util.logging.Level; -import java.util.logging.Logger; import moe.yushi.authlibinjector.internal.fi.iki.elonen.HTTPSession.ConnectionCloseException; /** @@ -111,7 +112,7 @@ public abstract class NanoHTTPD { // SocketTimeoutException, print the // stacktrace } catch (Exception e) { - NanoHTTPD.LOG.log(Level.SEVERE, "Communication with the client broken, or an bug in the handler code", e); + log(ERROR, "Communication with the client broken, or an bug in the handler code", e); } finally { safeClose(outputStream); safeClose(this.inputStream); @@ -189,7 +190,7 @@ public abstract class NanoHTTPD { final InputStream inputStream = finalAccept.getInputStream(); NanoHTTPD.this.asyncRunner.exec(new ClientHandler(inputStream, finalAccept)); } catch (IOException e) { - NanoHTTPD.LOG.log(Level.FINE, "Communication with the client broken", e); + log(DEBUG, "Communication with the client broken", e); } } while (!NanoHTTPD.this.myServerSocket.isClosed()); } @@ -202,11 +203,6 @@ public abstract class NanoHTTPD { */ public static final int SOCKET_READ_TIMEOUT = 5000; - /** - * logger to log to. - */ - static final Logger LOG = Logger.getLogger(NanoHTTPD.class.getName()); - static final void safeClose(Object closeable) { try { if (closeable != null) { @@ -221,7 +217,7 @@ public abstract class NanoHTTPD { } } } catch (IOException e) { - NanoHTTPD.LOG.log(Level.SEVERE, "Could not close", e); + log(ERROR, "Could not close", e); } } @@ -345,7 +341,7 @@ public abstract class NanoHTTPD { this.myThread.join(); } } catch (Exception e) { - NanoHTTPD.LOG.log(Level.SEVERE, "Could not stop all connections", e); + log(ERROR, "Could not stop all connections", e); } } diff --git a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/Response.java b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/Response.java index c595d0b..a56a344 100644 --- a/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/Response.java +++ b/src/main/java/moe/yushi/authlibinjector/internal/fi/iki/elonen/Response.java @@ -47,6 +47,8 @@ package moe.yushi.authlibinjector.internal.fi.iki.elonen; import static java.util.Objects.requireNonNull; +import static moe.yushi.authlibinjector.util.Logging.log; +import static moe.yushi.authlibinjector.util.Logging.Level.ERROR; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.Closeable; @@ -64,7 +66,6 @@ import java.util.LinkedHashMap; import java.util.Locale; import java.util.Map; import java.util.TimeZone; -import java.util.logging.Level; /** * HTTP response. Return one of these from serve(). @@ -195,7 +196,7 @@ public class Response implements Closeable { outputStream.flush(); NanoHTTPD.safeClose(this.data); } catch (IOException ioe) { - NanoHTTPD.LOG.log(Level.SEVERE, "Could not send response to the client", ioe); + log(ERROR, "Could not send response to the client", ioe); } } @@ -210,7 +211,7 @@ public class Response implements Closeable { try { size = Long.parseLong(contentLengthString); } catch (NumberFormatException ex) { - NanoHTTPD.LOG.severe("content-length was no number " + contentLengthString); + log(ERROR, "content-length was not number " + contentLengthString); } } pw.print("Content-Length: " + size + "\r\n"); @@ -294,8 +295,7 @@ public class Response implements Closeable { } bytes = txt.getBytes(contentType.getEncoding()); } catch (UnsupportedEncodingException e) { - NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); - bytes = new byte[0]; + throw new RuntimeException(e); // never happens, utf-8 is always available } return newFixedLength(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length); }