0

我的编程任务要求我在客户端和服务器之间创建一个代理。我的客户端通过代理向服务器发出请求,然后代理转发它并将服务器的响应返回给客户端。

这是我采取的步骤:

1)从客户端获取请求并将请求数据存储到字节数组中

2) 使用缓冲读取器从字节数组中读取

3) 从 Host: 标头字段中获取主机名并用它创建一个 serverSocket

4)将请求数据转发到serverSocket输出流

5) 从 serverSocket 输入流中检索响应数据到另一个字节数组中

6) 将字节数组中的内容写入clientSocket输出流

但是,在第 6 步之后,浏览器无法显示响应数据。有什么帮助吗?

import java.io.*;
import java.net.*;
import java.util.*;

public class proxy2 {

  public static void main(String args[]) throws Exception
  {
    int port = Integer.parseInt(args[0]);
    File fileSub= new File(args[1]); //fileSub
    File fileRediect = new File(args[2]); //fileRedirect

    ServerSocket listener = new ServerSocket(port);


    while (true) {
      proxy_func(listener.accept());

    }

  }

  public static void proxy_func(Socket clientSocket) throws Exception{

    OutputStream outputToClient = clientSocket.getOutputStream();

    //store clientSocket's inputstream into a buffer
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead; //start offset in the data
    byte[] data = new byte[16384]; //create byte array
    //read each byte from InputStream and write it to a ByteArrayOutputStream
    while((nRead = clientSocket.getInputStream().read(data, 0, data.length)) != -1) {
      buffer.write(data, 0, nRead); 
    }
    buffer.flush(); //forces any buffered bytes to be written out
    data = buffer.toByteArray(); //retrieve the underlying byte array 
    System.out.println(new String(data));

    //create buffer reader for clientSocket's inputstream
    InputStream inputStream = new ByteArrayInputStream(data);
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(inputStream));

    String[] strArr;
    String line;
    String hostname = null;

    //get hostname
    while(  (line=readFromClient.readLine()) != null)
    {
      strArr = line.split(" ");
      if(strArr[0].equals("Host:"))
        hostname = strArr[1];
    }
    System.out.println("Host: " + hostname);

    //create server socket
    Socket serverSocket = new Socket(hostname, 80);
    OutputStream outputToServer = serverSocket.getOutputStream();
    InputStream inputFromServer = serverSocket.getInputStream();

    //forward request to server
    System.out.println("forward request to server...");
    outputToServer.write(data);

    //receive data from server and write response back to client
    byte[] receivedData = new byte[16384];
    int size;
    while((size = inputFromServer.read(receivedData)) != -1)
    {
      System.out.println("write response back to client...");
      System.out.println("size: " + size);
      System.out.println(new String(receivedData));
      outputToClient.write(receivedData, 0, size);
    }

    System.out.println("flushing...");
    outputToClient.flush();
    outputToClient.close();
  }
}

所以基本上我遵循这个 I/O 布局:

