From 8c4b9d846d8a5aa58f53906eb9d012900b3ded18 Mon Sep 17 00:00:00 2001 From: Marc0 Date: Mon, 22 Jun 2020 14:21:28 +0200 Subject: [PATCH] Allow swagger to reside behind a reverse proxy proposal to fix #101 --- lib/Raisin/Plugin/Swagger.pm | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Raisin/Plugin/Swagger.pm b/lib/Raisin/Plugin/Swagger.pm index f1016f0..a90dc38 100644 --- a/lib/Raisin/Plugin/Swagger.pm +++ b/lib/Raisin/Plugin/Swagger.pm @@ -92,7 +92,10 @@ sub _spec_20 { : qw(application/x-yaml application/json); my $base_path = $req->base->as_string; - $base_path =~ s#http(?:s?)://[^/]+##msix; + ### Respect proxied requests + # A proxy map is used to fill the "basePath" attribute. + my $_base = $req->env->{HTTP_X_FORWARDED_SCRIPT_NAME} || q(/); + $base_path =~ s#http(?:s?)://[^/]+/#$_base#msix; $DEFAULTS{consumes} = \@content_types; $DEFAULTS{produces} = \@content_types; @@ -100,7 +103,11 @@ sub _spec_20 { my %spec = ( swagger => '2.0', info => _info_object($app), - host => $req->env->{SERVER_NAME} || $req->env->{HTTP_HOST}, + ### Respect proxied requests + # The frontend hostname is used if set. + host => $req->env->{HTTP_X_FORWARDED_HOST} + || $req->env->{SERVER_NAME} + || $req->env->{HTTP_HOST}, basePath => $base_path, schemes => [$req->scheme], consumes => \@content_types,