nanohttpd: refactor: remove Response.lowerCaseHeader

This commit is contained in:
Haowei Wen 2020-08-26 19:13:34 +08:00
parent f0e635bff3
commit e88f1eb009
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
2 changed files with 10 additions and 48 deletions

View File

@ -262,7 +262,7 @@ class HTTPSession implements IHTTPSession {
r.setKeepAlive(keepAlive); r.setKeepAlive(keepAlive);
r.send(this.outputStream); r.send(this.outputStream);
} }
if (!keepAlive || r.isCloseConnection()) { if (!keepAlive || "close".equals(r.getHeader("connection"))) {
throw new SocketException("NanoHttpd Shutdown"); throw new SocketException("NanoHttpd Shutdown");
} }
} catch (SocketException e) { } catch (SocketException e) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2019 Haowei Wen <yushijinhun@gmail.com> and contributors * Copyright (C) 2020 Haowei Wen <yushijinhun@gmail.com> and contributors
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by * it under the terms of the GNU Affero General Public License as published by
@ -46,6 +46,7 @@
*/ */
package moe.yushi.authlibinjector.internal.fi.iki.elonen; package moe.yushi.authlibinjector.internal.fi.iki.elonen;
import static java.util.Objects.requireNonNull;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.Closeable; import java.io.Closeable;
@ -59,11 +60,10 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.HashMap; 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.Map.Entry;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
@ -89,23 +89,9 @@ public class Response implements Closeable {
private long contentLength; private long contentLength;
/** /**
* Headers for the HTTP response. Use addHeader() to add lines. the * Headers for the HTTP response. Use addHeader() to add lines.
* lowercase map is automatically kept up to date.
*/ */
private final Map<String, String> header = new HashMap<String, String>() { private final Map<String, String> headers = new LinkedHashMap<>();
@Override
public String put(String key, String value) {
lowerCaseHeader.put(key == null ? key : key.toLowerCase(), value);
return super.put(key, value);
};
};
/**
* copy of the header map with all the keys lowercase for faster
* searching.
*/
private final Map<String, String> lowerCaseHeader = new HashMap<>();
/** /**
* The request method that spawned this response. * The request method that spawned this response.
@ -147,39 +133,17 @@ public class Response implements Closeable {
* Adds given line to the header. * Adds given line to the header.
*/ */
public void addHeader(String name, String value) { public void addHeader(String name, String value) {
this.header.put(name, value); this.headers.put(name.toLowerCase(Locale.ROOT), requireNonNull(value));
} }
/** public String getHeader(String name) {
* Indicate to close the connection after the Response has been sent. return this.headers.get(name.toLowerCase(Locale.ROOT));
*
* @param close
* {@code true} to hint connection closing, {@code false} to
* let connection be closed by client.
*/
public void closeConnection(boolean close) {
if (close)
this.header.put("connection", "close");
else
this.header.remove("connection");
}
/**
* @return {@code true} if connection is to be closed after this
* Response has been sent.
*/
public boolean isCloseConnection() {
return "close".equals(getHeader("connection"));
} }
public InputStream getData() { public InputStream getData() {
return this.data; return this.data;
} }
public String getHeader(String name) {
return this.lowerCaseHeader.get(name.toLowerCase());
}
public String getMimeType() { public String getMimeType() {
return this.mimeType; return this.mimeType;
} }
@ -215,9 +179,7 @@ public class Response implements Closeable {
if (getHeader("date") == null) { if (getHeader("date") == null) {
printHeader(pw, "Date", gmtFrmt.format(new Date())); printHeader(pw, "Date", gmtFrmt.format(new Date()));
} }
for (Entry<String, String> entry : this.header.entrySet()) { this.headers.forEach((name, value) -> printHeader(pw, name, value));
printHeader(pw, entry.getKey(), entry.getValue());
}
if (getHeader("connection") == null) { if (getHeader("connection") == null) {
printHeader(pw, "Connection", (this.keepAlive ? "keep-alive" : "close")); printHeader(pw, "Connection", (this.keepAlive ? "keep-alive" : "close"));
} }