sub host_prefix_url ($$) {
my ($env, $url) = @_;
return $url if index($url, '//') >= 0;
- my $host_port = $env->{HTTP_HOST} //
- "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
- my $sn = $env->{SCRIPT_NAME} // '';
- "$env->{'psgi.url_scheme'}://\L$host_port\E$sn/$url";
+ require PublicInbox::Hval;
+ PublicInbox::Hval::psgi_base_url($env) . '/' . $url;
}
sub base_url { # for coderepos, PSGI-only
use base qw/Exporter/;
our @EXPORT_OK = qw(ascii_html obfuscate_addrs to_filename src_escape
to_attr prurl mid_href fmt_ts ts2str utf8_maybe
- sanitize_local_paths);
+ sanitize_local_paths psgi_base_url);
use POSIX qw(strftime);
my $enc_ascii = find_encoding('us-ascii');
use File::Spec::Functions qw(abs2rel);
}
}
+sub psgi_base_url ($) {
+ my ($env) = @_;
+ my $scheme = $env->{'psgi.url_scheme'};
+ my $host_port = $env->{HTTP_HOST} // do {
+ my @h = ($env->{SERVER_NAME}, $env->{SERVER_PORT});
+ pop(@h) if (($h[1] == 443 && $scheme eq 'https') ||
+ ($h[1] == 80 && $scheme eq 'http'));
+ join ':', @h;
+ };
+ $scheme . '://' . $host_port . $env->{SCRIPT_NAME};
+}
+
# for misguided people who believe in this stuff, give them a
# substitution for '.'
# ․ · and ͺ were also candidates:
use strict;
use v5.10.1;
use parent qw(PublicInbox::GzipFilter);
+use PublicInbox::Hval qw(psgi_base_url);
use PublicInbox::Eml;
sub referer_match ($) {
# n.b.: $ctx->{ibx}->base_url($env) with INBOX_URL won't work
# with dillo, we can only match "$url_scheme://$HTTP_HOST/" without
# path components
- my $base_url = lc($env->{'psgi.url_scheme'} . '://' .
- ($env->{HTTP_HOST} //
- "$env->{SERVER_NAME}:$env->{SERVER_PORT}") . '/');
+ my $base_url = psgi_base_url($env).'/';
index($referer, $base_url) == 0;
}
use Errno qw(EACCES ENOTDIR ENOENT);
use URI::Escape qw(uri_escape_utf8);
use PublicInbox::GzipFilter qw(gzf_maybe);
-use PublicInbox::Hval qw(ascii_html fmt_ts);
+use PublicInbox::Hval qw(ascii_html fmt_ts psgi_base_url);
use Plack::MIME;
our @EXPORT_OK = qw(@NO_CACHE r path_info_raw);
sub redirect_slash ($) {
my ($env) = @_;
- my $url = $env->{'psgi.url_scheme'} . '://';
- my $host_port = $env->{HTTP_HOST} //
- "$env->{SERVER_NAME}:$env->{SERVER_PORT}";
- $url .= $host_port . path_info_raw($env) . '/';
+ my $url = psgi_base_url($env) . path_info_raw($env) . '/';
my $body = "Redirecting to $url\n";
[ 302, [ qw(Content-Type text/plain), 'Location', $url,
'Content-Length', length($body) ], [ $body ] ]
$ibx->{feedmax} = 3;
my $ctx = {
ibx => $ibx,
- env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'http' },
+ env => {
+ HTTP_HOST => 'example.com',
+ 'psgi.url_scheme' => 'http',
+ SCRIPT_NAME => '',
+ },
};
my $string_feed = sub {
my $res = PublicInbox::Feed::generate($ctx);
my $solver = PublicInbox::SolverGit->new($ibx, sub { $res = $_[0] });
open my $log, '+>>', "$tmpdir/solve.log";
my $psgi_env = { 'psgi.errors' => \*STDERR, 'psgi.url_scheme' => 'http',
- 'HTTP_HOST' => 'example.com' };
+ 'HTTP_HOST' => 'example.com', SCRIPT_NAME => '' };
$solver->solve($psgi_env, $log, '69df7d5', {});
ok($res, 'solved a blob!');
my $wt_git = $res->[0];
}
my $ctx = bless {
- env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'https' },
+ env => {
+ HTTP_HOST => 'example.com',
+ 'psgi.url_scheme' => 'https',
+ SCRIPT_NAME => ''
+ },
ibx => $ibx,
www => Plack::Util::inline_object(style => sub {''}),
gz => PublicInbox::GzipFilter::gzip_or_die(),