source: main/trunk/greenstone2/perllib/cpan/Mojo/Server.pm

Last change on this file 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: 4.4 KB
Line 
1package Mojo::Server;
2use Mojo::Base 'Mojo::EventEmitter';
3
4use Carp 'croak';
5use Mojo::File 'path';
6use Mojo::Loader 'load_class';
7use Mojo::Util 'md5_sum';
8use POSIX ();
9use Scalar::Util 'blessed';
10
11has app => sub { shift->build_app('Mojo::HelloWorld') };
12has reverse_proxy => sub { $ENV{MOJO_REVERSE_PROXY} };
13
14sub build_app {
15 my ($self, $app) = (shift, shift);
16 local $ENV{MOJO_EXE};
17 return $self->app($app->new(@_))->app unless my $e = load_class $app;
18 die ref $e ? $e : qq{Can't find application class "$app" in \@INC. (@INC)\n};
19}
20
21sub build_tx {
22 my $self = shift;
23 my $tx = $self->app->build_tx;
24 $tx->req->reverse_proxy(1) if $self->reverse_proxy;
25 return $tx;
26}
27
28sub daemonize {
29
30 # Fork and kill parent
31 die "Can't fork: $!" unless defined(my $pid = fork);
32 exit 0 if $pid;
33 POSIX::setsid or die "Can't start a new session: $!";
34
35 # Close filehandles
36 open STDIN, '</dev/null';
37 open STDOUT, '>/dev/null';
38 open STDERR, '>&STDOUT';
39}
40
41sub load_app {
42 my ($self, $path) = @_;
43
44 # Clean environment (reset FindBin defensively)
45 {
46 local $0 = $path = path($path)->to_abs->to_string;
47 require FindBin;
48 FindBin->again;
49 local $ENV{MOJO_APP_LOADER} = 1;
50 local $ENV{MOJO_EXE};
51
52 # Try to load application from script into sandbox
53 delete $INC{$path};
54 my $app = eval
55 "package Mojo::Server::Sandbox::@{[md5_sum $path]}; require \$path";
56 die qq{Can't load application from file "$path": $@} if $@;
57 die qq{File "$path" did not return an application object.\n}
58 unless blessed $app && $app->can('handler');
59 $self->app($app);
60 };
61 FindBin->again;
62
63 return $self->app;
64}
65
66sub new {
67 my $self = shift->SUPER::new(@_);
68 $self->on(request => sub { shift->app->handler(shift) });
69 return $self;
70}
71
72sub run { croak 'Method "run" not implemented by subclass' }
73
741;
75
76=encoding utf8
77
78=head1 NAME
79
80Mojo::Server - HTTP/WebSocket server base class
81
82=head1 SYNOPSIS
83
84 package Mojo::Server::MyServer;
85 use Mojo::Base 'Mojo::Server';
86
87 sub run {
88 my $self = shift;
89
90 # Get a transaction
91 my $tx = $self->build_tx;
92
93 # Emit "request" event
94 $self->emit(request => $tx);
95 }
96
97=head1 DESCRIPTION
98
99L<Mojo::Server> is an abstract base class for HTTP/WebSocket servers and server
100interfaces, like L<Mojo::Server::CGI>, L<Mojo::Server::Daemon>,
101L<Mojo::Server::Hypnotoad>, L<Mojo::Server::Morbo>, L<Mojo::Server::Prefork>
102and L<Mojo::Server::PSGI>.
103
104=head1 EVENTS
105
106L<Mojo::Server> inherits all events from L<Mojo::EventEmitter> and can emit the
107following new ones.
108
109=head2 request
110
111 $server->on(request => sub {
112 my ($server, $tx) = @_;
113 ...
114 });
115
116Emitted when a request is ready and needs to be handled.
117
118 $server->on(request => sub {
119 my ($server, $tx) = @_;
120 $tx->res->code(200);
121 $tx->res->headers->content_type('text/plain');
122 $tx->res->body('Hello World!');
123 $tx->resume;
124 });
125
126=head1 ATTRIBUTES
127
128L<Mojo::Server> implements the following attributes.
129
130=head2 app
131
132 my $app = $server->app;
133 $server = $server->app(MojoSubclass->new);
134
135Application this server handles, defaults to a L<Mojo::HelloWorld> object.
136
137=head2 reverse_proxy
138
139 my $bool = $server->reverse_proxy;
140 $server = $server->reverse_proxy($bool);
141
142This server operates behind a reverse proxy, defaults to the value of the
143C<MOJO_REVERSE_PROXY> environment variable.
144
145=head1 METHODS
146
147L<Mojo::Server> inherits all methods from L<Mojo::EventEmitter> and implements
148the following new ones.
149
150=head2 build_app
151
152 my $app = $server->build_app('MyApp');
153 my $app = $server->build_app('MyApp', log => Mojo::Log->new);
154 my $app = $server->build_app('MyApp', {log => Mojo::Log->new});
155
156Build application from class and assign it to L</"app">.
157
158=head2 build_tx
159
160 my $tx = $server->build_tx;
161
162Let application build a transaction.
163
164=head2 daemonize
165
166 $server->daemonize;
167
168Daemonize server process.
169
170=head2 load_app
171
172 my $app = $server->load_app('/home/sri/myapp.pl');
173
174Load application from script and assign it to L</"app">.
175
176 say Mojo::Server->new->load_app('./myapp.pl')->home;
177
178=head2 new
179
180 my $server = Mojo::Server->new;
181 my $server = Mojo::Server->new(reverse_proxy => 1);
182 my $server = Mojo::Server->new({reverse_proxy => 1});
183
184Construct a new L<Mojo::Server> object and subscribe to L</"request"> event
185with default request handling.
186
187=head2 run
188
189 $server->run;
190
191Run server. Meant to be overloaded in a subclass.
192
193=head1 SEE ALSO
194
195L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
196
197=cut
Note: See TracBrowser for help on using the repository browser.