home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Perl_Libs / site_perl / HTTP / Status.pm < prev   
Text File  |  1997-12-10  |  5KB  |  205 lines

  1. #
  2. # $Id: Status.pm,v 1.21 1997/12/10 13:12:17 aas Exp $
  3.  
  4. package HTTP::Status;
  5.  
  6. require 5.002;   # becase we use prototypes
  7.  
  8. =head1 NAME
  9.  
  10. HTTP::Status - HTTP Status code processing
  11.  
  12. =head1 SYNOPSIS
  13.  
  14.  use HTTP::Status;
  15.  
  16.  if ($rc != RC_OK) {
  17.      print status_message($rc), "\n";
  18.  }
  19.  
  20.  if (is_success($rc)) { ... }
  21.  if (is_error($rc)) { ... }
  22.  if (is_redirect($rc)) { ... }
  23.  
  24. =head1 DESCRIPTION
  25.  
  26. I<HTTP::Status> is a library of routines for defining and
  27. classification of HTTP status codes for libwww-perl.  Status codes are
  28. used to encode the overall outcome of a HTTP response message.  Codes
  29. correspond to those defined in RFC 2068.
  30.  
  31. =head1 CONSTANTS
  32.  
  33. The following constant functions can be used as mnemonic status code
  34. names:
  35.  
  36.    RC_CONTINUE                (100)
  37.    RC_SWITCHING_PROTOCOLS        (101)
  38.  
  39.    RC_OK                (200)
  40.    RC_CREATED                (201)
  41.    RC_ACCEPTED                (202)
  42.    RC_NON_AUTHORITATIVE_INFORMATION    (203)
  43.    RC_NO_CONTENT            (204)
  44.    RC_RESET_CONTENT            (205)
  45.    RC_PARTIAL_CONTENT            (206)
  46.  
  47.    RC_MULTIPLE_CHOICES            (300)
  48.    RC_MOVED_PERMANENTLY            (301)
  49.    RC_MOVED_TEMPORARILY            (302)
  50.    RC_SEE_OTHER                (303)
  51.    RC_NOT_MODIFIED            (304)
  52.    RC_USE_PROXY                (305)
  53.  
  54.    RC_BAD_REQUEST            (400)
  55.    RC_UNAUTHORIZED            (401)
  56.    RC_PAYMENT_REQUIRED            (402)
  57.    RC_FORBIDDEN                (403)
  58.    RC_NOT_FOUND                (404)
  59.    RC_METHOD_NOT_ALLOWED        (405)
  60.    RC_NOT_ACCEPTABLE            (406)
  61.    RC_PROXY_AUTHENTICATION_REQUIRED    (407)
  62.    RC_REQUEST_TIMEOUT            (408)
  63.    RC_CONFLICT                (409)
  64.    RC_GONE                (410)
  65.    RC_LENGTH_REQUIRED            (411)
  66.    RC_PRECONDITION_FAILED        (412)
  67.    RC_REQUEST_ENTITY_TOO_LARGE        (413)
  68.    RC_REQUEST_URI_TOO_LARGE        (414)
  69.    RC_UNSUPPORTED_MEDIA_TYPE        (415)
  70.    RC_REQUEST_RANGE_NOT_SATISFIABLE     (416)
  71.  
  72.    RC_INTERNAL_SERVER_ERROR        (500)
  73.    RC_NOT_IMPLEMENTED            (501)
  74.    RC_BAD_GATEWAY            (502)
  75.    RC_SERVICE_UNAVAILABLE        (503)
  76.    RC_GATEWAY_TIMEOUT            (504)
  77.    RC_HTTP_VERSION_NOT_SUPPORTED    (505)
  78.  
  79. =cut
  80.  
  81. #####################################################################
  82.  
  83.  
  84. require Exporter;
  85. @ISA = qw(Exporter);
  86. @EXPORT = qw(is_info is_success is_redirect is_error status_message);
  87. @EXPORT_OK = qw(is_client_error is_server_error);
  88.  
  89. # Note also addition of mnemonics to @EXPORT below
  90.  
  91. my %StatusCode = (
  92.     100 => 'Continue',
  93.     101 => 'Switching Protocols',
  94.     200 => 'OK',
  95.     201 => 'Created',
  96.     202 => 'Accepted',
  97.     203 => 'Non-Authoritative Information',
  98.     204 => 'No Content',
  99.     205 => 'Reset Content',
  100.     206 => 'Partial Content',
  101.     300 => 'Multiple Choices',
  102.     301 => 'Moved Permanently',
  103.     302 => 'Moved Temporarily',
  104.     303 => 'See Other',
  105.     304 => 'Not Modified',
  106.     305 => 'Use Proxy',
  107.     400 => 'Bad Request',
  108.     401 => 'Unauthorized',
  109.     402 => 'Payment Required',
  110.     403 => 'Forbidden',
  111.     404 => 'Not Found',
  112.     405 => 'Method Not Allowed',
  113.     406 => 'Not Acceptable',
  114.     407 => 'Proxy Authentication Required',
  115.     408 => 'Request Timeout',
  116.     409 => 'Conflict',
  117.     410 => 'Gone',
  118.     411 => 'Length Required',
  119.     412 => 'Precondition Failed',
  120.     413 => 'Request Entity Too Large',
  121.     414 => 'Request-URI Too Large',
  122.     415 => 'Unsupported Media Type',
  123.     416 => 'Request Range Not Satisfiable',
  124.     500 => 'Internal Server Error',
  125.     501 => 'Not Implemented',
  126.     502 => 'Bad Gateway',
  127.     503 => 'Service Unavailable',
  128.     504 => 'Gateway Timeout',
  129.     505 => 'HTTP Version Not Supported',
  130. );
  131.  
  132. my $mnemonicCode = '';
  133. my ($code, $message);
  134. while (($code, $message) = each %StatusCode) {
  135.     # create mnemonic subroutines
  136.     $message =~ tr/a-z \-/A-Z__/;
  137.     $mnemonicCode .= "sub RC_$message () { $code }\t";
  138.     # make them exportable
  139.     $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
  140. }
  141. # warn $mnemonicCode; # for development
  142. eval $mnemonicCode; # only one eval for speed
  143. die if $@;
  144.  
  145. =head1 FUNCTIONS
  146.  
  147. The following additional functions are provided.  Most of them are
  148. exported by default.
  149.  
  150. =over 4
  151.  
  152. =item status_message($code)
  153.  
  154. The status_message() function will translate status codes to human
  155. readable strings. The string is the same as found in the constant
  156. names above.
  157.  
  158. =cut
  159.  
  160. sub status_message ($)
  161. {
  162.     return undef unless exists $StatusCode{$_[0]};
  163.     $StatusCode{$_[0]};
  164. }
  165.  
  166. =item is_info($code)
  167.  
  168. Return TRUE if C<$code> is an I<Informational> status code.
  169.  
  170. =item is_success($code)
  171.  
  172. Return TRUE if C<$code> is a I<Successful> status code.
  173.  
  174. =item is_redirect($code)
  175.  
  176. Return TRUE if C<$code> is a I<Redirection> status code.
  177.  
  178. =item is_error($code)
  179.  
  180. Return TRUE if C<$code> is an I<Error> status code.  The function
  181. return TRUE for both client error or a server error status codes.
  182.  
  183. =item is_client_error($code)
  184.  
  185. Return TRUE if C<$code> is an I<Client Error> status code.  This
  186. function is B<not> exported by default.
  187.  
  188. =item is_server_error($code)
  189.  
  190. Return TRUE if C<$code> is an I<Server Error> status code.   This
  191. function is B<not> exported by default.
  192.  
  193. =back
  194.  
  195. =cut
  196.  
  197. sub is_info         ($) { $_[0] >= 100 && $_[0] < 200; }
  198. sub is_success      ($) { $_[0] >= 200 && $_[0] < 300; }
  199. sub is_redirect     ($) { $_[0] >= 300 && $_[0] < 400; }
  200. sub is_error        ($) { $_[0] >= 400 && $_[0] < 600; }
  201. sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
  202. sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
  203.  
  204. 1;
  205.