复制代码代码如下 :
패키지 cn.liangjintang.httpproxy;
import java.io.bufferedReader;
import java.io.bytearrayinputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputStreamReader;
import java.io.outputStream;
import java.net.serversocket;
import java.net.socket;
공개 클래스 httpproxy {
정적 긴 스레드 Count = 0;
int mytcpport = 8080;
개인 서버 소켓 MyServersocket;
개인 실 신화;
public httpproxy (int port)는 ioexception {
mytcpport = 포트;
MyServersocket = New Serversocket (MyTCPport);
mythread = new Thread (new Runnable () {
public void run () {
노력하다 {
while (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 (소켓 s) {
mysocket = s;
스레드 t = 새 스레드 (this);
T. 세트 데몬 (true);
t.start ();
}
@보수
public void run () {
노력하다 {
++ ThreadCount;
inputStream은 = mySocket.getInputStream ();
if (is == null)
반품;
최종 int bufsize = 8192;
바이트 [] buf = 새로운 바이트 [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read (buf, 0, bufsize);
while (읽기> 0) {
rlen += 읽기;
splitByte = findheaderend (buf, rlen);
if (splitbyte> 0)
부서지다;
read = is.read (buf, rlen, bufsize -rlen);
}
BytearrayinputStream hbis = 새로운 Bytearrayinputstream (Buf,
0, rlen);
BufferedReader hin = New BufferedReader (
새 입력 스트리 프 리조더 (HBIS));
host host = new host ();
{
문자열 문자열;
부울 플래그 = 거짓;
while ((string = hin.readline ())! = null) {
if (string.tolowercase (). startswith ( "host :")) {
host.host = 문자열;
flag = true;
}
System.out.println (문자열);
}
if (! flag) {
mysocket.getOutputStream (). 쓰기 (
"오류!". GetBytes ());
mysocket.close ();
반품;
}
}
host.cal ();
System.out.println ( "주소 : [" + host.address + "] 포트 :"
+ host.port + "/n ----------------------/n");
노력하다 {
파이프 (buf, rlen, mySocket, mySocket.getInputStream (),
mySocket.getOutputStream (), host);
} catch (예외 e) {
System.out.println ( "실행 예외!");
e.printstacktrace ();
}
}
} catch (예외 e) {
}
System.out.println ( "ThreadCount :" + --threadCount);
}
/**
* Finad HTTP 헤더
**/
개인 int findheaderend (최종 바이트 [] 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')
리턴 스플릿 비트 + 4;
SplitByte ++;
}
반환 0;
}
void Pipe (byte [] request, int requestlen, socket client,
InputStream ClientIS, OutputStream ClientOS, 호스트 호스트)
예외 {{
바이트 바이트 [] = 새로운 바이트 [1024 * 32];
소켓 소켓 = 새로운 소켓 (host.address, host.port);
Socket.setSotimeout (3000);
outputStream os = socket.getOutputStream ();
inputStream은 = 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 ( "프록시 requset-connect broken, socket :"
+ socket.hashcode ());
} while (! mySocket.isclosed ()
&& (requestLen = clientIs.Read (request))! = -1);
} catch (예외 e) {
System.out.println ( "클라이언트 소켓 예외 :" + e.tostring ());
}
System.out.println ( "끝, 소켓 :" + socket.hashcode ());
os.close ();
is.close ();
clientis.close ();
clientos.close ();
socket.close ();
mysocket.close ();
}
// 대상 호스트 정보
최종 클래스 호스트 {
공개 문자열 주소;
공개 INT 포트;
공개 문자열 호스트;
공개 부울 cal () {
if (host == null)
거짓을 반환합니다.
int start = host.indexof ( ":");
if (start == -1)
거짓을 반환합니다.
int next = host.indexof ( ':', start + 2);
if (next == -1) {
포트 = 80;
주소 = host.Substring (시작 + 2);
} 또 다른 {
주소 = host.SubString (start + 2, next);
port = integer.valueof (host.substring (다음 + 1));
}
진실을 반환하십시오.
}
}
}
public static void main (String [] args) {
노력하다 {
새로운 httpproxy (8580);
} catch (ioexception ioe) {
System.err.println ( "Server :/n" + ioe를 시작할 수 없습니다);
System.exit (-1);
}
System.out.println ( "시작!");
노력하다 {
system.in.read ();
} catch (Throwable t) {
}
System.out.println ( "스톱!");
}
}