develooper Front page | perl.libwww | Postings from August 2001

Patch for LWP::UserAgent, simple_request = prepare_request + real_request

Thread Next
From:
Keiichiro Nagano
Date:
August 13, 2001 04:23
Subject:
Patch for LWP::UserAgent, simple_request = prepare_request + real_request
Message ID:
lyg0awdwzm.wl@www.sodan.org> <knagano@sodan.org
Nice to meet you all.  My name is Keiichiro Nagano, student at
Univ. of Tokyo, Japan.  I'm not so good at English.  Very sorry for
poor writing.

I think LWP::UserAgent->simple_request should be divided into 2 parts.
First part modifies given HTTP::Request object according to
UserAgent's properties.  And second part simply send given
HTTP::Request object.  This has advantages as follows: [1] One can see
the real content of the request before actual requesting.  [2] One
(maybe a skilled user) can send his/her HTTP::Request object
untouched.  [3] Friendly for inheritance from LWP::UserAgent.

So I made this patch.  Would you here me?


----- cut ----- cut ----- cut ----- cut ----- cut ----- cut ----- 
*** LWP/UserAgent.pm.ORIGINAL	Mon Aug 13 18:37:10 2001
--- LWP/UserAgent.pm	Mon Aug 13 20:15:30 2001
***************
*** 158,163 ****
--- 158,204 ----
  sub simple_request
  {
      my($self, $request, $arg, $size) = @_;
+ 
+     my $new_request = $self->prepare_request($request);
+     return($self->real_request($new_request, $arg, $size));
+ }
+ 
+ =item $ua->prepare_request($request)
+ 
+ This method modifies given C<HTTP::Request> object for
+ C<real_request>.  Used in C<simple_request>.  This helps those who
+ want to see the whole content of the request before actual requesting.
+ 
+ =cut
+ 
+ sub prepare_request
+ {
+     my($self, $request) = @_;
+ 
+     # Extract fields that will be used below
+     my ($agent, $from, $cookie_jar, $max_size) =
+ 	@{$self}{qw(agent from cookie_jar max_size)};
+ 
+     # Set User-Agent and From headers if they are defined
+     $request->init_header('User-Agent' => $agent) if $agent;
+     $request->init_header('From' => $from) if $from;
+     $request->init_header('Range' => "bytes=0-$max_size") if $max_size;
+     $cookie_jar->add_cookie_header($request) if $cookie_jar;
+ 
+     return($request);
+ }
+ 
+ =item $ua->real_request($request, $arg [, $size])
+ 
+ This method processes given <$request> with no modification (but
+ checks errors).  This helps those who wants to send his/her
+ C<HTTP::Request> object untouched.  Also frienldy for inheritance.
+ 
+ =cut
+ 
+ sub real_request
+ {
+     my($self, $request, $arg, $size) = @_;
      local($SIG{__DIE__});  # protect agains user defined die handlers
  
      my($method, $url) = ($request->method, $request->url);
***************
*** 172,177 ****
--- 213,222 ----
  
      LWP::Debug::trace("$method $url");
  
+     # 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)};
+ 
      # Locate protocol to use
      my $scheme = '';
      my $proxy = $self->_need_proxy($url);
***************
*** 189,206 ****
  	return HTTP::Response->new(&HTTP::Status::RC_NOT_IMPLEMENTED, $@)
      }
  
-     # Extract fields that will be used below
-     my ($agent, $from, $timeout, $cookie_jar,
-         $use_eval, $parse_head, $max_size) =
-       @{$self}{qw(agent from timeout cookie_jar
-                   use_eval parse_head max_size)};
- 
-     # Set User-Agent and From headers if they are defined
-     $request->init_header('User-Agent' => $agent) if $agent;
-     $request->init_header('From' => $from) if $from;
-     $request->init_header('Range' => "bytes=0-$max_size") if $max_size;
-     $cookie_jar->add_cookie_header($request) if $cookie_jar;
- 
      # Transfer some attributes to the protocol object
      $protocol->parse_head($parse_head);
      $protocol->max_size($max_size);
--- 234,239 ----
***************
*** 229,235 ****
      $response->header("Client-Date" => HTTP::Date::time2str(time));
      return $response;
  }
- 
  
  =item $ua->request($request, $arg [, $size])
  
--- 262,267 ----
----- cut ----- cut ----- cut ----- cut ----- cut ----- cut ----- 

-- 
Keiichiro Nagano

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About