[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Re: RFC: CAP::Plugin::PathInfo


On Date: Thu, 14 Jul 2005 00:08:53 -0400, Timothy Appnel <suppressed> wrote:
> On 7/12/05, David Emery <suppressed> wrote:
> > Q: I had the impression from your REST post that DELETE and PUT were
> > unusable from a practical POV. Are you actually able to use those in
> > real life?
> 
> It's still useful in non-browser systems like a web service API gateway. 

I see.

> ... 
> My instinct is all that is needed for now is a
> handler to register run modes based on some path_info prefix and HTTP
> request method and an associate to evaluate which method to execute. I
> don't think breaking the pathinfo up into pieces is that helpful
> because it will vary on the implementation. It's also not that hard to
> do -- probably a second split call should do it.

I've read a bit more about REST, and looked at the code of Catalyst
and REST::Application. I think what you suggest can be implemented by
overriding CAP's run mode setup and instead setting the run mode based
on user-defined regexes matched against the path_info.

I threw together a rough plugin as a proof of concept. I'm not really
sure that this is the ideal way to do it, but I've tried it out and it
seems to work as I'd hoped. Here's an example of how I use it in my
POC app. The sample URL's may not be entirely RESTful, but I think it
shows the general idea.

use CGI::Application::Plugin::REST; # no exported methods

...

sub setup {
  my $self = shift;

  # note: no regular run mode setup

  # Set path_info regexp to runmode map
  $self->param(
    'path_to_runmode' => {

       # The simplest scenario
       # If path_info starts with /list set runmode to rm_list
       # pointing to sub of same name
       #
       # i.e. http://xx.com/stuff.pl/list 
       # (the .pl isn't RESTful, but mod_rewrite can fix that)

       qr{^/list} => "rm_list",


       # Runmode set to rm_view
       # Matching parens content will be assigned to query params
       # called "item_id" and "output format" 
       #
       # i.e.  http://xx.com/stuff.pl/view/123.html
       #  or   http://xx.com/stuff.pl/view/123.pdf

       qr!^/view/(\d+)\.([a-zA-Z]{3,4})! => 
	               ["rm_view", "item_id", "output_format"],


       # Different runmode depending on request method
       #
       # i.e. http://xx.com/stuff.pl/edit/123

       qr{^/edit/(\d+)} =>{ 
			  GET => ["rm_edit", "item_id"],
			  POST => ["rm_edit_in", "item_id"],
  			  },


       # Since we've eliminated the normal runmode set-up, set a default
       default_runmode => "rm_list",
     }
   );

}

The above works pretty similarly to REST::Application's resourceHooks
(run modes) set-up. One difference is that RA passes the data in the
matching parens of the regex directly as arguments to the run
mode. That isn't possible with CAP, so I add them as query params with
the option of setting their names in the set-up.

I've implemented it as a plugin, which sets a callback at the prerun
stage to set prerun_mode(). Is there any other way to override CAP's
runmode setup?

The code inside the plugin is largely taken from REST::Application.
If anyone is interested, I can clean it up a bit and put it online
somewhere or email it next week. It's a long weekend here in Japan
(Monday is "Ocean Day") so I'll be away from the computer for a few
days.

Dave

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.