/** * Check if the given value is allowed in a header field value. * <p> * If it is, an empty value is returned, otherwise, the index of the first character that is not allowed * is returned. * * @param value to check * @return the index of the first character in the given value which is not allowed in a VCHAR field-value, if any. */ public static OptionalInt indexOfNotAllowedInHeaderValue(String value) { final char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (!isAllowedInHeaderValue(c)) { return OptionalInt.of(i); } } return OptionalInt.empty(); }
/** * @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); } }
/** * Check if the given value is allowed in a token field-value. * <p> * If it is, an empty value is returned, otherwise, the index of the first character that is not allowed in a token * is returned. * * @param value to check * @return the index of the first character in the given value which is not allowed in a token field-value, if any. */ public static OptionalInt indexOfNotAllowedInTokens(String value) { final char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (!isAllowedInTokens(c)) { return OptionalInt.of(i); } } return OptionalInt.empty(); }
for (int i = 0; i < headerNameChars.length; i++) { char c = headerNameChars[i]; if (validateHeaders && !FieldValues.isAllowedInTokens(c)) { throw new InvalidHttpHeader("Invalid header name (contains illegal character at index " + i + ")"); OptionalInt illegalIndex = FieldValues.indexOfNotAllowedInHeaderValue(value); if (illegalIndex.isPresent()) { throw new InvalidHttpHeader("Invalid header value (contains illegal character at index " +
OptionalInt illegalNameChar = FieldValues.indexOfNotAllowedInTokens(headerName); if (illegalNameChar.isPresent()) { throw new InvalidHttpHeader("Invalid header name (contains illegal character at index " + illegalNameChar.getAsInt() + ")"); OptionalInt illegalValueChar = FieldValues.indexOfNotAllowedInHeaderValue(value); if (illegalValueChar.isPresent()) { throw new InvalidHttpHeader("Invalid header value (contains illegal character at index " +
throw createError.apply("Header name is too long", lineNumber); if (FieldValues.isAllowedInTokens(c)) { metadataBuilder.append(c); } else { throw createError.apply("Header value is too long", lineNumber); if (FieldValues.isAllowedInHeaderValue(c)) { metadataBuilder.append(c); } else {
private RequestLine buildRequestLine(String requestLine) { if (requestLine.isEmpty()) { throw new InvalidHttpRequest("No content", 0); } String[] parts = requestLine.split("\\s"); if (parts.length == 2 || parts.length == 3) { String method = parts[0]; OptionalInt illegalIndex = FieldValues.indexOfNotAllowedInTokens(method); if (illegalIndex.isPresent()) { throw new InvalidHttpRequest("Invalid method name: illegal character at index " + illegalIndex.getAsInt(), 1); } URI uri = parseUri(parts[1]); HttpVersion httpVersion = options.insertHttpVersionIfMissing() ? HttpVersion.HTTP_1_1 : null; if (parts.length == 3) try { httpVersion = HttpVersion.parse(parts[2]); } catch (IllegalArgumentException e) { throw new InvalidHttpRequest("Invalid HTTP version", 1); } if (httpVersion == null) { throw new InvalidHttpRequest("Missing HTTP version", 1); } return new RequestLine(method, uri, httpVersion); } else { throw new InvalidHttpRequest("Invalid request line", 1); } }
} else { char c = (char) b; if (c == ' ' || FieldValues.isAllowedInVCHARs(c)) { metadataBuilder.append(c); } else {
for (int i = 0; i < headerNameChars.length; i++) { char c = headerNameChars[i]; if (validateHeaders && !FieldValues.isAllowedInTokens(c)) { throw new InvalidHttpHeader("Invalid header name (contains illegal character at index " + i + ")"); OptionalInt illegalIndex = FieldValues.indexOfNotAllowedInHeaderValue(value); if (illegalIndex.isPresent()) { throw new InvalidHttpHeader("Invalid header value (contains illegal character at index " +
OptionalInt illegalNameChar = FieldValues.indexOfNotAllowedInTokens(headerName); if (illegalNameChar.isPresent()) { throw new InvalidHttpHeader("Invalid header name (contains illegal character at index " + illegalNameChar.getAsInt() + ")"); OptionalInt illegalValueChar = FieldValues.indexOfNotAllowedInHeaderValue(value); if (illegalValueChar.isPresent()) { throw new InvalidHttpHeader("Invalid header value (contains illegal character at index " +
throw createError.apply("Header name is too long", lineNumber); if (FieldValues.isAllowedInTokens(c)) { metadataBuilder.append(c); } else { throw createError.apply("Header value is too long", lineNumber); if (FieldValues.isAllowedInHeaderValue(c)) { metadataBuilder.append(c); } else {
private RequestLine buildRequestLine(String requestLine) { if (requestLine.isEmpty()) { throw new InvalidHttpRequest("No content", 0); } String[] parts = requestLine.split("\\s"); if (parts.length == 2 || parts.length == 3) { String method = parts[0]; OptionalInt illegalIndex = FieldValues.indexOfNotAllowedInTokens(method); if (illegalIndex.isPresent()) { throw new InvalidHttpRequest("Invalid method name: illegal character at index " + illegalIndex.getAsInt(), 1); } URI uri = parseUri(parts[1]); HttpVersion httpVersion = options.insertHttpVersionIfMissing() ? HttpVersion.HTTP_1_1 : null; if (parts.length == 3) try { httpVersion = HttpVersion.parse(parts[2]); } catch (IllegalArgumentException e) { throw new InvalidHttpRequest("Invalid HTTP version", 1); } if (httpVersion == null) { throw new InvalidHttpRequest("Missing HTTP version", 1); } return new RequestLine(method, uri, httpVersion); } else { throw new InvalidHttpRequest("Invalid request line", 1); } }
} else { char c = (char) b; if (c == ' ' || FieldValues.isAllowedInVCHARs(c)) { metadataBuilder.append(c); } else {
/** * @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); } }
/** * Check if the given value is allowed in a token field-value. * <p> * If it is, an empty value is returned, otherwise, the index of the first character that is not allowed in a token * is returned. * * @param value to check * @return the index of the first character in the given value which is not allowed in a token field-value, if any. */ public static OptionalInt indexOfNotAllowedInTokens(String value) { final char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (!isAllowedInTokens(c)) { return OptionalInt.of(i); } } return OptionalInt.empty(); }
/** * Check if the given value is allowed in a header field value. * <p> * If it is, an empty value is returned, otherwise, the index of the first character that is not allowed * is returned. * * @param value to check * @return the index of the first character in the given value which is not allowed in a VCHAR field-value, if any. */ public static OptionalInt indexOfNotAllowedInHeaderValue(String value) { final char[] chars = value.toCharArray(); for (int i = 0; i < chars.length; i++) { char c = chars[i]; if (!isAllowedInHeaderValue(c)) { return OptionalInt.of(i); } } return OptionalInt.empty(); }