Mixed content & uri_for function

150 Views Asked by At

We have an web application using dancer2 framework.The website is using https. But in the page, it calls other page using uri_for. i.e The url is generated by uri_for and sending it to template.

The url generated is automatically set ‘http’, so the page has mixed content. And thus doesn’t load.

My question is where is the uri_for located. Why is automatically set ‘http’, how can I specify it to be ‘https’?

Many thanks Wendy

1

There are 1 best solutions below

1
Håkon Hægland On

My question is where is the uri_for located.

See line 283 in Dancer2::Core::Request :

sub uri_for {
    my ( $self, $part, $params, $dont_escape ) = @_;
 
    $part ||= '';
    my $uri = $self->base;
 
    # Make sure there's exactly one slash between the base and the new part
    my $base = $uri->path;
    $base =~ s|/$||;
    $part =~ s|^/||;
    $uri->path("$base/$part");
 
    $uri->query_form($params) if $params;
 
    return $dont_escape
           ? uri_unescape( ${ $uri->canonical } )
           : ${ $uri->canonical };
}

It obtains the scheme by calling $self->base, see line 242 :

sub base {
    my $self = shift;
    my $uri  = $self->_common_uri;
 
    return $uri->canonical;
}

which calls $self->_common_uri (see line 249) :

sub _common_uri {
    my $self = shift;
 
    my $path   = $self->env->{SCRIPT_NAME};
    my $port   = $self->env->{SERVER_PORT};
    my $server = $self->env->{SERVER_NAME};
    my $host   = $self->host;
    my $scheme = $self->scheme;
 
    my $uri = URI->new;
    $uri->scheme($scheme);
    $uri->authority( $host || "$server:$port" );
    $uri->path( $path      || '/' );
 
    return $uri;
}

which calls $self->scheme, see line 159 :

sub scheme {
    my ($self) = @_;
    my $scheme = $self->is_behind_proxy
               ? $self->forwarded_protocol
               : '';
 
    return $scheme || $self->env->{'psgi.url_scheme'};
}