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