carmen | 18 Apr 05:58 2007

customizing global request handler

what is the cleanest way to do something for every request of a particular type (Verb, and Content-Type
header)? i'm trying to figure out if shipping a custom camping.rb is avoidable, and if, how i should
structure a patch

my app http://whats-your.name/yard/ uses exclusively JSON based messaging between server and client.
this means POST bodies are JSON, and not querystring.. camping assumes:

    elsif  <at> method == "post"
        qs.merge!(C.qsp( <at> in.read))

if i patch to:

    elsif  <at> method == "post"
      case e.CONTENT_TYPE
      when "application/x-www-form-urlencoded"
        qs.merge!(C.qsp( <at> in.read))
      when "application/json"
         <at> input = JSON.parse( <at> in.read))
      end

theres two probs, 1) you now have to always make sure you send the x-www-form-urlencoded header (weird
browsers, or XHR requests might not), 2)   <at> input is overwritten by qs.dup, input isnt a query string!

i tried using a custom verb, which works fine in firefox, but not WebKit-QT4-Linux.. RFC 2616 does not
specify a format for the data in a POST body, and the overall function of the request is consistent with POST.

what do i do? :)
Jonas Pfenniger | 18 Apr 18:05 2007
Picon

Re: customizing global request handler

2007/4/18, carmen <_ <at> whats-your.name>:
> what is the cleanest way to do something for every request of a particular type (Verb, and Content-Type
header)? i'm trying to figure out if shipping a custom camping.rb is avoidable, and if, how i should
structure a patch
>
>
> my app http://whats-your.name/yard/ uses exclusively JSON based messaging between server and client.
this means POST bodies are JSON, and not querystring.. camping assumes:
>
>     elsif  <at> method == "post"
>         qs.merge!(C.qsp( <at> in.read))
>
> if i patch to:
>
>     elsif  <at> method == "post"
>       case e.CONTENT_TYPE
>       when "application/x-www-form-urlencoded"
>         qs.merge!(C.qsp( <at> in.read))
>       when "application/json"
>          <at> input = JSON.parse( <at> in.read))
>       end
>
> theres two probs, 1) you now have to always make sure you send the x-www-form-urlencoded header (weird
browsers, or XHR requests might not), 2)   <at> input is overwritten by qs.dup, input isnt a query string!
>
> i tried using a custom verb, which works fine in firefox, but not WebKit-QT4-Linux.. RFC 2616 does not
specify a format for the data in a POST body, and the overall function of the request is consistent with POST.
>
> what do i do? :)

(Continue reading)

Thomas Lockney | 18 Apr 19:19 2007
Picon

Re: customizing global request handler

On 4/18/07, Jonas Pfenniger <zimbatm-Q4cn4THVH2M@public.gmane.org> wrote:

Hi Carmen,

to avoid patching Camping, I suggest you look into overriding the
"service" method for the target controllers. You can do this in your
main application module or by including a custom module in your
controllers, as you prefer. Let me know if you need more hints.

I was going to suggest the same thing, but when I looked at it, it wasn't clear if this would allow for grabbing data like the request headers (allowing you to determine what form the submitted data takes). Based on the code Carmen posted, it looks like the idea is to be able to use the same controllers whether the data comes in from regular requests or as JSON data. Granted, that could still be done in the controllers, but it does end up being a bit less elegant.

--
Thomas Lockney | tlockney-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org | http://opposable-thumbs.net
_______________________________________________
Camping-list mailing list
Camping-list@...
http://rubyforge.org/mailman/listinfo/camping-list
carmen | 18 Apr 20:23 2007

Re: customizing global request handler

> controllers whether the data comes in from regular requests or as JSON data.
> Granted, that could still be done in the controllers, but it does end up
> being a bit less elegant.

the main idea is always get  <at> input in the controller regardless of the format - currently it unnecessarily
parses the POST data trying to get querystring values out of the JSON

maybe that method should be split up? it already combines the file upload mangling, and GET and POST
querystring parsing..

i will be using formats besides JSON to set  <at> input too... Turtle, for example..

> 
> -- 
> Thomas Lockney | tlockney@... | http://opposable-thumbs.net

> _______________________________________________
> Camping-list mailing list
> Camping-list@...
> http://rubyforge.org/mailman/listinfo/camping-list
Jonas Pfenniger | 18 Apr 20:38 2007
Picon

Re: customizing global request handler

2007/4/18, Thomas Lockney <tlockney@...>:
> On 4/18/07, Jonas Pfenniger <zimbatm@...> wrote:
> > Hi Carmen,
> >
> > to avoid patching Camping, I suggest you look into overriding the
> > "service" method for the target controllers. You can do this in your
> > main application module or by including a custom module in your
> > controllers, as you prefer. Let me know if you need more hints.
>
> I was going to suggest the same thing, but when I looked at it, it wasn't
> clear if this would allow for grabbing data like the request headers
> (allowing you to determine what form the submitted data takes). Based on the
> code Carmen posted, it looks like the idea is to be able to use the same
> controllers whether the data comes in from regular requests or as JSON data.
> Granted, that could still be done in the controllers, but it does end up
> being a bit less elegant.

Yes, Camping could probably benefit from some method splitting here. I
feel like this method is too big and not really correct. I don't think
that it should put the posted data in a Tempfile, especially if your
controller will never make a use of it. I'm not sure it follows the
RFC too.

