File | /project/perl/lib/LWP/Protocol.pm |
Statements Executed | 10214 |
Statement Execution Time | 150ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
461 | 1 | 1 | 107ms | 515ms | collect | LWP::Protocol::
461 | 1 | 1 | 40.8ms | 68.8ms | implementor | LWP::Protocol::
461 | 1 | 1 | 30.6ms | 115ms | create | LWP::Protocol::
461 | 1 | 1 | 15.9ms | 15.9ms | new | LWP::Protocol::
2 | 2 | 2 | 29µs | 29µs | CORE:match (opcode) | LWP::Protocol::
1 | 1 | 2 | 5µs | 5µs | CORE:subst (opcode) | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | BEGIN | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | __ANON__[:186] | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | collect_once | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | max_size | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | parse_head | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | request | LWP::Protocol::
0 | 0 | 0 | 0s | 0s | timeout | LWP::Protocol::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package LWP::Protocol; | ||||
2 | |||||
3 | # $Id: Protocol.pm,v 1.43 2004/11/12 13:34:10 gisle Exp $ | ||||
4 | |||||
5 | 1 | 272µs | require LWP::MemberMixin; | ||
6 | 1 | 10µs | @ISA = qw(LWP::MemberMixin); | ||
7 | 1 | 58µs | 1 | 20µs | $VERSION = sprintf("%d.%02d", q$Revision: 1.43 $ =~ /(\d+)\.(\d+)/); # spent 20µs making 1 call to LWP::Protocol::CORE:match |
8 | |||||
9 | 3 | 92µs | 1 | 28µs | use strict; # spent 28µs making 1 call to strict::import |
10 | 3 | 50µs | use Carp (); | ||
11 | 3 | 52µs | use HTTP::Status (); | ||
12 | 3 | 569µs | 1 | 143µs | use HTTP::Response; # spent 143µs making 1 call to Exporter::import |
13 | |||||
14 | 1 | 5µs | my %ImplementedBy = (); # scheme => classname | ||
15 | |||||
16 | |||||
17 | |||||
18 | sub new | ||||
19 | # spent 15.9ms within LWP::Protocol::new which was called 461 times, avg 35µs/call:
# 461 times (15.9ms+0s) by LWP::Protocol::create at line 42, avg 35µs/call | ||||
20 | 461 | 2.81ms | my($class, $scheme, $ua) = @_; | ||
21 | |||||
22 | 461 | 7.65ms | my $self = bless { | ||
23 | scheme => $scheme, | ||||
24 | ua => $ua, | ||||
25 | |||||
26 | # historical/redundant | ||||
27 | parse_head => $ua->{parse_head}, | ||||
28 | max_size => $ua->{max_size}, | ||||
29 | }, $class; | ||||
30 | |||||
31 | 461 | 7.09ms | $self; | ||
32 | } | ||||
33 | |||||
34 | |||||
35 | sub create | ||||
36 | # spent 115ms (30.6+84.7) within LWP::Protocol::create which was called 461 times, avg 250µs/call:
# 461 times (30.6ms+84.7ms) by LWP::UserAgent::send_request at line 191 of LWP/UserAgent.pm, avg 250µs/call | ||||
37 | 461 | 2.44ms | my($scheme, $ua) = @_; | ||
38 | 461 | 7.27ms | 461 | 68.8ms | my $impclass = LWP::Protocol::implementor($scheme) or # spent 68.8ms making 461 calls to LWP::Protocol::implementor, avg 149µs/call |
39 | Carp::croak("Protocol scheme '$scheme' is not supported"); | ||||
40 | |||||
41 | # hand-off to scheme specific implementation sub-class | ||||
42 | 461 | 11.5ms | 461 | 15.9ms | my $protocol = $impclass->new($scheme, $ua); # spent 15.9ms making 461 calls to LWP::Protocol::new, avg 35µs/call |
43 | |||||
44 | 461 | 6.07ms | return $protocol; | ||
45 | } | ||||
46 | |||||
47 | |||||
48 | sub implementor | ||||
49 | # spent 68.8ms (40.8+27.9) within LWP::Protocol::implementor which was called 461 times, avg 149µs/call:
# 461 times (40.8ms+27.9ms) by LWP::Protocol::create at line 38, avg 149µs/call | ||||
50 | 461 | 2.30ms | my($scheme, $impclass) = @_; | ||
51 | |||||
52 | 461 | 2.00ms | if ($impclass) { | ||
53 | $ImplementedBy{$scheme} = $impclass; | ||||
54 | } | ||||
55 | 461 | 2.76ms | my $ic = $ImplementedBy{$scheme}; | ||
56 | 461 | 7.16ms | return $ic if $ic; | ||
57 | |||||
58 | 1 | 26µs | 1 | 9µs | return '' unless $scheme =~ /^([.+\-\w]+)$/; # check valid URL schemes # spent 9µs making 1 call to LWP::Protocol::CORE:match |
59 | 1 | 7µs | $scheme = $1; # untaint | ||
60 | 1 | 26µs | 1 | 5µs | $scheme =~ s/[.+\-]/_/g; # make it a legal module name # spent 5µs making 1 call to LWP::Protocol::CORE:subst |
61 | |||||
62 | # scheme not yet known, look for a 'use'd implementation | ||||
63 | 1 | 5µs | $ic = "LWP::Protocol::$scheme"; # default location | ||
64 | 1 | 5µs | $ic = "LWP::Protocol::nntp" if $scheme eq 'news'; #XXX ugly hack | ||
65 | 3 | 1.64ms | 1 | 99µs | no strict 'refs'; # spent 99µs making 1 call to strict::unimport |
66 | # check we actually have one for the scheme: | ||||
67 | 1 | 19µs | unless (@{"${ic}::ISA"}) { | ||
68 | # try to autoload it | ||||
69 | 1 | 282µs | eval "require $ic"; | ||
70 | 1 | 5µs | if ($@) { | ||
71 | if ($@ =~ /Can't locate/) { #' #emacs get confused by ' | ||||
72 | $ic = ''; | ||||
73 | } | ||||
74 | else { | ||||
75 | die "$@\n"; | ||||
76 | } | ||||
77 | } | ||||
78 | } | ||||
79 | 1 | 7µs | $ImplementedBy{$scheme} = $ic if $ic; | ||
80 | 1 | 28µs | $ic; | ||
81 | } | ||||
82 | |||||
83 | |||||
84 | sub request | ||||
85 | { | ||||
86 | my($self, $request, $proxy, $arg, $size, $timeout) = @_; | ||||
87 | Carp::croak('LWP::Protocol::request() needs to be overridden in subclasses'); | ||||
88 | } | ||||
89 | |||||
90 | |||||
91 | # legacy | ||||
92 | sub timeout { shift->_elem('timeout', @_); } | ||||
93 | sub parse_head { shift->_elem('parse_head', @_); } | ||||
94 | sub max_size { shift->_elem('max_size', @_); } | ||||
95 | |||||
96 | |||||
97 | sub collect | ||||
98 | # spent 515ms (107+408) within LWP::Protocol::collect which was called 461 times, avg 1.12ms/call:
# 461 times (107ms+408ms) by LWP::Protocol::http::request at line 352 of LWP/Protocol/http.pm, avg 1.12ms/call | ||||
99 | 461 | 3.14ms | my ($self, $arg, $response, $collector) = @_; | ||
100 | 461 | 1.96ms | my $content; | ||
101 | 461 | 3.34ms | my($parse_head, $max_size) = @{$self}{qw(parse_head max_size)}; | ||
102 | |||||
103 | 461 | 2.09ms | my $parser; | ||
104 | 461 | 10.4ms | 461 | 101ms | if ($parse_head && $response->content_type eq 'text/html') { # spent 101ms making 460 calls to HTTP::Message::__ANON__[(eval 0)[HTTP/Message.pm:371]:1], avg 219µs/call
# spent 183µs making 1 call to HTTP::Message::AUTOLOAD |
105 | 461 | 12.4ms | require HTML::HeadParser; | ||
106 | 461 | 23.7ms | 461 | 195ms | $parser = HTML::HeadParser->new($response->{'_headers'}); # spent 195ms making 461 calls to HTML::HeadParser::new, avg 423µs/call |
107 | } | ||||
108 | 461 | 2.18ms | my $content_size = 0; | ||
109 | |||||
110 | 461 | 4.52ms | if (!defined($arg) || !$response->is_success) { | ||
111 | # scalar | ||||
112 | 461 | 11.2ms | 461 | 88.5ms | while ($content = &$collector, length $$content) { # spent 88.5ms making 461 calls to LWP::Protocol::http::__ANON__[LWP/Protocol/http.pm:352], avg 192µs/call |
113 | 7 | 137µs | if ($parser) { | ||
114 | 7 | 394µs | 14 | 1.69ms | $parser->parse($$content) or undef($parser); # spent 986µs making 7 calls to HTML::Parser::parse, avg 141µs/call
# spent 703µs making 7 calls to HTML::HeadParser::text, avg 100µs/call |
115 | } | ||||
116 | 7 | 148µs | 7 | 94µs | LWP::Debug::debug("read " . length($$content) . " bytes"); # spent 94µs making 7 calls to LWP::Debug::debug, avg 13µs/call |
117 | 7 | 135µs | 7 | 334µs | $response->add_content($$content); # spent 334µs making 7 calls to HTTP::Message::add_content, avg 48µs/call |
118 | 7 | 41µs | $content_size += length($$content); | ||
119 | 7 | 160µs | 7 | 2.53ms | if (defined($max_size) && $content_size > $max_size) { # spent 2.53ms making 7 calls to LWP::Protocol::http::__ANON__[LWP/Protocol/http.pm:352], avg 362µs/call |
120 | LWP::Debug::debug("Aborting because size limit exceeded"); | ||||
121 | $response->push_header("Client-Aborted", "max_size"); | ||||
122 | #my $tot = $response->header("Content-Length") || 0; | ||||
123 | #$response->header("X-Content-Range", "bytes 0-$content_size/$tot"); | ||||
124 | last; | ||||
125 | } | ||||
126 | } | ||||
127 | } | ||||
128 | elsif (!ref($arg)) { | ||||
129 | # filename | ||||
130 | open(OUT, ">$arg") or | ||||
131 | return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR, | ||||
132 | "Cannot write to '$arg': $!"); | ||||
133 | binmode(OUT); | ||||
134 | local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR | ||||
135 | while ($content = &$collector, length $$content) { | ||||
136 | if ($parser) { | ||||
137 | $parser->parse($$content) or undef($parser); | ||||
138 | } | ||||
139 | LWP::Debug::debug("read " . length($$content) . " bytes"); | ||||
140 | print OUT $$content or die "Can't write to '$arg': $!"; | ||||
141 | $content_size += length($$content); | ||||
142 | if (defined($max_size) && $content_size > $max_size) { | ||||
143 | LWP::Debug::debug("Aborting because size limit exceeded"); | ||||
144 | $response->push_header("Client-Aborted", "max_size"); | ||||
145 | #my $tot = $response->header("Content-Length") || 0; | ||||
146 | #$response->header("X-Content-Range", "bytes 0-$content_size/$tot"); | ||||
147 | last; | ||||
148 | } | ||||
149 | } | ||||
150 | close(OUT) or die "Can't write to '$arg': $!"; | ||||
151 | } | ||||
152 | elsif (ref($arg) eq 'CODE') { | ||||
153 | # read into callback | ||||
154 | while ($content = &$collector, length $$content) { | ||||
155 | if ($parser) { | ||||
156 | $parser->parse($$content) or undef($parser); | ||||
157 | } | ||||
158 | LWP::Debug::debug("read " . length($$content) . " bytes"); | ||||
159 | eval { | ||||
160 | &$arg($$content, $response, $self); | ||||
161 | }; | ||||
162 | if ($@) { | ||||
163 | chomp($@); | ||||
164 | $response->push_header('X-Died' => $@); | ||||
165 | $response->push_header("Client-Aborted", "die"); | ||||
166 | last; | ||||
167 | } | ||||
168 | } | ||||
169 | } | ||||
170 | else { | ||||
171 | return HTTP::Response->new(&HTTP::Status::RC_INTERNAL_SERVER_ERROR, | ||||
172 | "Unexpected collect argument '$arg'"); | ||||
173 | } | ||||
174 | 461 | 11.7ms | $response; | ||
175 | } | ||||
176 | |||||
177 | |||||
178 | sub collect_once | ||||
179 | { | ||||
180 | my($self, $arg, $response) = @_; | ||||
181 | my $content = \ $_[3]; | ||||
182 | my $first = 1; | ||||
183 | $self->collect($arg, $response, sub { | ||||
184 | return $content if $first--; | ||||
185 | return \ ""; | ||||
186 | }); | ||||
187 | } | ||||
188 | |||||
189 | 1 | 23µs | 1; | ||
190 | |||||
191 | |||||
192 | __END__ | ||||
193 | |||||
194 | =head1 NAME | ||||
195 | |||||
196 | LWP::Protocol - Base class for LWP protocols | ||||
197 | |||||
198 | =head1 SYNOPSIS | ||||
199 | |||||
200 | package LWP::Protocol::foo; | ||||
201 | require LWP::Protocol; | ||||
202 | @ISA=qw(LWP::Protocol); | ||||
203 | |||||
204 | =head1 DESCRIPTION | ||||
205 | |||||
206 | This class is used a the base class for all protocol implementations | ||||
207 | supported by the LWP library. | ||||
208 | |||||
209 | When creating an instance of this class using | ||||
210 | C<LWP::Protocol::create($url)>, and you get an initialised subclass | ||||
211 | appropriate for that access method. In other words, the | ||||
212 | LWP::Protocol::create() function calls the constructor for one of its | ||||
213 | subclasses. | ||||
214 | |||||
215 | All derived LWP::Protocol classes need to override the request() | ||||
216 | method which is used to service a request. The overridden method can | ||||
217 | make use of the collect() function to collect together chunks of data | ||||
218 | as it is received. | ||||
219 | |||||
220 | The following methods and functions are provided: | ||||
221 | |||||
222 | =over 4 | ||||
223 | |||||
224 | =item $prot = LWP::Protocol->new() | ||||
225 | |||||
226 | The LWP::Protocol constructor is inherited by subclasses. As this is a | ||||
227 | virtual base class this method should B<not> be called directly. | ||||
228 | |||||
229 | =item $prot = LWP::Protocol::create($scheme) | ||||
230 | |||||
231 | Create an object of the class implementing the protocol to handle the | ||||
232 | given scheme. This is a function, not a method. It is more an object | ||||
233 | factory than a constructor. This is the function user agents should | ||||
234 | use to access protocols. | ||||
235 | |||||
236 | =item $class = LWP::Protocol::implementor($scheme, [$class]) | ||||
237 | |||||
238 | Get and/or set implementor class for a scheme. Returns '' if the | ||||
239 | specified scheme is not supported. | ||||
240 | |||||
241 | =item $prot->request(...) | ||||
242 | |||||
243 | $response = $protocol->request($request, $proxy, undef); | ||||
244 | $response = $protocol->request($request, $proxy, '/tmp/sss'); | ||||
245 | $response = $protocol->request($request, $proxy, \&callback, 1024); | ||||
246 | |||||
247 | Dispatches a request over the protocol, and returns a response | ||||
248 | object. This method needs to be overridden in subclasses. Refer to | ||||
249 | L<LWP::UserAgent> for description of the arguments. | ||||
250 | |||||
251 | =item $prot->collect($arg, $response, $collector) | ||||
252 | |||||
253 | Called to collect the content of a request, and process it | ||||
254 | appropriately into a scalar, file, or by calling a callback. If $arg | ||||
255 | is undefined, then the content is stored within the $response. If | ||||
256 | $arg is a simple scalar, then $arg is interpreted as a file name and | ||||
257 | the content is written to this file. If $arg is a reference to a | ||||
258 | routine, then content is passed to this routine. | ||||
259 | |||||
260 | The $collector is a routine that will be called and which is | ||||
261 | responsible for returning pieces (as ref to scalar) of the content to | ||||
262 | process. The $collector signals EOF by returning a reference to an | ||||
263 | empty sting. | ||||
264 | |||||
265 | The return value from collect() is the $response object reference. | ||||
266 | |||||
267 | B<Note:> We will only use the callback or file argument if | ||||
268 | $response->is_success(). This avoids sending content data for | ||||
269 | redirects and authentication responses to the callback which would be | ||||
270 | confusing. | ||||
271 | |||||
272 | =item $prot->collect_once($arg, $response, $content) | ||||
273 | |||||
274 | Can be called when the whole response content is available as | ||||
275 | $content. This will invoke collect() with a collector callback that | ||||
276 | returns a reference to $content the first time and an empty string the | ||||
277 | next. | ||||
278 | |||||
279 | =head1 SEE ALSO | ||||
280 | |||||
281 | Inspect the F<LWP/Protocol/file.pm> and F<LWP/Protocol/http.pm> files | ||||
282 | for examples of usage. | ||||
283 | |||||
284 | =head1 COPYRIGHT | ||||
285 | |||||
286 | Copyright 1995-2001 Gisle Aas. | ||||
287 | |||||
288 | This library is free software; you can redistribute it and/or | ||||
289 | modify it under the same terms as Perl itself. | ||||
# spent 29µs within LWP::Protocol::CORE:match which was called 2 times, avg 14µs/call:
# once (20µs+0s) by LWP::UserAgent::BEGIN at line 7 of LWP/Protocol.pm
# once (9µs+0s) by LWP::Protocol::implementor at line 58 of LWP/Protocol.pm | |||||
# spent 5µs within LWP::Protocol::CORE:subst which was called
# once (5µs+0s) by LWP::Protocol::implementor at line 60 of LWP/Protocol.pm |