复制代码代码如下:
แพ็คเกจ cn.liangjintang.httpproxy;
นำเข้า java.io.bufferedreader;
นำเข้า Java.io.ByTearrayInputStream;
นำเข้า java.io.ioException;
นำเข้า Java.io.InputStream;
นำเข้า Java.io.InputStreamReader;
นำเข้า Java.io.OutputStream;
นำเข้า java.net.serversocket;
นำเข้า java.net.socket;
ชั้นเรียนสาธารณะ httpproxy {
threadCount ยาวคงที่ = 0;
int mytcpport = 8080;
เซิร์ฟเวอร์ส่วนตัว MyServersocket;
เธรดส่วนตัวตำนาน;
public httpproxy (พอร์ต int) โยน ioexception {
mytcpport = พอร์ต;
MyServersocket = ใหม่ Serversocket (MyTCPPORT);
MyThread = ใหม่เธรด (ใหม่ runnable () {
โมฆะสาธารณะเรียกใช้ () {
พยายาม {
ในขณะที่ (จริง)
ใหม่ httpsession (myserversocket.accept ());
} catch (ioexception ioe) {
-
-
-
Mythread.setdaemon (จริง);
mythread.start ();
-
-
* หยุดเซิร์ฟเวอร์
-
โมฆะสาธารณะหยุด () {
พยายาม {
MyServersocket.close ();
MYTHREAD.JOIN ();
} catch (ioexception ioe) {
} catch (interruptedException e) {
-
-
ชั้นเรียนสาธารณะ httpsession ใช้งานได้ {
ซ็อกเก็ตส่วนตัว Mysocket;
public httpsession (ซ็อกเก็ต s) {
mysocket = s;
เธรด t = เธรดใหม่ (นี่);
T.Setdaemon (จริง);
T.Start ();
-
@Override
โมฆะสาธารณะเรียกใช้ () {
พยายาม {
++ ThreadCount;
inputStream คือ = mysocket.getInputStream ();
ถ้า (คือ == null)
กลับ;
int สุดท้าย bufsize = 8192;
ไบต์ [] buf = ไบต์ใหม่ [bufsize];
int splitbyte = 0;
int rlen = 0;
-
int read = is.read (buf, 0, bufsize);
ในขณะที่ (อ่าน> 0) {
rlen += อ่าน;
SplitByte = findheaderend (buf, rlen);
ถ้า (splitbyte> 0)
หยุดพัก;
อ่าน = is.read (buf, rlen, bufsize - rlen);
-
ByTeArrayInputStream HBIS = New ByteArrayInputStream (buf,
0, rlen);
bufferedReader hin = new bufferedReader (
ใหม่ InputStreamReader (HBIS));
โฮสต์โฮสต์ = ใหม่โฮสต์ ();
-
สตริงสตริง;
ธงบูลีน = เท็จ;
ในขณะที่ ((string = hin.readline ())! = null) {
if (string.toLowerCase (). startswith ("โฮสต์:")) {
host.host = string;
ธง = จริง;
-
System.out.println (String);
-
ถ้า (! ธง) {
mysocket.getOutputStream () เขียน (
"ข้อผิดพลาด!". getBytes ());
mysocket.close ();
กลับ;
-
-
host.cal ();
System.out.println ("ที่อยู่: [" + host.address + "] พอร์ต:"
+ host.port + "/n -------------------/n");
พยายาม {
ท่อ (buf, rlen, mysocket, mysocket.getInputStream (),
mysocket.getOutputStream (), โฮสต์);
} catch (Exception e) {
System.out.println ("รันยกเว้น!");
E.PrintStackTrace ();
-
-
} catch (Exception e) {
-
System.out.println ("ThreadCount:" + -ThreadCount);
-
-
* ส่วนหัว Finad HTTP
-
Private int findheaderend (byte final [] buf, int rlen) {
int splitbyte = 0;
ในขณะที่ (splitbyte + 3 <rlen) {
if (buf [splitbyte] == '/r' && buf [splitbyte + 1] == '/n'
&& buf [splitbyte + 2] == '/r'
&& buf [splitbyte + 3] == '/n')
ส่งคืน Splitbyte + 4;
Splitbyte ++;
-
กลับ 0;
-
เป็นโมฆะไปป์ (ไบต์ [] คำขอ, int requestlen, ซ็อกเก็ตไคลเอนต์,
อินพุตสตรีมไคลเอนต์, OutputStream clientos โฮสต์โฮสต์)
โยนข้อยกเว้น {
ไบต์ไบต์ [] = ไบต์ใหม่ [1024 * 32];
ซ็อกเก็ตซ็อกเก็ต = ซ็อกเก็ตใหม่ (host.address, host.port);
Socket.setSotimeout (3000);
OutputStream OS = socket.getOutputStream ();
inputstream คือ = socket.getInputStream ();
พยายาม {
ทำ {
OS.WRITE (คำขอ, 0, RequestLen);
int resultLen = 0;
พยายาม {
ในขณะที่ ((resultlen = is.read (ไบต์))! = -1
&&! mysocket.isclosed () &&! socket.isclosed ()) {
clientos.write (ไบต์, 0, resultlen);
-
} catch (Exception e) {
System.out.println ("ข้อยกเว้นซ็อกเก็ตเป้าหมาย:"
+ e.tostring ());
-
System.out.println ("พร็อกซี requset-connect แตก, ซ็อกเก็ต:"
+ socket.hashCode ());
} ในขณะที่ (! mysocket.isclosed ()
&& (requestlen = clientis.read (คำขอ))! = -1);
} catch (Exception e) {
System.out.println ("ข้อยกเว้นซ็อกเก็ตไคลเอ็นต์:" + e.toString ());
-
System.out.println ("สิ้นสุด, ซ็อกเก็ต:" + socket.hashcode ());
os.close ();
is.close ();
clientis.close ();
clientos.close ();
Socket.close ();
mysocket.close ();
-
// ข้อมูลโฮสต์เป้าหมาย
โฮสต์คลาสสุดท้าย {
ที่อยู่สตริงสาธารณะ
พอร์ต int สาธารณะ;
โฮสต์สตริงสาธารณะ;
บูลีนสาธารณะ cal () {
if (host == null)
กลับเท็จ;
int start = host.indexof (":");
if (start == -1)
กลับเท็จ;
int next = host.indexof (':', start + 2);
ถ้า (next == -1) {
พอร์ต = 80;
address = host.substring (เริ่ม + 2);
} อื่น {
address = host.substring (เริ่ม + 2, ถัดไป);
พอร์ต = integer.valueof (host.substring (ถัดไป + 1));
-
กลับมาจริง;
-
-
-
โมฆะคงที่สาธารณะหลัก (สตริง [] args) {
พยายาม {
ใหม่ httpproxy (8580);
} catch (ioexception ioe) {
System.err.println ("ไม่สามารถเริ่มเซิร์ฟเวอร์:/n" + ioe);
System.Exit (-1);
-
System.out.println ("Start!");
พยายาม {
System.in.read ();
} catch (throwable t) {
-
System.out.println ("หยุด!");
-
-