Skip to content

Commit

Permalink
gh-24 fix npe
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow0 committed Dec 17, 2023
1 parent 0923fe2 commit cc586e5
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package unknow.server.http.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -32,6 +31,7 @@
import unknow.server.http.servlet.out.LengthOutputStream;
import unknow.server.http.servlet.out.Output;
import unknow.server.http.servlet.out.ServletWriter;
import unknow.server.nio.NIOConnection.Out;

/**
* @author unknow
Expand Down Expand Up @@ -63,7 +63,6 @@ public class ServletResponseImpl implements HttpServletResponse {

private static final DateTimeFormatter RFC1123 = DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC);

private final OutputStream out;
private final HttpConnection p;
private Output servletOut;

Expand All @@ -90,8 +89,6 @@ public class ServletResponseImpl implements HttpServletResponse {
*/
public ServletResponseImpl(HttpConnection p) {
this.p = p;
this.out = p.getOut();

headers = new HashMap<>();
cookies = new ArrayList<>();

Expand All @@ -109,6 +106,7 @@ public final void commit() throws IOException {
commited = true;

HttpError http = HttpError.fromStatus(status);
Out out = p.getOut();
out.write(http == null ? HttpError.encodeStatusLine(status, UNKNOWN) : http.encoded);
for (Entry<String, List<String>> e : headers.entrySet())
writeHeader(e.getKey(), e.getValue());
Expand Down Expand Up @@ -136,6 +134,7 @@ else if (servletOut == null || contentLength == 0)
}

public void writeHeader(String name, List<String> values) throws IOException {
Out out = p.getOut();
out.write(name.getBytes(StandardCharsets.US_ASCII));
out.write(':');
for (String s : values) {
Expand All @@ -146,6 +145,7 @@ public void writeHeader(String name, List<String> values) throws IOException {
}

public void writeCookie(Cookie c) throws IOException {
Out out = p.getOut();
out.write(COOKIE);
out.write(c.getName().getBytes(StandardCharsets.US_ASCII));
out.write('=');
Expand All @@ -170,6 +170,7 @@ public void writeCookie(Cookie c) throws IOException {

public void writeString(String s) throws IOException {
boolean escape = shouldEscape(s);
Out out = p.getOut();
if (escape)
out.write('"');
int l = 0;
Expand All @@ -196,6 +197,7 @@ public void checkCommited() {
}

public void sendError(HttpError e, int sc, String msg) throws IOException {
Out out = p.getOut();
if (msg == null) {
out.write(e == null ? HttpError.encodeEmptyReponse(sc, UNKNOWN) : e.empty());
return;
Expand All @@ -214,11 +216,11 @@ public void sendError(HttpError e, int sc, String msg) throws IOException {
out.write(ERROR_END);
}

@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "resource" })
private <T extends ServletOutputStream & Output> T createStream() {
if (contentLength < 0)
return (T) new ChunckedOutputStream(out, this, bufferSize);
return (T) new LengthOutputStream(out, this, contentLength);
return (T) new ChunckedOutputStream(p.getOut(), this, bufferSize);
return (T) new LengthOutputStream(p.getOut(), this, contentLength);
}

@Override
Expand Down Expand Up @@ -301,7 +303,7 @@ public void flushBuffer() throws IOException {
commit();
if (servletOut != null)
servletOut.flush();
out.flush();
p.getOut().flush();
}

@Override
Expand Down Expand Up @@ -370,6 +372,7 @@ public void sendRedirect(String location) throws IOException {
commited = true;
status = HttpError.FOUND.code;
commit();
Out out = p.getOut();
out.write(HttpError.FOUND.encoded);
out.write(CONTENT_LENGTH0);
out.write(LOCATION);
Expand Down

0 comments on commit cc586e5

Please sign in to comment.