1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 package videosite::Base;
13 my $self = {'_DEBUG' => 0,
14 '_OUT' => sub {printf(@_)},
15 '_CONNECTORS' => sub { return ({ 'name' => 'direct',
17 '_CONNECTOR' => undef,
23 $self->_prepare_parameters();
32 $data[0] = "(" . ref($self) . ") " . $data[0];
34 $self->{'_OUT'}(@data);
41 $data[0] = "DEBUG: " . $data[0];
42 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
50 return $self->{'_CONFIG'} unless defined($c);
52 foreach $o (keys(%{$c->{'option'}})) {
53 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
54 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
58 return $self->{'_CONFIG'};
61 sub _prepare_parameters {
65 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
67 foreach $p (keys(%{$self->{'_PARAMS'}})) {
68 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
77 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
78 $self->debug('Returning %s=%s', $key, $val);
88 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
89 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
91 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
99 $self->{'_OUT'} = $io;
104 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
108 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
109 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
111 $s .= sprintf(" %s: %s", $k, $p);
112 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
113 $s .= " (default)\n";
122 # Return a list of the parameters supported by the module
123 # Does not return the 'enabled' parameter
128 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
131 # Return a list of valid parameter values, if the parameter has
138 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
141 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
148 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
152 if (exists($self->{'DESC'})) {
153 $s .= "Description:\n " . $self->{'DESC'};
157 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
158 $p = $self->{'_PARAMS'}->{$k}->[0];
160 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
161 # The parameter has a list of allowed values. Add the keys and their help
162 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
163 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
164 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
167 # The parameter just has a default value and a help text
168 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
178 $self->{'_DEBUG'} = shift;
185 $ua = LWP::UserAgent->new(
186 'agent' => 'Mozilla/5.0',
187 'cookie_jar' => HTTP::Cookies->new,
191 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
192 delete($ENV{'HTTPS_PROXY'});
194 if (defined($self->{'_CONNECTOR'})) {
195 my $schemas = $self->{'_CONNECTOR'}->{'schemas'};
196 foreach (keys(%{$schemas})) {
197 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
199 # OK, so here's the gist.
201 # The usual way of reqesting an HTTPS URL through a proxy is
202 # to connect to the proxy server, issue a CONNECT request to
203 # create a channel to the web server and start an SSL session over
204 # this channel, so there is an end-to-end connection between
205 # the client and the server.
207 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
210 # LWP will connect to the proxy server, and issue a standard GET
211 # request for the target URL, which most proxy servers will refuse
214 # The way to use a proxy server is to set some environment variables
215 # and let the underlying Crypt::SSLeay module do the rest.
217 # This is positively appaling.
218 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
220 $ua->proxy($_, $schemas->{$_});
225 $self->{_CACHED_UA} = $ua;
233 return $self->{_CACHED_UA};
239 my $ua = shift || $self->ua();
243 return $r->decoded_content() if $r->is_success();
251 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
256 sub decode_querystring {
259 return { map { split /=/, $_, 2; } split /&/, shift };
265 return $self->{'_CONNECTORS'}->();
271 $self->{'_CONNECTOR'} = shift;
277 $self->{'_CONNECTORS'} = shift;