/** * Read the trailer-part. * <p> * This method must only be called after the last chunk has been read via * {@link ChunkedBodyParser#readNextChunk(InputStream)}. * * @param inputStream to read trailer from * @return the trailer part of the message * @throws IOException if an error occurs while reading the stream */ public RawHttpHeaders readTrailer(InputStream inputStream) throws IOException { BiFunction<String, Integer, RuntimeException> errorCreator = (msg, lineNumber) -> new IllegalStateException(msg + " (trailer header)"); try { return metadataParser.parseHeaders(inputStream, errorCreator); } catch (InvalidHttpHeader e) { throw new InvalidHttpHeader(e.getMessage() + " (trailer header)"); } }
/** * Read the trailer-part. * <p> * This method must only be called after the last chunk has been read via * {@link ChunkedBodyParser#readNextChunk(InputStream)}. * * @param inputStream to read trailer from * @return the trailer part of the message * @throws IOException if an error occurs while reading the stream */ public RawHttpHeaders readTrailer(InputStream inputStream) throws IOException { BiFunction<String, Integer, RuntimeException> errorCreator = (msg, lineNumber) -> new IllegalStateException(msg + " (trailer header)"); try { return metadataParser.parseHeaders(inputStream, errorCreator); } catch (InvalidHttpHeader e) { throw new InvalidHttpHeader(e.getMessage() + " (trailer header)"); } }
/** * 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); }
/** * Parses the HTTP request produced by the given stream. * * @param inputStream producing a HTTP request * @param senderAddress the address of the request sender, if known * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpRequest parseRequest(InputStream inputStream, @Nullable InetAddress senderAddress) throws IOException { RequestLine requestLine = metadataParser.parseRequestLine(inputStream); RawHttpHeaders originalHeaders = metadataParser.parseHeaders(inputStream, (message, lineNumber) -> // add 1 to the line number to correct for the start-line new InvalidHttpRequest(message, lineNumber + 1)); RawHttpHeaders.Builder modifiableHeaders = RawHttpHeaders.newBuilder(originalHeaders); // do a little cleanup to make sure the request is actually valid requestLine = verifyHost(requestLine, modifiableHeaders); RawHttpHeaders headers = modifiableHeaders.build(); @Nullable BodyReader bodyReader = requestHasBody(headers) ? createBodyReader(inputStream, requestLine, headers) : null; return new RawHttpRequest(requestLine, headers, bodyReader, senderAddress); }
/** * Parses the HTTP request produced by the given stream. * * @param inputStream producing a HTTP request * @param senderAddress the address of the request sender, if known * @return a parsed HTTP request object * @throws InvalidHttpRequest if the request is invalid * @throws IOException if a problem occurs accessing the stream */ public RawHttpRequest parseRequest(InputStream inputStream, @Nullable InetAddress senderAddress) throws IOException { RequestLine requestLine = metadataParser.parseRequestLine(inputStream); RawHttpHeaders originalHeaders = metadataParser.parseHeaders(inputStream, (message, lineNumber) -> // add 1 to the line number to correct for the start-line new InvalidHttpRequest(message, lineNumber + 1)); RawHttpHeaders.Builder modifiableHeaders = RawHttpHeaders.newBuilder(originalHeaders); // do a little cleanup to make sure the request is actually valid requestLine = verifyHost(requestLine, modifiableHeaders); RawHttpHeaders headers = modifiableHeaders.build(); @Nullable BodyReader bodyReader = requestHasBody(headers) ? createBodyReader(inputStream, requestLine, headers) : null; return new RawHttpRequest(requestLine, headers, bodyReader, senderAddress); }