Changeset 20


Ignore:
Timestamp:
Aug 14, 2012 8:55:41 PM (5 years ago)
Author:
psaiteja
Message:

made some minor changes in the comments and removed some unnecessary code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • VSNPullServer/src/PullServer.java

    r18 r20  
    1 /* <!-- in case someone opens this in a browser... --> <pre> */ 
    2 /* 
    3  * This is a simple multi-threaded Java proxy server 
    4  * for HTTP requests (HTTPS doesn't seem to work, because 
    5  * the CONNECT requests aren't always handled properly). 
    6  * I implemented the class as a thread so you can call it  
    7  * from other programs and kill it, if necessary (by using  
    8  * the closeSocket() method). 
    9  * 
    10  * We'll call this the 1.1 version of this class. All I  
    11  * changed was to separate the HTTP header elements with 
    12  * \r\n instead of just \n, to comply with the official 
    13  * HTTP specification. 
    14  *   
    15  * This can be used either as a direct proxy to other 
    16  * servers, or as a forwarding proxy to another proxy 
    17  * server. This makes it useful if you want to monitor 
    18  * traffic going to and from a proxy server (for example, 
    19  * you can run this on your local machine and set the 
    20  * fwdServer and fwdPort to a real proxy server, and then 
    21  * tell your browser to use "localhost" as the proxy, and 
    22  * you can watch the browser traffic going in and out). 
    23  * 
    24  * One limitation of this implementation is that it doesn't  
    25  * close the ProxyThread socket if the client disconnects 
    26  * or the server never responds, so you could end up with 
    27  * a bunch of loose threads running amuck and waiting for 
    28  * connections. As a band-aid, you can set the server socket 
    29  * to timeout after a certain amount of time (use the 
    30  * setTimeout() method in the ProxyThread class), although 
    31  * this can cause false timeouts if a remote server is simply 
    32  * slow to respond. 
    33  * 
    34  * Another thing is that it doesn't limit the number of 
    35  * socket threads it will create, so if you use this on a 
    36  * really busy machine that processed a bunch of requests, 
    37  * you may have problems. You should use thread pools if 
    38  * you're going to try something like this in a "real" 
    39  * application. 
    40  * 
    41  * Note that if you're using the "main" method to run this 
    42  * by itself and you don't need the debug output, it will 
    43  * run a bit faster if you pipe the std output to 'nul'. 
    44  * 
    45  * You may use this code as you wish, just don't pretend  
    46  * that you wrote it yourself, and don't hold me liable for  
    47  * anything that it does or doesn't do. If you're feeling  
    48  * especially honest, please include a link to nsftools.com 
    49  * along with the code. Thanks, and good luck. 
    50  * 
    51  * Julian Robichaux -- http://www.nsftools.com 
    52  */ 
    531import java.io.*; 
    542import java.net.*; 
     
    440388                                // tell the client there was an error 
    441389                                System.out.println("Error connecting to the server"); 
    442                                 String errMsg = "HTTP/1.0 503 Service Unavailable\r\nContent Type: text/plain\r\n" +  
    443                                                                 "<html><body>Error connecting to the server:\n" + e + "\n</body></html>\r\n"; 
     390                                String errMsg_body = "<html><head><title>503 Service Temporarily Unavailable</title></head>"+ 
     391                                        "<body><h1>VSN ERROR MESSAGE: Service Temporarily Unavailable</h1>"+ 
     392                                        "<p>The server("+hostName+") is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.</p>"+ 
     393                                        "</body></html>\r\n"; 
     394                                String errMsg_header = "HTTP/1.1 503 Service Unavailable\r\n"+ 
     395                                "Content-Type: text/html\r\n"+ 
     396                                                "Content-Length: "+errMsg_body.length()+"\r\n"+ 
     397                                        "Connection: close\r\n\r\n"; 
     398                                String errMsg = errMsg_header+errMsg_body;         
    444399                                clientOut.write(errMsg.getBytes(), 0, errMsg.length()); 
     400                                clientOut.flush(); 
    445401                        } 
    446402                         
     
    475431                                { 
    476432                                        System.out.println ("Socket timeout occurred - killing connection"); 
    477                                         String errMsg = "HTTP/1.0 504 Gateway Time-out\r\nContent Type: text/plain\r\n" +  
     433                                        String errMsg = "HTTP/1.0 504 Gateway Time-out\r\nContent-Type: text/html\r\n\r\n" +  
    478434                                                        "<html><body>Error connecting to the server:\n" + ste + "\n</body></html>\r\n"; 
    479435                                        clientOut.write(errMsg.getBytes(), 0, errMsg.length()); 
     
    592548                                                pre_url.setLength(0); 
    593549                                                pre_url.append(data.substring(0,pos)); 
    594                                                 //String suffix = data.substring(pos + 1,data.indexOf(" ", pos + 1)); 
    595                                                 //url.setLength(0); 
    596                                                 //url.append(suffix.trim()); 
    597550                                                url.setLength(0); 
    598551                                                url.append(data.substring(pos + 1,data.indexOf(" ", pos + 1))); 
     
    630583                                temp_header.append(data + "\r\n"); 
    631584                        } 
    632                          
    633 //                      if(url.indexOf(host.toString())>=0){ 
    634 //                              String newurl = url.substring(url.indexOf(host.toString())+host.length()); 
    635 //                              url.setLength(0); 
    636 //                              url.append(newurl); 
    637 //                      } 
    638585                         
    639586                        if(url.toString().startsWith(host.toString()) || url.toString().startsWith("http://"+host.toString())){ 
Note: See TracChangeset for help on using the changeset viewer.