Btw I've spotted some debug line here that could be removed :
http://code.whytheluckystiff.net/camping/browser/trunk/lib/camping-unabridged.rb#L402

--

-- 
Cheers,
  zimbatm
carmen | 18 Apr 20:53 2007

Re: customizing global request handler

On Wed Apr 18, 2007 at 08:38:30PM +0200, Jonas Pfenniger wrote:
> 2007/4/18, Thomas Lockney <tlockney@...>:
> > On 4/18/07, Jonas Pfenniger <zimbatm@...> wrote:
> > > Hi Carmen,
> > >
> > > to avoid patching Camping, I suggest you look into overriding the
> > > "service" method for the target controllers. You can do this in your
> > > main application module or by including a custom module in your
> > > controllers, as you prefer. Let me know if you need more hints.
> >
> > I was going to suggest the same thing, but when I looked at it, it wasn't
> > clear if this would allow for grabbing data like the request headers
> > (allowing you to determine what form the submitted data takes). Based on the
> > code Carmen posted, it looks like the idea is to be able to use the same
> > controllers whether the data comes in from regular requests or as JSON data.
> > Granted, that could still be done in the controllers, but it does end up
> > being a bit less elegant.
> 
> Yes, Camping could probably benefit from some method splitting here. I

perhaps, but

> feel like this method is too big and not really correct. I don't think
> that it should put the posted data in a Tempfile, especially if your
> controller will never make a use of it. I'm not sure it follows the
> RFC too.
> 
> Btw I've spotted some debug line here that could be removed :
> http://code.whytheluckystiff.net/camping/browser/trunk/lib/camping-unabridged.rb#L402

it appears -r188 added the patch i needed to use the service method without the unecessary parsing..

/me remembers what i was taught in first grade - always SVN up first!

> 
> -- 
> Cheers,
>   zimbatm
> _______________________________________________
> Camping-list mailing list
> Camping-list@...
> http://rubyforge.org/mailman/listinfo/camping-list
> 
carmen | 18 Apr 21:01 2007

Gem broekn?

gem install --source http://code.whytheluckystiff.net camping
Need to update 3 gems from http://code.whytheluckystiff.net
...
complete
ERROR:  While executing gem ... (TypeError)

gem install camping-omnibus --source http://code.whytheluckystiff.net
Install required dependency camping? [Yn]  y
ERROR:  While executing gem ... (TypeError)
    can't convert Hash into String

 ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]

/me scurries off to manually check out 
Jonas Pfenniger | 18 Apr 22:46 2007
Picon

Re: Gem broekn?

Try removing ~/.gem/source_cache first

2007/4/18, carmen <_ <at> whats-your.name>:
> gem install --source http://code.whytheluckystiff.net camping
> Need to update 3 gems from http://code.whytheluckystiff.net
> ...
> complete
> ERROR:  While executing gem ... (TypeError)
>
>
> gem install camping-omnibus --source http://code.whytheluckystiff.net
> Install required dependency camping? [Yn]  y
> ERROR:  While executing gem ... (TypeError)
>     can't convert Hash into String
>
>  ruby -v
> ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]
>
> /me scurries off to manually check out
> _______________________________________________
> Camping-list mailing list
> Camping-list@...
> http://rubyforge.org/mailman/listinfo/camping-list
>

--

-- 
Cheers,
  zimbatm
carmen | 19 Apr 05:06 2007

Re: Gem broekn?

On Wed Apr 18, 2007 at 10:46:34PM +0200, Jonas Pfenniger wrote:
> Try removing ~/.gem/source_cache first

that isnt it, rubygems just keeps on proving it hates me [1]

SVN head didnt like me either:

 NoMethodError undefined method `capture' for #<Array:0x2b8c0f1cc7f0> in #render
 so it was back to r180 cp -av'd from another machine since rubygems isnt working

definitely investigating tiny lua httpds tho, since redland's bindings like to segfault the ruby interp
(well, i dont know whose fault it is, other than it happens a lot)

1. http://rubyforge.org/pipermail/mongrel-users/2007-January/002753.html

> 
> 2007/4/18, carmen <_ <at> whats-your.name>:
> > gem install --source http://code.whytheluckystiff.net camping
> > Need to update 3 gems from http://code.whytheluckystiff.net
> > ...
> > complete
> > ERROR:  While executing gem ... (TypeError)
> >
> >
> > gem install camping-omnibus --source http://code.whytheluckystiff.net
> > Install required dependency camping? [Yn]  y
> > ERROR:  While executing gem ... (TypeError)
> >     can't convert Hash into String
> >
> >  ruby -v
> > ruby 1.8.6 (2007-03-13 patchlevel 0) [x86_64-linux]
> >
> > /me scurries off to manually check out
> > _______________________________________________
> > Camping-list mailing list
> > Camping-list@...
> > http://rubyforge.org/mailman/listinfo/camping-list
> >
> 
> 
> -- 
> Cheers,
>   zimbatm
> _______________________________________________
> Camping-list mailing list
> Camping-list@...
> http://rubyforge.org/mailman/listinfo/camping-list
> 
carmen | 19 Apr 19:17 2007

Re: request in controller

in the service method (or any controller method), is there a way to access the incoming request headers?
 <at> headers is initialized to {'Content-Type'=>'text/html'} for the response at this point.. 

im wondering if the request is already sitting around in some instance var, or setting e to  <at> e in initialize
is the best solution..

Gmane