Netscape Proxy Server

Previous      Next      RProxyServer.java      URL class      MalformedURLException class

// Proxy Server for Netscape.  Enter Netscape Preferences
// and open "Advanced" then "Proxies."  Open "Manual Proxy Configuration"
// and then open "View."  Under "HTTP Proxy" enter "localhost" and "8690"
// for the "Port."  Click on OK to save the configuration.  Netscape
// now points to the proxy server below, if it is running.  To do that,
// compile the code and run "java RProxyServer."  This is intended for
// demonstration purposes only and cannot serve all files correctly.
import java.io.*;
import java.net.*;

class RPS extends Thread {
   Socket incoming;
   URL url;
   OutputStream out = null;
   InputStream rms = null;
   FileOutputStream file = null;
   String protocol = null;
   String urlname = null;
   
   RPS (Socket incoming) { this.incoming = incoming; }

   // Change all %XX back to actual characters
   public String urlDecode(String in) {
      StringBuffer out = new StringBuffer(in.length());
      int i = 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);
      }
      return new String(out);      
   }

   String getPath(String msg) {
      if (msg.length() == 0 || msg.substring(0,3) == "GET") return null;
      String path = msg.substring(msg.indexOf(' ')+1);
      path = path.substring(0, path.indexOf(' '));
      return path;
   }

   // If the fetched file is an html file, add this header
   void sendHtmlHeader(OutputStream os, int size) {
      // Insert header midstream using PrintWriter
      PrintWriter fos = new PrintWriter(os, true);
      fos.print("HTTP:/1.0 200 OK\n");
      fos.print("MIME-Version: 1.0\n");
      fos.print("Content-type: text/html\n");
      fos.print("Content-length: "+size+"\n");
      fos.print("\n");
   }
   
   // If the fetched file is a gif file, add this header
   void sendGifHeader(OutputStream os, int size) {
      PrintWriter fos = new PrintWriter(os, true);
      fos.print("HTTP:/1.0 200 OK\n");
      fos.print("MIME-Version: 1.0\n");
      fos.print("Content-type: image/gif\n");
      fos.print("Content-length: "+size+"\n");
      fos.print("\n");
   }

   public void run () {
      try {
         InputStream ins = incoming.getInputStream();
         BufferedReader in = new BufferedReader(new InputStreamReader(ins));
         out = incoming.getOutputStream();

         String request = in.readLine();
         urlname = getPath(request);
         urlname = urlDecode(urlname);
         System.out.println("URL: "+urlname);
         String a = in.readLine();
         while (!a.equals("")) {
            System.out.println("Got: "+a);
            a = in.readLine();
         }
         System.out.println("----------------------------");
              
         try {
            // Hopefully hit the URL
            url = new URL(urlname);
            rms = url.openStream();
         }
         catch (MalformedURLException e) {
            // If the URL object can't handle the proposed URL do this
            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);
            
            // Hit the URL
            url = new URL(fullurl);
            InputStream rms = url.openStream();
         }

         int sz = rms.available();  // Get length of block received
         if (urlname.substring(urlname.indexOf(".",urlname.length()-5)+1).equals("gif")) {
            // If receiving a gif file, send gif header
            sendGifHeader(out, sz);
            file = new FileOutputStream("file.gif");
         } else {
            // If receiving an html file, send html header
            sendHtmlHeader(out, sz);
            file = new FileOutputStream("file.html");
         }

         // Route all received blocks to client
         while ((sz = rms.available()) > 0) {
            byte message[] = new byte[sz];
            rms.read(message, 0, sz);
            out.write(message, 0, sz);
            file.write(message, 0, sz);   // Save to a file
         }
      }
      catch (ArrayIndexOutOfBoundsException ai) {
         PrintWriter fos = new PrintWriter(out, true);
         fos.println("< h1 >No URL specified< /h1 >");
      }
      catch (ConnectException ce) {
         PrintWriter fos = new PrintWriter(out, true);            
         fos.println("< h1 >Connection Refused< /h1 >");
      }
      catch (UnknownHostException uh) {
         PrintWriter fos = new PrintWriter(out, true);            
         fos.println("< h1 >Uknown Host: "+urlname+"< /h1 >");
      }
      catch (Exception ex) {
         PrintWriter fos = new PrintWriter(out, true);            
         fos.println("< h1 >Unknown Error: " + ex +"< /h1 >");
      }

      try {
         incoming.close();
      }
      catch (Exception e) {
         PrintWriter fos = new PrintWriter(out, true);            
         fos.println("< h1 >Cannot close connection< /h1 >");
      }
   }
}

public class RProxyServer {
   public static void main (String arg[]) {
      ServerSocket s;
      try {
         s = new ServerSocket(8690);
         System.out.println("Server started and listening on port "+
                            s.getLocalPort());
         while (true) {
            Socket incoming = s.accept( );
            new RPS (incoming).start();
         }
      }
      catch (Exception e) {
         System.out.println(e.toString());
      }
   }
}