Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix empty resource #37

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}