| | |
| | | import org.apache.http.client.config.RequestConfig; |
| | | import org.apache.http.client.utils.URIUtils; |
| | | import org.apache.http.entity.InputStreamEntity; |
| | | import org.apache.http.entity.StringEntity; |
| | | import org.apache.http.impl.client.CloseableHttpClient; |
| | | import org.apache.http.impl.client.HttpClients; |
| | | import org.apache.http.message.BasicHeader; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.PrintWriter; |
| | | import java.net.HttpCookie; |
| | | import java.net.URI; |
| | | import java.net.URISyntaxException; |
| | |
| | | // copyRequestHeaders(request, proxyRequest, host); |
| | | //setXrForwardedForHeader(request, proxyRequest); |
| | | |
| | | //if (!StringHelper.isEmpty(cookie)) { |
| | | // proxyRequest.addHeader("Cookie", cookie + "; "); |
| | | //} |
| | | // if (!StringHelper.isEmpty(cookie)) proxyRequest.addHeader("Cookie", cookie + "; ") |
| | | |
| | | CloseableHttpClient client = null; |
| | | HttpResponse proxyResponse = null; |
| | |
| | | |
| | | Header[] headers = proxyResponse.getHeaders("content-type"); |
| | | if (null != headers && headers.length > 0 && headers[0].getValue().contains(StaticData.TEXT_XML)) { |
| | | String str = EntityUtils.toString(entity, "UTF-8"); |
| | | if (str.contains(res.getUrl())) { |
| | | String proxyUrl = res.getProxy().replace("{token}", response.getHeader("token")); |
| | | proxyUrl = request.getRequestURL().substring(0, request.getRequestURL().indexOf(proxyUrl) + proxyUrl.length()); |
| | | str = str.replace(res.getUrl(), proxyUrl); |
| | | String str = filterStr(request, response, res, EntityUtils.toString(entity, "UTF-8")); |
| | | |
| | | byte[] bytes = str.getBytes(StandardCharsets.UTF_8); |
| | | response.setContentLength(bytes.length); |
| | | outputStream.write(bytes); |
| | | } |
| | | byte[] bytes = str.getBytes(StandardCharsets.UTF_8); |
| | | response.setContentLength(bytes.length); |
| | | outputStream.write(bytes); |
| | | return; |
| | | } |
| | | |
| | | entity.writeTo(outputStream); |
| | | } |
| | | |
| | | private String filterStr(HttpServletRequest request, HttpServletResponse response, ResEntity res, String str) { |
| | | String url = res.getUrl(); |
| | | if (url.contains(StaticData.AK)) { |
| | | String[] strs = url.split("\\?"); |
| | | url = strs[0]; |
| | | |
| | | if (!StringHelper.isEmpty(strs[1])) { |
| | | str = str.replace("?" + strs[1], ""); |
| | | } |
| | | } |
| | | if (str.contains(url)) { |
| | | String proxyUrl = res.getProxy().replace("{token}", response.getHeader("token")); |
| | | proxyUrl = request.getRequestURL().substring(0, request.getRequestURL().indexOf(proxyUrl) + proxyUrl.length()); |
| | | str = str.replace(res.getUrl(), proxyUrl); |
| | | } |
| | | |
| | | return str; |
| | | } |
| | | } |