Re: Accessing post data
Kenneth Guerin <[email protected]> Wed, 3 Jun 2009 19:09:34 -0400
| Newsgroups | gmane.comp.apache.mod-ruby |
|---|---|
| Message-ID | <[email protected]> |
The problem is that cgi.rb, which I'm assuming you're requiring in =20
whatever CGI handler is processing the incoming XML, preprocesses the =20=
incoming CGI data via the CGI::parse <- CGI::initialize_query <- =20
CGI:initialize call stack.
In order to get around that for my own XML data handling, I built a =20
RawCGI class which is a shameless hacked copy of CGI (/usr/lib/ruby/=20
1.8/cgi.rb) which does not call CGI::parse or CGI::initialize_query. =20=
The CGI class is really for typical CGI use cases, which are key=3Dvalue =
=20
requests as seen in URLs.
While I won't post my rawcgi.rb file, I can point out the relevant =20
parts of cgi.rb and rawcgi.rb for comparison:
/usr/lib/ruby/1.8/cgi.rb:
def initialize(type =3D "query")
if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
Apache.request.setup_cgi_env
end
extend QueryExtension
@multipart =3D false
if defined?(CGI_PARAMS)
warn "do not use CGI_PARAMS and CGI_COOKIES"
@params =3D CGI_PARAMS.dup
@cookies =3D CGI_COOKIES.dup
else
initialize_query() # set @params, @cookies
end
(and more stuff ...)
rawcgi.rb:
def initialize
if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE")
Apache.request.setup_cgi_env
end
raise "Bad Request Method" if ENV['REQUEST_METHOD'] !=3D 'POST'
$stdin.binmode if defined? $stdin.binmode
@request =3D $stdin.read(Integer(ENV['CONTENT_LENGTH'])) or ''
end
attr_reader :request
Note the following:
- RawCGI contains a request attribute that is accessible from your =20
Ruby-based CGI handler. That is where your XML data would be.
- RawCGI and CGI probably cannot both be used in the same Ruby script =20=
b/ they both read data via $stdin:
- For my own designs, I use a separate service handler script for =20
Ajax type requests which uses RawCGI.
- Since ENV is accessible outside of the CGI object, you could =20
probably check for ENV['REQUEST_METHOD'] and require 'rawcgi' on =20
'POST' and require 'cgi' on 'GET'. I haven't tested this.
Ken
On Jun 3, 2009, at 5:40 PM, Fred Appelman wrote:
> Thank you for the quick reply. Unfortunately this will not work for =20=
> me (I think).
> The payload is an XML message and that is not parameterized. I need =20=
> to read the
> full XML message that is in the 250 bytes.
>
> Fred
>
> On 3 jun 2009, at 22:17, Sardem FF7 wrote:
>
>> Le 03/06/2009 22:09, Fred Appelman a =E9crit :
>>
>>> 2. Once I have the data I am able to see that the post data has a =20=
>>> size
>>> of 250 bytes. Also the request_method confirms there is POST data.
>>> I cannot find any method to actually access these bytes. The method
>>> read returns nil and the functions gets, readline and readlines
>>> all claim they are private to Apache::Request.
>>> Since there is no documentation I am out of ideas. Does anyone know
>>> how to access the POST data?
>>
>> Hello,
>>
>> I found that the folowing code makes GET and POST data in ARGS :
>> ARGS =3D Apache.request.paramtable
>> You can access the classic $_GET['type'] (in PHP) by ARGS['type']
>> (mod_ruby).
>> So, I think, the two types of datas are the same for mod_ruby.
>>
>> --=20
>>
>> Sardem FF7
>>
>
>