复制代码代码如下:
包cn.liangjintang.httpproxy;
导入java.io..bufferedreader;
导入Java.io.ByTearRayInputStream;
导入java.io.ioexception;
导入java.io.inputstream;
导入java.io.io.inputStreamReader;
导入java.io.outputstream;
导入java.net.serversocket;
导入java.net.socket;
公共类Httpproxy {
静态长线= 0;
int mytCpport = 8080;
私有式MyServerSocket;
私人线程神话;
公共httpproxy(int端口)抛出ioexception {
mytCpport = port;
myServerSocket = new serversocket(mytCpport);
mythread = new thread(new Runnable(){
public void run(){
尝试 {
而(true)
新的httpsession(myServerSocket.accept());
} catch(ioexception ioe){
}
}
});
mythread.setdaemon(true);
mythread.start();
}
/**
*停止服务器。
*/
public void stop(){
尝试 {
myServersocket.Close();
mythread.join();
} catch(ioexception ioe){
} catch(InterruptedException e){
}
}
公共类Httpsession实现可运行{
私人插座mysocket;
公共httpsession(socket s){
mysocket = s;
线程t =新线程(this);
t.setdaemon(true);
t.start();
}
@Override
public void run(){
尝试 {
++线程;
InputStream is = mySocket.getInputStream();
if(is == null)
返回;
最终int bufsize = 8192;
字节[] buf = new Byte [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.Read(buf,0,bufsize);
while(读> 0){
rlen += read;
splitbyte = findheaderend(buf,rlen);
if(splitbyte> 0)
休息;
read = is.Read(buf,rlen,bufsize -rlen);
}
bytearrayInputStream hbis = new bytearrayinputstream(buf,
0,rlen);
bufferedReader hin = new BufferedReader(
新的InputStreamReader(HBIS));
主机host = new host();
{
字符串;
boolean flag = false;
while(((字符串= hin.readline())!= null){
if(string.tolowercase()。startswith(“ host:”)){
host.host =字符串;
flag = true;
}
system.out.println(string);
}
如果(!flag){
mysocket.getOutputstream()。写(
“错误!”。getBytes());
mysocket.close();
返回;
}
}
host.cal();
system.out.println(“地址:” + host.address +“]端口:”
+ host.port +“/n ------------------------/n”);
尝试 {
管道(buf,rlen,mysocket,mysocket.getInputstream(),
mySocket.getOutputStream(),主机);
} catch(异常E){
system.out.println(“运行异常!”);
e.printstacktrace();
}
}
} catch(异常E){
}
system.out.println(“ threadCount:” + - threadCount);
}
/**
* Finad HTTP标头
**/
私有int findheaderend(final byte [] buf,int rlen){
int splitbyte = 0;
while(splitbyte + 3 <rlen){
if(buf [splitbyte] =='/r'&& buf [splitbyte + 1] =='/n'
&& buf [splitbyte + 2] =='/r'
&& buf [splitbyte + 3] =='/n')
返回splitbyte + 4;
SplitByte ++;
}
返回0;
}
void pipe(byte []请求,int requestlen,套接字客户端,
InputStream客户端,OutputStream Clientos,主机)
抛出异常{
字节字节[] = new Byte [1024 * 32];
套接字插座=新套接字(host.Address,host.port);
socket.setsotimeout(3000);
outputStream OS = socket.getOutputStream();
inputStream is = socket.getInputStream();
尝试 {
做 {
OS.WRITE(请求,0,请求Len);
int resultlen = 0;
尝试 {
while((resultlen = is.Read(bytes))!= -1
&&!mysocket.isclosed()&&!socket.isclosed()){
clientos.write(bytes,0,resultlen);
}
} catch(异常E){
system.out.println(“目标套接字异常:”
+ e.tostring());
}
system.out.println(“代理requset-connect损坏,插座:”
+ socket.hashcode());
} while(!mysocket.isclosed()
&&(requestLen = clientis.read(request))!= -1);
} catch(异常E){
system.out.println(“客户端套接字异常:” + e.ToString());
}
system.out.println(“ end,socket:” + socket.hashcode());
os.close();
is.close();
clientis.close();
clientos.close();
socket.Close();
mysocket.close();
}
//目标主机信息
最终班级主机{
公共字符串地址;
公共INT端口;
公共字符串主机;
public boolean cal(){
如果(主机== null)
返回false;
int start = host.indexof(“:”);
if(start == -1)
返回false;
int next = host.indexof(':',start + 2);
if(next == -1){
端口= 80;
地址= host.substring(start + 2);
} 别的 {
地址= host.substring(start + 2,下一个);
port = integer.valueof(host.substring(next + 1));
}
返回true;
}
}
}
公共静态void main(string [] args){
尝试 {
新的httpproxy(8580);
} catch(ioexception ioe){
system.err.println(“无法启动服务器:/n” + ioe);
system.exit(-1);
}
system.out.println(“ start!”);
尝试 {
System.in.Read();
} catch(可投掷T){
}
system.out.println(“停止!”);
}
}