/** * Creates the response received from the target host. * * @param home Home host * @param dest Destination URL * @param rsp Response received from the target host * @return Response */ private Response response(final String home, final URI dest, final com.jcabi.http.Response rsp) { final Collection<String> hdrs = new LinkedList<String>(); hdrs.add(String.format("X-Takes-TkProxy: from %s to %s by %s", home, dest, this.label)); for (final Map.Entry<String, List<String>> entry : rsp.headers().entrySet()) { for (final String value : entry.getValue()) { final String val; if (TkProxy.isHost(entry.getKey())) { val = this.target.toString(); } else { val = value; } hdrs.add(String.format("%s: %s", entry.getKey(), val)); } } return new RsWithStatus( new RsWithBody(new RsWithHeaders(hdrs), rsp.binary()), rsp.status(), rsp.reason()); }
/** * Creates the request to be forwarded to the target host. * * @param req Original request * @param dest Destination URL * @return Request to be forwarded * @throws IOException If some problem inside */ private com.jcabi.http.Request request(final Request req, final URI dest) throws IOException { final String method = new RqMethod.Base(req).method(); com.jcabi.http.Request proxied = new JdkRequest(dest).method(method); final RqHeaders headers = new RqHeaders.Base(req); for (final String name : headers.names()) { if ("content-length".equals(name.toLowerCase(Locale.ENGLISH))) { continue; } if (TkProxy.isHost(name)) { proxied = proxied.header(name, this.target); continue; } for (final String value : headers.header(name)) { proxied = proxied.header(name, value); } } if (headers.header("Content-Length").iterator().hasNext() || headers.header("Transfer-Encoding").iterator().hasNext()) { final ByteArrayOutputStream output = new ByteArrayOutputStream(); new RqPrint(new RqLengthAware(req)).printBody(output); proxied = proxied.body().set(output.toByteArray()).back(); } return proxied; }