Proxy cache

Hello,
I am having problems with a programming assignment. It's a proxy cache assignment, code has been written and I need to fill in the blanks, but I am confused on how to start.
Here's the Proxy cache code
* ProxyCache.java - Simple caching proxy
* $Id: ProxyCache.java,v 1.3 2004/02/16 15:22:00 kangasha Exp $
import java.net.*;
import java.io.*;
import java.util.*;
public class ProxyCache {
    /** Port for the proxy */
    private static int port;
    /** Socket for client connections */
    private static ServerSocket socket;
    /** Create the ProxyCache object and the socket */
    public static void init(int p) {
     port = p;
     try {
         socket = /* Fill in */;
     } catch (IOException e) {
         System.out.println("Error creating socket: " + e);
         System.exit(-1);
    public static void handle(Socket client) {
     Socket server = null;
     HttpRequest request = null;
     HttpResponse response = null;
     /* Process request. If there are any exceptions, then simply
      * return and end this request. This unfortunately means the
      * client will hang for a while, until it timeouts. */
     /* Read request */
     try {
         BufferedReader fromClient = /* Fill in */;
         request = /* Fill in */;
     } catch (IOException e) {
         System.out.println("Error reading request from client: " + e);
         return;
     /* Send request to server */
     try {
         /* Open socket and write request to socket */
         server = /* Fill in */;
         DataOutputStream toServer = /* Fill in */;
         /* Fill in */
     } catch (UnknownHostException e) {
         System.out.println("Unknown host: " + request.getHost());
         System.out.println(e);
         return;
     } catch (IOException e) {
         System.out.println("Error writing request to server: " + e);
         return;
     /* Read response and forward it to client */
     try {
         DataInputStream fromServer = /* Fill in */;
         response = /* Fill in */;
         DataOutputStream toClient = /* Fill in */;
         /* Fill in */
         /* Write response to client. First headers, then body */
         client.close();
         server.close();
         /* Insert object into the cache */
         /* Fill in (optional exercise only) */
     } catch (IOException e) {
         System.out.println("Error writing response to client: " + e);
    /** Read command line arguments and start proxy */
    public static void main(String args[]) {
     int myPort = 0;
     try {
         myPort = Integer.parseInt(args[0]);
     } catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("Need port number as argument");
         System.exit(-1);
     } catch (NumberFormatException e) {
         System.out.println("Please give port number as integer.");
         System.exit(-1);
     init(myPort);
     /** Main loop. Listen for incoming connections and spawn a new
      * thread for handling them */
     Socket client = null;
     while (true) {
         try {
          client = /* Fill in */;
          handle(client);
         } catch (IOException e) {
          System.out.println("Error reading request from client: " + e);
          /* Definitely cannot continue processing this request,
           * so skip to next iteration of while loop. */
          continue;
}

And the HTTPRequest code
* HttpRequest - HTTP request container and parser
* $Id: HttpRequest.java,v 1.2 2003/11/26 18:11:53 kangasha Exp $
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpRequest {
    /** Help variables */
    final static String CRLF = "\r\n";
    final static int HTTP_PORT = 80;
    /** Store the request parameters */
    String method;
    String URI;
    String version;
    String headers = "";
    /** Server and port */
    private String host;
    private int port;
    /** Create HttpRequest by reading it from the client socket */
    public HttpRequest(BufferedReader from) {
     String firstLine = "";
     try {
         firstLine = from.readLine();
     } catch (IOException e) {
         System.out.println("Error reading request line: " + e);
     String[] tmp = firstLine.split(" ");
     method = "GET";
     URI = "HTTP/";
     version = "1.1";
     System.out.println("URI is: " + URI);
     if (!method.equals("GET")) {
         System.out.println("Error: Method not GET");
     try {
         String line = from.readLine();
         while (line.length() != 0) {
          headers += line + CRLF;
          /* We need to find host header to know which server to
           * contact in case the request URI is not complete. */
          if (line.startsWith("Host:")) {
              tmp = line.split(" ");
              if (tmp[1].indexOf(':') > 0) {
               String[] tmp2 = tmp[1].split(":");
               host = tmp2[0];
               port = Integer.parseInt(tmp2[1]);
              } else {
               host = tmp[1];
               port = HTTP_PORT;
          line = from.readLine();
     } catch (IOException e) {
         System.out.println("Error reading from socket: " + e);
         return;
     System.out.println("Host to contact is: " + host + " at port " + port);
    /** Return host for which this request is intended */
    public String getHost() {
     return host;
    /** Return port for server */
    public int getPort() {
     return port;
     * Convert request into a string for easy re-sending.
    public String toString() {
     String req = "";
     req = method + " " + URI + " " + version + CRLF;
     req += headers;
     /* This proxy does not support persistent connections */
     req += "Connection: close" + CRLF;
     req += CRLF;
     return req;
}HTTPResponse code
* HttpResponse - Handle HTTP replies
* $Id: HttpResponse.java,v 1.2 2003/11/26 18:12:42 kangasha Exp $
import java.io.*;
import java.net.*;
import java.util.*;
public class HttpResponse {
    final static String CRLF = "\r\n";
    /** How big is the buffer used for reading the object */
    final static int BUF_SIZE = 8192;
    /** Maximum size of objects that this proxy can handle. For the
     * moment set to 100 KB. You can adjust this as needed. */
    final static int MAX_OBJECT_SIZE = 100000;
    /** Reply status and headers */
    String version;
    int status;
    String statusLine = "";
    String headers = "";
    /* Body of reply */
    byte[] body = new byte[MAX_OBJECT_SIZE];
    /** Read response from server. */
    public HttpResponse(DataInputStream fromServer) {
     /* Length of the object */
     int length = -1;
     boolean gotStatusLine = false;
     /* First read status line and response headers */
     try {
         String line = /* Fill in */;
         while (line.length() != 0) {
          if (!gotStatusLine) {
              statusLine = line;
              gotStatusLine = true;
          } else {
              headers += line + CRLF;
          /* Get length of content as indicated by
           * Content-Length header. Unfortunately this is not
           * present in every response. Some servers return the
           * header "Content-Length", others return
           * "Content-length". You need to check for both
           * here. */
          if (line.startsWith("GET") ||
              line.startsWith("HTTP/1.1")) {
              String[] tmp = line.split(" ");
              length = Integer.parseInt(tmp[1]);
          line = fromServer.readLine();
     } catch (IOException e) {
         System.out.println("Error reading headers from server: " + e);
         return;
     try {
         int bytesRead = 0;
         byte buf[] = new byte[BUF_SIZE];
         boolean loop = false;
         /* If we didn't get Content-Length header, just loop until
          * the connection is closed. */
         if (length == -1) {
          loop = true;
         /* Read the body in chunks of BUF_SIZE and copy the chunk
          * into body. Usually replies come back in smaller chunks
          * than BUF_SIZE. The while-loop ends when either we have
          * read Content-Length bytes or when the connection is
          * closed (when there is no Connection-Length in the
          * response. */
         while (bytesRead < length || loop) {
          /* Read it in as binary data */
          int res = /* Fill in */;
          if (res == -1) {
              break;
          /* Copy the bytes into body. Make sure we don't exceed
           * the maximum object size. */
          for (int i = 0;
               i < res && (i + bytesRead) < MAX_OBJECT_SIZE;
               i++) {
              /* Fill in */
          bytesRead += res;
     } catch (IOException e) {
         System.out.println("Error reading response body: " + e);
         return;
     * Convert response into a string for easy re-sending. Only
     * converts the response headers, body is not converted to a
     * string.
    public String toString() {
     String res = "";
     res = statusLine + CRLF;
     res += headers;
     res += CRLF;
     return res;
}

Similar Messages

  • 11503 Loadbalance SSL sticky and HTTP not sticky to proxy-cache

    I am using a 11503 to balance 200 schools traffic to 5 caches. Some of the schools have firewalls so the CSS sees their PCs as coming from a single IP. If I set the rule to balance sticky then the load is not spread evenly to the 5 proxies causing them to get overloaded from time to time.
    If I balance the load non-sticky (say leastconn) then users have trouble accessing certain SSL sites.
    Does anyone know a good solution for this?

    Hi Joerg,
    Thanks for your reply. How would you code your solution? Currently I am using the following to work around particular sites:
    service Proxy1
    ip address 10.0.0.11
    type proxy-cache
    active
    service Proxy2 ... etc
    **************************** DQL ****************************
    dql domains-no-balance
    domain www.dontbalancethissite.com
    domain ... etc
    !*************************** OWNER ***************************
    owner admin
    content Proxy-servers
    add service Proxy1
    add service Proxy2
    add service Proxy3
    add service Proxy4
    add service Proxy5
    protocol tcp
    port 3128
    vip address 10.0.0.100
    sticky-inact-timeout 5
    balance leastconn
    active
    content no-load-balance
    vip address 10.0.0.100
    advanced-balance sticky-srcip
    balance leastconn
    add service Proxy1
    add service Proxy2
    add service Proxy3
    add service Proxy4
    add service Proxy5
    protocol tcp
    port 3128
    url "/*" dql domains-no-balance
    sticky-inact-timeout 5
    Regards,
    Ben

  • Using AIF and pacman through squid proxy cache

    moved the article to wiki...
    http://wiki.archlinux.org/index.php/Package_Proxy_Cache
    was getting pain to read and edit it in this forum ;P
    .andre
    Last edited by oskude (2010-05-23 14:36:23)

    Hi oskude,
    I really like the squid-proxy-cache thing, but I got one serious problem:
    I always got messages like "TCP_REFRESH_MISS/200 27189867 GET" in access.log, what means that "a newer copy is on the Server" and it does not have it in cache.
    I also played around with your configs in squid.conf, I tried even "refresh_pattern \.pkg\.tar\.   262080       100%       262080       ignore-reload override-expire" - but now I got only TCP-REFRESH_MISS in access.log. (before I had them from time to time, nearly random).
    I only use the ftp part, not http. (also only using ftp mirror).
    A ftp-mirror should never say that there is a newer copy available, because that can not be true. (repackaging etcpp. would increase version number in file name) - so I think I have a problem anywhere in my config.
    Do you have any idea where the problem is here?

  • CSM - Reverse Proxy Cache

    Please, let me know how to configure the reverse proxy cache on CSM.
    Thanks in advance.

    I think that the following URL has all the steps you are looking for to configure reverse proxy cache. http://www.cisco.com/univercd/cc/td/doc/product/webscale/uce/acns42/cnfg42/rproxy.htm

  • SWF verification behind a reverse proxy cache

    Hi!
    If I place an set of FMS servers behind some reverse proxy caches, will I get problem with SWF verification if the cache layer caches the .f4m meta data file with the SWF verification data? Is there any documented best practice on the requirements to build large scale deployment with security enabled?
    best regards
    Johan Acevbedo

    Hello Johan,
    Is in your case drm is embedded inside the f4m??
    HLS-VOD
    Set the TTL for your f4m to max equal to an interval at which you are expecting the swf hashes to update.
    For example, if you expect, you may add/remove swf hashes at interval of say 1 hr, then set the TTL for the f4m as say 50 min (10 min taken as allowed error in your estimation of swf hash update).
    You may set HttpStreamingF4MMaxAge under hds-vod (if that is hds vod case) as per your required TTL. Most proxy cashes should ideally respect the TTL dictated by origin response an should re-request the f4m after that period.
    HDS-LIVE
    Otherwise if this is hds-live case, then I don't think drm is embedded into the f4m. Just verify. Drm is a serperate request. In that case, you can set TTL on drm (HttpStreamingDrmmetaMaxAge) request also under hls-live in httpd.conf.
    Read more about these configs http://help.adobe.com/en_US/flashmediaserver/devguide/WSd391de4d9c7bd609a95b3f112a373a7115 -7fff.html#WSae20eaa80bf612516499f756131e06fb583-7fff
    You can also set the drm update interval time in the recording section of the  application.xml as per your need. Read more about the config at http://help.adobe.com/en_US/flashmediaserver/devguide/WSd391de4d9c7bd609a95b3f112a373a7115 -7fff.html#WSc1a546382286f18f-4a910076130ddc59d17-7ffe . Config setting will only update drm on the disk. But you will still have to set the proper TTL in Apache httpd.conf for the request of the DRM to be sent by the proxy to the origin to fetch it.
    -Nitin

  • SQUID Proxy cache issue in front of Oracle webcache & Oracle Portal 10g

    We are having issue with our site designed on oracle portal front ended by webcache and squid proxy. Current our squid proxy is configure to cache site data which is causing the problem.
    The problem is, we have website in multiple language (china english and china chinese), when you navigate the site in both the languages and once the navigation pages are cached in our squid proxy cache, the users see the cached data from the proxy cache(ie. they will see china chinese when navigating china english site and vice versa). Following are my questions?
    1. I was wondering anyone of you had similar issues and what did you do to resolve it?
    2. What proxy were you using, name, version etc would be helpful?
    3. Did you have similar issues with Other proxys out there? besides squid? what was the fix?
    Your response is appreciated.
    Thanks,
    N

    Hi user623118
    Did you manage to find a solution to this problem. I'm having the same issue and can't find a solution.
    Thanks

  • [REQ] wwwoffle - proxy cache utility

    Could someone produce a PKGBUILD for wwwoffle.  Its a proxy cache designed to be simple to setup & configure.  Particularly useful across a LAN or for dialup.
    http://www.gedanken.demon.co.uk/wwwoffle/

    Configuration file editing
       The configuration file editing functions through the http interface that are
       included in WWWOFFLE add some security concerns.  When the files are edited
       the WWWOFFLE process will need to be able to write out the new files.  The
       new files will be written with the same permissions as the old files into the
       same location.  Included configuration files must be in the same directory as
       the main configuration file.  To simplify management of this the default
       location for the configuration files is the directory '/etc/wwwoffle' which
       must be writable by the WWWOFFLE server but not all users.
    Thus, /etc/wwwoffle must be owned by the wwwoffle user so that it can create further conf files there. I'll change that and upload a new release.

  • How do i change proxy settings so it doesnt keep asking me "authentication req. The proxy web2.ucsd.edu is requesting a username and password. The site says: ucsd Squid Proxy-cache"?

    I changed my proxy setting to access a restricted school website. I don't know how to change it back to normal settings! Every time i'm browsing internet, Authentication Required windows pop up like 4-7 times a day! randomly! it says "the proxy web2.ucsd.edu:3128 is requesting a username and password. The site says: UCSD Squid proxy-cache". and makes me put in username and password every time. sooo annoying. how do i make the setting go back to default??

    1. Open firefox
    2. Go to "Tools" tab
    3. Go to "Options"
    4. Click on "Advanced"
    5. Open "Network" tab
    6. Click on "Settings"
    7. Select "No Proxy"
    8. Click "OK"

  • Proxy / Cache server for flash content / videos / stream

    hi there,
    i've tried to search the web, but so far no straight answer...
    we are looking to implement/deploy a proxy server to cache and then serve flash content (published in the internet), so we can reduce our internet bandwidth utilization. Windows Server 2008 has Windows Media Services which have a feature to cache windows media content or split live stream. (http://www.microsoft.com/windows/windowsmedia/howto/articles/cache_proxy.aspx)
    is there a similar product to deal with flash content?
    thanks in advance for all responses!

    I'm more or less in the same boat. I've got the admin console
    up. I am able to run the vodtest application locally pointing the
    video file to rtmp://localhost/vod/sample.flv or using the server
    name instead of localhost as in rtmp://mclmedia/vod/sample.flv
    I can also load the html and swf file to my xp machine that
    has access to the server via our internal network. (DNS isn't
    configured yet) But the connectionFailed message appears when I try
    to call the videos using rtmp://mclmedia/vod/sample.flv

  • Web forward proxy cache authentication

    Greetings
    just doing some testing. I enabled forward proxy on my web server. Clients can access the cache and can use proxy services.
    How do I setup User based Authentication to access this proxy ? does some magic find my local LDAP or do I need to set something up in httpd.conf ?
    insight and flames welcome

    It's common for people to use the Apache Commons HttpClient library for these kinds of connections. It provides for both proxy and basic auth.

  • Proxy Cache/sizing

    Looking at our proxy console we see the cache is only holding files for about 30-60 minutes, 30% hits. This figure is the same for two different caching configurations:
    proxyserver1:Aging=default, Control HashT=1024, HotNodes=66000, Location=3 12GB Trad.volumes, 512 dirs.
    proxyserver2:Aging=7d,6h,20m,7d,7d,0m,30m, Control http=450MB, HashT=256, HotNodes=50000, Location=3 12GB Trad.volumes, 384 dirs.
    Any suggestions for better caching on these two very busy servers with 2000-6000 connections, ~500 fills in progress?
    Marco

    In article <[email protected]>, Marcoba wrote:
    > Any suggestions for better caching on these two very busy servers with
    > 2000-6000 connections, ~500 fills in progress?
    >
    The caching you are seeing may be dependent on the maximum hot
    unreferenced time (default=30 minutes, Novell recommends upping to 60).
    You have to balance that setting against the maximum hot nodes you are
    hitting. (You never want to hit that maximum, or the server will really
    seem slow). Max hot nodes default=7000 (way to low, Novell recommends
    raising to 50,000 and I think the max is 60,000. This is dependent on a
    max file lock settings of 100,000, which I have in my tuneup.ncf file in
    tip #23 at the URL below).
    The cache percent is actually not bad - most of the servers using my
    proxy.cfg (tip #63) end up with a cache hit percent in the teens,
    because I tell the proxy not to cache sites with cookies. (I've seen
    that cause issues with sites that use cookie-based passwords).
    You need to balance max hot nodes with max hot unreferenced time, RAM
    and sheer speed of disk I/O. The most critical thing I have seen is to
    make sure you never hit the max hot nodes. I view the tuning as partly
    an exercise for keeping under the maximum. On really busy servers with
    fast internet connections I've had to lower the max hot unreferenced
    time to have the proxy begin releasing hot nodes earlier so it doesn't
    max out.
    If you are really busy, you might just consider setting up another proxy
    and load-balancing between them.
    Craig Johnson
    Novell Knowledge Partner
    *** For a current patch list, tips, handy files and books on
    BorderManager, go to http://www.craigjconsulting.com ***

  • Using WSUS like a Proxy cache for windows updates.

    Long story short I want updates to be done exactly as if there was no WSUS server, I could'nt care less about managing what updates my users can download or not download. I just want only
    1 copy of the updates to be downloaded to WSUS or some proxy so i don't have 1000's of users downloading the same file 1000's of time.   I really don't want a system that I need to approve any updates or retain 100's of GB's on my server.  but
    if a user needs a obscure printer driver I don't want to keep it on the server  just because one user might need it. 
    Any Ideas?

    Hi,
    Any update?
    Just checking in to see if the suggestions were helpful. Please let us know if you would like further assistance.
    Best Regards,
    Andy Qi
    TechNet Subscriber Support
    If you are
    TechNet Subscription user and have any feedback on our support quality, please send your feedback
    here.
    Andy Qi
    TechNet Community Support

  • CSS & CE Reverse Proxy Caching - Freshness and Content - Help !!!

    We have a pair of 11054's and a pair of 570 CE and a pair os SCA's and using them in a RPC configuration in front of a series of static content web servers.
    My problem is I want the CE's to server content even when the origin server(s) are down but where the content is cached fresh or not. So of if the cache has the content, serve it no matter what its freshness. I don't want to have a sorry server used unless the content isn't cached at all.
    Or can I pre-position content on the same CE and have it server from there. What content types can be prepositioned ? Is someone going to tell me I need a content router now of something ?
    Anyone have any bright Ideas ??
    Thanks
    Simon

    Since there has been no response to your post, it appears to be either too complex or too rare an issue for other forum members to assist you. If you don't get a suitable response to your post, you may wish to review our resources at the online Technical Assistance Center (http://www.cisco.com/tac) or speak with a TAC engineer. You can open a TAC case online at http://www.cisco.com/tac/caseopen
    If anyone else in the forum has some advice, please reply to this thread.
    Thank you for posting.

  • CSS & Reverse Proxy Caching

    With the following configuration example:
    http://www.cisco.com/en/US/products/sw/conntsw/ps491/products_configuration_guide_chapter09186a00800af658.html#xtocid7
    How does the CE know to pass traffic destined for the origin server(s) to the <cache_miss_vip>?
    ~Zach

    Nevermind. I found what I was looking for:
    'rule use-proxy ...' on the CE.
    ~Zach

  • CSS & Reverse Proxy Caching, Part II

    What potential problems can I run into if I configure the 'rule use-proxy' command on the CE to point directly to the origin server (as opposed to routing the request to the origin server back through the CE)?
    ~Zach

    Nevermind. I found what I was looking for:
    'rule use-proxy ...' on the CE.
    ~Zach

Maybe you are looking for