Error: Expected: HTTP-version, Received: ""

Great job! The logic flow of your code is clear and correct.

However, the line String content = in.readLine() may not be entirely safe. It assumes the file content (1) contains only one line and (2) ends with a newline, which isn’t always the case.

Consider reading exact n bytes as specified in the request header instead:

Content-Length: n


Debug re-enactment:

  • Problem: The code was responding correctly, but the tester did not receive anything, as indicated by [tester::#QV8] Received: "" (no content received). Why?!

  • Line of Attack: What if we ignore the file processing temporarily, and focus only on ensuring that the tester receive our response first?

  • Brute-Force: I repeatedly inserted the getOutputStream().write line at every position of the code.

  • Intermediate result:

    1. When the write line was below String content = in.readLine(), the error stayed the same .
    2. When the write line was above String content = in.readLine(), a new “Error reading file” appeared, indicating that the tester had received our response but complained that we hadn’t saved the file.
  • Target Acquired: So String content = in.readLine() must have been the culprit, causing the tester to start reading our response before we were able to send it.

  • Nitty-gritty: What exactly happened when String content = in.readLine() was run?

    1. The remaining content to be read was grape strawberry banana apple raspberry pineapple strawberry banana, with no newline at the end.
    2. Without encountering a newline, readLine would attempt to read beyond the end of BufferedReader, instead of stopping just before the end.
    3. TBH, I don’t know exactly what happened here, but that must have caused the tester to start reading our response before we could send it, causing the [tester::#QV8] Received: "" (no content received) error.
1 Like