| 1 | # Copyright 2008, 2009, 2010, 2011, 2016, 2018 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::UserAgent; | ||
| 18 | use 5.006; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use App::Chart; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | use HTTP::Message 5.814; # for decodable() | ||
| 24 | use base 'LWP::UserAgent'; | ||
| 25 | |||
| 26 | use Class::Singleton 1.03; # 1.03 for _new_instance() | ||
| 27 | use base 'Class::Singleton'; | ||
| 28 | *_new_instance = \&new; | ||
| 29 | |||
| 30 | # Crib notes: | ||
| 31 | # | ||
| 32 | # URIs: | ||
| 33 | # Current RFC3986 | ||
| 34 | # | ||
| 35 | # Past RFC2396 -- previous URI spec | ||
| 36 | # RFC1808 -- relative URI spec | ||
| 37 | # RFC1738 -- previous again | ||
| 38 | # RFC1736, RFC1737 -- functional specs | ||
| 39 | # | ||
| 40 | # Proxies: | ||
| 41 | # /usr/share/doc/libwww-doc/html/Library/User/Using/Proxy.html | ||
| 42 | # http_proxy, no_proxy | ||
| 43 | # Eg. http_proxy=http://proxy.zipworld.com.au:8080/ | ||
| 44 | # | ||
| 45 | # /usr/share/doc/lynx/lynx_help/keystrokes/environments.html | ||
| 46 | # shows spaces in no_proxy | ||
| 47 | # | ||
| 48 | # wget.info Proxies | ||
| 49 | # | ||
| 50 | # | ||
| 51 | # something ... | ||
| 52 | # @LWP::Protocol::http::EXTRA_SOCK_OPTS = (PeerAddr => "foo.com"); | ||
| 53 | # | ||
| 54 | |||
| 55 | sub new { | ||
| 56 | my ($class, %options) = @_; | ||
| 57 | if (! exists $options{'keep_alive'} | ||
| 58 | && ! exists $options{'conn_cache'}) { | ||
| 59 | $options{'keep_alive'} = 1; # connection cache 1 sock | ||
| 60 | } | ||
| 61 | if (! exists $options{'agent'}) { | ||
| 62 | # not given, use our default | ||
| 63 | $options{'agent'} = $class->_agent; | ||
| 64 | } elsif (defined $options{'agent'} && $options{'agent'} =~ / $/) { | ||
| 65 | # ends in space, append our default | ||
| 66 | $options{'agent'} .= $class->_agent; | ||
| 67 | } | ||
| 68 | my $self = $class->SUPER::new (timeout => 60, # seconds, default | ||
| 69 | %options); | ||
| 70 | |||
| 71 | # with lwp bit appended | ||
| 72 | # $self->agent (); | ||
| 73 | |||
| 74 | # ask for everything decoded_content() accepts | ||
| 75 | $self->default_header ('Accept-Encoding' => scalar HTTP::Message::decodable()); | ||
| 76 | |||
| 77 | # trace on redirect ... | ||
| 78 | |||
| 79 | return $self; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub _agent { | ||
| 83 | my ($class) = @_; | ||
| 84 | return "Chart/$App::Chart::VERSION " . $class->SUPER::_agent; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub redirect_ok { | ||
| 88 | my ($self, $prospective_request, $response) = @_; | ||
| 89 | my $ret = $self->SUPER::redirect_ok ($prospective_request, $response); | ||
| 90 | if ($ret) { | ||
| 91 | print "Redirect to ",$prospective_request->uri,"\n"; | ||
| 92 | } | ||
| 93 | return $ret; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub request { | ||
| 97 | my ($self, @args) = @_; | ||
| 98 | $self->{__PACKAGE__.'.last_time'} = time() - 1; # provoke initial display | ||
| 99 | return $self->SUPER::request (@args); | ||
| 100 | } | ||
| 101 | |||
| 102 | # method call up from LWP::UserAgent | ||
| 103 | sub progress { | ||
| 104 | my ($self, $status, $response) = @_; | ||
| 105 | |||
| 106 | my $str = $status; | ||
| 107 | if ($status eq 'begin') { | ||
| 108 | $str = __('connect'); | ||
| 109 | |||
| 110 | } elsif ($response && $status ne 'end') { | ||
| 111 | my $time = time(); | ||
| 112 | if ($time != $self->{__PACKAGE__.'.last_time'}) { | ||
| 113 | $self->{__PACKAGE__.'.last_time'} = $time; | ||
| 114 | |||
| 115 | my $got = length ($response->content); | ||
| 116 | my $total = $response->content_length; | ||
| 117 | if (defined $total) { | ||
| 118 | $str = __x('{got} of {total} bytes', | ||
| 119 | got => $got, total => $total); | ||
| 120 | } else { | ||
| 121 | $str = __x('{got} bytes', got => $got); | ||
| 122 | } | ||
| 123 | } else { | ||
| 124 | $str = undef; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | if (defined $str) { | ||
| 128 | require App::Chart::Download; | ||
| 129 | App::Chart::Download::substatus ($str); | ||
| 130 | } | ||
| 131 | |||
| 132 | return $self->SUPER::progress ($status, $response); | ||
| 133 | } | ||
| 134 | |||
| 135 | # use Data::Dumper; | ||
| 136 | # *LWP::Protocol::http::SocketMethods::configure = sub { | ||
| 137 | # my $self = shift; | ||
| 138 | # print Dumper (\@_); | ||
| 139 | # $self->SUPER::configure (@_); | ||
| 140 | # }; | ||
| 141 | |||
| 142 | |||
| 143 | 1; | ||
| 144 | __END__ | ||
| 145 | |||
| 146 | =for stopwords LWP useragent gzip eg | ||
| 147 | |||
| 148 | =head1 NAME | ||
| 149 | |||
| 150 | App::Chart::UserAgent -- LWP useragent subclass | ||
| 151 | |||
| 152 | =head1 SYNOPSIS | ||
| 153 | |||
| 154 | use App::Chart::UserAgent; | ||
| 155 | my $ua = App::Chart::UserAgent->instance; | ||
| 156 | |||
| 157 | =head1 CLASS HIERARCHY | ||
| 158 | |||
| 159 | LWP::UserAgent | ||
| 160 | App::Chart::UserAgent | ||
| 161 | |||
| 162 | =head1 DESCRIPTIONS | ||
| 163 | |||
| 164 | This is a small subclass of C<LWP::UserAgent> which sets up, by default, | ||
| 165 | |||
| 166 | =over 4 | ||
| 167 | |||
| 168 | =item * | ||
| 169 | |||
| 170 | Connection caching, currently just 1 kept open at any time. | ||
| 171 | |||
| 172 | =item * | ||
| 173 | |||
| 174 | C<User-Agent> identification header. | ||
| 175 | |||
| 176 | =item * | ||
| 177 | |||
| 178 | C<Accept-Encoding> header with C<HTTP::Message::decodable()> to let the | ||
| 179 | server send gzip etc. This means all responses should be accessed with | ||
| 180 | C<< $resp->decoded_content() >>, not raw C<content()>. | ||
| 181 | |||
| 182 | =item * | ||
| 183 | |||
| 184 | Progress and redirection messages (back through C<App::Chart::Download>). | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | =head1 FUNCTIONS | ||
| 189 | |||
| 190 | =over 4 | ||
| 191 | |||
| 192 | =item C<< App::Chart::UserAgent->instance >> | ||
| 193 | |||
| 194 | Return a shared C<App::Chart::UserAgent> object. This shared instance is | ||
| 195 | meant for all normal use. | ||
| 196 | |||
| 197 | =item C<< App::Chart::UserAgent->new (key => value, ...) >> | ||
| 198 | |||
| 199 | Create and return a new C<App::Chart::UserAgent> object. | ||
| 200 | |||
| 201 | =item C<< App::Chart::UserAgent->_agent >> | ||
| 202 | |||
| 203 | =item C<< $ua->_agent >> | ||
| 204 | |||
| 205 | Return the default C<User-Agent> header string. This is Chart plus the LWP | ||
| 206 | default, eg. | ||
| 207 | |||
| 208 | Chart/100 libwww-perl/5.814 | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =head1 SEE ALSO | ||
| 213 | |||
| 214 | L<LWP::UserAgent> | ||
| 215 | |||
| 216 | =head1 HOME PAGE | ||
| 217 | |||
| 218 | L<http://user42.tuxfamily.org/chart/index.html> | ||
| 219 | |||
| 220 | =head1 LICENCE | ||
| 221 | |||
| 222 | Copyright 2008, 2009, 2010, 2011, 2016, 2018 Kevin Ryde | ||
| 223 | |||
| 224 | Chart is free software; you can redistribute it and/or modify it under the | ||
| 225 | terms of the GNU General Public License as published by the Free Software | ||
| 226 | Foundation; either version 3, or (at your option) any later version. | ||
| 227 | |||
| 228 | Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 229 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 230 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 231 | details. | ||
| 232 | |||
| 233 | You should have received a copy of the GNU General Public License along with | ||
| 234 | Chart; see the file F<@chartdatadir@/COPYING>. Failing that, see | ||
| 235 | L<http://www.gnu.org/licenses/>. | ||
| 236 | |||
| 237 | =cut |