Sunday, November 22, 2009

[Catalyst] Re: Validating single arg id

* Bill Moseley <moseley@hank.org> [2009-10-18 01:40]:
> On Sat, Oct 17, 2009 at 12:50 PM, Aristotle Pagaltzis <pagaltzis@gmx.de>wrote:
> > * iain <iainhubbard@googlemail.com> [2009-10-16 17:30]:
> > > until we did this we had boilerplate validation at the top
> > > of all the local actions.
> >
> > ++
> >
> > Bill, in another thread you asked me for an example of how
> > Chained helps make things like complex auth checks more DRY.
> > I've been meaning to respond with an example out of my $job
> > codebase, but until I get around to it, here's your hands-on
> > example of something it'd buy your codebase immediately. :-)
>
> I meant to respond about that. Perhaps I'm missing something,
> but the chained example works great if there multiple actions
> that need to get at that "Foo" object. They can all chain back
> to the same root which validates the id and then fetches the
> Foo object with that id and stuffs it into the stash.

Exactly.

> But, its often the case (well, my case) that there's only *one*
> action that fetches Foo, but there's also single actions for
> find()ing a number of other objects. If I'm following the
> chained example correctly, then there would be root for each --
> Foo, Bar, Baz, and so on each running basically the same
> validation.

OK, that's a situation in which Chained indeed won't help (for
this particular aspect of the task).

> I suppose the root can be written in a way to fetch many
> different object types by inspecting the current action and
> thus the fetch for Foo, Bar, Baz, etc. objects could all have
> the same root of the chain.

No no no, that's not what I was suggesting. That's no good. If
you need to contort the code into unnatural structures, then
either you're not using Chained right or it's not the right tool.
Chained is good, but it's not end in itself; it's good because it
helps simplify your code, not just because.

> If I have many actions that do $c->model( $whatever )->find(
> $id ) and I want to validate id doesn't it seems like find()
> (or really search_rs() ) would be the method to override?

Yes, that would indeed be better in your case.

> Perhaps a better approach would be add a "safe_find()" as J.
> Shirley suggested, but what good are methods if you don't
> override them. ;)

That depends on several a lot of very subtle considerations. Does
that method get called internally by its framework? Do they
depend on its precise semantics? How transparent is your new
interface, in general? If the overridden method's semantics
change in a future framework version (adding back-compatible
features, most likely), will you have an easy way to provide
those new features to your own code also, or will your overridden
interface make it impossible or difficult?

After a few mildly painful experiences, I have become a bit gun
shy about overriding things, particularly for making them "more
powerful", and when in doubt, I would avoid it. For some kinds of
making things safer, though, overriding is the better choice.
(Yours might be such a case.) But it needs a lot of deliberation.
Just adding a different method is much less vagarious.

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

[Catalyst] Re: Strange warning...

* Rippl, Steve <rippls@woodlandschools.org> [2009-11-20 19:00]:
> Catalyst version 5.80011
>
> $c->response->redirect($c->uri_for($c->controller('WsdSis::Controller::Section')->action_for('list')));

$c->response->redirect($c->uri_for_action('/sections/list'));

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

[Catalyst] Re: Avoiding UTF8 in Catalyst

* Marc SCHAEFER <schaefer@alphanet.ch> [2009-11-21 23:30]:
> After investigating, the Content-Length: is one off per non
> 7-bit character. As if the standard iso-8859-1 byte stream was
> sent as is, but was, internally converted to UTF-8 just for
> generating a wrong byte count. Very strange. Normally that
> process should really output something wrong or generate an
> error in the conversion. It doesn't.

No, it was not converted to UTF-8. Its internal representation
was upgraded. That's not the same thing (and if you think it is,
you have at the very least not understood Unicode in Perl). It
should have no observable effect.

As a quick fix, you want to utf8::downgrade the $c->res->body at
the last moment before emitting the data to the wire. I'm not
sure off hand which method to wrap in the application class to do
that, though.

Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Saturday, November 21, 2009

Re: [Catalyst] Avoiding UTF8 in Catalyst



On Sat, Nov 21, 2009 at 2:22 PM, Marc SCHAEFER <schaefer@alphanet.ch> wrote:
Hi,

my goal: no UTF8, in short:

Can you explain why?  That sounds like something to regret later down the road.
Even if your templates have latin1 characters you can still output utf8.  Less to to worry about with entity encoding, I guess.

 

  - all the perl code, all the data files, all the template files and
    the UNIX locale are all in ISO-8859-1

Hopefully you don't have much text in your perl code.  Get the database into utf8 will be a win long term.

 

  - the HTML result should be in ISO-8859-1
    (Content-Type: text/html; charset=iso-8859-1)

You need to encode the body, too. Plugin: Unicode::Encoding was just updated by Tom, IIRC.

 

  - the Content-Length: should be correct.

First, I modified lib/MyApp/View/TT.pm as follows:

  __PACKAGE__->config(TEMPLATE_EXTENSION => '.tt',
                      DEFAULT_ENCODING   => 'ISO-8859-1',
                      WRAPPER => 'wrapper.tt');

There's a DEFAULT_ENCODING option?  Isn't it just ENCODING?

 

Apparently all diacritic characters are expanded into HTML entities.

Where does that happen?

 


--
Bill Moseley
moseley@hank.org

[Catalyst] Avoiding UTF8 in Catalyst

Hi,

