复制代码代码如下:
Package CN.Liangjintang.httpproxy;
Importer java.io.bufferedReader;
Importer java.io.ByteArrayInputStream;
Importer java.io.ioException;
import java.io.inputStream;
Importer java.io.inputStreamReader;
import java.io.outputStream;
import java.net.serversocket;
import java.net.socket;
classe publique httpproxy {
ThreadCount long statique = 0;
int mytcpport = 8080;
SERVERSANT PRIVÉ MYSERVERSOCKET;
fil privé Mythread;
Le public httpproxy (port int) lève ioException {
mytcpport = port;
MySerVersocket = new SERVERSocket (mytcpport);
mythread = nouveau thread (new Runnable () {
public void run () {
essayer {
tandis que (vrai)
new httpSession (myServersocket.Accept ());
} catch (ioException ioe) {
}
}
});
mythread.setDaemon (true);
mythread.start ();
}
/ **
* Arrête le serveur.
* /
public void stop () {
essayer {
MySerVersocket.Close ();
mythread.join ();
} catch (ioException ioe) {
} catch (InterruptedException e) {
}
}
classe publique HttpSession implémente Runnable {
Socket privé MySocket;
HttpSession publique (socket s) {
mysocket = s;
Thread t = nouveau thread (this);
t.setDaemon (true);
t.start ();
}
@Outrepasser
public void run () {
essayer {
++ threadCount;
InputStream est = mysocket.getInputStream ();
si (est == null)
retour;
final int bufsize = 8192;
octet [] buf = nouveau octet [bufsize];
int SplitByte = 0;
int rlen = 0;
{
int lien = is.read (buf, 0, bufsize);
while (lire> 0) {
rlen + = read;
SplitByte = findHeleDend (buf, rlen);
if (Splipbyte> 0)
casser;
read = is.read (buf, rlen, bufsize - rlen);
}
ByteArrayInputStream HBIS = NOUVEAU BYTEARRAYInputStream (BUF,
0, rlen);
BufferedReader hin = new BufferedReader (
Nouveau InputStreamReader (HBIS));
Host host = new host ();
{
String String;
booléen drapeau = false;
while ((string = hin.readline ())! = null) {
if (string.tolowercase (). startSwith ("host:")) {
host.host = string;
Flag = true;
}
System.out.println (String);
}
if (! Flag) {
mysocket.getOutputStream ().
"Erreur!". GetBytes ());
mysocket.close ();
retour;
}
}
host.cal ();
System.out.println ("Adresse: [" + host.address + "] Port:"
+ host.port + "/ n ------------------- / n");
essayer {
tuyau (buf, rlen, mysocket, mysocket.getInputStream (),
mysocket.getOutputStream (), hôte);
} catch (exception e) {
System.out.println ("Run Exception!");
e.printStackTrace ();
}
}
} catch (exception e) {
}
System.out.println ("ThreadCount:" + --ThreadCount);
}
/ **
* En-tête HTTP Finad
** /
private int findheleDerend (byte final [] 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')
Retour SplitByte + 4;
SplitByte ++;
}
retour 0;
}
pipe void (requête octet [], int demandeur, client de socket,
InputStream ClientRis, OutputStream ClientOs, Host Host)
lance l'exception {
octets octets [] = nouvel octet [1024 * 32];
Socket socket = new socket (host.address, host.port);
Socket.SetSoTimeout (3000);
OutputStream os = socket.getOutputStream ();
InputStream est = socket.getInputStream ();
essayer {
faire {
OS.Write (request, 0, requestlen);
int resultlen = 0;
essayer {
while ((resultlen = is.read (octets))! = -1
&&! mysocket.isclosed () &&! socket.isclosed ()) {
ClientoS.Write (octets, 0, résultat);
}
} catch (exception e) {
System.out.println ("Exception du socket cible:"
+ e.toString ());
}
System.out.println ("Proxy requset-Connect cassé, prise:"
+ socket.hashcode ());
} while (! mysocket.isclosed ()
&& (requestLen = clientis.read (request))! = -1);
} catch (exception e) {
System.out.println ("Exception du socket client:" + e.toString ());
}
System.out.println ("end, socket:" + socket.hashcode ());
os.close ();
is.close ();
Clientis.close ();
ClientoS.Close ();
socket.close ();
mysocket.close ();
}
// cible les informations de l'hôte
Hôte de classe finale {
adresse de chaîne publique;
Port Int public;
hôte de chaîne publique;
public booléen cal () {
if (host == null)
retourne false;
int start = host.indexof (":");
if (start == -1)
retourne false;
int next = host.indexof (':', start + 2);
if (next == -1) {
port = 80;
adresse = host.substring (start + 2);
} autre {
adresse = host.substring (start + 2, suivant);
port = Integer.ValueOf (host.substring (suivant + 1));
}
Retour Vrai;
}
}
}
public static void main (String [] args) {
essayer {
Nouveau httpproxy (8580);
} catch (ioException ioe) {
System.err.println ("Impossible de démarrer Server: / N" + IOE);
System.exit (-1);
}
System.out.println ("Start!");
essayer {
System.in.read ();
} catch (t) {
}
System.out.println ("Stop!");
}
}