复制代码代码如下:
pacote cn.liangjintang.httpProxy;
importar java.io.bufferedReader;
importar java.io.byteArrayInputStream;
importar java.io.ioException;
importar java.io.inputStream;
importar java.io.inputStreamReader;
importar java.io.OutputStream;
importar java.net.serversocket;
importar java.net.socket;
classe pública httpproxy {
estático longo threadCount = 0;
int mytcpport = 8080;
Servidor privado MyServersocket;
mythread de tópicos privados;
public httpproxy (int porta) lança IoException {
mytcpport = porta;
myServersocket = new ServerSocket (mytcpport);
mythread = new Thread (new Runnable () {
public void run () {
tentar {
enquanto (verdadeiro)
novo httpSession (myServersocket.accept ());
} catch (ioexception ioe) {
}
}
});
mythread.setDaemon (true);
mythread.start ();
}
/**
* Para o servidor.
*/
public void Stop () {
tentar {
myServersocket.close ();
mythread.join ();
} catch (ioexception ioe) {
} catch (interruptedException e) {
}
}
classe pública httpsession implementa Runnable {
soquete privado mysocket;
public httpsession (soquete s) {
mysocket = s;
Thread t = novo thread (this);
t.SetDaemon (true);
t.start ();
}
@Override
public void run () {
tentar {
++ ThreadCount;
InputStream é = mysocket.getInputStream ();
se (é == null)
retornar;
final int bufsize = 8192;
byte [] buf = novo byte [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read (buf, 0, bufsize);
while (leia> 0) {
rlen += leia;
splitbyte = findheaderend (buf, rlen);
if (splitbyte> 0)
quebrar;
leia = is.read (buf, rlen, bufsize - rlen);
}
ByteArrayInputStream hbis = new ByteArrayInputStream (BUF,
0, rlen);
BufferredReader Hin = New BufferredReader (
novo InputStreamReader (HBIS));
Host host = new host ();
{
String string;
bandeira booleana = false;
while ((string = hin.readline ())! = null) {
if (string.tolowercase (). startSwith ("host:")) {
host.host = string;
bandeira = true;
}
System.out.println (string);
}
if (! Flag) {
mysocket.getOutputStream (). write (
"Erro!". getBytes ());
mysocket.close ();
retornar;
}
}
host.cal ();
System.out.println ("Endereço: [" + host.address + "] porta:"
+ host.port + "/n -------------------/n");
tentar {
tubo (buf, rlen, mysocket, mysocket.getInputStream (),
mysocket.getOutputStream (), host);
} catch (Exceção e) {
System.out.println ("Exceção de execução!");
E.PrintStackTrace ();
}
}
} catch (Exceção e) {
}
System.out.println ("ThreadCount:" + --threadCount);
}
/**
* Cabeçalho Finad HTTP
**/
Private Int Findheaderend (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')
retornar splitbyte + 4;
splitbyte ++;
}
retornar 0;
}
Void Pipe (BYTE [] solicitação, int requestlen, Socket Client,
InputStream Clientis, OutputStream ClientOS, host host)
joga exceção {
byte bytes [] = novo byte [1024 * 32];
Soquete soquete = novo soquete (host.address, host.port);
Socket.SetSotimeout (3000);
OutputStream OS = Socket.getOutputStream ();
InputStream é = soket.getInputStream ();
tentar {
fazer {
OS.Write (solicitação, 0, requestlen);
int resultlen = 0;
tentar {
while ((resultlen = is.read (bytes))! = -1
&&! mysocket.iscosed () &&! socket.iscled ()) {
clientos.write (bytes, 0, resultlen);
}
} catch (Exceção e) {
System.out.println ("Exceção do soquete de destino:"
+ e.toString ());
}
System.out.println ("Proxy Requset-Connect quebrado, soquete:"
+ soket.hashcode ());
} while (! mysocket.isclosed ()
&& (requestlen = clientis.read (request))! = -1);
} catch (Exceção e) {
System.out.println ("Exceção do soquete do cliente:" + e.toString ());
}
System.out.println ("end, soquete:" + soket.hashcode ());
os.close ();
is.close ();
clientis.close ();
clientOS.Close ();
Socket.Close ();
mysocket.close ();
}
// Informações do host de destino
Host da classe final {
endereço de string pública;
public int Port;
host público de string;
public boolean cal () {
if (host == null)
retornar falso;
int start = host.indexof (":");
if (start == -1)
retornar falso;
int próximo = host.indexof (':', start + 2);
if (next == -1) {
porta = 80;
endereço = host.substring (start + 2);
} outro {
endereço = host.substring (start + 2, próximo);
porta = integer.valueof (host.substring (próximo + 1));
}
retornar true;
}
}
}
public static void main (string [] args) {
tentar {
novo httpproxy (8580);
} catch (ioexception ioe) {
System.err.println ("Não foi possível iniciar o servidor:/n" + ioo);
System.Exit (-1);
}
System.out.println ("Start!");
tentar {
System.in.read ();
} catch (throwable t) {
}
System.out.println ("Stop!");
}
}