.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "XML::Atom::Server 3pm" .TH XML::Atom::Server 3pm 2024-07-13 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME XML::Atom::Server \- A server for the Atom API .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 11 \& package My::Server; \& use base qw( XML::Atom::Server ); \& sub handle_request { \& my $server = shift; \& $server\->authenticate or return; \& my $method = $server\->request_method; \& if ($method eq \*(AqPOST\*(Aq) { \& return $server\->new_post; \& } \& ... \& } \& \& my %Passwords; \& sub password_for_user { \& my $server = shift; \& my($username) = @_; \& $Passwords{$username}; \& } \& \& sub new_post { \& my $server = shift; \& my $entry = $server\->atom_body or return; \& ## $entry is an XML::Atom::Entry object. \& ## ... Save the new entry ... \& } \& \& package main; \& my $server = My::Server\->new; \& $server\->run; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\fIXML::Atom::Server\fR provides a base class for Atom API servers. It handles all core server processing, both the SOAP and REST formats of the protocol, and WSSE authentication. It can also run as either a mod_perl handler or as part of a CGI program. .PP It does not provide functions specific to any particular implementation, such as posting an entry, retrieving a list of entries, deleting an entry, etc. Implementations should subclass \fIXML::Atom::Server\fR, overriding the \&\fIhandle_request\fR method, and handle all functions such as this themselves. .SH SUBCLASSING .IX Header "SUBCLASSING" .SS "Request Handling" .IX Subsection "Request Handling" Subclasses of \fIXML::Atom::Server\fR must override the \fIhandle_request\fR method to perform all request processing. The implementation must set all response headers, including the response code and any relevant HTTP headers, and should return a scalar representing the response body to be sent back to the client. .PP For example: .PP .Vb 8 \& sub handle_request { \& my $server = shift; \& my $method = $server\->request_method; \& if ($method eq \*(AqPOST\*(Aq) { \& return $server\->new_post; \& } \& ## ... handle GET, PUT, etc \& } \& \& sub new_post { \& my $server = shift; \& my $entry = $server\->atom_body or return; \& my $id = save_this_entry($entry); ## Implementation\-specific \& $server\->response_header(Location => $server\->uri . \*(Aq/entry_id=\*(Aq . $id); \& $server\->response_code(201); \& $server\->response_content_type(\*(Aqapplication/x.atom+xml\*(Aq); \& return serialize_entry($entry); ## Implementation\-specific \& } .Ve .SS Authentication .IX Subsection "Authentication" Servers that require authentication for posting or retrieving entries or feeds should override the \fIpassword_for_user\fR method. Given a username (from the WSSE header), \fIpassword_for_user\fR should return that user's password in plaintext. This will then be combined with the nonce and the creation time to generate the digest, which will be compared with the digest sent in the WSSE header. If the supplied username doesn't exist in your user database or alike, just return \f(CW\*(C`undef\*(C'\fR. .PP For example: .PP .Vb 6 \& my %Passwords = ( foo => \*(Aqbar\*(Aq ); ## The password for "foo" is "bar". \& sub password_for_user { \& my $server = shift; \& my($username) = @_; \& $Passwords{$username}; \& } .Ve .SH METHODS .IX Header "METHODS" \&\fIXML::Atom::Server\fR provides a variety of methods to be used by subclasses for retrieving headers, content, and other request information, and for setting the same on the response. .SS "Client Request Parameters" .IX Subsection "Client Request Parameters" .IP \(bu 4 \&\f(CW$server\fR\->uri .Sp Returns the URI of the Atom server implementation. .IP \(bu 4 \&\f(CW$server\fR\->request_method .Sp Returns the name of the request method sent to the server from the client (for example, \f(CW\*(C`GET\*(C'\fR, \f(CW\*(C`POST\*(C'\fR, etc). Note that if the client sent the request in a SOAP envelope, the method is obtained from the \fISOAPAction\fR HTTP header. .IP \(bu 4 \&\f(CW$server\fR\->request_header($header) .Sp Retrieves the value of the HTTP request header \fR\f(CI$header\fR\fI\fR. .IP \(bu 4 \&\f(CW$server\fR\->request_content .Sp Returns a scalar containing the contents of a POST or PUT request from the client. .IP \(bu 4 \&\f(CW$server\fR\->request_param($param) .Sp \&\fIXML::Atom::Server\fR automatically parses the PATH_INFO sent in the request and breaks it up into key-value pairs. This can be used to pass parameters. For example, in the URI .Sp .Vb 1 \& http://localhost/atom\-server/entry_id=1 .Ve .Sp the \fIentry_id\fR parameter would be set to \f(CW1\fR. .Sp \&\fIrequest_param\fR returns the value of the value of the parameter \fR\f(CI$param\fR\fI\fR. .SS "Setting up the Response" .IX Subsection "Setting up the Response" .IP \(bu 4 \&\f(CW$server\fR\->response_header($header, \f(CW$value\fR) .Sp Sets the value of the HTTP response header \fR\f(CI$header\fR\fI\fR to \fI\fR\f(CI$value\fR\fI\fR. .IP \(bu 4 \&\f(CW$server\fR\->response_code([ \f(CW$code\fR ]) .Sp Returns the current response code to be sent back to the client, and if \&\fR\f(CI$code\fR\fI\fR is given, sets the response code. .IP \(bu 4 \&\f(CW$server\fR\->response_content_type([ \f(CW$type\fR ]) .Sp Returns the current \fIContent-Type\fR header to be sent back to the client, and \&\fR\f(CI$type\fR\fI\fR is given, sets the value for that header. .SS "Processing the Request" .IX Subsection "Processing the Request" .IP \(bu 4 \&\f(CW$server\fR\->authenticate .Sp Attempts to authenticate the request based on the authentication information present in the request (currently just WSSE). This will call the \fIpassword_for_user\fR method in the subclass to obtain the cleartext password for the username given in the request. .IP \(bu 4 \&\f(CW$server\fR\->atom_body .Sp Returns an \fIXML::Atom::Entry\fR object containing the entry sent in the request. .SH USAGE .IX Header "USAGE" Once you have defined your server subclass, you can set it up either as a CGI program or as a mod_perl handler. .PP A simple CGI program would look something like this: .PP .Vb 2 \& #!/usr/bin/perl \-w \& use strict; \& \& use My::Server; \& my $server = My::Server\->new; \& $server\->run; .Ve .PP A simple mod_perl handler configuration would look something like this: .PP .Vb 5 \& PerlModule My::Server \& \& SetHandler perl\-script \& PerlHandler My::Server \& .Ve .SH "ERROR HANDLING" .IX Header "ERROR HANDLING" If you wish to return an error from \fIhandle_request\fR, you can use the built-in \fIerror\fR method: .PP .Vb 5 \& sub handle_request { \& my $server = shift; \& ... \& return $server\->error(500, "Something went wrong"); \& } .Ve .PP This will be returned to the client with a response code of 500 and an error string of \f(CW\*(C`Something went wrong\*(C'\fR. Errors are automatically serialized into SOAP faults if the incoming request is enclosed in a SOAP envelope. .SH "AUTHOR & COPYRIGHT" .IX Header "AUTHOR & COPYRIGHT" Please see the \fIXML::Atom\fR manpage for author, copyright, and license information.