Base url in Mojolicious template rendering - perl

Is there a way to have urls such as /foo/bar replaced bay baseurl/foo/bar for each template rendering?

Absolutely. You can do this with the before_dispatch hook (or maybe the before_routes hook) in the Mojolicious::App object.
app->hook( before_dispatch => sub {
my $c = shift;
unless ($c->req->url->path->to_route =~ m#/baseurl#) {
$c->req->url->path( "/baseurl" . $c->req->url->path->to_string);
}
} );
...
app->start;

I can't add comments yet but I just wanted to add that mob is correct - the example code using hooks is the recommended approach for URL rewriting:
http://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Rewriting

Related

Adding a comment from a specific user in a bugzilla extension

I am writing a bugzilla extension which adds a comment to bugs when they are submitted using the bug_end_of_create() hook. Regardless of what I pass in the "who" parameter, the comment always appears to have been created by the user that submitted the bug. How do you set the user when creating a comment?
use Bugzilla::User;
our $VERSION = '0.01';
sub install_update_db {
my ($self, $args) = #_;
}
sub bug_end_of_create {
my ($self, $args) = #_;
my $bug = $args->{'bug'};
$bug->add_comment('[automated message]', {"who" => 'me#domain.com'});
}
__PACKAGE__->NAME;
The docs for add_comment do not mention the "who" parameter.
However, there is Bugzilla::Comment, which has an author.
I would therefore think that the way of achieving what you want is to make a new Bugzilla::Comment with the bug id and author.
You should have the bug id available in the hook.
You will have to fetch the Bugzilla::User object for the author ( via the email, which you seem to have ).
I have not tested this.

Route to static file in Mojo

