Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19266326
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: BSD

2008-10-31 16:25:30

一般应用中有ajax进行跨子域代理调用时,通常需要设置HTTP代理,解决域的权限问题。BSD爱好者乐园e,z+jmC_
举例,blog.chinajavaworld.com中用ajax调search子系统的服务,Resin的HttpProxyServlet设置如下:BSD爱好者乐园6uL7CHNl!o4s([

  http-proxy
  com.caucho.servlets.HttpProxyServlet
  'search.chinajavaworld.com:80'/>
 
 
"/ajax/search/*"servlet-name="http-proxy"/>
BSD爱好者乐园&X$Be8fF3sz.B m9K
BSD爱好者乐园v&MOUt.C
在search.chinajavaworld.com/ajax/search/test.jsp中
,Mm w5Z,M;vs6u;O1f
<%out.print(request.getHeader("Host"));%>

c YI&MNZy现在访问:BSD爱好者乐园.af }Vd0g;D
http://blog.chinajavaworld.com/ajax/search/test.jsp,输出:
?,s qIDW!pc:i
search.chinajavaworld.com

$P6vz#{]T2c.Ig4b"s
#j$QL*CO(j当应用要支持访问时,问题来了:BSD爱好者乐园4f0Nlb w0v7Z.i$s'u
BSD爱好者乐园t@6iK q2r | Fj

6N"I5B4hRHh*O1o如用 http://blog.chinajavaworld-bbb.com/ajax/search/test.jsp 进行访问,输出也是:BSD爱好者乐园y;O)K0aj)ZW
BSD爱好者乐园Z wVh)sX`h
search.chinajavaworld.com

r&l;o&ak如果设置两个host
(]?Y-pb,K

  http-proxy
  com.caucho.servlets.HttpProxyServlet
  'search.chinajavaworld.com:80'/>
  'search.chinajavaworld-bbb.com:80'/>

aE-@|\#U则用两个域名访问的话,都将输出:BSD爱好者乐园O.yo8K GJq
search.chinajavaworld-bbb.com

8U R7y#M+t
@ z1X8JtLFr k qU现在的需求是,用哪个主域名访问的,就必须返回与该主域名一致的子域BSD爱好者乐园"k7A;N K)Z;q(@
BSD爱好者乐园x)ch,l}m [I0^3Nm
解决方法:修改 HttpProxyServlet 源码:
h;E];Q ik"y-DTcpPool.java加入以下方法:
Z-][-O1Kz)]opublicServer nextServer(String mainHost){BSD爱好者乐园WfE&p+b Ox)p
Server bestServer =null;BSD爱好者乐园0P+u+K^ y(m,T'b:l?
synchronized(this){BSD爱好者乐园 R.cz'k_)u@
String _URL =null;
IGG/|*y\e |for(inti = 0; i < _servers.length; i++){BSD爱好者乐园:S` d FGdqPN
Server server = _servers[i];BSD爱好者乐园/II)I)d,K/H5B4N
if(server.getURL().indexOf(mainHost) != -1){
)Lk D;h$fyK]//System.out.println("ok,found host.return server.");BSD爱好者乐园Gj9nO.edm z
bestServer = server;BSD爱好者乐园*CF ~ `/yB
break;
pstL|1Q(~}
6_+h@@Q9`#Y$V0kif(i == _servers.length - 1) _URL = server.getURL();}BSD爱好者乐园i*u8l A%eUqjm
if(bestServer ==null){
B4v&CH6\N'Xr Sw@//System.out.println(mainHost+" is not in server list. add now.");
.M2Lju)\Ts-n5TString oldMainHost = getMainDomain(_URL);
\d|,lU R_URL = _URL.replace(oldMainHost, mainHost);
S.}(i8bf&e,ch YO'I'RString _host = getDomain(_URL);BSD爱好者乐园+x#rQ| JS9[ q
bestServer =Server(_host);
_ Wa*l5c yA_hosts.add(_host);
2GL;h/{ \ yt0SServer[] servers =newServer[_servers.length + 1];
a`+~A#cd r3vSystem.arraycopy(_servers, 0, servers, 0, _servers.length);BSD爱好者乐园z[p#Jw d.B1Kk_1v
servers[_servers.length] = bestServer;
9[A:NB(L^ u_servers = servers;
2n1S8B*P1H5F}BSD爱好者乐园%Za0FCq9Yo;}
returnbestServer;BSD爱好者乐园u Ejg(`w:j;W3R6N[
}BSD爱好者乐园ig6T YD5J+Y FZ
}BSD爱好者乐园@5lpZ da e

E5Z1_D3e7[[BSD爱好者乐园4t}"s:YE:O3k u'G"i
HttpProxyServlet中加入:BSD爱好者乐园RCv1_Q9P9ku

r*w*BT+KString host = req.getHeader("Host");
;bc$w4f:dnN'["L#rString mainHost = TcpPool.getMainDomain(host);
3U!}De+h
je#o[4z ?-~3v,X"W&fBSD爱好者乐园fH3c/~&fmg7z
然后将
t4jfC%EBSD爱好者乐园r5U6e~2f
TcpPool.Server server = _tcpPool.nextServer();
aPQLq
i:G:L8S rA ^替换为BSD爱好者乐园kc._!Z? X1~0F

I+\2EbT W.c/Q{TcpPool.Server server = _tcpPool.nextServer(mainHost);BSD爱好者乐园%j [f\8l&}*T N8f/\
阅读(1351) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~