source: main/trunk/greenstone2/perllib/cpan/Mojolicious/Plugin/HeaderCondition.pm@ 32205

Last change on this file since 32205 was 32205, checked in by ak19, 6 years ago

First set of commits to do with implementing the new 'paged_html' output option of PDFPlugin that uses using xpdftools' new pdftohtml. So far tested only on Linux (64 bit), but things work there so I'm optimistically committing the changes since they work. 2. Committing the pre-built Linux binaries of XPDFtools for both 32 and 64 bit built by the XPDF group. 2. To use the correct bitness variant of xpdftools, setup.bash now exports the BITNESS env var, consulted by gsConvert.pl. 3. All the perl code changes to do with using xpdf tools' pdftohtml to generate paged_html and feed it in the desired form into GS(3): gsConvert.pl, PDFPlugin.pm and its parent ConvertBinaryPFile.pm have been modified to make it all work. xpdftools' pdftohtml generates a folder containing an html file and a screenshot for each page in a PDF (as well as an index.html linking to each page's html). However, we want a single html file that contains each individual 'page' html's content in a div, and need to do some further HTML style, attribute and structure modifications to massage the xpdftool output to what we want for GS. In order to parse and manipulate the HTML 'DOM' to do this, we're using the Mojo::DOM package that Dr Bainbridge found and which he's compiled up. Mojo::DOM is therefore also committed in this revision. Some further changes and some display fixes are required, but need to check with the others about that.

File size: 2.3 KB
Line 
1package Mojolicious::Plugin::HeaderCondition;
2use Mojo::Base 'Mojolicious::Plugin';
3
4sub register {
5 my ($self, $app) = @_;
6
7 $app->routes->add_condition(headers => \&_headers);
8 $app->routes->add_condition(
9 agent => sub { _headers(@_[0 .. 2], {'User-Agent' => $_[3]}) });
10 $app->routes->add_condition(
11 host => sub { _check($_[1]->req->url->to_abs->host, $_[3]) });
12}
13
14sub _check {
15 my ($value, $pattern) = @_;
16 return 1
17 if $value && $pattern && ref $pattern eq 'Regexp' && $value =~ $pattern;
18 return $value && defined $pattern && $pattern eq $value;
19}
20
21sub _headers {
22 my ($route, $c, $captures, $patterns) = @_;
23 return undef unless $patterns && ref $patterns eq 'HASH' && keys %$patterns;
24
25 # All headers need to match
26 my $headers = $c->req->headers;
27 _check($headers->header($_), $patterns->{$_}) || return undef
28 for keys %$patterns;
29 return 1;
30}
31
321;
33
34=encoding utf8
35
36=head1 NAME
37
38Mojolicious::Plugin::HeaderCondition - Header condition plugin
39
40=head1 SYNOPSIS
41
42 # Mojolicious
43 $app->plugin('HeaderCondition');
44 $app->routes->get('/:controller/:action')
45 ->over(headers => {Referer => qr/example\.com/});
46
47 # Mojolicious::Lite
48 plugin 'HeaderCondition';
49 get '/' => (headers => {Referer => qr/example\.com/}) => sub {...};
50
51 # All headers need to match
52 $app->routes->get('/:controller/:action')->over(headers => {
53 'X-Secret-Header' => 'Foo',
54 Referer => qr/example\.com/
55 });
56
57 # The "agent" condition is a shortcut for the "User-Agent" header
58 get '/' => (agent => qr/Firefox/) => sub {...};
59
60 # The "host" condition is a shortcut for the detected host
61 get '/' => (host => qr/mojolicious\.org/) => sub {...};
62
63=head1 DESCRIPTION
64
65L<Mojolicious::Plugin::HeaderCondition> is a route condition for header-based
66routes.
67
68This is a core plugin, that means it is always enabled and its code a good
69example for learning to build new plugins, you're welcome to fork it.
70
71See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
72by default.
73
74=head1 METHODS
75
76L<Mojolicious::Plugin::HeaderCondition> inherits all methods from
77L<Mojolicious::Plugin> and implements the following new ones.
78
79=head2 register
80
81 $plugin->register(Mojolicious->new);
82
83Register conditions in L<Mojolicious> application.
84
85=head1 SEE ALSO
86
87L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
88
89=cut
Note: See TracBrowser for help on using the repository browser.