parsing canned response 
Author Message
 parsing canned response

Hello,

I have a form that posts data to a server.  It returns with a canned
response. (I grab the page with LWP.)  Amongst the garbage spit back are
multiple instances of Content-Type and Content-Length.  The idea is to
separate that data from the rest and save those variables.

I have successfully separated (split) the response by its content
boundaries and saved them to an array in which I can call the individual
"slices." (which contain the content and length values). But I cannot
figure out how I can take the new slices and extract out only the
Content-Type and Content-Length fields into name-value pairs!

Can anyone give me any advice on how I can take these slices from the
canned response and split them into the pairs?

Thanks for your input : )

Mike Gardner

http://www.*-*-*.com/



Wed, 28 May 2003 06:47:20 GMT  
 parsing canned response

Quote:

>I have a form that posts data to a server.  It returns with a canned
>response. (I grab the page with LWP.)  Amongst the garbage spit back are
>multiple instances of Content-Type and Content-Length.  The idea is to
>separate that data from the rest and save those variables.

>I have successfully separated (split) the response by its content
>boundaries and saved them to an array in which I can call the individual
>"slices." (which contain the content and length values). But I cannot
>figure out how I can take the new slices and extract out only the
>Content-Type and Content-Length fields into name-value pairs!

>Can anyone give me any advice on how I can take these slices from the
>canned response and split them into the pairs?

Maybe I don't understand the question, but this simple example can split
HTTP headers fed to it into name value pairs.  Are you getting stuck on
how to split or how to pattern search the header lines from non-header
lines (the headers end at the first blank line)?

#!/usr/bin/perl -w
while(<STDIN>) {
    chomp;
    ($name,$value) = split /:\s+/, $_;
    print "$name = $value\n";

Quote:
}

--

http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


Wed, 28 May 2003 10:58:39 GMT  
 parsing canned response

Quote:

>I have successfully separated (split) the response by its content
>boundaries and saved them to an array in which I can call the individual
>"slices." (which contain the content and length values). But I cannot
>figure out how I can take the new slices and extract out only the
>Content-Type and Content-Length fields into name-value pairs!

>Can anyone give me any advice on how I can take these slices from the
>canned response and split them into the pairs?

I forgot about possible carriage returns.  So my example should have been:

#!/usr/bin/perl -w
while(<STDIN>) {
    s/\015|\012//g;
    ($name,$value) = split /:\s+/, $_;
    print "$name = $value\n";

Quote:
}

--

http://www.autox.chicago.il.us/  http://www.berniesfloral.net/
http://cgi-help.virtualave.net/  http://hammer.prohosting.com/~cgi-wiz/


Wed, 28 May 2003 11:13:15 GMT  
 parsing canned response

Quote:

>I have a form that posts data to a server.  It returns with a canned
>response. (I grab the page with LWP.)  Amongst the garbage spit back are
>multiple instances of Content-Type and Content-Length.  The idea is to
>separate that data from the rest and save those variables.

HTTP headers are very much like e-mail headers. Er... guess what: HTTP
headers ARE e-mail headers. So treat them as such. Randal once wrote a
very clever way of stuffing e-mail headers into a hash. I'd do that, and
get out the hash entries you're interested in. Only, I can only vaguely
remember how it went. I bet it's in the standard documentation
somewhere.

E-mail headers may contain CRLF pairs. Simplest is to drop the returns,
and keep the newlines:

        tr/\r//d;

Secondly, an e-mail header may be wrapped. In that case, the continued
line starts with whitespace (space/tab). So get rid of those.

        s/\n[ \t]+/ /g;

And finally: split on newlines, and then on colons (plus whitespace).
This is what I can make of what I remember of Randal's code:

        %header = map { split /: */, $_, 2 } split /\n/;

To be on the safe side, the headers (hash keys) should have been turned
into consistent case. Titlecase (every word uppercased) is the common
thing.

        %header = map { my($key, $value) = split /: */, $_, 2;
          $key =~ s/(\w+)/\u\L/g; ($key, $value) } split /\n/;

A simple lc($key) or uc($key) would have worked just as well, really.

--
        Bart.



Wed, 28 May 2003 19:15:57 GMT  
 parsing canned response
All of this data returned back (canned response garbage, content-type and
content-length data) ends up in the browser body.  Does this make a
difference to the approach?

Thanks again,

Mike Gardner

http://www.a2zcables.com

Quote:


> >I have a form that posts data to a server.  It returns with a canned
> >response. (I grab the page with LWP.)  Amongst the garbage spit back are
> >multiple instances of Content-Type and Content-Length.  The idea is to
> >separate that data from the rest and save those variables.

> HTTP headers are very much like e-mail headers. Er... guess what: HTTP
> headers ARE e-mail headers. So treat them as such. Randal once wrote a
> very clever way of stuffing e-mail headers into a hash. I'd do that, and
> get out the hash entries you're interested in. Only, I can only vaguely
> remember how it went. I bet it's in the standard documentation
> somewhere.

> E-mail headers may contain CRLF pairs. Simplest is to drop the returns,
> and keep the newlines:

>         tr/\r//d;

> Secondly, an e-mail header may be wrapped. In that case, the continued
> line starts with whitespace (space/tab). So get rid of those.

>         s/\n[ \t]+/ /g;

> And finally: split on newlines, and then on colons (plus whitespace).
> This is what I can make of what I remember of Randal's code:

>         %header = map { split /: */, $_, 2 } split /\n/;

> To be on the safe side, the headers (hash keys) should have been turned
> into consistent case. Titlecase (every word uppercased) is the common
> thing.

>         %header = map { my($key, $value) = split /: */, $_, 2;
>           $key =~ s/(\w+)/\u\L/g; ($key, $value) } split /\n/;

> A simple lc($key) or uc($key) would have worked just as well, really.

> --
>         Bart.



Wed, 28 May 2003 23:26:52 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Receive Authorize.NET AIM response from SSL port 443 and Parse

2. parse response from system query - too many ways to do it

3. Receive Authorize.NET AIM response from SSL port 443 and Parse

4. Parse HTTP RESPONSE Headers Active Perl CGI

5. Errors in HTTP/Response & LWP/Response

6. question about canned perl script

7. Troubles with a Canned script

8. canned reply script

9. canned reply??

10. canned replies

11. HTTP::Response - how to read 'set-cookie' attribute from response header?

12. newbie...To parse or not to parse

 

 
Powered by phpBB® Forum Software