You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by simran <si...@reflectit.com> on 2004/04/14 10:01:04 UTC

data from STDIN

Hello All, 

I am tying into someone's API, and their API calls a URL on my server
when it wants to send a message, it does this by posting an entry like:

-------------------------------------------------------------
Content-type: text/xml
<?xml version="1.0" encoding="UTF-8"?>
<MessageData>
    <SMSMessage id="123" originatingId="123" CarrierId="1"
        MappingId="ABC" MSISDN="61404111111" MessageType="TEXT">
        <MessageText>Sample Message</MessageText>
    </SMSMessage>
</MessageData>
-------------------------------------------------------------

Now, when my Apache::Request (or CGI::Query) object get instantiated, it
reads in the data from STDIN. I assume it expects data with the
content-type: application/x-www-form-urlencoded
as is indicative to me by the error i get in my logs:
-------------------------------------------------------------
[Wed Apr 14 10:26:12 2004] [error] [client ip-address-hidden] [libapreq]
unknown content-type: `text/xml; charset=UTF-8'
-------------------------------------------------------------

Also, doing a dumper on the query object shows content like: 

bless( {
                 '.charset' => 'ISO-8859-1',
                 '<?xml version' => [
                                      '"1.0" encoding="UTF-8"?>
<MessageData>
    <SMSMessage id="123" originatingId="123" CarrierId="1"
        MappingId="ABC" MSISDN="61404111111" MessageType="TEXT">
        <MessageText>Sample Message</MessageText>
    </SMSMessage>
</MessageData>
'
                                    ],
                 '.parameters' => [
                                    '<?xml version',
                                    'c',
                                    'mm_task'
                                  ],
                 '.fieldnames' => {},
                 'c' => [
                          'mm'
                        ],
                 'mm_task' => [
                                'api'
                              ]
               }, 'CGI' )

-------------------------------------------------------------

What i really want to do is read the raw data from STDIN
(as obviously its not in a format that Apache::Request or CGI::Query can
understand... or is it? because of the content-type set on the POST?)

Now, i don't know how to get to the STDIN, as it will have already been
read by the Apache::Request object... is there a function i can call on
the object to give me the "raw data"? 

I have tried searching the archives as i remember questions like this
coming but, up doing a search for "STDIN" on the 1.0 docs (as i use
mod_perl 1.x) did not yeild results that answered my question. 

Any help would be greatly appreciated, 

kind regards, 

simran.






-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by Tom Schindl <to...@gmx.at>.
You could use

$r->read($buf, $bytes_to_read, [$offset])

if I'm not completely wrong.

See:
http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by simran <si...@reflectit.com>.
Thanks to Tom and Geoff for your helpful hints. 

I've got a few ideas which i'm going to try based on  your ideas and
hopefully one of them will work. 

ps: Geoff, the domain www.modperlcookbook.org seems to have lost its A 
    record (modperlcookbook.org seems to be fine...). 


> simran wrote:
> >>You could use
> >>
> >>$r->read($buf, $bytes_to_read, [$offset])
> >>
> >>if I'm not completely wrong.
> >>
> >>See:
> >>http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__
> > 
> > 
> > It would work if something had not already read the data once from the
> > filehandle... and since its STDIN i don't think i can do a seek(...) on
> > teh filehandle to put it at the start again... so what i really need is
> > to be able to get the raw content from $r... 
> 
> take a look at this module
> 
> http://www.modperlcookbook.org/~geoff/modules/experimental/Apache-CachePOSTRegistry-0.01.tar.gz
> 
> I think it's doing the kind of thing you have in mind.  unfortunately,
> recent versions of CGI.pm (3.01 and higher IIRC) have changed their read
> syntax so the once cool hack no longer works for CGI.pm-based modules.  but
> the module might give you an insight on how to solve your problem.
> 
> HTH
> 
> --Geoff


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by Geoffrey Young <ge...@modperlcookbook.org>.

simran wrote:
>>You could use
>>
>>$r->read($buf, $bytes_to_read, [$offset])
>>
>>if I'm not completely wrong.
>>
>>See:
>>http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__
> 
> 
> It would work if something had not already read the data once from the
> filehandle... and since its STDIN i don't think i can do a seek(...) on
> teh filehandle to put it at the start again... so what i really need is
> to be able to get the raw content from $r... 

take a look at this module

http://www.modperlcookbook.org/~geoff/modules/experimental/Apache-CachePOSTRegistry-0.01.tar.gz

I think it's doing the kind of thing you have in mind.  unfortunately,
recent versions of CGI.pm (3.01 and higher IIRC) have changed their read
syntax so the once cool hack no longer works for CGI.pm-based modules.  but
the module might give you an insight on how to solve your problem.

HTH

--Geoff

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by Tom Schindl <to...@gmx.at>.
simran wrote:
>>But there's nobody reading the STDIN if you avoid doing something like this:
>>
>>my $cgi = new CGI();
>>my $rq = Apache::Request->new($r);
>>my $content = $r->content();
>>
>>Why would you do that if you know that none of these methods will work 
>>because you don't get a real post.
> 
> 
> I wish i could avoid the above methods being called, but unfortunately
> the application framework i am working in, calls the above methods
> (which of course cause the read from STDIN). 
> 
> My particular bit of code (in the application i am working in) is
> treated as a "plugin" and as such is passed things like the request
> object, but only after some internal pre-processing. 
> 
> My ideal situation would be if i could someone query the passed (and
> original) $r to give me the raw contents... 
> 
> 

Oh I see. So I have no idea how to solve it besides creating your own 
HTTP-Server in front of your apache. Taking the ugly POST-Request 
parsing the content and sending a real POST to your 
Apache-Framework-Solution.

1. Running an other Apache in front of your actually running Apache 
triggering the specific SMS request, all other request are simply 
redirected using RewriteRules

2. Using something like POE to create your own webserver
e.g.:
* http://poe.perl.org/?POE_Cookbook
* http://poe.perl.org/?POE_Cookbook/Web_Proxy,
* http://poe.perl.org/?POE_Cookbook/Web_Server

Tom

-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by simran <si...@reflectit.com>.
> But there's nobody reading the STDIN if you avoid doing something like this:
> 
> my $cgi = new CGI();
> my $rq = Apache::Request->new($r);
> my $content = $r->content();
> 
> Why would you do that if you know that none of these methods will work 
> because you don't get a real post.

I wish i could avoid the above methods being called, but unfortunately
the application framework i am working in, calls the above methods
(which of course cause the read from STDIN). 

My particular bit of code (in the application i am working in) is
treated as a "plugin" and as such is passed things like the request
object, but only after some internal pre-processing. 

My ideal situation would be if i could someone query the passed (and
original) $r to give me the raw contents... 




> 
> Tom
> 
> simran wrote:
> >>You could use
> >>
> >>$r->read($buf, $bytes_to_read, [$offset])
> >>
> >>if I'm not completely wrong.
> >>
> >>See:
> >>http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__
> > 
> > 
> > It would work if something had not already read the data once from the
> > filehandle... and since its STDIN i don't think i can do a seek(...) on
> > teh filehandle to put it at the start again... so what i really need is
> > to be able to get the raw content from $r... 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 



-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by Tom Schindl <to...@gmx.at>.
But there's nobody reading the STDIN if you avoid doing something like this:

my $cgi = new CGI();
my $rq = Apache::Request->new($r);
my $content = $r->content();

Why would you do that if you know that none of these methods will work 
because you don't get a real post.

Tom

simran wrote:
>>You could use
>>
>>$r->read($buf, $bytes_to_read, [$offset])
>>
>>if I'm not completely wrong.
>>
>>See:
>>http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__
> 
> 
> It would work if something had not already read the data once from the
> filehandle... and since its STDIN i don't think i can do a seek(...) on
> teh filehandle to put it at the start again... so what i really need is
> to be able to get the raw content from $r... 
> 
> 
> 
> 
> 
> 
> 
> 


-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html


Re: data from STDIN

Posted by simran <si...@reflectit.com>.
> You could use
> 
> $r->read($buf, $bytes_to_read, [$offset])
> 
> if I'm not completely wrong.
> 
> See:
> http://perl.apache.org/docs/1.0/api/Apache.html#_r_E_gt_read__buf___bytes_to_read____offset__

It would work if something had not already read the data once from the
filehandle... and since its STDIN i don't think i can do a seek(...) on
teh filehandle to put it at the start again... so what i really need is
to be able to get the raw content from $r... 








-- 
Report problems: http://perl.apache.org/bugs/
Mail list info: http://perl.apache.org/maillist/modperl.html
List etiquette: http://perl.apache.org/maillist/email-etiquette.html