web代理服务器实验_第1页
web代理服务器实验_第2页
web代理服务器实验_第3页
web代理服务器实验_第4页
web代理服务器实验_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Web代理服务器实验附录代码:1. ProxyCache类import .*;import java.io.*;import java.util.*;public class ProxyCache private static HashMap<String,byte> URIBuff = new HashMap<String,byte>();/ 代理入口private static int port;/* 客户链接的套接字, port是要代理侦听来自客户端的传入连接的端口号*/private static ServerSocket socket;/* 创建一个Proxy

2、对象和套接字 */public static void init(int p) port = p; try socket = new ServerSocket(port); catch (IOException e) System.out.println("Error creating socket: " + e);System.exit(-1);public static void handle(Socket client) Socket server = null; HttpRequest request = null; HttpResponse response =

3、null;/ 处理请求。 如果存在异常,就回复并终止它。 这意味着客户端将会挂起一段时间,直到超时. 读取请求try BufferedReader fromClient = new BufferedReader(new InputStreamReader(client.getInputStream();request = new HttpRequest(fromClient); String URI = request.URI; byte buff = URIBuff.get(URI);if (buff != null) DataOutputStream toClient; /* 创建一个to

4、Client 对象*/try toClient = new DataOutputStream(client.getOutputStream();toClient.writeBytes(response.toString();toClient.write(buff);/缓存/ 写响应到客户端。首先头部,然后主体client.close();System.out.print("Use Buffered"); catch (IOException e) e.printStackTrace();return;/ 不再往下走 catch (IOException e) System.

5、out.println("Error reading request from client: " + e);return;/ 向服务器发送请求try / 打开套接字并向其中写入请求server = new Socket(request.getHost(), request.getPort();DataOutputStream toServer = new DataOutputStream(server.getOutputStream();toServer.writeBytes(request.toString();/ 以下toServer不能够close,它关闭会导致连接

6、server也会关闭/ toServer.close();System.out.println("Request forwarded."); catch (UnknownHostException e) System.out.println("Unknown host: " + request.getHost();System.out.println(e);return; catch (IOException e) System.out.println("Error writing request to server: " + e);

7、return;/ 读取相应并将其转发给客户端try DataInputStream fromServer = new DataInputStream(server.getInputStream();response = new HttpResponse(fromServer);DataOutputStream toClient = new DataOutputStream(client.getOutputStream();toClient.writeBytes(response.toString();URIBuff.put(request.URI, response.body);toClien

8、t.write(response.body);/ 写响应到客户端。首先头部,然后主体client.close();server.close();/ 插入对象到缓存 填写(只有自选动作) catch (IOException e) /e.printStackTrace();/System.out.println("Error writing response to client: " + e);/ 读取命令行参数,并开始代理public static void main(String args) int myPort = 0;try myPort = Integer.pars

9、eInt(args0); catch (ArrayIndexOutOfBoundsException e) System.out.println("Need port number as argument");System.exit(-1); catch (NumberFormatException e) System.out.println("Please give port number as integer.");System.exit(-1);init(myPort);/ 主循环 侦听传入的连接,并处理它们生成一个新线程/ = null;whil

10、e (true) try Socket client = socket.accept();handle(client); catch (IOException e) System.out.println("Error reading request from client: " + e);/ 明确不能继续处理这个请求,因此跳转到而下一轮循环continue;2. HttpRequest类import java.io.*; import .*; import java.util.*; public class HttpRequest /帮助参数 final static St

11、ring CRLF = "rn" /*回车换行*/ final static int HTTP_PORT = 80; / 存储请求参数 String method; String URI; String version; String headers = "" / 服务器和端口 private String host; private int port; / 通过从客户端套接字读取它来创建Http请求 / private HashMap<> public HttpRequest(BufferedReader from) String firs

12、tLine = "" try firstLine = from.readLine(); catch (IOException e) System.out.println("Error reading request line: " + e); String tmp = firstLine.split(" "); method = tmp0; URI = tmp1; version = tmp2; System.out.println("URI is: " + URI); if (!method.equals(&qu

13、ot;GET") System.out.println("Error: Method not GET"); try String line = from.readLine(); while (line.length() != 0) headers += line + CRLF; /请求不完整,需要找出主机头部以获知去连接哪个服务器 if (line.startsWith("Host:") tmp = line.split(" "); if (tmp1.indexOf(':') > 0 ) String

14、tmp2 = tmp1.split(":"); host = tmp20; port = Integer.parseInt(tmp21); else host = tmp1; port = HTTP_PORT; line = from.readLine(); catch (IOException e) System.out.println("Error reading from socket: " + e); return; System.out.println("Host to contact is: " + host + &quo

15、t; at port " + port); / 返回请求的主机 public String getHost() return host; / 返回服务器的端口 public int getPort() return port; / 将请求转换成一个便于重新发送字符串 public String toString() String req = "" req = method + " " + URI + " " + version + CRLF; req += headers; / 这个代理不支持持久的连接 req += &qu

16、ot;Connection: close" + CRLF; req += CRLF; return req; 3. HttpResponse类import java.io.*; import .*; import java.util.*; public class HttpResponse final static String CRLF = "rn" / 用于读取对象的缓存区的大小 final static int BUF_SIZE = 81920;/8192; / 最大化代理可以处理的对象,暂时设置为100KB,可以根据需要调整 final static in

17、t MAX_OBJECT_SIZE = 100000; / 应答状态和头部 String version; int status; String statusLine = "" String headers = "" / 应答的实体 byte body = new byteMAX_OBJECT_SIZE; / 从服务器读取相应public HttpResponse(DataInputStream fromServer) / 对象的长度 int length = -1; boolean gotStatusLine = false; / 先读取状态行和响应头

18、部 try String line = fromServer.readLine(); while (line.length() != 0) if (!gotStatusLine) statusLine = line; gotStatusLine = true; else headers += line + CRLF; /获取Content-Length头部显示的内容长度,一些服务器返回头部"Content-Length",另外一些返回"Content-Length",这里都需要确认一下 if (line.startsWith("Content-

19、Length") | line.startsWith("Content-length") String tmp = line.split(" "); length = Integer.parseInt(tmp1); line = fromServer.readLine(); catch (IOException e) System.out.println("Error reading headers from server: " + e); return; try int bytesRead = 0; byte buf = new byteBUF_SIZE; boolean loop = false; / 如果没有获得Content-Length的头部,继续循

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论