import java.net.*;
import java.io.*;
import java.util.*;
/**
*
* @author Harry Lee 043717
* a proxy HTTP server
*
*/
/**
* the main class of the proxy server
*/
public class WebProxyServer {
/**
* port used to comunicate with the proxy server user
*/
static final int PORT=8000;
/**
* the constructor of the proxy server
* @param s the socket used to comunicate with the client
* @throws IOException
*/
public WebProxyServer(Socket s) throws IOException{
new WebProxyServerThread(s);
}
/**
* the class to so the multiply task
* @author Harry Lee
*
*/
class WebProxyServerThread extends Thread{
/**
* the socket used to comunicate with romote host
*/
private Socket socket;
/**
* the input and output stream used to comunicate with the the client and
* romote host
*/
private BufferedReader inFromClient;
private DataOutputStream outToClient;
private DataInputStream inFromServer;
private DataOutputStream outToServer;
/**
*
* @param s client socket
* @throws IOException
*/
public WebProxyServerThread(Socket s)throws IOException{
socket=s;
/**
* innitialnize the stream
*/
inFromClient= new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToClient= new DataOutputStream(socket.getOutputStream());
/**
* start the Thread
*/
this.start();
}
/**
* the run() function of the thread
*/
public void run(){
try {
while(true){
System.out.println("the proxyServer is now handing the request");
/**
* get the user request
*/
String clientRequest = inFromClient.readLine();
/**
* analyse the user input and handle the error
*/
StringTokenizer token = new StringTokenizer(clientRequest," ");
/**
* handle the illlegal input
*/
if (token.countTokens() < 2) {
System.out.println("requst error");
outToClient.writeBytes("HTTP/1.1 400 Bad Request\n\r");
break;
} else{
String method = token.nextToken();
String url = token.nextToken();
if (!method.equals("GET")) {
System.out.println("method error!");
System.out.println("HTTP/1.1 400 Bad Request\n\r");
break;
}
if (!url.toUpperCase().startsWith("HTTP://")) {
System.out.println("url error!");
System.out.println("HTTP/1.1 400 Bad Request\n\r");
break;
}
/**
*get the requst of the client and send the request to the romote host
*/
int port = 0;
int stringIndex = 0;
String hostName = "";
String filePath = "";
url = url.substring(7);
stringIndex = url.indexOf("/");
hostName = url.substring(0, url.indexOf("/"));
filePath = url.substring(url.indexOf("/"));
if(filePath.equals("/")){
filePath="/index.html";
}
System.out.println(filePath);
if (hostName.indexOf(":")!= -1) {
stringIndex = hostName.indexOf(":");
port = Integer.parseInt(hostName.substring(stringIndex + 1));
hostName = hostName.substring(0, stringIndex);
} else {
port = 80;
}
System.out.println(hostName);
System.out.println(port);
System.out.println(filePath);
System.out.println(url);
Socket clientSocket= new Socket(hostName,port);
inFromServer = new DataInputStream(clientSocket.getInputStream());
outToServer = new DataOutputStream(clientSocket.getOutputStream());
/**
* send request to the remote host
*/
outToServer.writeBytes("GET" +" "+ filePath +" "+ "HTTP/1.0\r\n\r\n");
/**
* send the file returned from remote host to the client
*/
byte[] bytes = new byte[1024];
int size = 0;
while ((size = inFromServer.read(bytes, 0, 1024)) != -1) {
outToClient.write(bytes, 0, size);
}
/**
* when all works are done,close the TCP socket
*/
socket.close();
break;
}
}
} catch (NumberFormatException e) {
System.out.println("the number format is wrong");
e.printStackTrace();
} catch (UnknownHostException e) {
System.out.println("can not connect to the remote host");
e.printStackTrace();
} catch (IOException e) {
System.out.println("input or output error");
e.printStackTrace();
}
/**
* close the streams that are ever used
*/
try {
if (inFromClient != null)
inFromClient.close();
if (outToClient != null)
outToClient.close();
if (inFromServer != null)
inFromServer.close();
if (outToServer != null)
outToServer.close();
if (socket != null)
socket.close();
} catch (IOException e) {
System.out.println("input or output error");
e.printStackTrace();
}
}
}
/**
* the enterance of the programm
* @param args
* @throws IOException
*/
public static void main(String[] args ) throws IOException {
/**
* the server socket listens the port
*/
ServerSocket s;
System.out.println("The web proxy server is started");
/**
* creat a client socket when a requst is reveuived
*/
s = new ServerSocket(PORT);
while(true){
Socket socket=s.accept();
new WebProxyServer(socket);
}
}
}
/**********************************HOW to use*****************************************/
this is a simple HTTP proxy server,user cam get web pagers using this programm.
how to use:
ATTENTION: you can use your web brower or telnet command to send request to the proxy server,but in some cases,telnet may not work well,because telnet is dened by most of the web servers.
1.IN THE BROWER MODLE:
1)make sure your brower's proxy config is right.
2)input the web address into the address box,then press enter,the proxy server will fetch the pagers to you.
2.IN COMMAND LINE MODLE:
1)telnet the proxy server using telnet:
telnet [-a][-e escape char][-f log file][-l user][-t term][host [port]]
eg: telnet 192.168.1.102 8000
2)using "GET" method
GET [hostname]:[port]/[file] HTTP/1.0
eg: GET www.baidu.com:80/index HTTP/1.0
then the web pager will be displayed in your command line in the html form.
- 1
- 2
前往页