/** * @return an instance of {@link RawHttpOptions} that is as strict as the HTTP specification. It will not, * for example, allow LF without a CR character, or insert a Host header in a request when it's missing. */ public static RawHttpOptions strict() { return RawHttpOptions.newBuilder() .doNotAllowNewLineWithoutReturn() .doNotInsertHostHeaderIfMissing() .build(); }
/** * Parses the HTTP messages' request-line from the given input stream. * * @param inputStream supplying the request-line * @return request-line * @throws IOException if an error occurs while consuming the stream */ public RequestLine parseRequestLine(InputStream inputStream) throws IOException { return buildRequestLine(parseStartLine(inputStream, InvalidHttpRequest::new, options.ignoreLeadingEmptyLine())); }
/** * Parses a HTTP response's status-line. * * @param inputStream providing the status-line * @return the status-line * @throws InvalidHttpResponse if the status-line is invalid * @throws IOException if an error occurs while consuming the stream */ public StatusLine parseStatusLine(InputStream inputStream) throws IOException { return buildStatusLine(parseStartLine(inputStream, InvalidHttpResponse::new, options.ignoreLeadingEmptyLine())); }
private static RawHttpHeaders contentTypeHeaderWithValue(String value) { return RawHttpHeaders.newBuilder() .with("Content-Type", value) .build(); }
/** * Parses the HTTP messages' headers from the given input stream. * * @param inputStream supplying the header fields * @param createError error factory - used in case an error is encountered * @return the {@link RawHttpHeaders} * @throws IOException if an error occurs while consuming the stream */ public RawHttpHeaders parseHeaders(InputStream inputStream, BiFunction<String, Integer, RuntimeException> createError) throws IOException { RawHttpHeaders headers = buildHeaders(inputStream, createError).build(); options.getHttpHeadersOptions().getHeadersValidator().accept(headers); return headers; }
private RawHttpHeaders.Builder buildHeaders( InputStream stream, BiFunction<String, Integer, RuntimeException> createError) throws IOException { RawHttpHeaders.Builder builder = RawHttpHeaders.newBuilderSkippingValidation(); int lineNumber = 1; Map.Entry<String, String> header; while ((header = parseHeaderField(stream, lineNumber, createError)) != null) { builder.with(header.getKey(), header.getValue()); lineNumber++; } return builder; }
/** * @return a strict HTTP metadata parser (uses {@link RawHttpOptions#strict()}. */ public static HttpMetadataParser createStrictHttpMetadataParser() { return new HttpMetadataParser(RawHttpOptions.strict()); }
public ChunkedBodyParser(HttpMetadataParser metadataParser) { this.metadataParser = metadataParser; this.allowNewLineWithoutReturn = metadataParser.getOptions().allowNewLineWithoutReturn(); }
/** * @param c character * @return true if the given char is allowed in a header value, false otherwise. */ public static boolean isAllowedInHeaderValue(char c) { return c == ' ' || c == '\t' || isAllowedInVCHARs(c) || isAllowedInObsText(c); } }
/** * Parses the HTTP response produced by the given stream. * * @param inputStream producing a HTTP response * @return a parsed HTTP response object * @throws InvalidHttpResponse if the response is invalid * @throws IOException if a problem occurs accessing the stream */ public final RawHttpResponse<Void> parseResponse(InputStream inputStream) throws IOException { return parseResponse(inputStream, null); }
static EagerHttpResponse<Void> getServerErrorResponse(HttpVersion httpVersion) { if (httpVersion.isOlderThan(HttpVersion.HTTP_1_1)) { return SERVER_ERROR_500_HTTP1_0; } else { return SERVER_ERROR_500_HTTP1_1; } }
/** * Create a new instance of {@link RawHttp} using the default {@link RawHttpOptions} instance. */ public RawHttp() { this(RawHttpOptions.defaultInstance()); }
/** * Create a configured instance of {@link RawHttp}. * * @param options configuration options */ public RawHttp(RawHttpOptions options) { this.options = options; this.metadataParser = new HttpMetadataParser(options); }
/** * Parses a HTTP request's request-line. * * @param statusLine the request-line * @return the request-line * @throws InvalidHttpRequest if the request-line is invalid */ public RequestLine parseRequestLine(String statusLine) { return buildRequestLine(statusLine); }
/** * @return an instance of {@link RawHttpOptions} that is as strict as the HTTP specification. It will not, * for example, allow LF without a CR character, or insert a Host header in a request when it's missing. */ public static RawHttpOptions strict() { return RawHttpOptions.newBuilder() .doNotAllowNewLineWithoutReturn() .doNotInsertHostHeaderIfMissing() .build(); }
private RawHttpHeaders.Builder buildHeaders( InputStream stream, BiFunction<String, Integer, RuntimeException> createError) throws IOException { RawHttpHeaders.Builder builder = RawHttpHeaders.newBuilderSkippingValidation(); int lineNumber = 1; Map.Entry<String, String> header; while ((header = parseHeaderField(stream, lineNumber, createError)) != null) { builder.with(header.getKey(), header.getValue()); lineNumber++; } return builder; }
/** * Parses the HTTP messages' request-line from the given input stream. * * @param inputStream supplying the request-line * @return request-line * @throws IOException if an error occurs while consuming the stream */ public RequestLine parseRequestLine(InputStream inputStream) throws IOException { return buildRequestLine(parseStartLine(inputStream, InvalidHttpRequest::new, options.ignoreLeadingEmptyLine())); }
/** * Parses a HTTP response's status-line. * * @param inputStream providing the status-line * @return the status-line * @throws InvalidHttpResponse if the status-line is invalid * @throws IOException if an error occurs while consuming the stream */ public StatusLine parseStatusLine(InputStream inputStream) throws IOException { return buildStatusLine(parseStartLine(inputStream, InvalidHttpResponse::new, options.ignoreLeadingEmptyLine())); }
/** * @return a strict HTTP metadata parser (uses {@link RawHttpOptions#strict()}. */ public static HttpMetadataParser createStrictHttpMetadataParser() { return new HttpMetadataParser(RawHttpOptions.strict()); }
public ChunkedBodyParser(HttpMetadataParser metadataParser) { this.metadataParser = metadataParser; this.allowNewLineWithoutReturn = metadataParser.getOptions().allowNewLineWithoutReturn(); }