A framework for building restful web-applications.
# MyRESTApp REST::Application instance / mod_perl handler package MyRESTApp; use Apache; use Apache::Constants qw(:common); sub handler { _\|_PACKAGE_\|_->new(request => $r)->run(); return OK; } sub getMatchText { return Apache->uri } sub setup { my $self = shift; $self->resourceHooks( qr{/rest/parts/(\d+)} => 'get_part', # ... other handlers here ... ); } sub get_part { my ($self, $part_num) = @_; # Business logic to retrieve part num } # Apache conf <Location /rest> perl-script .cgi PerlHandler MyRESTApp </Location>
This module acts as a base class for applications which implement a RESTful interface. When an \s-1HTTP\s0 request is received some dispatching logic in REST::Application is invoked, calling different handlers based on what the kind of \s-1HTTP\s0 request it was (i.e. \s-1GET\s0, \s-1PUT\s0, etc) and what resource it was trying to access. This module won't ensure that your \s-1API\s0 is RESTful but hopefully it will aid in developing a \s-1REST\s0 \s-1API\s0.
The following list describes the basic way this module is intended to be used. It does not capture everything the module can do.
Subclass REST::Application, i.e. \*(C`use base 'REST::Application'\*(C'. Overload the \*(C`setup()\*(C' method and set up some resource hooks with the \*(C`resourceHooks()\*(C' method. Hooks are mappings of the form: REGEX => handler where handler is either a method name, a code reference, an object which supports a method with the same name as the \s-1HTTP\s0 method (or \*(C`getResource\*(C' if no such method), or a reference to an array of the form: \*(C`[$objectRef, "methodName"]\*(C' ($objectRef can be a class name instead). The regular expressions are applied, by default, to the path info of the \s-1HTTP\s0 request. Anything captured by parens in the regex will be passed into the handler as arguments. For example: qr{/parts/(\d+)$} => "getPartByNumber", The above hook will call a method named \*(C`getPartByNumber\*(C' on the current object (i.e. $self, an instance of REST::Application) if the path info of the requested \s-1URI\s0 matches the above regular expression. The first argument to the method will be the part number, since that's the first element captured in the regular expression.
Write the code for the handler specified above. So here we'd define the \*(C`getPartByNumber\*(C' method.
Create an Apache handler, for example: use MyRESTApp; sub handler { my $r = shift; my $app = MyRESTApp->new(request => $r); $app->run(); } or a small \s-1CGI\s0 script with the following code: #!/usr/bin/perl use MyRESTApp; MyRESTApp->new()->run(); In the second case, for a \s-1CGI\s0 script, you'll probably need to do something special to get Apache to load up your script unless you give it a .cgi extension. It would be unRESTful to allow your script to have a .cgi extension, so you should go the extra mile and configure Apache to run your script without it. For example, it'd be bad to have your users go to: http://www.foo.tld/parts.cgi/12345.html When the \*(C`run()\*(C' method is called the path info is extracted from the \s-1HTTP\s0 request. The regexes specified in step 2 are processed, in order, and if one matches then the handler is called. If the regex had paren. matching then the matched elements are passed into the handler. A handler is also passed a copy of the REST::Application object instance (except for the case when the handler is a method on the REST::Application object, in that case it'd be redundant). So, when writing a subroutine handler you'd do: sub rest_handler { my ($rest, @capturedArgs) = @_; ... }
The handler is processed and should return a string or a scalar reference to a string. Optionally the handler should set any header information via the \*(C`header()\*(C' method on instance object pased in.
The REST::Application base class provides a good number of methods, each of which can be overloaded. By default you only need to overload the \*(C`setup()\*(C' method but you may wish to overload others. To help with this the following outline is the calling order of the various methods in the base class. You can find detailed descriptions of each method in the \s-1METHODS\s0 section of this document.
If a method is followed by the string \s-1NOOP\s0 then that means it does nothing by default and it exists only to be overloaded.
new() setup() - NOOP run() preRun() - NOOP loadResource() getMatchText() getPathInfo() query() defaultQueryObject() defaultResourceHandler() - NOOP resourceHooks() checkMatch() _setLastRegexMatches() _getHandlerFromHook() resourceHooks() defaultResourceHandler() - NOOP getRequestMethod() query() defaultQueryObject() bestContentType() simpleContentNegotiation getContentPrefs getAcceptHeader scoreType() callHandler() getHandlerArgs _getLastRegexMatches() extraHandlerArgs() preHandler() - NOOP ... your handler called here ... postHandler() - NOOP postRun() - NOOP getHeaders() headerType() query() defaultQueryObject() header() addRepresentation()
The only methods not called as part of the new() or run() methods are the helper methods \*(C`resetHeader()\*(C' and \*(C`setRedirect()\*(C', both of which call the \*(C`header()\*(C' and \*(C`headerType()\*(C' methods.
For example, if you wanted to have your code branch on the entire \s-1URI\s0 of the \s-1HTTP\s0 request rather than just the path info you'd merely overload \*(C`getMatchText()\*(C' to return the \s-1URI\s0 rather than the path info.
\$1
This method creates a new REST::Application object and returns it. The arguments passed in via %args, if any, are passed untouched to the \*(C`setup()\*(C' method. This accessor/mutator retrieves the current \s-1CGI\s0 query object or sets it if one is passed in. This method retrieves/sets the default query object. This method is called if \*(C`query()\*(C' is called for the first time and no query object has been set yet. This method is used to set the resource hooks. A REST::Application hook is a regex to handler mapping. The hooks are passed in as a hash (or a reference to one) and the keys are treated as regular expressions while the values are treated as handlers should \s-1PATH_INFO\s0 match the regex that maps to that handler.
Handlers can be code references, methods on the current object, methods on other objects, or class methods. Also, handlers can be differ based on what the \s-1REQUEST_METHOD\s0 was (e.g. \s-1GET\s0, \s-1PUT\s0, \s-1POST\s0, \s-1DELETE\s0, etc).
The handler's types are as follows:
The handler is considered to be a method on the current REST::Application instance.
The code ref is considered to be the handler.
The object is considered to have a method the same name as the \s-1HTTP\s0 method. That is, if the object is being called because of \s-1GET\s0 then \*(C`GET()\*(C' is called, if it is called because of a \*(C`DELETE\*(C' then \*(C`DELETE()\*(C' is called. \*(C`getResource()\*(C' method will be used if \*(C`getRequestMethod()\*(C' returns false.
The array is expected to be two elements long, the first element is a class name or object instance. The 2nd element is a method name on that class/instance. \s-1IF\s0 the 2nd element is ommitted then the method name is assumed to be the same as the \s-1REQUEST_METHOD\s0, e.g. \*(C`GET()\*(C', \*(C`PUT()\*(C', whatever.
The current \s-1REQUEST_METHOD\s0 is used as a key to the hash, the value should be one the four above handler types. In this way you can specify different handlers for each of the request types. The request method can also be specified as '*', in which case that is used if a more specific match is not found. It is possible for the value of the handler to be another hash ref, rather than one of the four above types. In this case it is assumed content-negotion is wanted. The keys of this second hash are \s-1MIME\s0 types and the values are one of the four above types. For example: $self->resourceHooks( qr{/parts/(\d+)} => { GET => { 'text/json' => 'get_json', 'text/xml', => 'get_xml', 'text/xml' => 'get_html', '*/*' => 'get_html', }, '*' => sub { die "Bad Method!" }, } );
The return value of the handler is expected to be a string, which REST::Application will then send to the browser with the \*(C`sendRepresentation()\*(C' method.
If no argument is supplied to \*(C`resourceHooks()\*(C' then the current set of hooks is returned. The returned hash referces is a tied IxHash, so the keys are kept sorted. This method will take the value of \s-1PATH_INFO\s0, iterate through the path regex's set in \*(C`resourceHooks()\*(C' and if it finds a match call the associated handler and return the handler's value, which should be a scalar. If $path is passed in then that is used instead of \s-1PATH_INFO\s0. This method calls \*(C`loadResource()\*(C' with no arguments and then takes that output and sends it to the remote client. Headers are sent with \*(C`sendHeaders()\*(C' and the representation is sent with \*(C`sendRepresentation()\*(C'.
If the environment variable \s-1REST_APP_RETURN_ONLY\s0 is set then output isn't sent to the client. The return value of this method is the text output it sends (or would've sent). This method returns the headers as a string. This method just returns $representation. It is provided solely for overloading purposes. This accessor/mutator controls the type of header to be returned. This method returns one of \*(L"header, redirect, or none.\*(R" If $type is passed in then that is used to set the header type. This accessor/mutator controls the header values sent. If called without arguments then it simply returns the current header values as a hash, where the keys are the header fields and the values are the header field values.
If this method is called multiple times then the values of %args are additive. So calling \*(C`$self->header(-type => 'text/html')\*(C' and \*(C`$self->header(-foo => 'bar')\*(C' results in both the content-type header being set and the \*(L"foo\*(R" header being set. This header causes the current header values to be reset. The previous values are returned. This method is called by \*(C`loadResource()\*(C' if no regex in \*(C`resourceHooks()\*(C' matches the current \s-1PATH_INFO\s0. It returns undef by default, it exists for overloading. Given a list of \s-1MIME\s0 types this function returns the best matching type considering the Accept header of the current request (as returned by \*(C`getAcceptHeader()\*(C'). Given a list of \s-1MIME\s0 types this function returns the same list sorted from best match to least considering the Accept header as returned by \*(C`getAcceptHeader()\*(C'. Returns the list of \s-1MIME\s0 types in the Accept header form most preferred to least preferred. Quality weights are taken into account. Returns the value of the Accept header as a single string. Returns an integer, only good for sorting, for where $type fits among the @accept_types. This method takes wildcards into account. So \*(C`text/plain\*(C' matches \*(C`text/*\*(C'. The integer returned is the position in @accept_types of the matching \s-1MIME\s0 type. It assumped @accept_types is already sorted from best to worst. Returns the last path passed to \*(C`checkMatch()\*(C' which successfully matched against. Unless you're overloading things in funny ways the value returned will be the path that caused the current handler to be invoked. Similar to \*(C`getLastMatchPath()\*(C' except this is the pattern that was applied to the path. This method tries to be smart and allow tunneling of the other \s-1HTTP\s0 methods over \s-1GET\s0 or \s-1PUT\s0. You can tunnel three ways with the higher up taking precedence:
1) Pass an X-HTTP-Method header 2) Pass the 'http_method' query parameter 3) Pass a parameter via \s-1POST\s0
Only \s-1POST\s0 and \s-1GET\s0, being the most common, can be used to tunnel. In an attempt to prevent people from being bad, \s-1GET\s0 can only be used to tunnel \s-1GET\s0 or \s-1HEAD\s0. \s-1POST\s0 can be used to tunnel anything.
Matthew O'Connor <[email protected]>
This program is free software. It is subject to the same license as Perl itself.
\s-1CGI\s0, CGI::Application, Tie::IxHash, CGI::Application::Dispatch