source: main/trunk/greenstone2/perllib/cpan/Mojo/Cookie/Response.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: 3.4 KB
Line 
1package Mojo::Cookie::Response;
2use Mojo::Base 'Mojo::Cookie';
3
4use Mojo::Date;
5use Mojo::Util qw(quote split_cookie_header);
6
7has [qw(domain expires host_only httponly max_age path secure)];
8
9my %ATTRS = map { $_ => 1 } qw(domain expires httponly max-age path secure);
10
11sub parse {
12 my ($self, $str) = @_;
13
14 my @cookies;
15 my $tree = split_cookie_header $str // '';
16 while (my $pairs = shift @$tree) {
17 my ($name, $value) = splice @$pairs, 0, 2;
18 push @cookies, $self->new(name => $name, value => $value // '');
19
20 while (my ($name, $value) = splice @$pairs, 0, 2) {
21 next unless $ATTRS{my $attr = lc $name};
22 $value =~ s/^\.// if $attr eq 'domain' && defined $value;
23 $value = Mojo::Date->new($value // '')->epoch if $attr eq 'expires';
24 $value = 1 if $attr eq 'secure' || $attr eq 'httponly';
25 $cookies[-1]{$attr eq 'max-age' ? 'max_age' : $attr} = $value;
26 }
27 }
28
29 return \@cookies;
30}
31
32sub to_string {
33 my $self = shift;
34
35 # Name and value
36 return '' unless length(my $name = $self->name // '');
37 my $value = $self->value // '';
38 my $cookie = join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value;
39
40 # "expires"
41 my $expires = $self->expires;
42 $cookie .= '; expires=' . Mojo::Date->new($expires) if defined $expires;
43
44 # "domain"
45 if (my $domain = $self->domain) { $cookie .= "; domain=$domain" }
46
47 # "path"
48 if (my $path = $self->path) { $cookie .= "; path=$path" }
49
50 # "secure"
51 $cookie .= "; secure" if $self->secure;
52
53 # "HttpOnly"
54 $cookie .= "; HttpOnly" if $self->httponly;
55
56 # "Max-Age"
57 if (defined(my $max = $self->max_age)) { $cookie .= "; Max-Age=$max" }
58
59 return $cookie;
60}
61
621;
63
64=encoding utf8
65
66=head1 NAME
67
68Mojo::Cookie::Response - HTTP response cookie
69
70=head1 SYNOPSIS
71
72 use Mojo::Cookie::Response;
73
74 my $cookie = Mojo::Cookie::Response->new;
75 $cookie->name('foo');
76 $cookie->value('bar');
77 say "$cookie";
78
79=head1 DESCRIPTION
80
81L<Mojo::Cookie::Response> is a container for HTTP response cookies, based on
82L<RFC 6265|http://tools.ietf.org/html/rfc6265>.
83
84=head1 ATTRIBUTES
85
86L<Mojo::Cookie::Response> inherits all attributes from L<Mojo::Cookie> and
87implements the following new ones.
88
89=head2 domain
90
91 my $domain = $cookie->domain;
92 $cookie = $cookie->domain('localhost');
93
94Cookie domain.
95
96=head2 expires
97
98 my $expires = $cookie->expires;
99 $cookie = $cookie->expires(time + 60);
100
101Expiration for cookie.
102
103=head2 host_only
104
105 my $bool = $cookie->host_only;
106 $cookie = $cookie->host_only($bool);
107
108Host-only flag, indicating that the canonicalized request-host is identical to
109the cookie's L</"domain">.
110
111=head2 httponly
112
113 my $bool = $cookie->httponly;
114 $cookie = $cookie->httponly($bool);
115
116HttpOnly flag, which can prevent client-side scripts from accessing this
117cookie.
118
119=head2 max_age
120
121 my $max_age = $cookie->max_age;
122 $cookie = $cookie->max_age(60);
123
124Max age for cookie.
125
126=head2 path
127
128 my $path = $cookie->path;
129 $cookie = $cookie->path('/test');
130
131Cookie path.
132
133=head2 secure
134
135 my $bool = $cookie->secure;
136 $cookie = $cookie->secure($bool);
137
138Secure flag, which instructs browsers to only send this cookie over HTTPS
139connections.
140
141=head1 METHODS
142
143L<Mojo::Cookie::Response> inherits all methods from L<Mojo::Cookie> and
144implements the following new ones.
145
146=head2 parse
147
148 my $cookies = Mojo::Cookie::Response->parse('f=b; path=/');
149
150Parse cookies.
151
152=head2 to_string
153
154 my $str = $cookie->to_string;
155
156Render cookie.
157
158=head1 SEE ALSO
159
160L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
161
162=cut
Note: See TracBrowser for help on using the repository browser.