$self->req->url->???

25 views
Skip to first unread message

iaw4

unread,
May 19, 2017, 8:34:53 PM5/19/17
to Mojolicious

I am trying to extract pieces of the current URL.  I see that `$self->req->url->to_abs()` gives me the full URL, which I could then decompose myself.  however,   I do not understand the documentation---I also see functions, like host, port, etc., already in the doc.  I first thought something like the following set of calls should work, but they do not.

## $self->req->url
##   ->to_abs:   http://a.b.c.d:1/e/f?g=h&i=j#k
##   ->fragment: k
##   ->host: a.b.c.d
##   ->port: 1
##   ->host_port: a.b.c.d:1
##   ->scheme: http
##   ->url->parse($)->host: a.b.c.d:1
##   ->url->parse($)->path: /e/f
##   ->path_query: /e/f?g=h&i=j ?#k


can I use these functions?  or should I just rewrite the URI parser myself?  probably has an obvious answer.

advice appreciated.

/iaw

Stefan Adams

unread,
May 20, 2017, 11:14:07 AM5/20/17
to mojolicious

On Fri, May 19, 2017 at 6:46 PM, iaw4 <ivo...@gmail.com> wrote:
$self->req->url->to_abs->host has the full URL request, such as http://l4.l3.mysite.tld:3000/abc/def?x=123&y=456 .  I can write my own functions to decompose it,

Don't do that.  Use Mojo::URL.
 
but some already exist in M, such as $c->req->url (for /abc/def?x=123&456).  are there others?

Yes, see Attributes and Methods in Mojo::URL.
 
extracting l4.l3 (when localhost, presumably l4.l3.localhost:3000), 3000, l4.l3.mysite.tld, etc.

Oh, it doesn't parse down a hostname for you.  It wouldn't make sense to, there's no convention for that.
 
I looked at the http://mojolicious.org/perldoc/Mojo/URL , but I probably do not understand how to read this.  I tried to dumper $self->req->url, but it does not list all defined functions.

Dumping $self->req->url wouldn't list all the functions, you need to read the reference manual.

Stefan Adams

unread,
May 20, 2017, 11:19:52 AM5/20/17
to mojolicious
On Fri, May 19, 2017 at 7:34 PM, iaw4 <ivo...@gmail.com> wrote:
can I use these functions?

Yes!
 
or should I just rewrite the URI parser myself?

No!!

$ cat /tmp/url 
use 5.010;
use Mojo::URL;
my $url = Mojo::URL->new('http://a.b.c.d:1/e/f?g=h&i=j#k');
say $url->to_abs;
say $url->fragment;
say $url->host;
say $url->port;
say $url->scheme;
say $url->path_query;
$ perl /tmp/url
k
a.b.c.d
1
http
/e/f?g=h&i=j

$c->req->url is a Mojo::URL object (as above).

Reply all
Reply to author
Forward
0 new messages