on 9/25/02 12:50 AM, [EMAIL PROTECTED] purportedly said:

> * Keary Suska ([EMAIL PROTECTED]) [25 Sep 2002 16:47]:
>> I found myself in a position of needing to specify source addresses
>> for LWP http connections, which LWP isn't coded to allow. So I hacked
>> UserAgent to take a local_addr parameter which is propagated to
>> Protocol:http.
> 
>> It's pretty basic but if anyone cares I can post the diffs.
> 
> Please do. I'm curious to see what you mean by 'source addresses'.

Every socket has a source IP/port and a destination IP/port. IIRC, unless
specified, the source address is the "default" address for the machine,
which can vary depending on the network configuration, hosts file, etc.

I added support for a local_addr parameter which takes the form
"host[:port]", according to the IO::Socket::Inet LocalAddr parameter. The
diff is below.

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"

--- LWP/Protocol/http.pm        Wed Sep 25 00:16:38 2002
***************
*** 19,25 ****

  sub _new_socket
  {
!     my($self, $host, $port, $timeout) = @_;
      my $conn_cache = $self->{ua}{conn_cache};
      if ($conn_cache) {
        if (my $sock = $conn_cache->withdraw("http", "$host:$port")) {
--- 19,25 ----

  sub _new_socket
  {
!     my($self, $host, $port, $timeout,$local_addr) = @_;
      my $conn_cache = $self->{ua}{conn_cache};
      if ($conn_cache) {
        if (my $sock = $conn_cache->withdraw("http", "$host:$port")) {
***************
*** 34,39 ****
--- 34,40 ----
      local($^W) = 0;  # IO::Socket::INET can be noisy
      my $sock = $self->socket_class->new(PeerAddr => $host,
                                        PeerPort => $port,
+                                       LocalAddr => $local_addr,
                                        Proto    => 'tcp',
                                        Timeout  => $timeout,
                                        KeepAlive => !!$conn_cache,
***************
*** 118,124 ****

  sub request
  {
!     my($self, $request, $proxy, $arg, $size, $timeout) = @_;
      LWP::Debug::trace('()');

      $size ||= 4096;
--- 119,125 ----

  sub request
  {
!     my($self, $request, $proxy, $arg, $size, $timeout, $local_addr) = @_;
      LWP::Debug::trace('()');

      $size ||= 4096;
***************
*** 151,157 ****
      }

      # connect to remote site
!     my $socket = $self->_new_socket($host, $port, $timeout);
      $self->_check_sock($request, $socket);

      my @h;
--- 152,158 ----
      }

      # connect to remote site
!     my $socket = $self->_new_socket($host, $port, $timeout, $local_addr);
      $self->_check_sock($request, $socket);

      my @h;
Only in /usr/local/lib/perl5/site_perl/5.6.1/LWP/Protocol: http11.pm
Only in /usr/local/lib/perl5/site_perl/5.6.1/LWP/Protocol: https11.pm
diff -rBC3 /usr/local/lib/perl5/site_perl/5.6.1/LWP/UserAgent.pm
LWP/UserAgent.pm
*** /usr/local/lib/perl5/site_perl/5.6.1/LWP/UserAgent.pm       Tue Dec 11
14:11:29 2001
--- LWP/UserAgent.pm    Wed Sep 25 00:22:41 2002
***************
*** 180,185 ****
--- 180,188 ----
      my $conn_cache = delete $cnf{conn_cache};
      my $keep_alive = delete $cnf{keep_alive};

+     # set local address
+     my $local_addr = delete $cnf{local_addr};
+
      Carp::croak("Can't mix conn_cache and keep_alive")
          if $conn_cache && $keep_alive;

***************
*** 211,216 ****
--- 213,219 ----
                      parse_head  => $parse_head,
                      max_size    => $max_size,
                      proxy       => undef,
+                     local_addr => $local_addr,
                      no_proxy    => [],
                        protocols_allowed => $protocols_allowed,
                        protocols_forbidden => $protocols_forbidden,
***************
*** 337,351 ****
      }

      # Extract fields that will be used below
!     my ($timeout, $cookie_jar, $use_eval, $parse_head, $max_size) =
!       @{$self}{qw(timeout cookie_jar use_eval parse_head max_size)};

      my $response;
      if ($use_eval) {
        # we eval, and turn dies into responses below
        eval {
            $response = $protocol->request($request, $proxy,
!                                          $arg, $size, $timeout);
        };
        if ($@) {
            $@ =~ s/ at .* line \d+.*//s;  # remove file/line number
--- 340,354 ----
      }

      # Extract fields that will be used below
!     my ($timeout, $cookie_jar, $use_eval, $parse_head, $max_size,
$local_addr) =
!       @{$self}{qw(timeout cookie_jar use_eval parse_head max_size
local_addr)};

      my $response;
      if ($use_eval) {
        # we eval, and turn dies into responses below
        eval {
            $response = $protocol->request($request, $proxy,
!                                          $arg, $size, $timeout,
$local_addr);
        };
        if ($@) {
            $@ =~ s/ at .* line \d+.*//s;  # remove file/line number

Reply via email to