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,
22 $self->_prepare_parameters();
31 $data[0] = "(" . ref($self) . ") " . $data[0];
33 $self->{'_OUT'}(@data);
40 $data[0] = "DEBUG: " . $data[0];
41 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
49 return $self->{'_CONFIG'} unless defined($c);
51 foreach $o (keys(%{$c->{'option'}})) {
52 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
53 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
57 return $self->{'_CONFIG'};
60 sub _prepare_parameters {
64 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
66 foreach $p (keys(%{$self->{'_PARAMS'}})) {
67 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
76 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
77 $self->debug('Returning %s=%s', $key, $val);
87 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
88 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
90 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
98 $self->{'_OUT'} = $io;
103 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
107 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
108 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
110 $s .= sprintf(" %s: %s", $k, $p);
111 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
112 $s .= " (default)\n";
121 # Return a list of the parameters supported by the module
122 # Does not return the 'enabled' parameter
127 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
130 # Return a list of valid parameter values, if the parameter has
137 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
140 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
147 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
151 if (exists($self->{'DESC'})) {
152 $s .= "Description:\n " . $self->{'DESC'};
156 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
157 $p = $self->{'_PARAMS'}->{$k}->[0];
159 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
160 # The parameter has a list of allowed values. Add the keys and their help
161 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
162 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
163 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
166 # The parameter just has a default value and a help text
167 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
177 $self->{'_DEBUG'} = shift;
184 $ua = LWP::UserAgent->new('agent' => 'Mozilla/5.0', 'cookie_jar' => HTTP::Cookies->new);
186 # Remove a currently defined HTTPS proxy. See below for a longer explanation.
187 delete($ENV{'HTTPS_PROXY'});
189 if (defined($self->{'_CONNECTOR'})) {
190 my $schemas = $self->{'_CONNECTOR'}->{-schemas};
191 foreach (keys(%{$schemas})) {
192 $self->debug("Adding schema %s with proxy %s", $_, $schemas->{$_});
194 # OK, so here's the gist.
196 # The usual way of reqesting an HTTPS URL through a proxy is
197 # to connect to the proxy server, issue a CONNECT request to
198 # create a channel to the web server and start an SSL session over
199 # this channel, so there is an end-to-end connection between
200 # the client and the server.
202 # Setting a proxy for the https schema in LWP WILL NOT ACCOMPLISH
205 # LWP will connect to the proxy server, and issue a standard GET
206 # request for the target URL, which most proxy servers will refuse
209 # The way to use a proxy server is to set some environment variables
210 # and let the underlying Crypt::SSLeay module do the rest.
212 # This is positively appaling.
213 $ENV{'HTTPS_PROXY'} = $schemas->{$_};
215 $ua->proxy($_, $schemas->{$_});
229 $d =~ s/%([[:xdigit:]]{2})/chr(hex($1))/ge;
234 sub decode_querystring {
237 return { map { split /=/, $_, 2; } split /&/, shift };
243 return $self->{'_CONNECTORS'}->();
249 $self->{'_CONNECTOR'} = shift;
255 $self->{'_CONNECTORS'} = shift;