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

I’m stuck on Stage #qv8.

I’ve tried checking the file content. The last couple of lines check the contents of the file being written and I’m getting the correct content back. Not sure what the error is about though.

Here are my logs:

remote: [your_program] /tmp/data/codecrafters.io/http-server-tester/pear_orange_raspberry_pear
remote: [tester::#QV8] Failed to read response: 
remote: [tester::#QV8] Received: "" (no content received)
remote: [tester::#QV8]            ^ error
remote: [tester::#QV8] Error: Expected: HTTP-version, Received: ""
remote: [tester::#QV8] Test failed
remote: [tester::#QV8] Terminating program
remote: [your_program] checking
remote: [your_program] grape strawberry banana apple raspberry pineapple strawberry banana
remote: [tester::#QV8] Program terminated successfully
remote: 

And here’s a snippet of my code:

                    try {
                        in.readLine();
                        in.readLine();
                        String[] path = lines[1].split("/");
                        String fileName = path[2];
                        String content = in.readLine();
                        String filePath = commandLine.getArgList().get(0) + fileName;
                        System.out.println(filePath);
                        FileReaderUtil fileReaderUtil = new FileReaderUtil(filePath);
                        fileReaderUtil.writeStringToFileAndCreateDirectory(content);
                        System.out.println("checking");
                        FileReaderUtil fileReaderUtilCheck = new FileReaderUtil(filePath);
                        String fileContent = fileReaderUtilCheck.readFileAsString();
                        System.out.println(fileContent);
                        this.clientSocket.getOutputStream().write(response.get("fileCreated").getBytes());
                    }catch (IOException e){
                        this.clientSocket.getOutputStream().write(response.get("notFound").getBytes());
                        e.printStackTrace();
                    }

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

@andy1li This was the issue, it’s resolved now. Thank You for a detailed explanation of the issue and a potential solution. :slight_smile:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.