20-CS-694 Advanced Programming Techniques Spring 2012
Networking

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Next     ProxyServer.java     ProxyClient.java     URL Class      All lectures

Proxy Server

The compiled ProxyServer class resides in <httpd-root>/cgi-bin. It is started by means of shell script named "server" and with the following contents:
     #!/bin/sh
     /usr/local/java/bin/java ProxyServer $1
The script is also in the cgi-bin directory and is invoked from a browser with, for example, the following URL:
     http://gauss.ececs.uc.edu/cgi-bin/server?yahoo.com
import java.io.*;
import java.net.*;

class ProxyServer {
   public static String urlDecode(String in) {
      StringBuffer out = new StringBuffer(in.length());
      int i = 0;
      int j = 0;
     
      while (i < in.length()) {
         char ch = in.charAt(i);
         i++;
         if (ch == '+') ch = ' ';
         else if (ch == '%') {
            ch = (char)(Integer.parseInt("0x"+ in.substring(i, i + 2)));
            i++;
         }
         out.append(ch);
         j++;
      }
      return new String(out);      
   }

   public static void main(String[] args) {
      URL url;
      String protocol, host="";
      
      try {
         String urlname = urlDecode(args[0]);
         BufferedReader is = null;
         try {
            // Hopefully hit the URL
            url = new URL(urlname);
            InputStream ins = url.openStream();
            is = new BufferedReader(new InputStreamReader(ins));         
         } catch (MalformedURLException e) {
            String fullurl;
            // Check if a protocol is specified.  If not, assume http   
            int pos = urlname.indexOf("://");
            if (pos < 0)
               protocol = "http";
            else
               protocol = urlname.substring(0, pos);
            if (pos < 0) pos = 0; else pos += 3;

            // Assemble a reasonable URL
            int pos1 = urlname.indexOf(":", pos);
            if (pos == pos1)
               fullurl = protocol+"://localhost"+urlname.substring(pos);
            else
               fullurl = protocol+"://"+urlname.substring(pos);
            
            // Find name of host from URL
            int pos2 = urlname.indexOf("/", pos);
            if (pos1 > 0) host = urlname.substring (pos, pos1);
            else
            if (pos2 > 0) host = urlname.substring (pos, pos2);
            else
               host = urlname;

            // Hit the URL
            url = new URL(fullurl);
            InputStream ins = url.openStream();
            is = new BufferedReader(new InputStreamReader(ins));
         }
      
         System.out.print("Content-type: text/html\n\n");
         boolean more = true;
         while (more) {
            String str = is.readLine();
            if (str == null) more = false;
            else System.out.println(str);
         }
      } catch (ArrayIndexOutOfBoundsException ai) {
         System.out.println("<h1>No URL specified</h1>");
      } catch (ConnectException ce) {
         System.out.println("<h1>Connection Refused</h1>");
      } catch (UnknownHostException uh) {
         System.out.println("<h1>Uknown Host: "+host+"</h1>");
      } catch (Exception ex) {
         System.out.println("<h1>Unknown Error: " + ex +"</h1>");
      }
   }
}