Skip to content

Commit f7789e9

Browse files
committed
Working
1 parent 88e9f31 commit f7789e9

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

src/main/java/io/fusionauth/http/server/internal/HTTPWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public void run() {
257257
closeSocketOnly(reason);
258258
} catch (ParseException e) {
259259
logger.debug("[{}] Closing socket with status [{}]. Bad request, failed to parse request. Reason [{}] Parser state [{}]", Thread.currentThread().threadId(), Status.BadRequest, e.getMessage(), e.getState());
260-
closeSocketOnError(response, Status.HTTPVersionNotSupported);
260+
closeSocketOnError(response, Status.BadRequest);
261261
} catch (SocketException e) {
262262
// When the HTTPServerThread shuts down, we will interrupt each client thread, so debug log it accordingly.
263263
// - This will cause the socket to throw a SocketException, so log it.

src/test/java/io/fusionauth/http/BaseSocketTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ private void assertResponse(String request, String chunkedExtension, String resp
9999
var os = socket.getOutputStream();
100100
os.write(request.getBytes(StandardCharsets.UTF_8));
101101

102-
// 1. Write bytes, the content-length is short by one byte.
103-
// 2. Next byte \n
104-
// 3. Success.
105-
// 4. Next keep-alive request reads preamble, and reads the \n and throws an exception
106-
107102
assertHTTPResponseEquals(socket, response);
108103
}
109104
}

src/test/java/io/fusionauth/http/BaseTest.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -386,33 +386,32 @@ public void tearDown() {
386386
protected void assertHTTPResponseEquals(Socket socket, String expectedResponse) throws Exception {
387387
var is = socket.getInputStream();
388388
var expectedResponseLength = expectedResponse.getBytes(StandardCharsets.UTF_8).length;
389-
try {
390389

391-
byte[] buffer = new byte[expectedResponseLength * 2];
392-
int read = is.read(buffer);
393-
var actualResponse = new String(buffer, 0, read, StandardCharsets.UTF_8);
394-
395-
// Perform an initial equality check, this is fast. If it fails, it may because there are remaining bytes left to read. This is slower.
396-
if (!actualResponse.equals(expectedResponse)) {
397-
// Note this is going to block until the socket keep-alive times out.
398-
try {
399-
assertResponseEquals(is, actualResponse, expectedResponse);
400-
} catch (SocketException se) {
401-
// If the server has not read the entire request, trying to read from the InputStream will cause a SocketException due to Connection reset.
402-
// - Attempt to recover from this condition and read the response.
403-
// - Note that "normal" HTTP clients won't do this, so this isn't to show what a client would normally see, but it is to show what the server
404-
// is returning regardless if the client is smart enough or cares enough to read the response.
405-
if (se.getMessage().equals("Connection reset")) {
406-
var addr = socket.getRemoteSocketAddress();
407-
socket.close();
390+
byte[] buffer = new byte[expectedResponseLength * 2];
391+
int read = is.read(buffer);
392+
var actualResponse = new String(buffer, 0, read, StandardCharsets.UTF_8);
393+
394+
// Perform an initial equality check, this is fast. If it fails, it may because there are remaining bytes left to read. This is slower.
395+
if (!actualResponse.equals(expectedResponse)) {
396+
// Note this is going to block until the socket keep-alive times out.
397+
try {
398+
assertResponseEquals(is, actualResponse, expectedResponse);
399+
} catch (SocketException se) {
400+
// If the server has not read the entire request, trying to read from the InputStream will cause a SocketException due to Connection reset.
401+
// - Attempt to recover from this condition and read the response.
402+
// - Note that "normal" HTTP clients won't do this, so this isn't to show what a client would normally see, but it is to show what the server
403+
// is returning regardless if the client is smart enough or cares enough to read the response.
404+
if (se.getMessage().equals("Connection reset")) {
405+
var addr = socket.getRemoteSocketAddress();
406+
socket.close();
407+
try {
408408
socket.connect(addr);
409409
assertResponseEquals(socket.getInputStream(), actualResponse, expectedResponse);
410+
} catch (Exception e) {
411+
assertEquals(actualResponse, expectedResponse, "[" + e.getClass().getSimpleName() + "] was thrown trying to read. We are going to assert on what we have.\n");
410412
}
411413
}
412414
}
413-
} catch (SocketException e) {
414-
System.out.println(e);
415-
fail("Failed!", e);
416415
}
417416
}
418417

@@ -470,7 +469,7 @@ private void assertResponseEquals(InputStream is, String actualResponse, String
470469
var remainingBytes = is.readAllBytes();
471470
String fullResponse = actualResponse + new String(remainingBytes, StandardCharsets.UTF_8);
472471
// Use assertEquals so we can get Eclipse error formatting
473-
assertEquals(fullResponse, expectedResponse);
472+
assertEquals(fullResponse, expectedResponse, "An additional [" + remainingBytes.length + "] was read from the InputStream to complete the message.\nInitial expected response\n[" + expectedResponse + "]\nInitial actual response\n[" + actualResponse + "]\n");
474473
} catch (SocketTimeoutException e) {
475474
assertEquals(actualResponse, expectedResponse, "[SocketTimeoutException] was thrown trying to read. We are going to assert on what we have.\n");
476475
}

src/test/java/io/fusionauth/http/HTTP11SocketTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,12 @@ public void transfer_encoding_content_length() throws Exception {
390390
Content-Length: {contentLength}\r
391391
Transfer-Encoding: chunked\r
392392
\r
393-
{body}
394-
"""
393+
{body}"""
395394
).expectResponse("""
396395
HTTP/1.1 200 \r
397396
connection: keep-alive\r
398397
content-length: 0\r
399398
\r
400399
""");
401-
402-
// Order of operation:
403-
// 1. Write body w/ a trailing line return not accounted for in Content-Length.
404-
// 2. Read body, write 200 response. Keep alive.
405-
// 3. Read remaining byte, parse error. Write 400 response. Close connection.
406400
}
407401
}

0 commit comments

Comments
 (0)