How can I extract only the schema, host and port from a URL in Perl?

I need to be able to retrieve only the schema, host and port from the URL.

So, if my URL is in a browser: http://www.example.com:80/something.pl I need to be able to:http://www.example.com:80

+3
source share
4 answers

The URI module can help you cut and cut URIs in any way you want.

If you are trying to do this from a CGI script, you need to look $ENV{SERVER_NAME}and $ENV{SERVER_PORT}.

Using urlthe CGI module method that you use (e.g. CGI.pm or CGI :: Simple ) will make things simpler.

+9
source

URI , :

use 5.010;
use URI;

my $url = 'http://www.example.com:80/something.pl';

my $uri = URI->new( $url );

say $uri->scheme;
say $uri->host;
say $uri->port;
+6

modperl, Apache2:: RequestRec, uri, unparsed_uri.

You cannot get the exact text entered in the user’s browser, only what is presented on the server.

The server name (virtual host) is in the Server object.

+3
source
sub cgi_hostname {
  my $h = $ENV{HTTP_HOST} || $ENV{SERVER_NAME} || 'localhost';
  my $dp =$ENV{HTTPS} ? 443 : 80;
  my $ds =$ENV{HTTPS} ? "s" : "";
  my $p = $ENV{SERVER_PORT} || $dp;
  $h .= ":$p" if ($h !~ /:\d+$/ && $p != $dp);
  return "http$ds\://$h";
}
-5
source

Source: https://habr.com/ru/post/1734462/


All Articles