Ken MacLeod는 Perl로 XML-RPC를 구현했다. 그의 웹사이트나 CPAN을 통해 Frontier::RPC 모듈을 구할 수 있다.
Frontier::RPC를 설치하려면, 패키지를 내려받고 Perl의 표준 방식으로 컴파일하면 된다:
bash$ gunzip -c Frontier-RPC-0.07b1.tar.gz | tar xvf - bash$ cd Frontier-RPC-0.07b1 bash$ perl Makefile.PL bash$ make bash$ make test bash$ su -c 'make install' |
(이 과정은 윈도우즈나 root 계정이 아닌 경우에는 조금 다를 수 있다. 자세한 것은 Perl 문서를 참고하라.)
다음의 프로그램은 Perl로 XML-RPC 서버를 호출하는 방법을 보여준다:
use Frontier::Client;
# Make an object to represent the XML-RPC server.
$server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php';
$server = Frontier::Client->new(url => $server_url);
# Call the remote server and get our result.
$result = $server->call('sample.sumAndDifference', 5, 3);
$sum = $result->{'sum'};
$difference = $result->{'difference'};
print "Sum: $sum, Difference: $difference\n"; |
다음의 프로그램은 Perl로 XML-RPC 서버를 만드는 방법을 보여준다:
use Frontier::Daemon;
sub sumAndDifference {
my ($x, $y) = @_;
return {'sum' => $x + $y, 'difference' => $x - $y};
}
# Call me as http://localhost:8080/RPC2
$methods = {'sample.sumAndDifference' => \&sumAndDifference};
Frontier::Daemon->new(LocalPort => 8080, methods => $methods)
or die "Couldn't start HTTP server: $!"; |
Frontier::RPC2는 CGI 기반의 서버 지원 기능을 내장하고 있지는 않다. 하지만 필요한 대부분 것을 구현할 수 있다.
다음의 코드를 웹서버의 cgi-bin 디렉토리에 sumAndDifference.cgi 라는 이름으로 저장하라. (유닉스 시스템에서는 chmod +x sumAndDifference.cgi로 실행 가능하도록 만들어 주어야 한다.)
#!/usr/bin/perl -w
use strict;
use Frontier::RPC2;
sub sumAndDifference {
my ($x, $y) = @_;
return {'sum' => $x + $y, 'difference' => $x - $y};
}
process_cgi_call({'sample.sumAndDifference' => \&sumAndDifference});
#==========================================================================
# CGI Support
#==========================================================================
# Simple CGI support for Frontier::RPC2. You can copy this into your CGI
# scripts verbatim, or you can package it into a library.
# (Based on xmlrpc_cgi.c by Eric Kidd <http://xmlrpc-c.sourceforge.net/>.)
# Process a CGI call.
sub process_cgi_call ($) {
my ($methods) = @_;
# Get our CGI request information.
my $method = $ENV{'REQUEST_METHOD'};
my $type = $ENV{'CONTENT_TYPE'};
my $length = $ENV{'CONTENT_LENGTH'};
# Perform some sanity checks.
http_error(405, "Method Not Allowed") unless $method eq "POST";
http_error(400, "Bad Request") unless $type eq "text/xml";
http_error(411, "Length Required") unless $length > 0;
# Fetch our body.
my $body;
my $count = read STDIN, $body, $length;
http_error(400, "Bad Request") unless $count == $length;
# Serve our request.
my $coder = Frontier::RPC2->new;
send_xml($coder->serve($body, $methods));
}
# Send an HTTP error and exit.
sub http_error ($$) {
my ($code, $message) = @_;
print <<"EOD";
Status: $code $message
Content-type: text/html
<title>$code $message</title>
<h1>$code $message</h1>
<p>Unexpected error processing XML-RPC request.</p>
EOD
exit 0;
}
# Send an XML document (but don't exit).
sub send_xml ($) {
my ($xml_string) = @_;
my $length = length($xml_string);
print <<"EOD";
Status: 200 OK
Content-type: text/xml
Content-length: $length
EOD
# We want precise control over whitespace here.
print $xml_string;
} |
이 유틸리티 루틴을 CGI 스크립트에 집어넣으면 된다.