my goal: no UTF8, in short:

- all the perl code, all the data files, all the template files and
the UNIX locale are all in ISO-8859-1

- the HTML result should be in ISO-8859-1
(Content-Type: text/html; charset=iso-8859-1)

- the Content-Length: should be correct.

First, I modified lib/MyApp/View/TT.pm as follows:

__PACKAGE__->config(TEMPLATE_EXTENSION => '.tt',
DEFAULT_ENCODING => 'ISO-8859-1',
WRAPPER => 'wrapper.tt');

Apparently all diacritic characters are expanded into HTML entities.
Which is functional, but not optimal. However, with FormFu, this
unnecessary expansion doesn't happen, which is fine.

I got the following result:

- the HTML data is in ISO-8859-1 (or as HTML entities, which is
acceptable as a work-around) as wanted
- however the HTTP header charset is UTF8

After looking at line 45 of
/usr/local/share/perl/5.8.8/Catalyst/Action/RenderView.pm
it looks that the utf-8 charset HTTP header is hardcoded. I have thus modified
my lib/MyApp/Controller/Root.pm to do the following in
end : ActionClass('RenderView'):

$c->response->content_type('text/html; charset=iso-8859-1');

With this, I got the following result:

- the HTML data is in ISO-8859-1 as wanted (no change, logical)
- the HTTP header charset is now the correct iso-8859-1
- however, the Content-Length: sent is wrong.

After investigating, the Content-Length: is one off per non 7-bit
character. As if the standard iso-8859-1 byte stream was sent as
is, but was, internally converted to UTF-8 just for generating
a wrong byte count. Very strange. Normally that process should really
output something wrong or generate an error in the conversion. It
doesn't.

My questions:

- is there a better way to use the standard charset than to do all
of the above hacks ?

- if not, how to work-around the content length in
end : ActionClass('RenderView') ? Unfortunately, it looks like
$c->result->body is undefined at this point, and that
$c->finalize_body() doesn't do anything useful.

Version info:
Catalyst 5.80007 and 5.80013

PS: I wouldn't have noticed the Content-Length: issue if I hadn't use a
reverse proxy. With that reverse proxy, and the standalone Catalyst
server, you get 5-10 seconds hangs if the Content-Length is too big,
which is what happens with this strange UTF8 behaviour. Without it,
the size is wrong (as seen by wireshark != PageInfo Firefox), but
the WWW client seems to compensate.

PS/2: the http://www.catb.org/~esr/faqs/smart-questions.html URL doesn't
work currently, so maybe my question is unsmart.

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Re: [Catalyst] namespace problem

gutta wrote on 11/21/09 7:46 AM:

>
> So you want me to write a MyApp/lib/Controller/Base.pm as follows.
>
> package MyApp::Controller::Base;
>
> use strict;
> use warnings;
>
> use parent 'Catalyst::Controller';
>
> #
> # Sets the actions in this controller to be registered with no prefix
> # so they function identically to actions created in MyApp.pm
> #
>
> =head1 NAME
>
> MyApp::Controller::Base - Base Controller for MyApp
>
> =head1 DESCRIPTION
>
> [enter your description here]
>
> =cut
>
> 1;
>
> and will do "use parent 'MyApp::Controller::Base';" instead of my previous
> statement "use parent 'MyApp::Controller::Root';"
>


I think you want to keep stuff out of MyApp::Controller::* namespace unless you
want it instantiated at app startup.

Instead, create MyApp::Base::Controller and inherit from that in your
MyApp::Controller::* classes.

Otherwise, yes, you've got the idea.
--
Peter Karman . http://peknet.com/ . peter@peknet.com

_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/

Re: [Catalyst] namespace problem

Moritz Onken wrote:
>
>
> Am 21.11.2009 um 16:34 schrieb gutta:
>
>>
>> Hi,
>>
>> I tried to use "use parent 'MyApp::Controller::Root';"
>>
>> instead of use parent 'Catalyst::Controller'; in all my other
>> controllers(for ex: User.pm).
>>
>> The reason is that Root.pm is already inheriting Catalyst::Controller.
>>
>> But when i do this catalyst dispatcher is considerting all controller
>> actions in my other controllers under namespace ''.
>>
>> But if i declare __PACKAGE__->config->{namespace} = 'user'; its working
>> as
>> expected.
>
> You can try to set it to undef, so that the controller namespace magic can
> happen.
> But not sure if this is working. Worth a try, though.
>
> A better solution would be to create a base class MyApp::Controller::Base
> which does not set the namespace so that you only need to set it in the
> root controller.
>
> moritz
> _______________________________________________
> List: Catalyst@lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive:
> http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/
>
>

Hi Mortiz,
Thank you for your response.

So you want me to write a MyApp/lib/Controller/Base.pm as follows.

package MyApp::Controller::Base;

use strict;
use warnings;

use parent 'Catalyst::Controller';

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#

=head1 NAME

MyApp::Controller::Base - Base Controller for MyApp

=head1 DESCRIPTION

[enter your description here]

=cut

1;

and will do "use parent 'MyApp::Controller::Base';" instead of my previous
statement "use parent 'MyApp::Controller::Root';"

Thank you.

Best Regards,
Mohan
--
View this message in context: http://old.nabble.com/namespace-problem-tp26454140p26456702.html
Sent from the Catalyst Web Framework mailing list archive at Nabble.com.


_______________________________________________
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/