/** * Determines whether a response with the given status-line should have a body. * <p> * This method ignores the request-line of the request which produced such response. If the request * is known, use the {@link #responseHasBody(StatusLine, RequestLine)} method instead. * * @param statusLine status-line of response * @return true if such response has a body, false otherwise */ public static boolean responseHasBody(StatusLine statusLine) { return responseHasBody(statusLine, null); }
/** * Determines whether a response with the given status-line should have a body. * <p> * This method ignores the request-line of the request which produced such response. If the request * is known, use the {@link #responseHasBody(StatusLine, RequestLine)} method instead. * * @param statusLine status-line of response * @return true if such response has a body, false otherwise */ public static boolean responseHasBody(StatusLine statusLine) { return responseHasBody(statusLine, null); }
/** * Parses the HTTP response produced by the given stream. * * @param inputStream producing a HTTP response * @param requestLine optional {@link RequestLine} of the request which results in this response. * If provided, it is taken into consideration when deciding whether the response contains * a body. See <a href="https://tools.ietf.org/html/rfc7230#section-3.3">Section 3.3</a> * of RFC-7230 for details. * @return a parsed HTTP response object * @throws InvalidHttpResponse if the response is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpResponse<Void> parseResponse(InputStream inputStream, @Nullable RequestLine requestLine) throws IOException { StatusLine statusLine = metadataParser.parseStatusLine(inputStream); RawHttpHeaders headers = metadataParser.parseHeaders(inputStream, (message, lineNumber) -> // add 1 to the line number to correct for the start-line new InvalidHttpResponse(message, lineNumber + 1)); @Nullable BodyReader bodyReader = responseHasBody(statusLine, requestLine) ? createBodyReader(inputStream, statusLine, headers) : null; return new RawHttpResponse<>(null, null, statusLine, headers, bodyReader); }
/** * Parses the HTTP response produced by the given stream. * * @param inputStream producing a HTTP response * @param requestLine optional {@link RequestLine} of the request which results in this response. * If provided, it is taken into consideration when deciding whether the response contains * a body. See <a href="https://tools.ietf.org/html/rfc7230#section-3.3">Section 3.3</a> * of RFC-7230 for details. * @return a parsed HTTP response object * @throws InvalidHttpResponse if the response is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpResponse<Void> parseResponse(InputStream inputStream, @Nullable RequestLine requestLine) throws IOException { StatusLine statusLine = metadataParser.parseStatusLine(inputStream); RawHttpHeaders headers = metadataParser.parseHeaders(inputStream, (message, lineNumber) -> // add 1 to the line number to correct for the start-line new InvalidHttpResponse(message, lineNumber + 1)); @Nullable BodyReader bodyReader = responseHasBody(statusLine, requestLine) ? createBodyReader(inputStream, statusLine, headers) : null; return new RawHttpResponse<>(null, null, statusLine, headers, bodyReader); }