复制代码代码如下:
paket cn.liangjintang.httpproxy;
impor java.io.bufferedReader;
impor java.io.bytearrayInputStream;
impor java.io.ioException;
impor java.io.inputstream;
impor java.io.inputStreamReader;
impor java.io.outputStream;
impor java.net.serversocket;
impor java.net.socket;
kelas publik httpproxy {
statis long threadcount = 0;
int mytcpport = 8080;
Server Private MyServerCocket;
utas pribadi mythread;
public httpproxy (int port) melempar ioException {
mytcpport = port;
myServersocket = server baru (mytcpport);
myThread = utas baru (runnable baru () {
public void run () {
mencoba {
sementara (benar)
httpsession baru (myserversocket.accept ());
} catch (ioException ioe) {
}
}
});
mythread.setdaemon (true);
mythread.start ();
}
/**
* Menghentikan server.
*/
public void stop () {
mencoba {
myserversocket.close ();
mythread.join ();
} catch (ioException ioe) {
} catch (InterruptedException e) {
}
}
Kelas publik httpsession mengimplementasikan runnable {
soket pribadi mysocket;
httpsession publik (soket s) {
mysocket = s;
Utas t = utas baru (ini);
t.setdaemon (true);
t.start ();
}
@Mengesampingkan
public void run () {
mencoba {
++ ThreadCount;
InputStream adalah = mysocket.getInputStream ();
if (is == null)
kembali;
final int bufsize = 8192;
byte [] buf = byte baru [bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read (buf, 0, bufsize);
while (baca> 0) {
rlen += baca;
splitByte = findheaderend (buf, rlen);
if (splitbyte> 0)
merusak;
baca = is.read (buf, rlen, bufsize - rlen);
}
BytearrayInputStream hbis = bytearrayInputStream baru (buf,
0, rlen);
BufferedReader hin = BufferedReader baru (
InputStreamReader baru (HBIS));
Host host = host baru ();
{
String string;
bendera boolean = false;
while ((string = hin.readline ())! = null) {
if (string.tolowercase (). startswith ("host:")) {
host.host = string;
bendera = true;
}
System.out.println (String);
}
if (! flag) {
mysocket.getoutputStream (). write (
"Kesalahan!". GetBytes ());
mysocket.close ();
kembali;
}
}
host.cal ();
System.out.println ("Alamat: [" + host.address + "] port:"
+ host.port + "/n -------------------/n");
mencoba {
pipa (buf, rlen, mysocket, mysocket.getInputStream (),
mysocket.getoutputStream (), host);
} catch (Exception e) {
System.out.println ("Jalankan Exception!");
e.printstacktrace ();
}
}
} catch (Exception e) {
}
System.out.println ("ThreadCount:" + --threadCount);
}
/**
* header http finad
**/
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')
return splitbyte + 4;
SplitByte ++;
}
kembali 0;
}
Void Pipe (BYTE [] permintaan, int revandlen, klien soket,
InputStream Clientis, OutputStream ClientOS, Host Host)
melempar pengecualian {
byte bytes [] = byte baru [1024 * 32];
Soket soket = soket baru (host.address, host.port);
socket.setsoTimeout (3000);
OutputStream os = socket.getoutputStream ();
InputStream adalah = socket.getInputStream ();
mencoba {
Mengerjakan {
os.write (permintaan, 0, requestlen);
int resultlen = 0;
mencoba {
while ((resultlen = is.read (bytes))! = -1
&&! mysocket.isclosed () &&! socket.isclosed ()) {
clientos.write (bytes, 0, resultlen);
}
} catch (Exception e) {
System.out.println ("Pengecualian Soket Target:"
+ e.tostring ());
}
System.out.println ("Proxy Requset-Connect rusak, soket:"
+ socket.hashcode ());
} while (! mysocket.isclosed ()
&& (requestLen = clientis.read (request))! = -1);
} catch (Exception e) {
System.out.println ("Pengecualian Soket Klien:" + E.ToString ());
}
System.out.println ("End, Socket:" + Socket.HashCode ());
os.close ();
is.close ();
clientis.close ();
clientos.close ();
socket.close ();
mysocket.close ();
}
// Target Info Host
Host kelas terakhir {
alamat string publik;
port int public;
host string publik;
Public Boolean Cal () {
if (host == null)
mengembalikan false;
int start = host.indexof (":");
if (start == -1)
mengembalikan false;
int next = host.indexof (':', mulai + 2);
if (next == -1) {
port = 80;
alamat = host.substring (mulai + 2);
} kalau tidak {
alamat = host.substring (mulai + 2, berikutnya);
port = integer.valueof (host.substring (berikutnya + 1));
}
Kembali Benar;
}
}
}
public static void main (string [] args) {
mencoba {
baru httpproxy (8580);
} catch (ioException ioe) {
System.err.println ("tidak dapat memulai server:/n" + ioe);
System.exit (-1);
}
System.out.println ("Mulai!");
mencoba {
System.in.read ();
} catch (Throwable t) {
}
System.out.println ("Stop!");
}
}