DDG
view release on metacpan or search on metacpan
lib/DDG/Rewrite.pm view on Meta::CPAN
if($uses_echo_module) {
# we need to make sure we have plain text coming back until we have a way
# to unilaterally gunzip responses from the upstream since the echo module
# will intersperse plaintext with gzip which results in encoding errors.
# https://github.com/agentzh/echo-nginx-module/issues/30
$cfg .= "\tproxy_set_header Accept-Encoding '';\n";
}
if($uses_echo_module || $content_type_javascript) {
# This is a workaround that deals with endpoints that don't support callback functions.
# So endpoints that don't support callback functions return a content-type of 'application/json'
# because what they're returning is not meant to be executed in the first place.
# Setting content-type to application/javascript for those endpoints solves blocking due to
# mime type mismatches.
$cfg .= "\tmore_set_headers 'Content-Type: application/javascript; charset=utf-8';\n";
}
$cfg .= "\techo_before_body '$callback(';\n" if $wrap_jsonp_callback;
$cfg .= "\techo_before_body '$callback".qq|("';\n| if $wrap_string_callback;
my $upstream;
if( $spice_name ) {
$upstream = '$'.$spice_name.'_upstream';
lib/DDG/Rewrite.pm view on Meta::CPAN
}
$cfg .= "\trewrite ^".$self->path.($self->has_from ? $self->from : "(.*)")." ".$uri_path." break;\n";
$cfg .= "\tproxy_pass $upstream;\n";
$cfg .= "\tproxy_set_header ".$self->proxy_x_forwarded_for.";\n" if $is_duckduckgo;
$cfg .= "\tproxy_ssl_server_name on;\n" if $scheme =~ /https/;
if($self->has_proxy_cache_valid) {
# This tells Nginx how long the response should be kept.
$cfg .= "\tproxy_cache_valid " . $self->proxy_cache_valid . ";\n";
# Some response headers from the endpoint can affect `proxy_cache_valid` so we ignore them.
# http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_ignore_headers
$cfg .= "\tproxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;\n";
}
$cfg .= "\tproxy_ssl_session_reuse ".$self->proxy_ssl_session_reuse.";\n" if $self->has_proxy_ssl_session_reuse;
$cfg .= "\techo_after_body ');';\n" if $wrap_jsonp_callback;
$cfg .= "\techo_after_body '\");';\n" if $wrap_string_callback;
# proxy_intercept_errors is used to handle endpoints that don't return 200 OK
# When we get errors from the endpoint, instead of replying a blank page, it should reply the function instead with no parameters,
# 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
# something about it (we know that the Spice failed because it should return Spice.failed('...') when the parameters are not valid).
if($callback) {
$cfg .= "\tproxy_intercept_errors on;\n";
if ($self->error_fallback) {
$cfg .= "\terror_page 301 302 303 403 500 502 503 504 =200 /js/failed/$callback;\n";
$cfg .= "\terror_page 404 =200 \@404_$callback;\n";
} else {
$cfg .= "\terror_page 301 302 303 403 404 500 502 503 504 =200 /js/failed/$callback;\n";
}
( run in 0.396 second using v1.01-cache-2.11-cpan-b61123c0432 )