GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Web::Dispatch::HTTPMethods(3) User Contributed Perl Documentation Web::Dispatch::HTTPMethods(3)

Web::Dispatch::HTTPMethods - Helpers to make RESTFul Dispatchers Easier

    package MyApp:WithHTTPMethods;

    use Web::Simple;
    use Web::Dispatch::HTTPMethods;

    sub as_text {
      [200, ['Content-Type' => 'text/plain'],
        [$_[0]->{REQUEST_METHOD}, $_[0]->{REQUEST_URI}] ]
    }

    sub dispatch_request {
      sub (/get) {
        GET { as_text(pop) }
      },
      sub (/get-head) {
        GET { as_text(pop) }
        HEAD { [204,[],[]] },
      },
      sub (/get-post-put) {
        GET { as_text(pop) }  ## NOTE: no commas separating http methods
        POST { as_text(pop) }
        PUT { as_text(pop) }
      },
    }

Exports the most commonly used HTTP methods as subroutine helpers into your Web::Simple based application. Use of these methods additionally adds an automatic HTTP code 405 "Method Not Allowed" response if none of the HTTP methods match for a given dispatch and also adds a dispatch rule for "HEAD" if no "HEAD" exists but a "GET" does (in which case the "HEAD" returns the "GET" dispatch with an empty body.)

We also add support at the end of the chain for the OPTIONS method. This defaults to HTTP 200 OK + Allows http headers.

We also try to set correct HTTP headers such as "Allows" as makes sense based on your dispatch chain.

The following dispatch chains are basically the same:

    sub dispatch_request {
      sub (/get-http-methods) {
        GET { [200, ['Content-Type' => 'text/plain'], ['Hello World']] }
      },
      sub(/get-classic) {
        sub (GET) { [200, ['Content-Type' => 'text/plain'], ['Hello World']] },
        sub (HEAD)  { [200, ['Content-Type' => 'text/plain'], []] },
        sub (OPTIONS)  {
          [200, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'], []];
        },
        sub () {
          [405, ['Content-Type' => 'text/plain', Allows=>'GET,HEAD,OPTIONS'], 
           ['Method Not Allowed']]
        },
      }
    }

The idea here is less boilerplate to distract the reader from the main point of the code and also to encapsulate some best practices.

NOTE You currently cannot mix http method style and prototype sub style in the same scope, as in the following example:

    sub dispatch_request {
      sub (/get-head) {
        GET { ... }
        sub (HEAD) { ... }
      },
    }

If you try this our code will notice and issue a "die". If you have a good use case please bring it to the authors.

This automatically exports the following subroutines:

    GET
    PUT
    POST
    HEAD
    DELETE
    OPTIONS

See Web::Simple for AUTHOR

See Web::Simple for CONTRIBUTORS

See Web::Simple for COPYRIGHT

See Web::Simple for LICENSE
2014-07-11 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.