nanohttpd: refactor: move NanoHTTPD.MIME_PLAINTEXT-> IOUtils.CONTENT_TYPE_TEXT

This commit is contained in:
Haowei Wen 2020-08-26 20:11:23 +08:00
parent 61b787aa4d
commit 848e6da6a9
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
4 changed files with 10 additions and 10 deletions

View file

@ -16,6 +16,7 @@
*/
package moe.yushi.authlibinjector.httpd;
import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_TEXT;
import static moe.yushi.authlibinjector.util.IOUtils.transfer;
import static moe.yushi.authlibinjector.util.Logging.log;
import static moe.yushi.authlibinjector.util.Logging.Level.DEBUG;
@ -131,7 +132,7 @@ public class URLProcessor {
result = filter.handle(domain, path, session);
} catch (Throwable e) {
log(WARNING, "An error occurred while processing request [" + session.getUri() + "]", e);
return Response.newFixedLength(Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Internal Server Error");
return Response.newFixedLength(Status.INTERNAL_ERROR, CONTENT_TYPE_TEXT, "Internal Server Error");
}
if (result.isPresent()) {
@ -147,11 +148,11 @@ public class URLProcessor {
return reverseProxy(session, target);
} catch (IOException e) {
log(WARNING, "Reverse proxy error", e);
return Response.newFixedLength(Status.BAD_GATEWAY, MIME_PLAINTEXT, "Bad Gateway");
return Response.newFixedLength(Status.BAD_GATEWAY, CONTENT_TYPE_TEXT, "Bad Gateway");
}
} else {
log(DEBUG, "No handler is found for [" + session.getUri() + "]");
return Response.newFixedLength(Status.NOT_FOUND, MIME_PLAINTEXT, "Not Found");
return Response.newFixedLength(Status.NOT_FOUND, CONTENT_TYPE_TEXT, "Not Found");
}
}
};

View file

@ -47,6 +47,7 @@
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 java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
@ -275,11 +276,11 @@ class HTTPSession implements IHTTPSession {
// exception up the call stack.
throw ste;
} catch (IOException ioe) {
Response resp = Response.newFixedLength(Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
Response resp = Response.newFixedLength(Status.INTERNAL_ERROR, CONTENT_TYPE_TEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
resp.send(this.outputStream);
NanoHTTPD.safeClose(this.outputStream);
} catch (ResponseException re) {
Response resp = Response.newFixedLength(re.getStatus(), NanoHTTPD.MIME_PLAINTEXT, re.getMessage());
Response resp = Response.newFixedLength(re.getStatus(), CONTENT_TYPE_TEXT, re.getMessage());
resp.send(this.outputStream);
NanoHTTPD.safeClose(this.outputStream);
} finally {

View file

@ -46,6 +46,7 @@
*/
package moe.yushi.authlibinjector.internal.fi.iki.elonen;
import static moe.yushi.authlibinjector.util.IOUtils.CONTENT_TYPE_TEXT;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
@ -201,10 +202,6 @@ public abstract class NanoHTTPD {
*/
public static final int SOCKET_READ_TIMEOUT = 5000;
/**
* Common MIME type for dynamic content: plain text
*/
public static final String MIME_PLAINTEXT = "text/plain";
/**
* logger to log to.
*/
@ -291,7 +288,7 @@ public abstract class NanoHTTPD {
* @return HTTP response, see class Response for details
*/
public Response serve(IHTTPSession session) {
return Response.newFixedLength(Status.NOT_FOUND, NanoHTTPD.MIME_PLAINTEXT, "Not Found");
return Response.newFixedLength(Status.NOT_FOUND, CONTENT_TYPE_TEXT, "Not Found");
}
/**

View file

@ -29,6 +29,7 @@ import java.net.URL;
public final class IOUtils {
public static final String CONTENT_TYPE_JSON = "application/json; charset=utf-8";
public static final String CONTENT_TYPE_TEXT = "text/plain; charset=utf-8";
private static HttpURLConnection createConnection(String url, Proxy proxy) throws IOException {
if (proxy == null) {