void startResponse(Resp resp, Channel channel, int code, boolean isKeepAlive, MediaType contentType) { channel.write(code == 200 ? HTTP_200_OK : HttpResponseCodes.get(code)); addDefaultHeaders(channel, isKeepAlive, contentType); if (resp != null) { if (U.notEmpty(resp.headers())) { for (Map.Entry<String, String> e : resp.headers().entrySet()) { addCustomHeader(channel, e.getKey().getBytes(), e.getValue().getBytes()); } } if (U.notEmpty(resp.cookies())) { for (Map.Entry<String, String> e : resp.cookies().entrySet()) { String cookie = e.getKey() + "=" + e.getValue(); addCustomHeader(channel, HttpHeaders.SET_COOKIE.getBytes(), cookie.getBytes()); } } } }
private static void postProcessRedirect(Resp resp) { String redirect = resp.redirect(); if (redirect != null) { if (resp.code() < 300 || resp.code() >= 400) { resp.code(303); } resp.headers().put(HttpHeaders.LOCATION.name(), redirect); if (resp.result() == null && resp.body() == null) { resp.body(EMPTY_RESPONSE); } } }
private void process(final Req req, final Resp resp, final ProxyMapping mapping, final int attempts, final long since) { final String targetUrl = mapping.getTargetUrl(req); Map<String, String> headers = U.map(req.headers()); headers.remove("transfer-encoding"); headers.remove("content-length"); addExtraRequestHeaders(req, headers); HttpClient client = getOrCreateClient(); client.req() .verb(req.verb()) .url(targetUrl) .headers(headers) .cookies(req.cookies()) .body(req.body()) .raw(true) .execute((result, error) -> { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }); }
void startResponse(Resp resp, Channel channel, int code, boolean isKeepAlive, MediaType contentType) { channel.write(code == 200 ? HTTP_200_OK : HttpResponseCodes.get(code)); addDefaultHeaders(channel, isKeepAlive, contentType); if (resp != null) { if (U.notEmpty(resp.headers())) { for (Map.Entry<String, String> e : resp.headers().entrySet()) { addCustomHeader(channel, e.getKey().getBytes(), e.getValue().getBytes()); } } if (U.notEmpty(resp.cookies())) { for (Map.Entry<String, String> e : resp.cookies().entrySet()) { String cookie = e.getKey() + "=" + e.getValue(); addCustomHeader(channel, HttpHeaders.SET_COOKIE.getBytes(), cookie.getBytes()); } } } }
private static void postProcessRedirect(Resp resp) { String redirect = resp.redirect(); if (redirect != null) { if (resp.code() < 300 || resp.code() >= 400) { resp.code(303); } resp.headers().put(HttpHeaders.LOCATION.name(), redirect); if (resp.result() == null && resp.body() == null) { resp.body(EMPTY_RESPONSE); } } }
@Override public void onDone(HttpResp result, Throwable error) { if (error == null) { resp.code(result.code()); resp.body(result.bodyBytes()); // process the response headers SimpleHttpResp proxyResp = new SimpleHttpResp(); HttpUtils.proxyResponseHeaders(result.headers(), proxyResp); if (proxyResp.contentType != null) resp.contentType(proxyResp.contentType); if (proxyResp.headers != null) resp.headers().putAll(proxyResp.headers); if (proxyResp.cookies != null) resp.cookies().putAll(proxyResp.cookies); resp.done(); } else { handleError(error, req, resp, mapping, attempts, since); } }