nanohttpd: refactor: remove NanoHTTPD.LOG

This commit is contained in:
Haowei Wen 2020-08-26 20:38:02 +08:00
parent 728e5d0d61
commit 85f38d47ac
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
3 changed files with 18 additions and 23 deletions

View file

@ -48,6 +48,8 @@ package moe.yushi.authlibinjector.internal.fi.iki.elonen;
import static java.nio.charset.StandardCharsets.ISO_8859_1; 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.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.BufferedInputStream;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -68,7 +70,6 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.function.Function; import java.util.function.Function;
import java.util.logging.Level;
class HTTPSession implements IHTTPSession { class HTTPSession implements IHTTPSession {
@ -182,7 +183,7 @@ class HTTPSession implements IHTTPSession {
this.protocolVersion = st.nextToken(); this.protocolVersion = st.nextToken();
} else { } else {
this.protocolVersion = "HTTP/1.1"; 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<String, String> headers = new LinkedHashMap<>(); Map<String, String> headers = new LinkedHashMap<>();
@ -363,13 +364,11 @@ class HTTPSession implements IHTTPSession {
* "foo bar" * "foo bar"
*/ */
private static String decodePercent(String str) { private static String decodePercent(String str) {
String decoded = null;
try { try {
decoded = URLDecoder.decode(str, "UTF8"); return URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException ignored) { } catch (UnsupportedEncodingException e) {
NanoHTTPD.LOG.log(Level.WARNING, "Encoding not supported, ignored", ignored); throw new RuntimeException(e); // never happens
} }
return decoded;
} }
/** /**

View file

@ -47,6 +47,9 @@
package moe.yushi.authlibinjector.internal.fi.iki.elonen; package moe.yushi.authlibinjector.internal.fi.iki.elonen;
import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_TEXT; 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.Closeable;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -57,8 +60,6 @@ import java.net.Socket;
import java.util.List; import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicLong; 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; import moe.yushi.authlibinjector.internal.fi.iki.elonen.HTTPSession.ConnectionCloseException;
/** /**
@ -111,7 +112,7 @@ public abstract class NanoHTTPD {
// SocketTimeoutException, print the // SocketTimeoutException, print the
// stacktrace // stacktrace
} catch (Exception e) { } 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 { } finally {
safeClose(outputStream); safeClose(outputStream);
safeClose(this.inputStream); safeClose(this.inputStream);
@ -189,7 +190,7 @@ public abstract class NanoHTTPD {
final InputStream inputStream = finalAccept.getInputStream(); final InputStream inputStream = finalAccept.getInputStream();
NanoHTTPD.this.asyncRunner.exec(new ClientHandler(inputStream, finalAccept)); NanoHTTPD.this.asyncRunner.exec(new ClientHandler(inputStream, finalAccept));
} catch (IOException e) { } 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()); } while (!NanoHTTPD.this.myServerSocket.isClosed());
} }
@ -202,11 +203,6 @@ public abstract class NanoHTTPD {
*/ */
public static final int SOCKET_READ_TIMEOUT = 5000; 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) { static final void safeClose(Object closeable) {
try { try {
if (closeable != null) { if (closeable != null) {
@ -221,7 +217,7 @@ public abstract class NanoHTTPD {
} }
} }
} catch (IOException e) { } 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(); this.myThread.join();
} }
} catch (Exception e) { } catch (Exception e) {
NanoHTTPD.LOG.log(Level.SEVERE, "Could not stop all connections", e); log(ERROR, "Could not stop all connections", e);
} }
} }

View file

@ -47,6 +47,8 @@
package moe.yushi.authlibinjector.internal.fi.iki.elonen; package moe.yushi.authlibinjector.internal.fi.iki.elonen;
import static java.util.Objects.requireNonNull; 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.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.Closeable; import java.io.Closeable;
@ -64,7 +66,6 @@ import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.logging.Level;
/** /**
* HTTP response. Return one of these from serve(). * HTTP response. Return one of these from serve().
@ -195,7 +196,7 @@ public class Response implements Closeable {
outputStream.flush(); outputStream.flush();
NanoHTTPD.safeClose(this.data); NanoHTTPD.safeClose(this.data);
} catch (IOException ioe) { } 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 { try {
size = Long.parseLong(contentLengthString); size = Long.parseLong(contentLengthString);
} catch (NumberFormatException ex) { } 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"); pw.print("Content-Length: " + size + "\r\n");
@ -294,8 +295,7 @@ public class Response implements Closeable {
} }
bytes = txt.getBytes(contentType.getEncoding()); bytes = txt.getBytes(contentType.getEncoding());
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
NanoHTTPD.LOG.log(Level.SEVERE, "encoding problem, responding nothing", e); throw new RuntimeException(e); // never happens, utf-8 is always available
bytes = new byte[0];
} }
return newFixedLength(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length); return newFixedLength(status, contentType.getContentTypeHeader(), new ByteArrayInputStream(bytes), bytes.length);
} }