I have small app based on mojolicious. And I have index.html in public dir. I want to have route to this file when user asks for '/'.
I wrote two solution, but I don't like them.
First solution - add simple controller.
sub stratup {
//...
$r->get('/')->to('general#index_html');
//...
}
package MyPackage::General;
use Mojo::Base 'Mojolicious::Controller';
use strict;
use warnings;
sub index_html {
my $self = shift;
$self->render_static('index.html');
return;
}
1;
Second solution - add hook
sub startup {
my $self = shift;
$self->hook(before_dispatch => sub {
my $self = shift;
if ($self->req->url eq '/') {
$self->req->url( Mojo::URL->new('/index.html') );
}
});
What I want:
$r->get('/')->to('/index.html');
or something like that.
P.S. I know, than usualy nginx/apache do it, but I use morbo to run code.
You want:
$r->get('...')->to(cb => sub {
my $c = shift;
$c->reply->static('index.html')
});
(As long as you're after Mojolicous 5.45 2014-09-26)
By far the simplest way is
get "/" => "index";
I'll dig this up from the graveyard, why not.
I found myself similarly trying to serve a static html file in a docker container that I had using to serve both a Mojolicious REST API and a Vue.js front end. After searching around and piecing sporadic information together, this is what seems to work for me.
** disclaimer: I have not fully tested this with Vue routing and other aspects as yet.
My directory structure:
/app
/app/script
/app/modules/ui
/app/modules/ui/dist
From the command line the app directory, using morbo to test:
morbo script/ui.pl
ui.pl script
#!/usr/bin/env perl
use Mojolicious::Lite -signatures;
use Mojo::File qw(curfile);
use v5.25;
my $app = app;
my $static = $app->static;
push #{$static->paths}, curfile->dirname->sibling('modules/ui/dist')->to_string;
any '/' => sub {
my $c = shift;
my $content = $static->file("/index.html")->slurp;
$c->render(text => $content);
};
$app->start;
Using a combo of information from https://metacpan.org/pod/Mojolicious::Static and basic routing information at https://docs.mojolicious.org/Mojolicious/Lite, I could get the vue.js index page to render as expected.
** UPDATED A DAY LATER **
As it turns out, there is an easier way, though not clearly documented. If you place the static files inside your public folder, you can use the default helpers included with Mojolicious to render the files. The documentation refers to it here, https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Serving-static-files, but it's not very clear on how to make it happen.
I tooled around some, but it took browsing the code of Controller.pm of for Mojolicious to sort it out. This section of the POD led me to determine how to get the reply object:
=head2 helpers
my $helpers = $c->helpers;
Return a proxy object containing the current controller object and on which helpers provided by /app can be called. This includes all helpers from Mojolicious::Plugin::DefaultHelpers and Mojolicious::Plugin::TagHelpers.
# Make sure to use the "title" helper and not the controller method
$c->helpers->title('Welcome!');
# Use a nested helper instead of the "reply" controller method
$c->helpers->reply->not_found;
Based on this, I can drop my files into the public folder:
/app/public/index.html
Then modify my controller to match:
# https://docs.mojolicious.org/Mojolicious/Guides/Rendering#Serving-static-files
any '/' => sub {
my $c = shift;
$c->helpers->reply->static('index.html');
};

Removing top-directory-only URLs from a list of URLs?

I have a question that I'm having trouble researching, as I don't know how to ask it correctly on a search engine.
I have a list of URLs. I would like to have some automated way (Perl for preference) to go through the list and remove all URLs that are top directory only.
So for example I might have this list:
http://www.example.com/hello.html
http://www.foo.com/this/thingrighthere.html
In this case I would want to remove example.com from my list, as it is either top-directory only or they reference files in a top directory.
I'm trying to figure out how to do that. My first thought was, count forward slashes and if there's more than two, eliminate the URL from the list. But then you have trailing forward slashes, so that wouldn't work.
Any ideas or thoughts would be much appreciated.
Something like this:
use URI::Split qw( uri_split );
my $url = "http://www.foo.com/this/thingrighthere.html";
my ($scheme, $auth, $path, $query, $frag) = uri_split( $url );
if (($path =~ tr/\///) > 1 ) {
print "I care about this $url";
}
http://metacpan.org/pod/URI::Split
You could do this with regexes, but its much less work to let the URI library do it for you. You won't get caught out by funny schemes, escapes, and extra stuff before and after the path (query, anchor, authorization...). There's some trickiness around how paths are represented by path_segments(). See the comments below and the URI docs for details.
I have assumed that http://www.example.com/foo/ is considered a top directory. Adjust as necessary, but its something you have to think about.
#!/usr/bin/env perl
use URI;
use File::Spec;
use strict;
use warnings;
use Test::More 'no_plan';
sub is_top_level_uri {
my $uri = shift;
# turn it into a URI object if it isn't already
$uri = URI->new($uri) unless eval { $uri->isa("URI") };
# normalize it
$uri = $uri->canonical;
# split the path part into pieces
my #path_segments = $uri->path_segments;
# for an absolute path, which most are, the absoluteness will be
# represented by an empty string. Also /foo/ will come out as two elements.
# Strip that all out, it gets in our way for this purpose.
#path_segments = grep { $_ ne '' } #path_segments;
return #path_segments <= 1;
}
my #filtered_uris = (
"http://www.example.com/hello.html",
"http://www.example.com/",
"http://www.example.com",
"https://www.example.com/",
"https://www.example.com/foo/#extra",
"ftp://www.example.com/foo",
"ftp://www.example.com/foo/",
"https://www.example.com/foo/#extra",
"https://www.example.com/foo/?extra",
"http://www.example.com/hello.html#extra",
"http://www.example.com/hello.html?extra",
"file:///foo",
"file:///foo/",
"file:///foo.txt",
);
my #unfiltered_uris = (
"http://www.foo.com/this/thingrighthere.html",
"https://www.example.com/foo/bar",
"ftp://www.example.com/foo/bar/",
"file:///foo/bar",
"file:///foo/bar.txt",
);
for my $uri (#filtered_uris) {
ok is_top_level_uri($uri), $uri;
}
for my $uri (#unfiltered_uris) {
ok !is_top_level_uri($uri), $uri;
}
Use the URI module from CPAN. http://search.cpan.org/dist/URI
This is a solved problem. People have already written, tested and debugged code that handles this already. Whenever you have a programming problem that others have probably had to deal with, then look for existing code that does it for you.

How can I validate a website URL in Perl?

I need a regular expression or module for validating the website URL using Perl.
Regexp::Common::URI::http
I don't use regular expressions. I try to create a URI object and see what happens. If it works, I have a URI object that I can query to get the scheme (the other things get turned into "schemeless" URIs).
use URI;
while( <DATA> )
{
chomp;
my $uri = URI->new( $_, 'http' );
if( $uri->scheme ) { print "$uri is a URL\n"; }
else { print "$uri is not a URL\n"; }
}
__END__
foo.html
http://www.example.com/index.html
abc
www.example.com
If I'm looking for a specific sort of URI, I can query the object to see if it satisfies whatever I need, such as a particular domain name. If I'm doing something with URLs, I'm probably going to make an object anyway, so I might as well start with it.
Since you are talking about "a website URL", I guess you are interested in HTTP and HTTPS URLs only.
For that, instead of using regex, you can use the Perl's Data::Validate::URI module.
For example, to validate HTTP and HTTPS URLs:
use Data::Validate::URI;
my $url = "http://google.com";
my $uriValidator = new Data::Validate::URI();
print "Valid web URL!" if $uriValidator->is_web_uri($url)
And, to validate HTTP URL only:
print "Valid HTTP URL!" if $uriValidator->is_http_uri($url)
Finally, to validate any well-formatted URI:
print "Valid URI!" if $uriValidator->is_uri($url)
If instead, for any reason, you actually want a regex, then you can use something like the following to validate HTTP/HTTPS/FTP/SFTP URLs:
print "Valid URL!\n" if $url =~ /^(?:(?:https?|s?ftp))/i;
use Regexp::Common qw /URI/;
while (<>) {
/($RE{URI}{HTTP})/ and print "$1 is an HTTP URI.\n";
}

How can I set the Cache-Control header for every response in Catalyst?

It seems that by default Catalyst does not output Cache-Control:, etc. headers. I know I can output them in a given controller method like this:
$c->response->headers->last_modified(time);
$c->response->headers->expires(time + $self->{cache_time});
$c->response->headers->header(cache_control => "public, max-age=$self->{cache_time}");
It'd get pretty painful doing that in each method, though! What I'd prefer is:
A default set of headers (expires now, last modified now, cache-control: no-cache, pragma: no-cache)
A way to, per-method, override the default.
Is there a good way to accomplish this?
derobert:
Excellent question. I covered exactly this in an article for the Catalyst advent calendar.
Basically you create a stash variable that defines your cache time for the given action, and then you process it in your Root end routine. See the article for all the details.
JayK
Update: Based on your response to my earlier suggestion, I decided to bite the bullet and look at the Catalyst docs. It seems to me, the place to do this is in:
sub end : Private {
my ( $self, $c ) = #_;
# handle errors etc.
if ( $c->res->body ) {
if ( "some condition" ) {
set_default_response_headers( $c->response->headers );
return;
}
else {
do_something_else();
return;
}
}
$c->forward( 'MyApp::View::TT' ); # render template
}
Earlier response: I do not use Catalyst, but couldn't you just write a sub for your application?
sub set_default_response_headers {
my ($h) = #_;
$h->last_modified(time);
$h->expires(time + $self->{cache_time});
$h->header(cache_control => "public, max-age=$self->{cache_time}");
return $h;
}
Call with set_default_response_headers( $c->response->headers ).