复制代码代码如下::
Paket cn.liangjintang.httpproxy;
Import Java.io.BufferedReader;
importieren java.io.BytearrayInputStream;
importieren java.io.ioException;
importieren java.io.inputstream;
importieren java.io.inputStreamReader;
importieren java.io.outputstream;
importieren java.net.serversocket;
importieren java.net.socket;
öffentliche Klasse httpproxy {
statischer langer ThreadCount = 0;
int mytcpport = 8080;
Private Serversocket MyServersocket;
Privat Thread MyThread;
public httpproxy (int port) löst IoException {aus
mytcpport = port;
myServersocket = new serversocket (mytcpport);
mythead = neuer Thread (new Runnable () {
public void run () {
versuchen {
während (wahr)
new httpSession (myServersocket.accept ());
} catch (ioException ioe) {
}
}
});
mythead.setdaemon (true);
mythead.start ();
}
/**
* Stoppt den Server.
*/
public void stop () {
versuchen {
myServersocket.close ();
MyThread.Join ();
} catch (ioException ioe) {
} catch (InterruptedException e) {
}
}
öffentliche Klasse httpSession implementiert runnable {
private Socket Mysocket;
public httpSession (Socket s) {
Mysocket = S;
Thread t = neuer Thread (this);
T.Setdaemon (wahr);
t.start ();
}
@Override
public void run () {
versuchen {
++ ThreadCount;
InputStream ist = mysocket.getInputStream ();
if (ist == null)
zurückkehren;
endgültig 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)
brechen;
read = is.read (buf, rlen, bufsize - rlen);
}
Bytearrayinputstream hbis = new bytearrayinputstream (buf,
0, rlen);
BufferedReader Hin = New BufferedReader (
neuer InputStreamReader (HBIS));
Host Host = new Host ();
{
Zeichenfolge;
boolesche Flagge = Falsch;
while ((string = hin.readline ())! = null) {
if (string.tolowerCase (). StartsWith ("Host:")) {
Host.host = String;
Flag = wahr;
}
System.out.println (String);
}
if (! Flagge) {
mysocket.getOutputStream (). Schreiben (
"Fehler!". getBytes ());
mysocket.close ();
zurückkehren;
}
}
Host.cal ();
System.out.println ("Adresse: [" + host.address + "] port:"
+ host.port + "/n -------------------/n");
versuchen {
Pfeife (buf, rlen, mysocket, mysocket.getInputstream (),
mysocket.getOutputStream (), Host);
} catch (Ausnahme e) {
System.out.println ("Ausnahme ausführen!");
E. printstacktrace ();
}
}
} catch (Ausnahme e) {
}
System.out.println ("ThreadCount:" + -threadCount);
}
/**
* FINAD HTTP -Header
**//
Private int findheaderend (endgültiges 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')
Return SplitByte + 4;
SplitByte ++;
}
Rückkehr 0;
}
Hohlraumrohr (Byte [] Anfrage, Int Requestlen, Socket Client,
InputStream Clientis, OutputStream Clientos, Host Host)
Ausnahme ausnahme {
Byte Bytes [] = neues Byte [1024 * 32];
Socket Socket = New Socket (Host.Address, Host.port);
Socket.Setsotimeout (3000);
OutputStream os = socket.getOutputStream ();
InputStream ist = Socket.getInputStream ();
versuchen {
Tun {
OS.Write (Anfrage, 0, Requestlen);
int resultlen = 0;
versuchen {
while ((resultlen = is.read (bytes))! = -1
&&! mysocket.isclosed () &&! socket.isclosed ()) {
clientos.write (bytes, 0, resultlen);
}
} catch (Ausnahme e) {
System.out.println ("Ziel -Socket -Ausnahme:"
+ e.toString ());
}
System.out.println ("Proxy Requset-Connect Broken, Socket:"
+ socket.hashCode ());
} while (! mysocket.isclosed ()
&& (requestlen = clientis.read (Anfrage))! = -1);
} catch (Ausnahme e) {
System.out.println ("Client -Socket -Ausnahme:" + e.toString ());
}
System.out.println ("Ende, Socket:" + socket.hashcode ());
os.close ();
is.close ();
Clientis.close ();
ClientOS.close ();
socket.close ();
mysocket.close ();
}
// Ziel -Hostinformationen
Hostklassenhost {
öffentliche String -Adresse;
public int port;
öffentlicher String -Host;
public boolean cal () {
if (host == null)
false zurückgeben;
int start = host.indexof (":");
if (start == -1)
false zurückgeben;
int next = host.indexof (':', start + 2);
if (next == -1) {
Port = 80;
address = host.substring (start + 2);
} anders {
Adresse = Host.substring (Start + 2, Weiter);
port = Integer.Valueof (Host.Substring (next + 1));
}
zurückkehren;
}
}
}
public static void main (String [] args) {
versuchen {
New Httpproxy (8580);
} catch (ioException ioe) {
System.err.println ("konnte nicht server starten:/n" + ioe);
System.exit (-1);
}
System.out.println ("Start!");
versuchen {
System.in.Read ();
} catch (throwable t) {
}
System.out.println ("Stop!");
}
}