'Parsing HTTP Request in Java

I am building a simple http request parser in Java as a learning exercise at work. My task is to simply get a POST request which includes the body, parse it and send back the response according to http protocol (status 200 OK).

When I post request with curl command, the request line and the headers get printed out but the body doesn't. The body gets printed after I press Ctrl c to get out of the curl.

The nc -l command works fine and the body gets printed out together with headers.

What is the difference between these two and how can I print the request body with curl post command?

My code is below, any input is appreciated.

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class Server {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static String inputLine;
private static PrintWriter output;

public static void main(String[] args) {
    try {
        serverSocket = new ServerSocket(3000);

        while (true) {
            List<String> list = new ArrayList<String>();
            clientSocket = serverSocket.accept();
            bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            output = new PrintWriter(clientSocket.getOutputStream());

            inputLine = bufferedReader.readLine();
            while (inputLine.length() > 0) {
                System.out.println(inputLine);
                list.add(inputLine);
                inputLine = bufferedReader.readLine();
            }

           String bodyLine = bufferedReader.readLine();
            while(bodyLine != null && bodyLine.length() > 0){
                System.out.println(bodyLine);
                output.println(bodyLine);
                bodyLine = bufferedReader.readLine();
            }

            /* My Request class has a parse method that takes a List and parses in accordingly. getRequestLine() returns the request line of the request and getMethod() its method. */

            Request r = Request.parse(list);
            if (r.getRequestLine().getMethod().equals("GET")){
                output.println("HTTP/1.0 200 OK");
                output.println("Content-Type: text/html");
                output.println("");
                output.println("<p>GET REQUEST: </p><p>" + r + "</p>");
            }

            if (r.getRequestLine().getMethod().equals("POST")){
                output.println("HTTP/1.0 200 OK");
                output.println("Content-Type: text/html");
                output.println("");
                output.println("<p>POST REQUEST:</p><p>" + r + "</p>");
            }

            output.close();
            bufferedReader.close();
            clientSocket.close();

        }


    } catch (IOException error) {
        System.out.println(error);

    }

}

}

Request class:

import java.util.*;

public class Request {
private Map<String, String> headers;
private String body;
private RequestLine requestLine;

public Request(Map<String, String> headers, String body, RequestLine requestLine) {
    this.headers = headers;
    this.body = body;
    this.requestLine = requestLine;
}

public Map<String, String> getHeaders() {
    return headers;
}

public String getBody() {
    return body;
}

public RequestLine getRequestLine() {
    return requestLine;
}

public String toString() {
    return "Request Line: " + getRequestLine() + '\n' +  "Headers: " + getHeaders();
}

public static Request parse(List<String> lines) {
    String requestLineStr = lines.get(0);
    String[] requestLineArr = requestLineStr.split(" ");
    String method = requestLineArr[0];
    String URI = requestLineArr[1];
    String status = requestLineArr[2];

    RequestLine requestLine = new RequestLine(URI, method, status);


    //  int index = lines.indexOf("\r\n\r\n");
    //  List<String> headerList = lines.subList(1, index);
    // List<String> bodyList = lines.subList(index, -1);

    List<String> headerList = lines.subList(1, lines.size()-1);
    Map<String, String> headersMap = new HashMap<>();

    for(int i = 0; i <headerList.size(); i++) {
        String line = headerList.get(i);
        String[] arr = line.split(" ");
        headersMap.put(arr[0], arr[1]);
    }

    /*String bodyString = "";
    for(int i = 0; i < bodyList.size(); i++) {
        bodyString += bodyList.get(i);
    }*/

    return new Request(headersMap, "", requestLine);
}

}



Solution 1:[1]

LF = HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body. RFC2616

The Header goes till \r\n. After that the body starts.

readLine

public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached Throws: IOException - If an I/O error occurs BufferedReader#readLine()

So just add an additional bufferedReader.readLine(); between your header reading and body reading.

inputLine = bufferedReader.readLine();
        while (inputLine.length() > 0) {
            System.out.println(inputLine);
            list.add(inputLine);
            inputLine = bufferedReader.readLine();
        }
  bufferedReader.readLine();
  String bodyLine = bufferedReader.readLine();
        while(bodyLine != null && bodyLine.length() > 0){
            System.out.println(bodyLine);
            output.println(bodyLine);
            bodyLine = bufferedReader.readLine();
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Wasilewski