From 1ce0f2f88e4dfe1a794fcf183acb8748a1050731 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Thu, 31 Mar 2016 20:17:20 -0400 Subject: [PATCH] #6 check for protocol --- src/main/java/io/jare/tk/TkRelay.java | 81 +++++++++++++++++++-------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/jare/tk/TkRelay.java b/src/main/java/io/jare/tk/TkRelay.java index ca43336..02a8bc2 100644 --- a/src/main/java/io/jare/tk/TkRelay.java +++ b/src/main/java/io/jare/tk/TkRelay.java @@ -73,6 +73,21 @@ public Response act(final Request req) throws IOException { ); } final URI uri = URI.create(param.next().trim()); + return new RsWithHeader( + new TkProxy(uri.toString()).act( + TkRelay.request(req, this.path(uri)) + ), + String.format("X-Jare-Target: %s", uri) + ); + } + + /** + * Build destination path. + * @param uri URI of destination + * @return Destination path + * @throws HttpException If fails + */ + private String path(final URI uri) throws HttpException { final String host = uri.getHost().toLowerCase(Locale.ENGLISH); final Iterator domains = this.base.domain(host); if (!domains.hasNext()) { @@ -81,35 +96,55 @@ public Response act(final Request req) throws IOException { String.format("domain \"%s\" is not registered", host) ); } + if (!uri.isAbsolute()) { + throw new HttpException( + HttpURLConnection.HTTP_BAD_REQUEST, + String.format("URI \"%s\" is not absolute", uri) + ); + } + final String protocol = uri.getScheme(); + if (!"https".equals(protocol) && !"http".equals(protocol)) { + throw new HttpException( + HttpURLConnection.HTTP_BAD_REQUEST, + String.format( + "protocol must be either HTTP or HTTPS at \"%s\"", + uri + ) + ); + } final String path; if (uri.getPath().isEmpty()) { path = "/"; } else { path = uri.getPath(); } - return new RsWithHeader( - new TkProxy(uri.toString()).act( - new Request() { - @Override - public Iterable head() throws IOException { - return Iterables.concat( - Collections.singleton( - String.format( - "GET %s HTTP/1.1", - path - ) - ), - Iterables.skip(req.head(), 1) - ); - } - @Override - public InputStream body() throws IOException { - return req.body(); - } - } - ), - String.format("X-Jare-Target: %s", uri) - ); + return path; } + /** + * The request to send. + * @param req Original request + * @param path Destination path + * @return Request + */ + private static Request request(final Request req, final String path) { + return new Request() { + @Override + public Iterable head() throws IOException { + return Iterables.concat( + Collections.singleton( + String.format( + "GET %s HTTP/1.1", + path + ) + ), + Iterables.skip(req.head(), 1) + ); + } + @Override + public InputStream body() throws IOException { + return req.body(); + } + }; + } }