source: main/trunk/greenstone2/perllib/cpan/Mojo/Server/PSGI.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.7 KB
Line 
1package Mojo::Server::PSGI;
2use Mojo::Base 'Mojo::Server';
3
4sub run {
5 my ($self, $env) = @_;
6
7 my $tx = $self->build_tx;
8 my $req = $tx->req->parse($env);
9 $tx->local_port($env->{SERVER_PORT})->remote_address($env->{REMOTE_ADDR});
10
11 # Request body (may block if we try to read too much)
12 my $len = $env->{CONTENT_LENGTH};
13 until ($req->is_finished) {
14 my $chunk = ($len && $len < 131072) ? $len : 131072;
15 last unless my $read = $env->{'psgi.input'}->read(my $buffer, $chunk, 0);
16 $req->parse($buffer);
17 last if ($len -= $read) <= 0;
18 }
19
20 $self->emit(request => $tx);
21
22 # Response headers
23 my $res = $tx->res->fix_headers;
24 my $hash = $res->headers->to_hash(1);
25 my @headers;
26 for my $name (keys %$hash) { push @headers, $name, $_ for @{$hash->{$name}} }
27
28 # PSGI response
29 my $io = Mojo::Server::PSGI::_IO->new(tx => $tx, empty => $tx->is_empty);
30 return [$res->code // 404, \@headers, $io];
31}
32
33sub to_psgi_app {
34 my $self = shift;
35
36 # Preload application and wrap it
37 $self->app->server($self);
38 return sub { $self->run(@_) }
39}
40
41package Mojo::Server::PSGI::_IO;
42use Mojo::Base -base;
43
44# Finish transaction
45sub close { shift->{tx}->closed }
46
47sub getline {
48 my $self = shift;
49
50 # Empty
51 return undef if $self->{empty};
52
53 # No content yet, try again later
54 my $chunk = $self->{tx}->res->get_body_chunk($self->{offset} //= 0);
55 return '' unless defined $chunk;
56
57 # End of content
58 return undef unless length $chunk;
59
60 $self->{offset} += length $chunk;
61 return $chunk;
62}
63
641;
65
66=encoding utf8
67
68=head1 NAME
69
70Mojo::Server::PSGI - PSGI server
71
72=head1 SYNOPSIS
73
74 use Mojo::Server::PSGI;
75
76 my $psgi = Mojo::Server::PSGI->new;
77 $psgi->unsubscribe('request')->on(request => sub {
78 my ($psgi, $tx) = @_;
79
80 # Request
81 my $method = $tx->req->method;
82 my $path = $tx->req->url->path;
83
84 # Response
85 $tx->res->code(200);
86 $tx->res->headers->content_type('text/plain');
87 $tx->res->body("$method request for $path!");
88
89 # Resume transaction
90 $tx->resume;
91 });
92 my $app = $psgi->to_psgi_app;
93
94=head1 DESCRIPTION
95
96L<Mojo::Server::PSGI> allows L<Mojolicious> applications to run on all L<PSGI>
97compatible servers.
98
99See L<Mojolicious::Guides::Cookbook/"DEPLOYMENT"> for more.
100
101=head1 EVENTS
102
103L<Mojo::Server::PSGI> inherits all events from L<Mojo::Server>.
104
105=head1 ATTRIBUTES
106
107L<Mojo::Server::PSGI> inherits all attributes from L<Mojo::Server>.
108
109=head1 METHODS
110
111L<Mojo::Server::PSGI> inherits all methods from L<Mojo::Server> and implements
112the following new ones.
113
114=head2 run
115
116 my $res = $psgi->run($env);
117
118Run L<PSGI>.
119
120=head2 to_psgi_app
121
122 my $app = $psgi->to_psgi_app;
123
124Turn L<Mojolicious> application into L<PSGI> application.
125
126=head1 SEE ALSO
127
128L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
129
130=cut
Note: See TracBrowser for help on using the repository browser.