Skip to content

Commit

Permalink
Issue #225 doHead contentLength
Browse files Browse the repository at this point in the history
Fix #225 by:
 + converting the contentLength field to a long
 + checking against bufferSize before setting a content length to simulate a buffer overflow
 + support flush and close
 + pass through async IO listener
  • Loading branch information
gregw committed May 31, 2021
1 parent c04d563 commit 7766577
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions api/src/main/java/jakarta/servlet/http/HttpServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,16 +606,22 @@ class NoBodyResponse extends HttpServletResponseWrapper {
// file private
NoBodyResponse(HttpServletResponse r) {
super(r);
noBody = new NoBodyOutputStream();
}

// file private
void setContentLength() {
void setContentLength() throws IOException {
if (!didSetContentLength) {
if (writer != null) {
writer.flush();
}
setContentLength(noBody.getContentLength());
if (noBody != null) {
long contentLength = noBody.getContentLength();
if (contentLength > getBufferSize() && !isCommitted()) {
flushBuffer(); // commit without content-length
} else {
setContentLengthLong(noBody.getContentLength());
}
}
}
}

Expand Down Expand Up @@ -667,6 +673,9 @@ public ServletOutputStream getOutputStream() throws IOException {
if (writer != null) {
throw new IllegalStateException(lStrings.getString("err.ise.getOutputStream"));
}
if (noBody == null) {
noBody = new NoBodyOutputStream(this);
}
usingOutputStream = true;

return noBody;
Expand Down Expand Up @@ -697,14 +706,18 @@ class NoBodyOutputStream extends ServletOutputStream {
private static final String LSTRING_FILE = "jakarta.servlet.http.LocalStrings";
private static ResourceBundle lStrings = ResourceBundle.getBundle(LSTRING_FILE);

private int contentLength = 0;
private final NoBodyResponse response;
private final ServletOutputStream wrapped;
private long contentLength = 0;

// file private
NoBodyOutputStream() {
NoBodyOutputStream(NoBodyResponse response) throws IOException {
this.response = response;
this.wrapped = response.getResponse().getOutputStream();
}

// file private
int getContentLength() {
long getContentLength() {
return contentLength;
}

Expand Down Expand Up @@ -732,13 +745,24 @@ public void write(byte buf[], int offset, int len) throws IOException {
contentLength += len;
}

@Override
public void flush() throws IOException {
wrapped.flush();
}

@Override
public void close() throws IOException {
response.setContentLength();
super.close();
}

@Override
public boolean isReady() {
return false;
return wrapped.isReady();
}

@Override
public void setWriteListener(WriteListener writeListener) {

wrapped.setWriteListener(writeListener);
}
}

0 comments on commit 7766577

Please sign in to comment.