/** * @param host the host to be used in the method line's URI. * @return a copy of this method line, but with the given host */ public RequestLine withHost(String host) { try { if (!host.matches("[a-z]{1,6}://.*")) { host = "http://" + host; } URI hostURI = URI.create(host); URI newURI = new URI(hostURI.getScheme(), hostURI.getUserInfo(), hostURI.getHost(), hostURI.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); return new RequestLine(method, newURI, httpVersion); } catch (Exception e) { throw new IllegalArgumentException("Invalid host format" + Optional.ofNullable( e.getMessage()).map(s -> ": " + s).orElse("")); } }
/** * @param host the host to be used in the method line's URI. * @return a copy of this method line, but with the given host */ public RequestLine withHost(String host) { try { if (!host.matches("[a-z]{1,6}://.*")) { host = "http://" + host; } URI hostURI = URI.create(host); URI newURI = new URI(hostURI.getScheme(), hostURI.getUserInfo(), hostURI.getHost(), hostURI.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); return new RequestLine(method, newURI, httpVersion); } catch (Exception e) { throw new IllegalArgumentException("Invalid host format" + Optional.ofNullable( e.getMessage()).map(s -> ": " + s).orElse("")); } }
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); } }
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); } }