File Coverage

blib/lib/WWW/HarWalk.pm
Criterion Covered Total %
statement 21 48 43.7
branch 0 10 0.0
condition 0 6 0.0
subroutine 7 8 87.5
pod 1 1 100.0
total 29 73 39.7


line stmt bran cond sub pod time code
1             package WWW::HarWalk;
2              
3 1     1   23180 use 5.006;
  1         4  
  1         55  
4 1     1   7 use strict;
  1         2  
  1         45  
5 1     1   6 use warnings FATAL => 'all';
  1         13  
  1         51  
6 1     1   7 use Carp qw(croak);
  1         2  
  1         67  
7 1     1   1238 use JSON;
  1         18108  
  1         6  
8 1     1   1168 use HTTP::Request;
  1         37154  
  1         47  
9 1     1   14 use base qw(Exporter);
  1         2  
  1         631  
10              
11             =head1 NAME
12              
13             WWW::HarWalk - Replay HTTP requests from HAR ( HTTP Archive ) file
14              
15             =head1 VERSION
16              
17             Version 0.13
18              
19             =cut
20              
21             our $VERSION = '0.13';
22              
23             our @EXPORT_OK = qw(walk_har);
24              
25             =head1 SYNOPSIS
26              
27             use LWP::UserAgent;
28             use WWW::HarWalk qw(walk_har);
29            
30             my $ua = LWP::UserAgent->new;
31              
32             # simple usage
33             walk_har($ua, 'c.lietou.com.har');
34            
35             # with hooks
36             walk_har($ua, # a LWP::UserAgent instance
37             'c.lietou.com.har', # har file path
38             sub {
39             my $entry = shift; # entries item in har
40            
41             # request of the entry item. Note: this is not the HTTP::Request instance
42             my $request = $entry->{request};
43            
44             # return false to skip this entry
45             return 0 if $request->{url} =~ /\.(?:gif|png|css|js)(?:\?.*)?$/;
46            
47             # modify post params
48             if ($request->{url} =~ /login.php/) {
49             $request->{postData}->{text} =~ s/username=\w+/username=Tom/;
50             }
51            
52             # must return true to request this entry
53             return 1;
54             },
55             sub {
56             # $res is a HTTP::Response intance, decoded
57             my ($entry, $res, $entries) = @_;
58            
59             # you can print or capture something from some response
60             if ($entry->{request}->{url} =~ /refreshresume/) {
61             print $res->content, "\n";
62             }
63             });
64              
65             =head1 EXPORT
66              
67             walk_har
68              
69             =head1 SUBROUTINES/METHODS
70              
71             =head2 walk_har($ua, $har_file, $before_sub, $after_sub)
72              
73             Walk through all the entries in the HAR file, and issue each request.
74              
75             The first two arguments is required. The $ua is a LWP::UserAgent instance, you can do some configuration first, eg: set timeout. $har_file is the HAR file you recorded.
76              
77             The last two arguments are for hooks. In the before hook you can decide wheather this request shall be sent, you can return false to skip some unnessary request, such as images, css, etc. You can modify the $entry here. Eg: you can change the username and password in the postData to replay twitter requests with another user. The prototype of the before hook is:
78              
79             sub {
80             my ($entry) = @_;
81             return 1;
82             }
83              
84             The after hook is for people to get some information from the response. Eg: get some link to download and push them into @$entries. It is prototype is :
85              
86             sub {
87             my ($entry, $res, $entries) = @_;
88             }
89              
90             The $entry is the item in the entries array in the HAR file ( log -> entries ). The $res is a decoded HTTP::Response instance.
91              
92             =cut
93              
94             sub walk_har {
95 0     0 1   my ($ua, $harfile, $before_sub, $after_sub) = @_;
96 0 0         open my $fh, $harfile or croak "can not open harfile for read " . $!;
97 0           my $content;
98             {
99 0           local $/;
  0            
100 0           $content = <$fh>;
101             }
102 0           close $fh;
103 0           my $json = JSON->new->utf8;
104 0           my $o = $json->decode($content);
105 0           while (my $entry = shift @{$o->{log}->{entries}}) {
  0            
106 0           my $request = $entry->{request};
107 0 0 0       if ($before_sub && ref $before_sub eq 'CODE') {
108 0           my $rv = $before_sub->($entry);
109 0 0         next unless $rv;
110             }
111 0           my $method = $request->{method};
112 0           my $url = $request->{url};
113 0           my $headers = $request->{headers};
114 0           my $req = HTTP::Request->new($method, $url);
115 0           for my $h (@$headers) {
116 0           $req->header($h->{name}, $h->{value});
117             }
118 0 0         if (defined $request->{postData}) {
119 0           $req->content($request->{postData}->{text});
120             }
121 0           my $res = $ua->request($req);
122 0           $res->decode;
123 0 0 0       if ($after_sub && ref $after_sub eq 'CODE') {
124 0           $after_sub->($entry, $res, $o->{log}->{entries});
125             }
126             }
127             }
128              
129              
130             =head1 AUTHOR
131              
132             Achilles Xu, C<< >>
133              
134             =head1 BUGS
135              
136             Please report any bugs or feature requests to C, or through
137             the web interface at L. I will be notified, and then you'll
138             automatically be notified of progress on your bug as I make changes.
139              
140             =head1 SEE ALSO
141              
142             HAR Specifiction: L
143              
144             =head1 SUPPORT
145              
146             You can find documentation for this module with the perldoc command.
147              
148             perldoc WWW::HarWalk
149              
150              
151             You can also look for information at:
152              
153             =over 4
154              
155             =item * RT: CPAN's request tracker (report bugs here)
156              
157             L
158              
159             =item * AnnoCPAN: Annotated CPAN documentation
160              
161             L
162              
163             =item * CPAN Ratings
164              
165             L
166              
167             =item * Search CPAN
168              
169             L
170              
171             =back
172              
173              
174             =head1 ACKNOWLEDGEMENTS
175              
176              
177             =head1 LICENSE AND COPYRIGHT
178              
179             Copyright 2013 Achilles Xu.
180              
181             This program is free software; you can redistribute it and/or modify it
182             under the terms of the the Artistic License (2.0). You may obtain a
183             copy of the full license at:
184              
185             L
186              
187             Any use, modification, and distribution of the Standard or Modified
188             Versions is governed by this Artistic License. By using, modifying or
189             distributing the Package, you accept this license. Do not use, modify,
190             or distribute the Package, if you do not accept this license.
191              
192             If your Modified Version has been derived from a Modified Version made
193             by someone other than you, you are nevertheless required to ensure that
194             your Modified Version complies with the requirements of this license.
195              
196             This license does not grant you the right to use any trademark, service
197             mark, tradename, or logo of the Copyright Holder.
198              
199             This license includes the non-exclusive, worldwide, free-of-charge
200             patent license to make, have made, use, offer to sell, sell, import and
201             otherwise transfer the Package with respect to any patent claims
202             licensable by the Copyright Holder that are necessarily infringed by the
203             Package. If you institute patent litigation (including a cross-claim or
204             counterclaim) against any party alleging that the Package constitutes
205             direct or contributory patent infringement, then this Artistic License
206             to you shall terminate on the date that such litigation is filed.
207              
208             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
209             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
210             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
211             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
212             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
213             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
214             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
215             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
216              
217              
218             =cut
219              
220             1; # End of WWW::HarWalk