复制代码代码如下:
paquete 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;
clase pública httpproxy {
static long threadcount = 0;
int mytcpport = 8080;
Servidor de servidor privado mySerVersocket;
hilo privado mythread;
public httpproxy (int puerto) lanza ioexception {
mytcpport = puerto;
mySerVersocket = new ServerSocket (mytcpport);
mythread = new Thread (new Runnable () {
public void run () {
intentar {
mientras (verdadero)
nuevo httpsession (mySerVersocket.accept ());
} Catch (IOException IOE) {
}
}
});
mythread.setdaemon (verdadero);
mythread.start ();
}
/**
* Detiene el servidor.
*/
public void stop () {
intentar {
mySerVersocket.close ();
mythread.Join ();
} Catch (IOException IOE) {
} capt (interruptedException e) {
}
}
clase pública httpsession implementa runnable {
Socket privado mysocket;
httpsession public (socket s) {
mysocket = s;
Hilo t = nuevo hilo (esto);
T.SetDaemon (verdadero);
t.Start ();
}
@Anular
public void run () {
intentar {
++ threadcount;
InputStream es = mySocket.getInputStream ();
if (is == nulo)
devolver;
final int bufSize = 8192;
byte [] buf = new Byte [bufSize];
int splitByte = 0;
int rlen = 0;
{
int read = is.read (buf, 0, bufsize);
while (leer> 0) {
rlen += leer;
splitByte = findHeaderend (buf, rlen);
if (splitByte> 0)
romper;
read = is.read (buf, rlen, bufsize - rlen);
}
BytearrayInputStream HBIS = new ByteArrayInputStream (BUF,
0, rlen);
BufferedReader hin = new BufferedReader (
nuevo InputStreamReader (HBIS));
Host host = new Host ();
{
Cadena de cadena;
bandera booleana = falso;
while ((string = hin.readline ())! = null) {
if (string.tolowercase (). startswith ("host:")) {
host.host = string;
bandera = verdadero;
}
System.out.println (cadena);
}
if (! flag) {
mysocket.getOutputStream (). Write (
"¡Error!". GetBytes ());
mysocket.close ();
devolver;
}
}
host.cal ();
System.out.println ("Dirección: [" + host.address + "] puerto:"
+ host.port + "/n -------------------/n");
intentar {
Pipe (buf, rlen, mysocket, mysocket.getInputStream (),
mysocket.getOutputStream (), host);
} capt (excepción e) {
System.out.println ("Ejecutar excepción!");
E.PrintStackTrace ();
}
}
} capt (excepción e) {
}
System.out.println ("ThreadCount:" + --ThreadCount);
}
/**
* Encabezado 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')
return splitByte + 4;
SplitByte ++;
}
regresar 0;
}
tubería void (byte [] solicitud, int requestlen, Socket Client,
InputStream Clientis, OutputStream ClientOS, Host Host)
lanza excepción {
byte bytes [] = nuevo byte [1024 * 32];
Socket socket = new Socket (host.address, host.port);
socket.setSotimeOut (3000);
OutputStream OS = Socket.getOutputStream ();
InputStream es = Socket.getInputStream ();
intentar {
hacer {
OS.Write (solicitud, 0, requestlen);
int resultlen = 0;
intentar {
while ((resultlen = is.read (bytes))! = -1
&&! MySocket.isClosed () &&! Socket.isclosed ()) {
clientos.write (bytes, 0, resultado);
}
} capt (excepción e) {
System.out.println ("Excepción de socket de destino:"
+ E.ToString ());
}
System.out.println ("Proxy Requset-Connect Break, Socket:"
+ socket.hashcode ());
} while (! mysocket.isClosed ()
&& (requestlen = clientis.read (request))! = -1);
} capt (excepción e) {
System.out.println ("Excepción de Socket Client:" + e.ToString ());
}
System.out.println ("end, socket:" + socket.hashcode ());
os.close ();
is.close ();
clientis.close ();
clientos.close ();
socket.close ();
mysocket.close ();
}
// Información de host de objetivo
Host de clase final {
dirección de cadena pública;
Público int Port;
host de cadena pública;
public boolean cal () {
if (host == nulo)
devolver falso;
int inicio = host.indexof (":");
if (inicio == -1)
devolver falso;
int next = host.indexof (':', inicio + 2);
if (next == -1) {
puerto = 80;
dirección = host.substring (inicio + 2);
} demás {
dirección = host.substring (inicio + 2, siguiente);
Port = Integer.ValueOf (host.substring (siguiente + 1));
}
devolver verdadero;
}
}
}
public static void main (string [] args) {
intentar {
nuevo httpproxy (8580);
} Catch (IOException IOE) {
System.err.println ("No se pudo iniciar el servidor:/n" + IOE);
System.exit (-1);
}
System.out.println ("¡Inicio!");
intentar {
System.in.read ();
} Catch (Throwable t) {
}
System.out.println ("¡Detente!");
}
}