Skip to content

Commit

Permalink
Still chance of upload corruption if an 0x0d is encountered, follow…
Browse files Browse the repository at this point in the history
…ed by an `0xff`. This was getting interpreted as end of stream.
  • Loading branch information
brett-smith committed Mar 9, 2024
1 parent e1c3eb5 commit 42cde4b
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/main/java/com/sshtools/uhttpd/UHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -4959,19 +4959,23 @@ enum State {
private ByteBuffer backBuffer;

private ByteBuffer readBuffer;

private int index = 0;

private MultipartBoundaryStream(InputStream chin, String boundary, MultipartFormDataPartIterator iterator) {
this.chin = chin;
this.iterator = iterator;
boundaryBytes = ("\r\n--" + boundary).getBytes(HTTP_CHARSET_ENCODING);

backBuffer = ByteBuffer.allocate(boundaryBytes.length);

}

@Override
public int read() throws IOException {
if (state == State.END)
return -1;


while (true) {

Expand All @@ -4981,10 +4985,10 @@ public int read() throws IOException {
if (r == -1) {
state = State.END;
return r;
}
}
} else {
if (readBuffer.hasRemaining())
return readBuffer.get();
return Byte.toUnsignedInt(readBuffer.get());
backBuffer.clear();
readBuffer = null;
continue;
Expand All @@ -5004,13 +5008,10 @@ public int read() throws IOException {
}
} else {
if (backBuffer.position() == 0) {
state = State.WAIT_BOUNDARY;
matchIndex = 0;
resetToState(State.WAIT_BOUNDARY);
return r;
} else {
backBuffer.put((byte) r);
backBuffer.flip();
readBuffer = backBuffer;
bufferAndFlip(r);
}
}
break;
Expand Down Expand Up @@ -5038,13 +5039,10 @@ public int read() throws IOException {
}
} else {
if (backBuffer.position() == 0) {
state = State.WAIT_BOUNDARY;
matchIndex = 0;
resetToState(State.WAIT_BOUNDARY);
return r;
} else {
backBuffer.put((byte) r);
backBuffer.flip();
readBuffer = backBuffer;
bufferAndFlip(r);
}
}
}
Expand All @@ -5061,12 +5059,10 @@ public int read() throws IOException {
}
} else {
if (backBuffer.position() == 0) {
matchIndex = 0;
resetToState(State.WAIT_BOUNDARY);
return r;
} else {
backBuffer.put((byte) r);
backBuffer.flip();
readBuffer = backBuffer;
bufferAndFlip(r);
}
}
break;
Expand All @@ -5075,6 +5071,17 @@ public int read() throws IOException {
}
}
}

private void resetToState(State state) {
this.state = state;
matchIndex = 0;
}

private void bufferAndFlip(int r) {
backBuffer.put((byte) r);
backBuffer.flip();
readBuffer = backBuffer;
}
}

private final static class MultipartFormDataPartIterator extends WebChannel implements Iterator<Part> {
Expand Down

0 comments on commit 42cde4b

Please sign in to comment.