Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Handling XML content with HTTP::Daemon

by hackmare (Pilgrim)
on Jul 09, 2002 at 11:27 UTC ( [id://180448]=perlquestion: print w/replies, xml ) Need Help??

hackmare has asked for the wisdom of the Perl Monks concerning the following question:

Bretheren,

I am trying to use HTTP::Daemon to return an XML snippet (SVG) generated with SVG.pm.

I am finding myself unable to return the complete XML message when I generate it. When I print the content of my XML to log, I get the entire, fully-qualified string. However, when I try to implement

$c->send_response($string);

where $string is a complete XML message, I only get the last snippet of XML (3-4 lines of comments at the bottom of the file.

So my Big Question is, how do I send XML content using HTTP::Daemon ? I'd appreciate any help on this...

use HTTP::Daemon; use HTTP::Status; use SVG; my $d = new HTTP::Daemon; print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { my $svg = SVG->new(width=>'100%',height=>'100%'); my $g = $svg->group(id=>'group-1'); $g->circle(id=>'circle-1', cx=>int(rand(400)), cy=>int(rand(400)), r=>int(rand(100)),); if ($r->method eq 'GET') { my $path = $r->url->path; $path =~ /\/(.+)/; $sessionid = $1 || '00'; $g->text(x=>int(rand(400)), y=>int(rand(400))) ->cdata($sessionid); $c->send_response($svg->xmlify()); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }

hackmare.

Replies are listed 'Best First'.
Re: Handling XML content with HTTP::Daemon
by Matts (Deacon) on Jul 09, 2002 at 12:31 UTC
    Looks like you're forgetting to send headers. Try:
    $c->send_response( "Content-type: text/xml+svg\r\n\r\n". $svg->xmlify() );

      Thanks a lot for pointing out what should have been obvious!, that got me a long way towards my goal. Now I just need to cleanly get rid of the following footer which seems to get appended to my file each time, confusing the XML parsers:

      ...the response... <!-- my comments... followed by two offending lines. --> Date: Tue, 09 Jul 2002 13:17:37 GMT Server: libwww-perl-daemon/1.21

      When I go into HTTP::Daemon and comment out these two lines:

      #(from HTTP::Daemon line 563) sub send_basic_header { my $self = shift; return if $self->antique_client; $self->send_status_line(@_); #taken out print $self "Date: ", time2str(time), $CRLF; my $product = $self->daemon->product_tokens; #taken out print $self "Server: $product$CRLF" if $product; }

      then the offending footers dissapear. Do you know what these two footer lines do, and should they not be in the header rather than in the footer? I can't find any way to override them other than by removing them from the module.

      ps. you had the wrong mime type in your solution (just to be clear about that). The SVG mimetype is image/svg+xml

      $c->send_response( "Content-type: image/svg+xml\r\n\r\n". $svg->xmlify());

      hackmare.
        Ah, HTTP::Daemon expects a result code too. So you need:
        $c->send_response( 200, "Content-type: image/svg+xml\r\n\r\n". $svg->xmlify() );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://180448]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found