复制代码代码如下:
パッケージcn.liangjintang.httpproxy;
java.io.bufferedreaderをインポートします。
java.io.bytearrayinputStreamをインポートします。
java.io.ioexceptionをインポートします。
java.io.inputStreamをインポートします。
java.io.inputStreamReaderをインポートします。
java.io.outputStreamをインポートします。
java.net.serversocketをインポートします。
java.net.socketをインポートします。
パブリッククラスhttpproxy {
静的long threadcount = 0;
int mytcpport = 8080;
Private Serversocket MyServersocket;
プライベートスレッドMythread;
public httpproxy(int port)はioexceptionをスローします{
mytcpport = port;
myServersocket = new Serversocket(mytcpport);
mythread = newスレッド(new runnable(){
public void run(){
試す {
while(true)
new httpsession(myserversocket.accept());
} catch(ioexception ioe){
}
}
});
mythread.setdaemon(true);
mythread.start();
}
/**
*サーバーを停止します。
*/
public void stop(){
試す {
myServersocket.close();
mythread.join();
} catch(ioexception ioe){
} catch(arternedexception e){
}
}
パブリッククラスのhttpsesssionは実行可能{
プライベートソケットマイソケット。
public httpsession(socket s){
mySocket = s;
スレッドT = newスレッド(this);
T.SetDaemon(True);
t.start();
}
@オーバーライド
public void run(){
試す {
++ threadcount;
inputstream is = mySocket.getInputStream();
if(is == null)
戻る;
final int bufsize = 8192;
byte [] buf = new byte [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read(buf、0、bufsize);
while(read> 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(
new inputStreamReader(HBIS));
ホストhost = new host();
{
文字列文字列;
ブールフラグ= false;
while((string = hin.readline())!= null){
if(string.tolowercase()。startswith( "host:")){
host.host = string;
flag = true;
}
System.out.println(string);
}
if(!flag){
mySocket.getOutputStream()。書き込み(
「エラー!」。GetBytes());
mysocket.close();
戻る;
}
}
host.cal();
System.out.println( "address:[" + host.address + "] port:"
+ host.port + "/n ------------------/n");
試す {
パイプ(buf、rlen、mysocket、mysocket.getinputstream()、
mySocket.getOutputStream()、host);
} catch(例外e){
System.out.println( "Run Exception!");
e.printstacktrace();
}
}
} catch(例外e){
}
system.out.println( "threadcount:" + -threadcount);
}
/**
* Finad HTTPヘッダー
**/
private 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 [] request、int requestlen、socket client、
inputstream clientis、outputstream clientos、ホストホスト)
例外をスローする{
バイトバイト[] = new byte [1024 * 32];
ソケットソケット= new Socket(host.Address、host.port);
socket.setsotimeout(3000);
outputStream os = socket.getOutputStream();
inputstream is = socket.getinputStream();
試す {
する {
os.write(request、0、requestlen);
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( "Proxy requset-connect broken、socket:" "
+ 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();
}
//ターゲットホスト情報
最終クラスのホスト{
パブリック文字列アドレス;
パブリックインターポート;
パブリックストリングホスト。
public boolean cal(){
if(host == null)
falseを返します。
int start = host.indexof( ":");
if(start == -1)
falseを返します。
int next = host.indexof( ':'、start + 2);
if(next == -1){
ポート= 80;
address = host.substring(start + 2);
} それ以外 {
address = host.substring(start + 2、next);
port = integer.valueof(host.substring(next + 1));
}
trueを返します。
}
}
}
public static void main(string [] args){
試す {
New Httpproxy(8580);
} catch(ioexception ioe){
System.err.println( "Server:/n" + IOEを起動できませんでした);
System.Exit(-1);
}
system.out.println( "start!");
試す {
system.in.read();
} catch(スロー可能なt){
}
System.out.println( "Stop!");
}
}