[ale] Implementing XML-RPC

Christopher Fowler cfowler at outpostsentinel.com
Fri Feb 11 09:47:36 EST 2005


Here is the constructor for the Frontier client:

----- Cut Here -----
sub new {
    my $class = shift;
    my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
                                                                                                                             
    bless $self, $class;
                                                                                                                             
    die "Frontier::RPC::new: no url defined\n"
        if !defined $self->{'url'};
                                                                                                                             
    $self->{'ua'} = LWP::UserAgent->new;
    $self->{'ua'}->proxy('http', $self->{'proxy'})
        if(defined $self->{'proxy'});
    $self->{'rq'} = HTTP::Request->new (POST => $self->{'url'});
    $self->{'rq'}->header('Content-Type' => 'text/xml');
                                                                                                                             
    my @options;
                                                                                                                             
    if(defined $self->{'encoding'}) {
        push @options, 'encoding' => $self->{'encoding'};
    }
                                                                                                                             
    if (defined $self->{'use_objects'} && $self->{'use_objects'}) {
        push @options, 'use_objects' => $self->{'use_objects'};
    }
                                                                                                                             
    $self->{'enc'} = Frontier::RPC2->new(@options);
                                                                                                                             
    return $self;
}
----- Cut Here -----

I think I'll modify it to accept a LWP::UserAgentboject instead of
creating a new one.  That will allow me to pass the cookie jar over. 
Basically to do form authentication on our device is this simple:

----- Cut Here -----
my $ua = LWP::UserAgent->new();
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
my $response = $ua->post("http://192.168.2.120/cgi-bin/login",
        { 'user' => "root", 'pass' => "password" });
----- Cut Here -----

This modification will allow me to pass over the LWP object that has
authenticated into our web site.  What do you think?  would modifying
the constructor to accept a LWP::UserAgent object be a good idea to get
past the form based authentication the site forces on a user?


On Fri, 2005-02-11 at 08:52, Christopher Fowler wrote:
> Thanks for all your help.  I happened to have the O'Reilly book but it
> was hidden by other books on my shelf.  Late last night I found it.  It
> is brief but is providing me some answers.  I've been able to create
> some code on our device from scratch that is working great with the
> Frontier client.  I've not done the XML parsing on the device or created
> any methods but I'm working on that today and next week.
> 
> 
> Has anyone used the Frontier client?
> 
> --- Test Code ---
> use Frontier::Client;
> $server = Frontier::Client->new( debug => 1, url =>
> 'https://192.168.2.120/cgi-bin/xml-rpc' );
> $result = $server->call('restart', ("now"));
> print "$result\n";
> --- Test Code ---
> 
> Here is the problem.  It works fine when the code is in stand alone
> debug mode.  But I'm not doing any authentication. Instead of using
> a string as the 'url' in the contstructor I need to use LWP to login to
> the site to get a valid cookie.  If I place this code in the production
> site without a valid cookie I'll simply be redirected to
> '/cgi-bin/login' .  I'm not really planning on using Frontier for
> production code.  The PERL in this case is all test code.  J2EE will be
> doing the interfacing with the remote device.  I'll eventually create a
> device object in Perl that will use XML-RPC to execute remote commands
> but that is not important now.  I'm just curious as to how I can use
> Frontier with my device since all pages on my device require form based
> authentication.
> 
> Thanks,
> Chris
> 
>   
> On Fri, 2005-02-11 at 08:13, Fletch wrote:
> > >>>>> "Christopher" == Christopher Fowler <cfowler at outpostsentinel.com> writes:
> > 
> > [...]
> > 
> >     Christopher> I think that is right.  I can not seem to find info
> >     Christopher> on how to return errors.  Simply like what XML do I
> >     Christopher> return if for example a method is called that does
> >     Christopher> not exist on the server?
> > 
> > I can't recall offhand (I want to say it's something like
> > <error><code>#</code><message>...</message></error>), but if you
> > install the Perl RPC::XML module (or use the Python one which is stock
> > post 2.3 I think, or the Ruby one that's stock post 1.8.2 :) and send
> > a simple server a bogus request you should be able to see what those
> > send back.
> 
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://www.ale.org/mailman/listinfo/ale



More information about the Ale mailing list