DDG/Rewrite.pm

filter: Statementstmt 91.1% 113 / 124 Branchbran 75.0% 45 / 60 Conditioncond 80.9% 17 / 21 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 3 totaltotal 82.9% 194 / 234 CC 41 SCAR 34.4
CC
41
Cov
85%
_build_nginx_conf
44.8
BUILD
7.0
__ANON__
2.0
CRAP 31.3
« DDG/Location.pm ↑ Summary DDG/Block/Words.pm »
1 package DDG::Rewrite;
2 13 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: A (mostly spice related) Rewrite definition in our system
4 13 $DDG::Rewrite::VERSION = '1017';
5 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub BUILD {
10 19 ▶ my ( $self ) = @_;
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
11 19 my $to = $self->to;
12 19 ▶ my $callback = $self->has_callback ? $self->callback : "";
Branch: 1 branch
BranchTrueFalseTotal
$self->has_callback ? : 6 13 19
13 19 ▶ croak "Missing callback attribute for {{callback}} in to" if ($to =~ s/\Q{{callback}}/$callback/g && !$self->has_callback);
Branch: 1 branch
BranchTrueFalseTotal
if $to =~ s/\{\{callback\}\}/$callback/g and not $self->has_callback 1 18 19
Condition100%3/3
$to =~ s/\{\{callback\}\}/$callback/g and not $self->has_callback
!ll&&!rl&&r
1351
MC/DC100%2/2
$to =~ s/\{\{callback\}\}/$callback/g and not $self->has_callback
$to =~ s/\{\{callback\}\}/$callback/g not $self->has_callback
Truth table100%3/3
A and not(B)
A= $to =~ s/\{\{callback\}\}/$callback/gB= not $self->has_callback
ABresult
0X0
100
111
14 # Make sure we replace "{{dollar}}"" with "{dollar}".
15 18 $to =~ s/\Q{{dollar}}/\$\{dollar\}/g;
16 18 my @missing_envs;
17 18 for ($to =~ m/\Q{{ENV{\E(\w+)}}}/g) {
18 3 ▶ if (defined $ENV{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{$_}) { } 2 1 3
19 2 my $val = $ENV{$_};
20 2 $to =~ s/\Q{{ENV{$_}}}/$val/g;
21 } else {
22 1 push @missing_envs, $_;
23 1 $to =~ s/\Q{{ENV{$_}}}//g;
24 }
25 }
26 18 ▶ $self->_missing_envs(\@missing_envs) if @missing_envs;
Branch: 1 branch
BranchTrueFalseTotal
if @missing_envs 1 17 18
27 18 $self->_parsed_to($to);
28 }
29  
30  
31 13 has path => (
32 is => 'ro',
33 required => 1,
34 );
35  
36 13 has to => (
37 is => 'ro',
38 required => 1,
39 );
40  
41 13 has from => (
42 is => 'ro',
43 predicate => 'has_from',
44 );
45  
46 13 has callback => (
47 is => 'ro',
48 predicate => 'has_callback',
49 );
50  
51 has wrap_jsonp_callback => (
52 is => 'ro',
53 15 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 13 );
55  
56 has wrap_string_callback => (
57 is => 'ro',
58 16 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 13 );
60  
61 13 has headers => (
62 is => 'ro',
63 predicate => 'has_headers',
64 );
65  
66 13 has proxy_cache_valid => (
67 is => 'ro',
68 predicate => 'has_proxy_cache_valid',
69 );
70  
71 13 has proxy_ssl_session_reuse => (
72 is => 'ro',
73 predicate => 'has_proxy_ssl_session_reuse',
74 );
75  
76 has proxy_x_forwarded_for => (
77 is => 'ro',
78 19 ▶ default => sub { 'X-Forwarded-For $proxy_add_x_forwarded_for' }
Subroutine __ANON__: called CC=1 SCAR=0.0
79 13 );
80  
81 13 has post_body => (
82 is => 'ro',
83 predicate => 'has_post_body',
84 );
85  
86 13 has nginx_conf => (
87 is => 'ro',
88 lazy => 1,
89 builder => '_build_nginx_conf',
90 );
91  
92 has error_fallback => (
93 is => 'rw',
94 16 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
95 13 );
96  
97 has upstream_timeouts => (
98 is => 'lazy',
99 predicate => 'has_upstream_timeouts',
100 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 13 );
102  
103 sub _build_nginx_conf {
104 15 ▶ my ( $self ) = @_;
Subroutine _build_nginx_conf: called CC=35 SCAR=38.0
105  
106 15 my $uri = URI->new($self->parsed_to);
107 15 my $host = $uri->host;
108 15 my $port = $uri->port;
109 15 my $scheme = $uri->scheme;
110 15 my $uri_path = $self->parsed_to;
111 15 $uri_path =~ s!$scheme://$host:$port!!;
112 15 $uri_path =~ s!$scheme://$host!!;
113 15 my $is_duckduckgo = $host =~ /(?:127\.0\.0\.1|duckduckgo\.com)/;
114  
115 # wrap various other things into jsonp
116 15 ▶ croak "Cannot use wrap_jsonp_callback and wrap_string callback at the same time!" if $self->wrap_jsonp_callback && $self->wrap_string_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wrap_jsonp_callback and $self->wrap_string_callback 0 15 15
Condition66%2/3
$self->wrap_jsonp_callback and $self->wrap_string_callback
!ll&&!rl&&r
1410
MC/DC0%0/2
$self->wrap_jsonp_callback and $self->wrap_string_callback
$self->wrap_jsonp_callback $self->wrap_string_callback
Truth table66%2/3
A and B
A= $self->wrap_jsonp_callbackB= $self->wrap_string_callback
ABresult
0X0
100
111
117 15 ▶ my $wrap_jsonp_callback = $self->has_callback && $self->wrap_jsonp_callback;
Condition100%3/3
$self->has_callback && $self->wrap_jsonp_callback
!ll&&!rl&&r
1221
MC/DC100%2/2
$self->has_callback && $self->wrap_jsonp_callback
$self->has_callback $self->wrap_jsonp_callback
Truth table100%3/3
A && B
A= $self->has_callbackB= $self->wrap_jsonp_callback
ABresult
0X0
100
111
118 15 ▶ my $wrap_string_callback = $self->has_callback && $self->wrap_string_callback;
Condition66%2/3
$self->has_callback && $self->wrap_string_callback
!ll&&!rl&&r
1230
MC/DC0%0/2
$self->has_callback && $self->wrap_string_callback
$self->has_callback $self->wrap_string_callback
Truth table66%2/3
A && B
A= $self->has_callbackB= $self->wrap_string_callback
ABresult
0X0
100
111
119 15 ▶ my $uses_echo_module = $wrap_jsonp_callback || $wrap_string_callback;
Condition66%2/3
$wrap_jsonp_callback || $wrap_string_callback
l!l&&r!l&&!r
1014
MC/DC50%1/2
$wrap_jsonp_callback || $wrap_string_callback
$wrap_jsonp_callback $wrap_string_callback
Truth table66%2/3
A || B
A= $wrap_jsonp_callbackB= $wrap_string_callback
ABresult
1X1
011
000
120 15 my $callback = $self->callback;
121 15 my ($spice_name) = $self->path =~ m{^/js/spice/(.+)/$};
122 15 ▶ $spice_name =~ s|/|_|og if $spice_name;
Branch: 1 branch
BranchTrueFalseTotal
if $spice_name 15 0 15
123  
124 15 my $cfg = "location ^~ ".$self->path." {\n";
125  
126 15 ▶ my $timeouts = $self->has_upstream_timeouts && $self->upstream_timeouts;
Condition66%2/3
$self->has_upstream_timeouts && $self->upstream_timeouts
!ll&&!rl&&r
1302
MC/DC50%1/2
$self->has_upstream_timeouts && $self->upstream_timeouts
$self->has_upstream_timeouts $self->upstream_timeouts
Truth table66%2/3
A && B
A= $self->has_upstream_timeoutsB= $self->upstream_timeouts
ABresult
0X0
100
111
127 15 ▶ if (ref $timeouts eq 'HASH' && keys %$timeouts) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $timeouts eq "HASH" and keys %$timeouts) 1 14 15
Condition100%3/3
ref $timeouts eq "HASH" and keys %$timeouts
!ll&&!rl&&r
1311
MC/DC100%2/2
ref $timeouts eq "HASH" and keys %$timeouts
ref $timeouts eq "HASH" keys %$timeouts
Truth table100%3/3
A and B
A= ref $timeouts eq "HASH"B= keys %$timeouts
ABresult
0X0
100
111
128 1 ▶ $cfg .= "\tproxy_connect_timeout $timeouts->{connect};\n" if $timeouts->{connect};
Branch: 1 branch
BranchTrueFalseTotal
if $timeouts->{"connect"} 1 0 1
129 1 ▶ $cfg .= "\tproxy_send_timeout $timeouts->{send};\n" if $timeouts->{send};
Branch: 1 branch
BranchTrueFalseTotal
if $timeouts->{"send"} 1 0 1
130 1 ▶ $cfg .= "\tproxy_read_timeout $timeouts->{read};\n" if $timeouts->{read};
Branch: 1 branch
BranchTrueFalseTotal
if $timeouts->{"read"} 1 0 1
131 }
132  
133 15 ▶ if ( $self->headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers) 4 11 15
134 4 ▶ if ( ref $self->headers eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->headers eq 'HASH') { } 2 2 4
elsif (ref $self->headers eq 'ARRAY') { } 1 1 2
135 2 for my $header ( sort keys %{$self->headers} ) {
136 3 $cfg .= "\tproxy_set_header $header \"" . $self->headers->{$header} . "\";\n";
137 }
138 }
139 elsif ( ref $self->headers eq 'ARRAY' ) {
140 1 for my $header ( @{ $self->headers } ) {
141 1 $cfg .= "\tproxy_set_header $header;\n";
142 }
143 }
144 else {
145 1 $cfg .= "\tproxy_set_header " . $self->headers . ";\n";
146 }
147 }
148  
149 15 ▶ if ( $self->has_post_body ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_post_body) 1 14 15
150 1 $cfg .= "\tproxy_method POST;\n";
151 1 $cfg .= "\tproxy_set_body '" . $self->post_body . "';\n";
152  
153 # This block sets the proxy cache key from the spice name and the combined
154 # set of captured GET parameters. The 'map' builds a hash of these capture
155 # parameters as keys to ensure each one occurs only once. We can then pull these
156 # out consistently by calling 'sort keys' on the returned hash and 'join' turns
157 # the sorted keys into a single string.
158 # e.g. post_body '{"method":"$2","query":"$1","cleaned_query":"$1"}'
159 # Would give a $cache_keys value of '$1$2'
160 1 my $cache_keys = join '', sort keys %{ {
161 1 map { $_ => 1 } ( $self->post_body =~ m/\$[0-9]+/g )
162 } };
163 1 $cfg .= "\tproxy_cache_key spice_${spice_name}_$cache_keys;\n"
164 }
165  
166 15 ▶ if($uses_echo_module) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uses_echo_module) 1 14 15
167 # we need to make sure we have plain text coming back until we have a way
168 # to unilaterally gunzip responses from the upstream since the echo module
169 # will intersperse plaintext with gzip which results in encoding errors.
170 # https://github.com/agentzh/echo-nginx-module/issues/30
171 1 $cfg .= "\tproxy_set_header Accept-Encoding '';\n";
172  
173 # This is a workaround that deals with endpoints that don't support callback functions.
174 # So endpoints that don't support callback functions return a content-type of 'application/json'
175 # because what they're returning is not meant to be executed in the first place.
176 # Setting content-type to application/javascript for those endpoints solves blocking due to
177 # mime type mismatches.
178 1 $cfg .= "\tmore_set_headers 'Content-Type: application/javascript; charset=utf-8';\n";
179 }
180  
181 15 ▶ $cfg .= "\techo_before_body '$callback(';\n" if $wrap_jsonp_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_jsonp_callback 1 14 15
182 15 ▶ $cfg .= "\techo_before_body '$callback".qq|("';\n| if $wrap_string_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_string_callback 0 15 15
183  
184 15 my $upstream;
185 15 ▶ if( $spice_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spice_name) { } 15 0 15
186 15 $upstream = '$'.$spice_name.'_upstream';
187 15 $cfg .= "\tset $upstream $scheme://$host:$port;\n";
188 } else {
189 0 warn "Error: Problem finding spice name in ".$self->path; return
190 0 }
191  
192 15 ▶ $cfg .= "\trewrite ^".$self->path.($self->has_from ? $self->from : "(.*)")." ".$uri_path." break;\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->has_from ? : 2 13 15
193 15 $cfg .= "\tproxy_pass $upstream;\n";
194 15 ▶ $cfg .= "\tproxy_set_header ".$self->proxy_x_forwarded_for.";\n" if $is_duckduckgo;
Branch: 1 branch
BranchTrueFalseTotal
if $is_duckduckgo 2 13 15
195 15 ▶ $cfg .= "\tproxy_ssl_server_name on;\n" if $scheme =~ /https/;
Branch: 1 branch
BranchTrueFalseTotal
if $scheme =~ /https/ 8 7 15
196  
197 15 ▶ if($self->has_proxy_cache_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_proxy_cache_valid) 1 14 15
198 # This tells Nginx how long the response should be kept.
199 1 $cfg .= "\tproxy_cache_valid " . $self->proxy_cache_valid . ";\n";
200 # Some response headers from the endpoint can affect `proxy_cache_valid` so we ignore them.
201 # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers
202 1 $cfg .= "\tproxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;\n";
203 }
204  
205 15 ▶ $cfg .= "\tproxy_ssl_session_reuse ".$self->proxy_ssl_session_reuse.";\n" if $self->has_proxy_ssl_session_reuse;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_proxy_ssl_session_reuse 0 15 15
206 15 ▶ $cfg .= "\techo_after_body ');';\n" if $wrap_jsonp_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_jsonp_callback 1 14 15
207 15 ▶ $cfg .= "\techo_after_body '\");';\n" if $wrap_string_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_string_callback 0 15 15
208  
209 # proxy_intercept_errors is used to handle endpoints that don't return 200 OK
210 # When we get errors from the endpoint, instead of replying a blank page, it should reply the function instead with no parameters,
211 # e.g., ddg_spice_dictionary_definition();. The benefit of doing that is that we know for sure that the Spice failed, and we can do
212 # something about it (we know that the Spice failed because it should return Spice.failed('...') when the parameters are not valid).
213 15 ▶ if($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 3 12 15
214 3 $cfg .= "\tproxy_intercept_errors on;\n";
215 3 ▶ if ($self->error_fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error_fallback) { } 0 3 3
216 0 $cfg .= "\terror_page 301 302 303 403 500 502 503 504 =200 /js/failed/$callback;\n";
217 0 $cfg .= "\terror_page 404 =200 \@404_$callback;\n";
218 } else {
219 3 $cfg .= "\terror_page 301 302 303 403 404 500 502 503 504 =200 /js/failed/$callback;\n";
220 }
221 }
222  
223 15 $cfg .= "\texpires 1s;\n";
224 15 $cfg .= "}\n";
225 15 ▶ if ($self->error_fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error_fallback) 0 15 15
226 0 my $fallback = $self->error_fallback;
227 0 $cfg .= "location \@404_$callback".qq( {\n);
228 0 ▶ $cfg .= "\techo_before_body '$callback(';\n" if $wrap_jsonp_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_jsonp_callback 0 0 0
229 0 $cfg .= qq(\techo '{"fallback": "$fallback"}';\n);
230 0 ▶ $cfg .= "\techo_after_body ');';\n" if $wrap_jsonp_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $wrap_jsonp_callback 0 0 0
231 0 $cfg .= qq( }\n);
232 }
233 15 return $cfg;
234 }
235  
236 13 has _missing_envs => (
237 is => 'rw',
238 predicate => 'has_missing_envs',
239 );
240 6 ▶sub missing_envs { shift->_missing_envs }
Subroutine missing_envs: called CC=1 SCAR=0.0
Pod undocumented
241  
242 13 has _parsed_to => (
243 is => 'rw',
244 );
245 30 ▶sub parsed_to { shift->_parsed_to }
Subroutine parsed_to: called CC=1 SCAR=0.0
Pod undocumented
246  
247 13 1;
248  
249 __END__
« DDG/Location.pm ↑ Summary DDG/Block/Words.pm »