title: Perl transparent proxy
body: <p>I'm trying to create one transparent <strong>HTTP proxy</strong>. It's purpose is to stay between the browser and the web server and be invisible. Here is the code I'm using. Unfortunately it's not working very well. When I open the web page (referenced by <code>$base_uri</code>) there are different results depending on that whether I've opened it using the browser only or the browser and the proxy. I'm trying it on a web site which is returning all kinds of responses including "<code>transfer-encoding: chunked</code>" (so I guess may be the problem could be there?!). I think that there could be also problems with the cookies but I don't know how to solve them (if any...).</p>
  
  <pre><code>#!/usr/bin/perl
  
  use strict;
  
  use HTTP::Daemon;
  use LWP::UserAgent;
  use HTTP::Cookies;
  
  my $cookie_jar = HTTP::Cookies-&gt;new();
  
  my $ua = LWP::UserAgent-&gt;new( max_redirect =&gt; 0, env_proxy =&gt; 0,keep_alive =&gt; 1, timeout =&gt; 30, agent =&gt; "Mozilla/4.76 [en] (Win98; U)");
  my $d = HTTP::Daemon-&gt;new( 
      LocalHost =&gt; "localhost",  # remove this to listen from other machines 
                     # (i.e. open-relay... be careful of spammers!)
      LocalPort =&gt; 33331
  ) || die;
  print "[Proxy URL:", $d-&gt;url, "]\n";
  
  fork(); fork(); fork();  # 2^3 = 8 processes
  
  $ua-&gt;cookie_jar($cookie_jar);
  
  my $base_uri = 'http://example.com/';
  
  while (my $c = $d-&gt;accept) {
          while (my $request = $c-&gt;get_request) {
              my $uri = $base_uri . $request-&gt;uri-&gt;as_string();
              my $method = $request-&gt;method;
              my $req = HTTP::Request-&gt;new($method, $uri);
  
              $request-&gt;uri($uri);
  
              print "[[ $method &gt;&gt; $uri ]]\n";
              my $response = $ua-&gt;simple_request($request);
              $c-&gt;send_response( $response );
          }
      $c-&gt;close;
      undef($c);
  }
  </code></pre>
  
  <p>Thank you in advance!</p>