nanohttpd: refactor: add missing this.

This commit is contained in:
Haowei Wen 2020-08-26 19:43:53 +08:00
parent e56a68ecc3
commit f84afc976e
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4

View File

@ -178,9 +178,9 @@ class HTTPSession implements IHTTPSession {
// NOTE: this now forces header names lower case since they are // NOTE: this now forces header names lower case since they are
// case insensitive and vary by client. // case insensitive and vary by client.
if (st.hasMoreTokens()) { if (st.hasMoreTokens()) {
protocolVersion = st.nextToken(); this.protocolVersion = st.nextToken();
} else { } else {
protocolVersion = "HTTP/1.1"; this.protocolVersion = "HTTP/1.1";
NanoHTTPD.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1."); NanoHTTPD.LOG.log(Level.FINE, "no protocol version specified, strange. Assuming HTTP/1.1.");
} }
@ -206,13 +206,13 @@ class HTTPSession implements IHTTPSession {
parseHeader(new BufferedReader(new InputStreamReader(readHeader(), ISO_8859_1))); parseHeader(new BufferedReader(new InputStreamReader(readHeader(), ISO_8859_1)));
String connection = this.headers.get("connection"); String connection = this.headers.get("connection");
boolean keepAlive = "HTTP/1.1".equals(protocolVersion) && (connection == null || !connection.matches("(?i).*close.*")); boolean keepAlive = "HTTP/1.1".equals(this.protocolVersion) && (connection == null || !connection.matches("(?i).*close.*"));
String transferEncoding = this.headers.get("transfer-encoding"); String transferEncoding = this.headers.get("transfer-encoding");
String contentLengthStr = this.headers.get("content-length"); String contentLengthStr = this.headers.get("content-length");
if (transferEncoding != null && contentLengthStr == null) { if (transferEncoding != null && contentLengthStr == null) {
if ("chunked".equals(transferEncoding)) { if ("chunked".equals(transferEncoding)) {
parsedInputStream = new ChunkedInputStream(inputStream); this.parsedInputStream = new ChunkedInputStream(this.inputStream);
} else { } else {
throw new ResponseException(Status.NOT_IMPLEMENTED, "Unsupported Transfer-Encoding"); throw new ResponseException(Status.NOT_IMPLEMENTED, "Unsupported Transfer-Encoding");
} }
@ -226,34 +226,34 @@ class HTTPSession implements IHTTPSession {
if (contentLength < 0) { if (contentLength < 0) {
throw new ResponseException(Status.BAD_REQUEST, "The request has an invalid Content-Length header."); throw new ResponseException(Status.BAD_REQUEST, "The request has an invalid Content-Length header.");
} }
parsedInputStream = new FixedLengthInputStream(inputStream, contentLength); this.parsedInputStream = new FixedLengthInputStream(this.inputStream, contentLength);
} else if (transferEncoding != null && contentLengthStr != null) { } else if (transferEncoding != null && contentLengthStr != null) {
throw new ResponseException(Status.BAD_REQUEST, "Content-Length and Transfer-Encoding cannot exist at the same time."); throw new ResponseException(Status.BAD_REQUEST, "Content-Length and Transfer-Encoding cannot exist at the same time.");
} else /* if both are null */ { } else /* if both are null */ {
// no request payload // no request payload
parsedInputStream = null; this.parsedInputStream = null;
} }
expect100Continue = "HTTP/1.1".equals(protocolVersion) this.expect100Continue = "HTTP/1.1".equals(this.protocolVersion)
&& "100-continue".equals(this.headers.get("expect")) && "100-continue".equals(this.headers.get("expect"))
&& parsedInputStream != null; && this.parsedInputStream != null;
continueSent = false; this.continueSent = false;
// Ok, now do the serve() // Ok, now do the serve()
this.isServing = true; this.isServing = true;
try { try {
r = handler.apply(this); r = handler.apply(this);
} finally { } finally {
synchronized (servingLock) { synchronized (this.servingLock) {
this.isServing = false; this.isServing = false;
} }
} }
if (!(parsedInputStream == null || (expect100Continue && !continueSent))) { if (!(this.parsedInputStream == null || (this.expect100Continue && !this.continueSent))) {
// consume the input // consume the input
while (parsedInputStream.read() != -1) while (this.parsedInputStream.read() != -1)
; ;
} }
@ -295,12 +295,12 @@ class HTTPSession implements IHTTPSession {
@Override @Override
public final InputStream getInputStream() throws IOException { public final InputStream getInputStream() throws IOException {
synchronized (servingLock) { synchronized (this.servingLock) {
if (!isServing) { if (!this.isServing) {
throw new IllegalStateException(); throw new IllegalStateException();
} }
if (expect100Continue && !continueSent) { if (this.expect100Continue && !this.continueSent) {
continueSent = true; this.continueSent = true;
this.outputStream.write("HTTP/1.1 100 Continue\r\n\r\n".getBytes(ISO_8859_1)); this.outputStream.write("HTTP/1.1 100 Continue\r\n\r\n".getBytes(ISO_8859_1));
} }
} }