home *** CD-ROM | disk | FTP | other *** search
/ ftp.f-secure.com / 2014.06.ftp.f-secure.com.tar / ftp.f-secure.com / support / hotfix / fsis / IS-SpamControl.fsfix / iufssc / lib / HTTP / Status.pm < prev   
Text File  |  2006-11-29  |  7KB  |  235 lines

  1. package HTTP::Status;
  2.  
  3. # $Id: Status.pm 2397 2005-12-23 13:06:15Z kankri $
  4.  
  5. use strict;
  6. require 5.002;   # becase we use prototypes
  7.  
  8. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
  9.  
  10. require Exporter;
  11. @ISA = qw(Exporter);
  12. @EXPORT = qw(is_info is_success is_redirect is_error status_message);
  13. @EXPORT_OK = qw(is_client_error is_server_error);
  14. $VERSION = sprintf("%d.%02d", q$Revision: 2397 $ =~ /(\d+)\.(\d+)/);
  15.  
  16. # Note also addition of mnemonics to @EXPORT below
  17.  
  18. my %StatusCode = (
  19.     100 => 'Continue',
  20.     101 => 'Switching Protocols',
  21.     102 => 'Processing',                      # WebDAV
  22.     200 => 'OK',
  23.     201 => 'Created',
  24.     202 => 'Accepted',
  25.     203 => 'Non-Authoritative Information',
  26.     204 => 'No Content',
  27.     205 => 'Reset Content',
  28.     206 => 'Partial Content',
  29.     207 => 'Multi-Status',                    # WebDAV
  30.     300 => 'Multiple Choices',
  31.     301 => 'Moved Permanently',
  32.     302 => 'Found',
  33.     303 => 'See Other',
  34.     304 => 'Not Modified',
  35.     305 => 'Use Proxy',
  36.     307 => 'Temporary Redirect',
  37.     400 => 'Bad Request',
  38.     401 => 'Unauthorized',
  39.     402 => 'Payment Required',
  40.     403 => 'Forbidden',
  41.     404 => 'Not Found',
  42.     405 => 'Method Not Allowed',
  43.     406 => 'Not Acceptable',
  44.     407 => 'Proxy Authentication Required',
  45.     408 => 'Request Timeout',
  46.     409 => 'Conflict',
  47.     410 => 'Gone',
  48.     411 => 'Length Required',
  49.     412 => 'Precondition Failed',
  50.     413 => 'Request Entity Too Large',
  51.     414 => 'Request-URI Too Large',
  52.     415 => 'Unsupported Media Type',
  53.     416 => 'Request Range Not Satisfiable',
  54.     417 => 'Expectation Failed',
  55.     422 => 'Unprocessable Entity',            # WebDAV
  56.     423 => 'Locked',                          # WebDAV
  57.     424 => 'Failed Dependency',               # WebDAV
  58.     500 => 'Internal Server Error',
  59.     501 => 'Not Implemented',
  60.     502 => 'Bad Gateway',
  61.     503 => 'Service Unavailable',
  62.     504 => 'Gateway Timeout',
  63.     505 => 'HTTP Version Not Supported',
  64.     507 => 'Insufficient Storage',            # WebDAV
  65. );
  66.  
  67. my $mnemonicCode = '';
  68. my ($code, $message);
  69. while (($code, $message) = each %StatusCode) {
  70.     # create mnemonic subroutines
  71.     $message =~ tr/a-z \-/A-Z__/;
  72.     $mnemonicCode .= "sub RC_$message () { $code }\t";
  73.     # make them exportable
  74.     $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
  75. }
  76. # warn $mnemonicCode; # for development
  77. eval $mnemonicCode; # only one eval for speed
  78. die if $@;
  79.  
  80. # backwards compatibility
  81. *RC_MOVED_TEMPORARILY = \&RC_FOUND;  # 302 was renamed in the standard
  82. push(@EXPORT, "RC_MOVED_TEMPORARILY");
  83.  
  84.  
  85. sub status_message  ($) { $StatusCode{$_[0]}; }
  86.  
  87. sub is_info         ($) { $_[0] >= 100 && $_[0] < 200; }
  88. sub is_success      ($) { $_[0] >= 200 && $_[0] < 300; }
  89. sub is_redirect     ($) { $_[0] >= 300 && $_[0] < 400; }
  90. sub is_error        ($) { $_[0] >= 400 && $_[0] < 600; }
  91. sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
  92. sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
  93.  
  94. 1;
  95.  
  96.  
  97. __END__
  98.  
  99. =head1 NAME
  100.  
  101. HTTP::Status - HTTP Status code processing
  102.  
  103. =head1 SYNOPSIS
  104.  
  105.  use HTTP::Status;
  106.  
  107.  if ($rc != RC_OK) {
  108.      print status_message($rc), "\n";
  109.  }
  110.  
  111.  if (is_success($rc)) { ... }
  112.  if (is_error($rc)) { ... }
  113.  if (is_redirect($rc)) { ... }
  114.  
  115. =head1 DESCRIPTION
  116.  
  117. I<HTTP::Status> is a library of routines for defining and
  118. classifying HTTP status codes for libwww-perl.  Status codes are
  119. used to encode the overall outcome of a HTTP response message.  Codes
  120. correspond to those defined in RFC 2616 and RFC 2518.
  121.  
  122. =head1 CONSTANTS
  123.  
  124. The following constant functions can be used as mnemonic status code
  125. names:
  126.  
  127.    RC_CONTINUE                (100)
  128.    RC_SWITCHING_PROTOCOLS        (101)
  129.    RC_PROCESSING                        (102)
  130.  
  131.    RC_OK                (200)
  132.    RC_CREATED                (201)
  133.    RC_ACCEPTED                (202)
  134.    RC_NON_AUTHORITATIVE_INFORMATION    (203)
  135.    RC_NO_CONTENT            (204)
  136.    RC_RESET_CONTENT            (205)
  137.    RC_PARTIAL_CONTENT            (206)
  138.    RC_MULTI_STATUS                      (207)
  139.  
  140.    RC_MULTIPLE_CHOICES            (300)
  141.    RC_MOVED_PERMANENTLY            (301)
  142.    RC_FOUND                (302)
  143.    RC_SEE_OTHER                (303)
  144.    RC_NOT_MODIFIED            (304)
  145.    RC_USE_PROXY                (305)
  146.    RC_TEMPORARY_REDIRECT        (307)
  147.  
  148.    RC_BAD_REQUEST            (400)
  149.    RC_UNAUTHORIZED            (401)
  150.    RC_PAYMENT_REQUIRED            (402)
  151.    RC_FORBIDDEN                (403)
  152.    RC_NOT_FOUND                (404)
  153.    RC_METHOD_NOT_ALLOWED        (405)
  154.    RC_NOT_ACCEPTABLE            (406)
  155.    RC_PROXY_AUTHENTICATION_REQUIRED    (407)
  156.    RC_REQUEST_TIMEOUT            (408)
  157.    RC_CONFLICT                (409)
  158.    RC_GONE                (410)
  159.    RC_LENGTH_REQUIRED            (411)
  160.    RC_PRECONDITION_FAILED        (412)
  161.    RC_REQUEST_ENTITY_TOO_LARGE        (413)
  162.    RC_REQUEST_URI_TOO_LARGE        (414)
  163.    RC_UNSUPPORTED_MEDIA_TYPE        (415)
  164.    RC_REQUEST_RANGE_NOT_SATISFIABLE     (416)
  165.    RC_EXPECTATION_FAILED        (417)
  166.    RC_UNPROCESSABLE_ENTITY              (422)
  167.    RC_LOCKED                            (423)
  168.    RC_FAILED_DEPENDENCY                 (424)
  169.  
  170.    RC_INTERNAL_SERVER_ERROR        (500)
  171.    RC_NOT_IMPLEMENTED            (501)
  172.    RC_BAD_GATEWAY            (502)
  173.    RC_SERVICE_UNAVAILABLE        (503)
  174.    RC_GATEWAY_TIMEOUT            (504)
  175.    RC_HTTP_VERSION_NOT_SUPPORTED    (505)
  176.    RC_INSUFFICIENT_STORAGE              (507)
  177.  
  178. =head1 FUNCTIONS
  179.  
  180. The following additional functions are provided.  Most of them are
  181. exported by default.
  182.  
  183. =over 4
  184.  
  185. =item status_message( $code )
  186.  
  187. The status_message() function will translate status codes to human
  188. readable strings. The string is the same as found in the constant
  189. names above.  If the $code is unknown, then C<undef> is returned.
  190.  
  191. =item is_info( $code )
  192.  
  193. Return TRUE if C<$code> is an I<Informational> status code.  This
  194. class of status code indicates a provisional response which can't have
  195. any content.
  196.  
  197. =item is_success( $code )
  198.  
  199. Return TRUE if C<$code> is a I<Successful> status code.
  200.  
  201. =item is_redirect( $code )
  202.  
  203. Return TRUE if C<$code> is a I<Redirection> status code. This class of
  204. status code indicates that further action needs to be taken by the
  205. user agent in order to fulfill the request.
  206.  
  207. =item is_error( $code )
  208.  
  209. Return TRUE if C<$code> is an I<Error> status code.  The function
  210. return TRUE for both client error or a server error status codes.
  211.  
  212. =item is_client_error( $code )
  213.  
  214. Return TRUE if C<$code> is an I<Client Error> status code. This class
  215. of status code is intended for cases in which the client seems to have
  216. erred.
  217.  
  218. This function is B<not> exported by default.
  219.  
  220. =item is_server_error( $code )
  221.  
  222. Return TRUE if C<$code> is an I<Server Error> status code. This class
  223. of status codes is intended for cases in which the server is aware
  224. that it has erred or is incapable of performing the request.
  225.  
  226. This function is B<not> exported by default.
  227.  
  228. =back
  229.  
  230. =head1 BUGS
  231.  
  232. Wished @EXPORT_OK had been used instead of @EXPORT in the beginning.
  233. Now too much is exported by default.
  234.  
  235.