Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
6.8 KiB

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