java实现轻量型http代理服务器示例
package 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;
public class HttpProxy {
static long threadCount = 0;
int myTcpPort = 8080;
private ServerSocket myServerSocket;
private Thread myThread;
public HttpProxy(int port) throws IOException {
myTcpPort = port;
myServerSocket = new ServerSocket(myTcpPort);
myThread = new Thread(new Runnable() {
public void run() {
try {
while (true)
new HTTPSession(myServerSocket.accept());
} catch (IOException ioe) {
}
}
});
myThread.setDaemon(true);
myThread.start();
}
/**
* Stops the server.
*/
public void stop() {
try {
myServerSocket.close();
myThread.join();
} catch (IOException ioe) {
} catch (InterruptedException e) {
}
}
public class HTTPSession implements Runnable {
private Socket mySocket;
public HTTPSession(Socket s) {
mySocket = s;
Thread t = new Thread(this);
t.setDaemon(true);
t.start();
}
@Override
public void run() {
try {
++threadCount;
InputStream is = mySocket.getInputStream();
if (is == null)
return;
final int bufsize = 8192;
byte[] buf = new byte[bufsize];
int splitbyte = 0;
int rlen = 0;
{
int read = is.read(buf, 0, bufsize);
while (read > 0) {
rlen += read;
splitbyte = findHeaderEnd(buf, rlen);
if (splitbyte > 0)
break;
read = is.read(buf, rlen, bufsize - rlen);
}
ByteArrayInputStream hbis = new ByteArrayInputStream(buf,
0, rlen);
BufferedReader hin = new BufferedReader(
new InputStreamReader(hbis));
Host host = new Host();
{
String string;
boolean flag = false;
while ((string = hin.readLine()) != null) {
if (string.toLowerCase().startsWith("host:")) {
host.host = string;
flag = true;
}
System.out.println(string);
}
if (!flag) {
mySocket.getOutputStream().write(
"error!".getBytes());
mySocket.close();
return;
}
}
host.cal();
System.out.println("address:[" + host.address + "]port:"
+ host.port + "\n-------------------\n");
try {
pipe(buf, rlen, mySocket, mySocket.getInputStream(),
mySocket.getOutputStream(), host);
} catch (Exception e) {
System.out.println("Run Exception!");
e.printStackTrace();
}
}
} catch (Exception e) {
}
System.out.println("threadcount:" + --threadCount);
}
/**
* finad http header
**/
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++;
}
return 0;
}
void pipe(byte[] request, int requestLen, Socket client,
InputStream clientIS, OutputStream clientOS, Host host)
throws Exception {
byte bytes[] = new byte[1024 * 32];
Socket socket = new Socket(host.address, host.port);
socket.setSoTimeout(3000);
OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
try {
do {
os.write(request, 0, requestLen);
int resultLen = 0;
try {
while ((resultLen = is.read(bytes)) != -1
&& !mySocket.isClosed() && !socket.isClosed()) {
clientOS.write(bytes, 0, resultLen);
}
} catch (Exception e) {
System.out.println("target Socket exception:"
+ e.toString());
}
System.out.println("proxy requset-connect broken,socket:"
+ socket.hashCode());
} while (!mySocket.isClosed()
&& (requestLen = clientIS.read(request)) != -1);
} catch (Exception e) {
System.out.println("client Socket exception:" + e.toString());
}
System.out.println("end,socket:" + socket.hashCode());
os.close();
is.close();
clientIS.close();
clientOS.close();
socket.close();
mySocket.close();
}
// target Host info
final class Host {
public String address;
public int port;
public String host;
public boolean cal() {
if (host == null)
return false;
int start = host.indexOf(": ");
if (start == -1)
return false;
int next = host.indexOf(':', start + 2);
if (next == -1) {
port = 80;
address = host.substring(start + 2);
} else {
address = host.substring(start + 2, next);
port = Integer.valueOf(host.substring(next + 1));
}
return true;
}
}
}
public static void main(String[] args) {
try {
new HttpProxy(8580);
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
System.exit(-1);
}
System.out.println("start!");
try {
System.in.read();
} catch (Throwable t) {
}
System.out.println("stop!");
}
}
相关文章
springboot +rabbitmq+redis实现秒杀示例
本文主要介绍了springboot +rabbitmq+redis实现秒杀示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2022-07-07浅析Java中SimpleDateFormat为什么是线程不安全的
SimpleDateFormat是Java中用于日期时间格式化的一个类,它提供了对日期的解析和格式化能力,本文主要来和大家一起探讨一下SimpleDateFormat为什么是线程不安全的,感兴趣的可以了解下2024-02-02Mybatis-Plus接口BaseMapper与Services使用详解
这篇文章主要为大家介绍了Mybatis-Plus接口BaseMapper与Services使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-05-05解读System.getProperty("ENM_HOME")中的值从哪获取的
这篇文章主要介绍了解读System.getProperty("ENM_HOME")中的值从哪获取的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-12-12java中MultipartFile和File最简单的互相转换示例
这篇文章主要给大家介绍了关于java中MultipartFile和File最简单的互相转换的相关资料,MultipartFile和File都是Java中用于处理文件上传的类,MultipartFile用于处理上传的文件,File用于处理本地磁盘上的文件,需要的朋友可以参考下2023-09-09
最新评论