⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpserver.java

📁 java HTTP代理多线程监听和处理连接 java HTTP代理多线程监听和处理连接
💻 JAVA
字号:
package proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.*;
/**
 * HTTP代理服务线程
 * @author liwen
 *
 */
public class HttpServer implements Service {
	protected UserAuthorized userAuthorized;
	private Socket socket;
	public void serve(Socket soc) throws Exception {
		this.socket = soc;
		this.doProxy(socket);
	}

	public UserAuthorized getUserAuthorized() throws Exception {
		return userAuthorized;
	}
	
	private void doProxy(Socket socket) throws Exception {
		InputStream i = socket.getInputStream();
		OutputStream o = socket.getOutputStream();
		int port = ProxyServer.HTTP_PORT;
		String line = "";
		String host = "";
		int state = 0;
		boolean space;
		// 连接目标服务器
		Socket descSocket = null;
		OutputStream descSocketOs = null;
		int isdisConnect = 0;
		byte[] disConnectStr = new byte[12];
		try {
			while (true) {
				int c = i.read();
				if (c == -1) {
					break;
				}
				
				space = Character.isWhitespace((char) c);
				switch (state) {
				case 0:
					if (space) {
						continue;
					}
					state = 1;
				case 1:
					if (space) {
						state = 2;
						continue;
					}
					line = line + (char) c;
					break;
				case 2:
					if (space) {
						continue; // 跳过多个空白字符
					}
					state = 3;
				case 3:
					if (space) {
						state = 4;
						// 只分析主机名称部分
						String host0 = host;
						int n;
						n = host.indexOf("//");
						if (n != -1) {
							host = host.substring(n + 2);
						}
						n = host.indexOf('/');
						if (n != -1) {
							host = host.substring(0, n);
						}
						// 分析可能存在的端口号
						n = host.indexOf(":");
						if (n != -1) {
							port = Integer.parseInt(host.substring(n + 1));
							host = host.substring(0, n);
						}
						Logs.log("Connection to " + host + " " + port);
						//System.out.println("Remote host:" + host + " port:"
						//+ port);
						//host = processHostName(host0, host, port, socket);
												
						int retry = ProxyServer.CONNECT_RETRIES;
						while (retry-- != 0) {
							try {
								//descSocket = new Socket("10.154.145.9", 10083);
								descSocket = new Socket(host, port);
								
								break;
							} catch (Exception e) {
								// 写入日志
								Logs.log("Create remote socket exception: host:"
												+ host
												+ "  port:"
												+ port
												+ " "
												+ e);
							}
							// 等待
							Thread.sleep(ProxyServer.CONNECT_RETRIES_SLEEP);
						}
						if (descSocket == null) {
							break;
						}
						descSocket.setSoTimeout(ProxyServer.SERVER_TIME_OUT);
						
						descSocketOs = descSocket.getOutputStream();
						descSocketOs.write(line.getBytes());
						descSocketOs.write(' ');
						descSocketOs.write(host0.getBytes());
						descSocketOs.write(' ');
						System.out.println("line:" + line + " host0:"
								+ host0);
						pipe(i, descSocket.getInputStream(), descSocketOs, o);
						break;
					}
					host = host + (char) c;
					break;
				}
			}
		} catch (Exception e) {
		} finally {
			try {
				descSocket.close();
				//Logs.log("Connection to " + host + " " + port + " closed");
			} catch (Exception e2) {
			}
		}

	}


	private void pipe(InputStream clientIn, InputStream serverIn, OutputStream serverOut,
			OutputStream clientOut) throws IOException {
		try {
			int ir;
			byte bytes[] = new byte[ProxyServer.BUFER_SIZE];
			String testRtnStr = "";
			//boolean isfirst = true;
			while (true) {
				try {
					if ((ir = clientIn.read(bytes)) > 0) {
						System.out.println("receive ....");
						if (ProxyServer._DEBUG_){
							for (int i = 0; i < ir; i ++ ){
								int c = bytes[i];
								testRtnStr = testRtnStr + (char)c;
							}
							System.out.println("Client RTN:\n"+testRtnStr);
						}
						serverOut.write(bytes, 0, ir);
						serverOut.flush();
					} else if (ir < 0) {
						break;
					}
				} catch (InterruptedIOException e) {
					// System.out.println(" put server InterruptedIOException: "
					// + e);
				}

				try {
					if ((ir = serverIn.read(bytes)) > 0) {
						System.out.println("send ....");
						if (ProxyServer._DEBUG_){
							//查看返回字符
							for (int i = 0; i < ir; i ++ ){
								int c = bytes[i];
								testRtnStr = testRtnStr + (char)c;
							}
							System.out.println("Server RTN:\n"+testRtnStr);
						}
						clientOut.write(bytes, 0, ir);
						clientOut.flush();
						System.out.println("Write------------------:\n"+testRtnStr);
					} else if (ir < 0) {
						System.out.println("breakbreakbreakbreakbreakbreakbreakbreakbreak");
						System.out.println("breakbreakbreakbreakbreakbreakbreakbreakbreak");
						System.out.println("breakbreakbreakbreakbreakbreakbreakbreakbreak");
						System.out.println("breakbreakbreakbreakbreakbreakbreakbreakbreak");
						break;
					}
				} catch (InterruptedIOException e) {
					// System.out.println(" from server InterruptedIOException:
					// " + e);
				}
			}
		} catch (Exception e0) {
			Logs.log("Pipe exception: " + e0);
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -