Atitit.实现反向代理(1)----url rewrite 配置and内容改写 and -绝对路径链接改写 java php
Atitit.实现反向代理(1)----url rewrite 配置and内容改写 and -绝对路径链接改写 java php
3. ---------反向代理rewrite 配置mode_rewrite 1
4. 排除自身(apache_mode_rewrite) 2
4. Abs url 2 /app/http://xxxxxx.jpg 4
1. 代理的实现:::普通代理and反向代理??
普通代理设置麻烦的,韩式反向代理容易的实现..
反向代理使用流程:: localhost/vod/ >>url rewrite >>>内容更改
2. url rewrite 的框架选型
Php的mode_rewrite好的...java的org.tuckey.web.filters.urlrewrite.UrlRewriteFilter 不太好使用,,,子好嘎自实现兰...
作者::老哇的爪子Attilax艾龙,EMAIL:1466519819@qq.com
转载请注明来源:http://blog.csdn.net/attilax
3. ---------反向代理rewrite 配置mode_rewrite
RewriteCond %{REQUEST_URI} !=/process.php
RewriteRule ^(.*)$ /process.php?file=http://www.budejie.com/$1&noext=1 [L] [L]
1. Java的实现
<!--o8j2 -->
<filter>
<filter-name>urlrwt</filter-name>
<filter-class>com.attilax.net.urlRewriteFilterAti</filter-class>
</filter>
<filter-mapping>
<filter-name>urlrwt</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end o8j2-->
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/spr/txtrwt?url="+url+"&urlNoHost="+url_noHost2);
requestDispatcher.forward(request, response);//
4. 排除自身(apache_mode_rewrite)
RewriteCond %{REQUEST_URI} !=/process.php
RewriteCond %{REQUEST_URI} !^.*(\.js|\.gif|\.png|\.jpg|\.jpeg|\.xml|\.ico|\.css)
#other pic css js proxy
RewriteCond %{REQUEST_URI} !=/process.php
RewriteCond %{REQUEST_URI} !=/lensyaohwer/basic.png
RewriteRule ^(.*)$ http://lengxiaohua.com/$1 [P,L]
2. Java的实现 排除自身
//except
if(requestURI.contains("spr/txtrwt"))
{
chain.doFilter(request, response);
return;
}
5. 内容改写(php mode_rwt)
a.文本正则表达式方式
$content=preg_replace('/百思不得姐/', '哈米', $content);
$content=str_replace('http://img.spriteapp.cn/ws/www/img/layout/logo.png', 'http://img.spriteapp.cn/ws/www/img/layout/logoxx.png', $content);
$content=str_replace('不得姐', '哈米', $content);
b.html dom 方式
//nbp
require_once "simple_html_dom.php";
$html = str_get_html($content);
$arr= $html->find('.web_content_right .web_right_down',0) ;
$arr->innertext='a2';
echo $html ;
3. Java 内容改写
@RequestMapping(value = "/txtrwt", produces = "text/plain;charset=gb2312")
// @ResponseBody
public Object process(HttpServletRequest request,
HttpServletResponse response) throws Exception {
core.log("=========process.json");
String url = "" + request.getParameter("url");// +"?"+request.getQueryString();
String url_no_host=request.getParameter("urlNoHost");
// System.out.println(); request.getParameter("tid")
System.out.println(request.getQueryString());
if (url.endsWith("jpg") || url.endsWith("jpeg") || url.endsWith("gif")) {
if(url_no_host.startsWith("/http://"))
{
String url3=url_no_host.replaceAll("/http://", "http://");
byte[] ba = websitex.WebpageContent_Bin(url3);
if( !isLjJpg(ba) )
outputImg(response, url);
}
else
outputImg(response, url);
}
//
else
outputTxt(response, url,request);
return null;
}
6. 内容改写---绝对路径链接改写
4. Abs url 2 /app/http://xxxxxx.jpg
private void outputTxt(HttpServletResponse response, String url, HttpServletRequest request)
throws UnsupportedEncodingException, IOException {
Map<String, String> headProps = Mapx
.<String, String> $()
.add("Cookie",
"lastfid=0; lastvisit=9271%091408838229%09%2Fread.php%3Ftid%3D1715718%26fpage%3D2; ol_offset=32204; ipstate=1408836660; __utma=99888095.1396678057.1405101634.1408806230.1408837132.29; __utmz=99888095.1405101634.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=99888095; pin6=done; __utmb=99888095.3.10.1408837132")
.toMap() ;
String txt=websitex.WebpageContentO8f (url,"gbk",headProps);
txt=txt.replaceAll("http://", pathx.webAppPath_webfmt(request)+ "/http://");
5. Filter 二次导航
if(url_noHost.startsWith("/http://"))
{
String url5=url_noHost.substring(1, url_noHost.length());
String url5_encode= URLEncoder.encode(url5);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/spr/imgrwt_abspath?url="+url5_encode+"&urlNoHost="+url_noHost2);
requestDispatcher.forward(request, response);//
return;
}
6. 判断and代替改写
@RequestMapping(value = "/imgrwt_abspath")
// @ResponseBody
public Object process_imgrwt_abspath(HttpServletRequest request,
HttpServletResponse response) {
core.log("=========process.json");
String url = "" + request.getParameter("url");// +"?"+request.getQueryString();
String url_no_host = request.getParameter("urlNoHost");
// System.out.println(); request.getParameter("tid")
System.out.println(request.getQueryString());
byte[] ba = websitex.WebpageContent_Bin(url);
if (!isLjJpg(ba))
outputImg(response, url);
if(isLjJpg(ba))
outputImg_garfile_holder(response );
return null;
}
/**
@author attilax 老哇的爪子
\t@since Aug 23, 2014 8:48:02 PM$
* @param response
*/
private void outputImg_garfile_holder(HttpServletResponse response) {
// attilax 老哇的爪子 8:48:02 PM Aug 23, 2014
{
String url = pathx.classPath() + "/com/attilax/clr/def.jpg";
byte[] ba = filex.readImageData(url);
outputImg(response, url, ba);
}
}
7. 引用
paip.基于urlrewrite的反向代理以及内容改写