API/Router.pm

filter: Statementstmt 100% 107 / 107 Branchbran 85.0% 17 / 20 Conditioncond 90.0% 18 / 20 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 25 / 25 Podpod 100% 1 / 1 totaltotal 96.2% 177 / 184 CC 20 SCAR 12.3
CC
20
Cov
97%
__ANON__
7.0
__ANON__
6.0
__ANON__
5.1
CRAP 3.4
« API/Indexer.pm ↑ Summary API.pm »
1 package PGXN::API::Router;
2  
3 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use PGXN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Plack::App::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::App::Directory;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PGXN::API::Searcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec::Functions qw(catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 our $VERSION = v0.22.1;
17  
18 2 ▶sub app {
Subroutine app: called CC=4 SCAR=13.9
Pod documented
19 2 my ($class, %params) = @_;
20  
21 2 ▶ unless ($params{errors_to} && $params{errors_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"errors_to"} and $params{"errors_from"}) 1 1 2
Condition66%2/3
$params{"errors_to"} and $params{"errors_from"}
!ll&&!rl&&r
101
MC/DC50%1/2
$params{"errors_to"} and $params{"errors_from"}
$params{"errors_to"} $params{"errors_from"}
Truth table66%2/3
A and B
A= $params{"errors_to"}B= $params{"errors_from"}
ABresult
0X0
100
111
22 1 die "Missing required parameters errors_to and errors_from\n";
23 }
24  
25 PGXN::API->instance->doc_root(delete $params{doc_root})
26 1 ▶ if $params{doc_root};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"doc_root"} 0 1 1
27 1 my $root = PGXN::API->instance->doc_root;
28  
29 # Identify distribution files as zip files.
30 1 my ($zip_ext) = PGXN::API->instance->uri_templates->{download} =~ /([.][^.]+)$/;
31 1 $Plack::MIME::MIME_TYPES->{$zip_ext} = $Plack::MIME::MIME_TYPES->{'.zip'};
32  
33 1 ▶ builder {
Subroutine __ANON__: called CC=6 SCAR=17.9
34 1 enable 'ErrorDocument', 500, '/error', subrequest => 1;
35 1 enable 'HTTPExceptions';
36 1 enable 'StackTrace', no_print_errors => 1;
37 1 enable 'JSONP', callback_key => 'callback';
38 1 ▶ enable sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 my $app = shift;
40 40 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 40 my $res = $app->(@_);
42 40 ▶ Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 40 my $res = shift;
44 40 push @{ $res->[1] },
45 'X-PGXN-API-Version' => PGXN::API->version_string;
46 40 });
47 1 };
48 1 };
49  
50 # Serve most stuff as plain files.
51 1 my $files = Plack::App::File->new(root => $root)->to_app;
52 7 ▶ mount '/' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
53 7 my $env = shift;
54 7 ▶ $env->{PATH_INFO} = '/index.html' if $env->{PATH_INFO} eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"PATH_INFO"} eq "/" 1 6 7
55 7 $files->($env);
56 1 };
57  
58 # Handle searches.
59 1 my $searcher = PGXN::API::Searcher->new($root);
60 23 ▶ mount '/search' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
61 23 my $req = Plack::Request->new(shift);
62  
63 # Make sure we have a valid request.
64 23 local $1;
65 return [
66 23 ▶ 404,
Branch: 1 branch
BranchTrueFalseTotal
unless $req->path_info =~ m[^/((?:d(?:oc|ist)|extension|user|tag)s)/?$]u 5 18 23
67 ['Content-Type' => 'text/plain', 'Content-Length' => 9],
68 ['not found']
69 ] if $req->path_info !~ m{^/((?:d(?:oc|ist)|extension|user|tag)s)/?$};
70 18 my $in = $1;
71  
72 18 my $params = $req->query_parameters;
73 18 my $q = $params->{q};
74 return [
75 400,
76 ['Content-Type' => 'text/plain', 'Content-Length' => 38],
77 ['Bad request: Invalid or missing "q" query param.']
78 18 ▶ ] if !defined $q || any { $q eq $_ } '', '*', '?';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if not defined $q or &any(sub { $q eq $_; } , "", "*", "?") 4 14 18
Condition100%3/3
not defined $q or &any(sub { $q eq $_; } , "", "*", "?")
l!l&&r!l&&!r
1314
MC/DC100%2/2
not defined $q or &any(sub { $q eq $_; } , "", "*", "?")
not defined $q &any(sub { $q eq $_; } , "", "*", "?")
Truth table100%3/3
not(A) or B
A= not defined $qB= &any(sub { $q eq $_; } , "", "*", "?")
ABresult
1X1
011
000
79  
80 # Make sure "o" and "l" params are valid.
81 14 for my $param (qw(o l)) {
82 27 my $val = $params->{$param};
83 return [
84 27 ▶ 400,
Branch: 1 branch
BranchTrueFalseTotal
if $val and not $val =~ /^\d+$/u 2 25 27
Condition100%3/3
$val and not $val =~ /^\d+$/u
!ll&&!rl&&r
5202
MC/DC100%2/2
$val and not $val =~ /^\d+$/u
$val not $val =~ /^\d+$/u
Truth table100%3/3
A and not(B)
A= $valB= not $val =~ /^\d+$/u
ABresult
0X0
100
111
85 ['Content-Type' => 'text/plain', 'Content-Length' => 37],
86 [qq{Bad request: invalid "$param" query param.}]
87 ] if $val && $val !~ /^\d+$/;
88 }
89  
90 # Give 'em the results.
91 my $json = encode_json $searcher->search(
92 in => $in,
93 query => decode_utf8($q),
94 offset => scalar $params->{o},
95 limit => scalar $params->{l},
96 12 );
97 return [
98 12 200,
99 ['Content-Type' => 'application/json', 'Content-Length' => length $json ],
100 [$json],
101 ]
102 1 };
103  
104 # For source browsing, some things should be text/plain.
105 1 my $mimes = { %{ $Plack::MIME::MIME_TYPES } };
106 1 for my $ext (keys %{ $mimes }) {
107 $mimes->{$ext} = 'text/plain'
108 if $mimes->{$ext} =~ /html|x-c|calendar|vcard/
109 || ($mimes->{$ext} =~ /xml/ && $mimes->{$ext} !~ /\Aimage\//)
110 640 ▶ || any { $ext eq $_ } qw(.bat .css .eml .js .json .mime .swf);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $mimes->{$ext} =~ /html|x-c|calendar|vcard/u or $mimes->{$ext} =~ /xml/u and not $mimes->{$ext} =~ m[\Aimage/]u or &any(sub { $ext eq $_; } , ".bat", ".css", ".eml", ".js", ".json", ".mime", ".swf") 102 538 640
Condition100%9/9
$mimes->{$ext} =~ /html|x-c|calendar|vcard/u or $mimes->{$ext} =~ /xml/u and not $mimes->{$ext} =~ m[\Aimage/]u or &any(sub { $ext eq $_; } , ".bat", ".css", ".eml", ".js", ".json", ".mime", ".swf")
l!l&&r!l&&!r
957538
$mimes->{$ext} =~ /html|x-c|calendar|vcard/u or $mimes->{$ext} =~ /xml/u and not $mimes->{$ext} =~ m[\Aimage/]u
l!l&&r!l&&!r
3263545
$mimes->{$ext} =~ /xml/u and not $mimes->{$ext} =~ m[\Aimage/]u
!ll&&!rl&&r
543263
MC/DC100%4/4
$mimes->{$ext} =~ /html|x-c|calendar|vcard/u or $mimes->{$ext} =~ /xml/u and not $mimes->{$ext} =~ m[\Aimage/]u or &any(sub { $ext eq $_; } , ".bat", ".css", ".eml", ".js", ".json", ".mime", ".swf")
$mimes->{$ext} =~ /html|x-c|calendar|vcard/u $mimes->{$ext} =~ /xml/u not $mimes->{$ext} =~ m[\Aimage/]u &any(sub { $ext eq $_; } , ".bat", ".css", ".eml", ".js", ".json", ".mime", ".swf")
Truth table83%5/6
A or B and not(C) or D
A= $mimes->{$ext} =~ /html|x-c|calendar|vcard/uB= $mimes->{$ext} =~ /xml/uC= not $mimes->{$ext} =~ m[\Aimage/]uD= &any(sub { $ext eq $_; } , ".bat", ".css", ".eml", ".js", ".json", ".mime", ".swf")
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
111 }
112 1 my $src_dir = Plack::App::Directory->new(
113 root => catdir $root, 'src'
114 )->to_app;
115  
116 5 ▶ mount '/src' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 5 local $Plack::MIME::MIME_TYPES = $mimes;
118 5 $src_dir->(shift)
119 1 };
120  
121 4 ▶ mount '/_index' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 # Never allow access here.
123 return [
124 4 404,
125 ['Content-Type' => 'text/plain', 'Content-Length' => 9],
126 ['not found']
127 ];
128 1 };
129  
130 1 ▶ mount '/error' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
131 1 my $env = shift;
132  
133 # Pull together the original request environment.
134 my $err_env = { map {
135 26 my $k = $_;
136 s/^psgix[.]errordocument[.]//
137 26 ▶ ? /plack[.]stacktrace[.]/ ? () : ($_ => $env->{$k} )
Branch: 2 branches
BranchTrueFalseTotal
s/^psgix[.]errordocument[.]//u ? : 3 23 26
/plack[.]stacktrace[.]/u ? : 0 3 3
138 : ();
139 1 } keys %{ $env } };
140 1 my $uri = Plack::Request->new($err_env)->uri;
141  
142 1 ▶ if (%{ $err_env }) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$err_env;}) 1 0 1
143 # Send an email to the administrator.
144 # XXX Need configuration.
145 1 require Email::MIME;
146 1 require Email::Sender::Simple;
147 1 require Data::Dump;
148 my $email = Email::MIME->create(
149 header => [
150 From => $params{errors_from},
151 To => $params{errors_to},
152 Subject => 'PGXN API Internal Server Error',
153 ],
154 attributes => {
155 content_type => 'text/plain',
156 charset => 'UTF-8',
157 },
158 body => "An error occurred during a request to $uri.\n\n"
159 . "Environment:\n\n" . Data::Dump::pp($err_env)
160 . "\n\nTrace:\n\n"
161 1 ▶ . ($env->{'plack.stacktrace.text'} || 'None found. :-(')
Condition50%1/2
$env->{'plack.stacktrace.text'} || 'None found. :-('
l!l
10
MC/DC0%0/1
$env->{'plack.stacktrace.text'} || 'None found. :-('
$env->{'plack.stacktrace.text'}
Truth table50%1/2
A
A= $env->{'plack.stacktrace.text'}
Aresult
11
00
162 . "\n",
163 );
164 1 Email::Sender::Simple->send($email);
165 }
166  
167 return [
168 1 200, # Only handled by ErrorDocument, which keeps 500.
169 ['Content-Type' => 'text/plain', 'Content-Length' => 21],
170 ['internal server error']
171 ];
172 1 };
173  
174 1 };
175 }
176  
177 1 1;
178  
179 =head1 Name
180  
181 PGXN::API::Router - The PGXN::API request router.
182  
183 =head1 Synopsis
184  
185 # In app.pgsi
186 use PGXN::API::Router;
187 PGXN::API::Router->app;
188  
189 =head1 Description
190  
191 This class defines the HTTP request routing table used by PGXN::API. Unless
192 you're modifying the PGXN::API routes, you won't have to worry about it. Just
193 know that this is the class that Plack uses to fire up the app.
194  
195 =head1 Interface
196  
197 =head2 Class Methods
198  
199 =head3 C<app>
200  
201 PGXN::API->app(%params);
202  
203 Returns the PGXN::API Plack app. The supported parameters are:
204  
205 =over
206  
207 =item C<doc_root>
208  
209 The path to use for the API document root.
210  
211 =item C<errors_to>
212  
213 An email address to which error emails should be sent.
214  
215 =item C<errors_from>
216  
217 An email address from which alert emails should be sent.
218  
219 =back
220  
221 See L<pgxn_api_server> for an example usage.
222  
223 =head1 Author
224  
225 David E. Wheeler <david.wheeler@pgexperts.com>
226  
227 =head1 Copyright and License
228  
229 Copyright (c) 2011-2026 David E. Wheeler.
230  
231 This module is free software; you can redistribute it and/or modify it under
232 the L<PostgreSQL License|http://www.opensource.org/licenses/postgresql>.
233  
234 Permission to use, copy, modify, and distribute this software and its
235 documentation for any purpose, without fee, and without a written agreement is
236 hereby granted, provided that the above copyright notice and this paragraph
237 and the following two paragraphs appear in all copies.
238  
239 In no event shall David E. Wheeler be liable to any party for direct,
240 indirect, special, incidental, or consequential damages, including lost
241 profits, arising out of the use of this software and its documentation, even
242 if David E. Wheeler has been advised of the possibility of such damage.
243  
244 David E. Wheeler specifically disclaims any warranties, including, but not
245 limited to, the implied warranties of merchantability and fitness for a
246 particular purpose. The software provided hereunder is on an "as is" basis,
247 and David E. Wheeler has no obligations to provide maintenance, support,
248 updates, enhancements, or modifications.
249  
250 =cut
« API/Indexer.pm ↑ Summary API.pm »