Skip to content

Commit

Permalink
fix empty resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon committed Feb 21, 2024
1 parent 308d634 commit eb34910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ private void process(HttpServletRequest req, HttpServletResponse resp, boolean c

if (!content)
return;
ServletOutputStream os = resp.getOutputStream();
os.write(data);

try (ServletOutputStream os = resp.getOutputStream()) {
os.write(data);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import unknow.server.http.HttpConnection;
import unknow.server.http.HttpError;
import unknow.server.http.servlet.out.ChunckedOutputStream;
import unknow.server.http.servlet.out.EmptyStream;
import unknow.server.http.servlet.out.LengthOutputStream;
import unknow.server.http.servlet.out.Output;
import unknow.server.http.servlet.out.ServletWriter;
Expand Down Expand Up @@ -220,6 +221,8 @@ public void sendError(HttpError e, int sc, String msg) throws IOException {
private <T extends ServletOutputStream & Output> T createStream() {
if (contentLength < 0)
return (T) new ChunckedOutputStream(p.getOut(), this, bufferSize);
if (contentLength == 0)
return (T) EmptyStream.INSTANCE;
return (T) new LengthOutputStream(p.getOut(), this, contentLength);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public void setWriteListener(WriteListener writeListener) { // OK
}

@Override
public void write(int b) throws IOException { // OK
public void write(int b) throws IOException {
throw new IOException("empty stream");
}

@Override
public void write(byte[] b) throws IOException {
if (b.length > 0)
throw new IOException("empty stream");
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
if (len > 0)
throw new IOException("empty stream");
}
}

0 comments on commit eb34910

Please sign in to comment.