Skip to content

Commit b287d4c

Browse files
committed
Cleanup and rework of counting
1 parent aac9fd6 commit b287d4c

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

src/main/java/io/fusionauth/http/server/io/HTTPInputStream.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,17 @@ public int read(byte[] b, int off, int len) throws IOException {
162162
reportBytesRead -= extraBytes;
163163
pushbackInputStream.push(b, (int) bytesRemaining, extraBytes);
164164
}
165-
}
166165

167-
if (read > 0 && fixedLength) {
168166
bytesRemaining -= reportBytesRead;
169167
}
170168

171169
bytesRead += reportBytesRead;
172170

173171
// Note that when the request is fixed length, we will have failed early during commit().
174172
// - This will handle all requests that are not fixed length.
175-
if (maximumContentLength != -1) {
176-
if (bytesRead > maximumContentLength) {
177-
String detailedMessage = "The maximum request size has been exceeded. The maximum request size is [" + maximumContentLength + "] bytes.";
178-
throw new ContentTooLargeException(maximumContentLength, detailedMessage);
179-
}
173+
if (maximumContentLength != -1 && bytesRead > maximumContentLength) {
174+
String detailedMessage = "The maximum request size has been exceeded. The maximum request size is [" + maximumContentLength + "] bytes.";
175+
throw new ContentTooLargeException(maximumContentLength, detailedMessage);
180176
}
181177

182178
return reportBytesRead;
@@ -207,11 +203,9 @@ private void commit() {
207203
// If we have a maximumContentLength, and this is a fixed content length request, before we read any bytes, fail early.
208204
// For good measure do this last so if anyone downstream wants to read from the InputStream they could in theory because
209205
// we will have set up the InputStream.
210-
if (contentLength != null && maximumContentLength != -1) {
211-
if (contentLength > maximumContentLength) {
212-
String detailedMessage = "The maximum request size has been exceeded. The reported Content-Length is [" + contentLength + "] and the maximum request size is [" + maximumContentLength + "] bytes.";
213-
throw new ContentTooLargeException(maximumContentLength, detailedMessage);
214-
}
206+
if (contentLength != null && maximumContentLength != -1 && contentLength > maximumContentLength) {
207+
String detailedMessage = "The maximum request size has been exceeded. The reported Content-Length is [" + contentLength + "] and the maximum request size is [" + maximumContentLength + "] bytes.";
208+
throw new ContentTooLargeException(maximumContentLength, detailedMessage);
215209
}
216210
}
217211
}

src/main/java/io/fusionauth/http/util/HTTPTools.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,9 @@ public static void parseRequestPreamble(PushbackInputStream inputStream, int max
296296

297297
int read = 0;
298298
int index = 0;
299-
int bytesRead = 0;
300-
boolean readExceeded = false;
299+
int premableLength = 0;
301300

302301
while (state != RequestPreambleState.Complete) {
303-
if (readExceeded) {
304-
throw new RequestHeadersTooLargeException(maxRequestHeaderSize, "The maximum size of the request header has been exceeded. The maximum size is [" + maxRequestHeaderSize + "] bytes.");
305-
}
306-
307302
long start = System.currentTimeMillis();
308303
read = inputStream.read(requestBuffer);
309304

@@ -316,17 +311,10 @@ public static void parseRequestPreamble(PushbackInputStream inputStream, int max
316311
logger.trace("Read [{}] from client for preamble.", read);
317312

318313
// Tell the callback that we've read at least one byte
319-
if (bytesRead == 0) {
314+
if (premableLength == 0) {
320315
readObserver.run();
321316
}
322317

323-
// bytesRead will include bytes that are read past the end of the preamble. This should be ok because we will only throw an exception
324-
// for readExceeded once we have reached this state AND we are not yet in a complete state.
325-
// - If we exceed the max header size from this read, as long as this buffer includes the entire preamble we will not throw
326-
// an exception.
327-
bytesRead += read;
328-
readExceeded = maxRequestHeaderSize != -1 && bytesRead >= maxRequestHeaderSize;
329-
330318
for (index = 0; index < read && state != RequestPreambleState.Complete; index++) {
331319
// If there is a state transition, store the value properly and reset the builder (if needed)
332320
byte ch = requestBuffer[index];
@@ -352,6 +340,12 @@ public static void parseRequestPreamble(PushbackInputStream inputStream, int max
352340

353341
state = nextState;
354342
}
343+
344+
// index is the number of bytes we processed as part of the preamble
345+
premableLength += index;
346+
if (premableLength > maxRequestHeaderSize) {
347+
throw new RequestHeadersTooLargeException(maxRequestHeaderSize, "The maximum size of the request header has been exceeded. The maximum size is [" + maxRequestHeaderSize + "] bytes.");
348+
}
355349
}
356350

357351
// Push back the leftover bytes

0 commit comments

Comments
 (0)