{
    OutputStream outputToClient = clientSocket.getOutputStream();
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    //step 1
    //get the HTTP request header from your firefox
    String line = readFromClient.readLine();

    //if the Content_Length within the HTTP header exists and not equals to 0
    char [] buffer = new char [#];
    readFromClient.read(buffer, 0, Content_Length); //get the body message

    OutputStream outputToServer = serverSocket.getOutputStream();
    InputStream inputFromServer = serverSocket.getInputStream();
    //step 2
    outputToServer.write(Bytes[])  //forword the HTTP requests to server host.

    //step 3
    byte[] data = new byte[#];
    while ((size = inputFromServer.read(data)) != -1)   //get the response data from server
    {
        //step 4
        outputToClient.write(data, 0, size);    //forward the data to your firefox
    }
}
4

3 回答 3

1

最后一部分有个问题:

byte[] receivedData = new byte[16384];
int size;
while((size = inputFromServer.read(receivedData)) != -1)
{
  System.out.println("write response back to client...");
  System.out.println("size: " + size);
  System.out.println(new String(receivedData));
}
outputToClient.write(receivedData);

因此,假设服务器发回 100 万字节。循环按 16384 字节的块读取它们,一旦读取了所有块,它就会receivedData在最后一次读取之后将缓冲区中剩余的内容发回。所以浏览器只会收到服务器发送的 1,000,000 个字节中的 16384 个字节。

您必须将从服务器读取的所有内容发送到浏览器:

byte[] receivedData = new byte[16384];
int size;
while((size = inputFromServer.read(receivedData)) != -1)
{
  System.out.println("write response back to client...");
  System.out.println("size: " + size);
  System.out.println(new String(receivedData));
  outputToClient.write(receivedData, 0, size);      
}
于 2013-10-06T14:00:36.790 回答
0

好的,我找到了问题的原因。我的 while 循环被阻塞了,因为我没有检测到 HTTP 请求的结束。这个答案简洁地解决了我的问题:https ://stackoverflow.com/a/9542710

只是在这里为那些偶然发现这个问题并迷路的人发帖。

这是我更新的可以工作的代码(请注意,我添加了一个检测 HTTP 请求结束的方法):

import java.io.*;
import java.net.*;
import java.util.*;

public class proxy2 {

  public static void main(String args[]) throws Exception
  {
    int port = Integer.parseInt(args[0]);
    File fileSub= new File(args[1]); //fileSub
    File fileRediect = new File(args[2]); //fileRedirect

    //create listener socket to listen for requests from browser
    ServerSocket listener = new ServerSocket(port);

    while (true) {
      proxy_func(listener.accept());
    }

  }

  public static void proxy_func(Socket clientSocket) throws Exception{

    OutputStream outputToClient = clientSocket.getOutputStream();
    InputStream inputFromClient = clientSocket.getInputStream();

    //store clientSocket's inputstream into a buffer

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int nRead; //start offset in the data
    byte[] data = new byte[16384]; 
    //read each byte from InputStream and write it to a ByteArrayOutputStream
    while((nRead = inputFromClient.read(data, 0, data.length)) != -1) {
      System.out.println("reading from client...");
      buffer.write(data, 0, nRead);
      //if reached end of HTTP request, break out of this loop
      if(endOfRequest(data) == true)
        break;
    }
    buffer.flush(); //forces any buffered bytes to be written out
    data = buffer.toByteArray(); //retrieve the underlying byte array 
    System.out.println(new String(data));

    //create buffered reader for byte array input stream (request data)
    InputStream inputStream = new ByteArrayInputStream(data);
    BufferedReader readFromClient = new BufferedReader(new InputStreamReader(inputStream));

    String[] strArr;
    String line;
    String hostname = null;

    //get hostname
    while(  (line=readFromClient.readLine()) != null)
    {
      strArr = line.split(" ");
      if(strArr[0].equals("Host:"))
        hostname = strArr[1];
    }
    System.out.println("Host: " + hostname);

    //create server socket
    Socket serverSocket = new Socket(hostname, 80);
    OutputStream outputToServer = serverSocket.getOutputStream();
    //InputStream inputFromServer = serverSocket.getInputStream();

    //forward request to server
    System.out.println("forward request to server...");
    outputToServer.write(data);

    //receive data from server and write response back to client
    DataInputStream inputFromServer = new DataInputStream(serverSocket.getInputStream());
    byte[] receivedData = new byte[1024];
    int size;
    while((size = inputFromServer.read(receivedData, 0, receivedData.length)) != -1)
    {
      System.out.println("write response back to client...");
      System.out.println(new String(receivedData));
      outputToClient.write(receivedData, 0, size);
      outputToClient.flush();
    }

    System.out.println("done sending");
    outputToClient.close();
  }

  //method to detect end of HTTP request
  //return true if detected, otherwise false
  public static boolean endOfRequest(byte[] data) throws Exception
  {
    System.out.println("parsing request..");
    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
    String line;

    while( (line=br.readLine()) != null) {
      if (line.equals(""))
        return true;
    }
    return false;
  }
}
于 2013-10-07T09:28:11.347 回答
0

你做这一切都是错的。HTTP 代理只需要读取 CONNECT 命令并对其进行操作。之后,您只需尽可能快地同时在两个方向上复制字节。你越是尝试了解代理中的 HTTP 协议,你就会引入越多的 bug。

于 2013-10-07T10:00:30.500 回答