/** * Determines whether a response with the given status-line should have a body. * <p> * If provided, the request-line of the request which produced such response is taken into * consideration. See <a href="https://tools.ietf.org/html/rfc7230#section-3.3">Section 3.3</a> * of RFC-7230 for details. * * @param statusLine status-line of response * @param requestLine request-line of request, if any * @return true if such response has a body, false otherwise */ public static boolean responseHasBody(StatusLine statusLine, @Nullable RequestLine requestLine) { if (requestLine != null) { if (requestLine.getMethod().equalsIgnoreCase("HEAD")) { return false; // HEAD response must never have a body } if (requestLine.getMethod().equalsIgnoreCase("CONNECT") && startsWith(2, statusLine.getStatusCode())) { return false; // CONNECT successful means start tunelling } } int statusCode = statusLine.getStatusCode(); // All 1xx (Informational), 204 (No Content), and 304 (Not Modified) // responses do not include a message body. boolean hasNoBody = startsWith(1, statusCode) || statusCode == 204 || statusCode == 304; return !hasNoBody; }
/** * Determines whether a response with the given status-line should have a body. * <p> * If provided, the request-line of the request which produced such response is taken into * consideration. See <a href="https://tools.ietf.org/html/rfc7230#section-3.3">Section 3.3</a> * of RFC-7230 for details. * * @param statusLine status-line of response * @param requestLine request-line of request, if any * @return true if such response has a body, false otherwise */ public static boolean responseHasBody(StatusLine statusLine, @Nullable RequestLine requestLine) { if (requestLine != null) { if (requestLine.getMethod().equalsIgnoreCase("HEAD")) { return false; // HEAD response must never have a body } if (requestLine.getMethod().equalsIgnoreCase("CONNECT") && startsWith(2, statusLine.getStatusCode())) { return false; // CONNECT successful means start tunelling } } int statusCode = statusLine.getStatusCode(); // All 1xx (Informational), 204 (No Content), and 304 (Not Modified) // responses do not include a message body. boolean hasNoBody = startsWith(1, statusCode) || statusCode == 204 || statusCode == 304; return !hasNoBody; }