source: main/trunk/greenstone2/perllib/cpan/HTTP/Status.pm@ 27174

Last change on this file since 27174 was 27174, checked in by davidb, 11 years ago

Perl modules from CPAN that are used in supporting activate.pl, but not part of the Perl core. Only PMs included.

File size: 8.8 KB
Line 
1package HTTP::Status;
2
3use strict;
4require 5.002; # because we use prototypes
5
6use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
7
8require 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 = "6.03";
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
19my %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 208 => 'Already Reported', # RFC 5842
32 300 => 'Multiple Choices',
33 301 => 'Moved Permanently',
34 302 => 'Found',
35 303 => 'See Other',
36 304 => 'Not Modified',
37 305 => 'Use Proxy',
38 307 => 'Temporary Redirect',
39 400 => 'Bad Request',
40 401 => 'Unauthorized',
41 402 => 'Payment Required',
42 403 => 'Forbidden',
43 404 => 'Not Found',
44 405 => 'Method Not Allowed',
45 406 => 'Not Acceptable',
46 407 => 'Proxy Authentication Required',
47 408 => 'Request Timeout',
48 409 => 'Conflict',
49 410 => 'Gone',
50 411 => 'Length Required',
51 412 => 'Precondition Failed',
52 413 => 'Request Entity Too Large',
53 414 => 'Request-URI Too Large',
54 415 => 'Unsupported Media Type',
55 416 => 'Request Range Not Satisfiable',
56 417 => 'Expectation Failed',
57 418 => 'I\'m a teapot', # RFC 2324
58 422 => 'Unprocessable Entity', # RFC 2518 (WebDAV)
59 423 => 'Locked', # RFC 2518 (WebDAV)
60 424 => 'Failed Dependency', # RFC 2518 (WebDAV)
61 425 => 'No code', # WebDAV Advanced Collections
62 426 => 'Upgrade Required', # RFC 2817
63 428 => 'Precondition Required',
64 429 => 'Too Many Requests',
65 431 => 'Request Header Fields Too Large',
66 449 => 'Retry with', # unofficial Microsoft
67 500 => 'Internal Server Error',
68 501 => 'Not Implemented',
69 502 => 'Bad Gateway',
70 503 => 'Service Unavailable',
71 504 => 'Gateway Timeout',
72 505 => 'HTTP Version Not Supported',
73 506 => 'Variant Also Negotiates', # RFC 2295
74 507 => 'Insufficient Storage', # RFC 2518 (WebDAV)
75 509 => 'Bandwidth Limit Exceeded', # unofficial
76 510 => 'Not Extended', # RFC 2774
77 511 => 'Network Authentication Required',
78);
79
80my $mnemonicCode = '';
81my ($code, $message);
82while (($code, $message) = each %StatusCode) {
83 # create mnemonic subroutines
84 $message =~ s/I'm/I am/;
85 $message =~ tr/a-z \-/A-Z__/;
86 $mnemonicCode .= "sub HTTP_$message () { $code }\n";
87 $mnemonicCode .= "*RC_$message = \\&HTTP_$message;\n"; # legacy
88 $mnemonicCode .= "push(\@EXPORT_OK, 'HTTP_$message');\n";
89 $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
90}
91eval $mnemonicCode; # only one eval for speed
92die if $@;
93
94# backwards compatibility
95*RC_MOVED_TEMPORARILY = \&RC_FOUND; # 302 was renamed in the standard
96push(@EXPORT, "RC_MOVED_TEMPORARILY");
97
98%EXPORT_TAGS = (
99 constants => [grep /^HTTP_/, @EXPORT_OK],
100 is => [grep /^is_/, @EXPORT, @EXPORT_OK],
101);
102
103
104sub status_message ($) { $StatusCode{$_[0]}; }
105
106sub is_info ($) { $_[0] >= 100 && $_[0] < 200; }
107sub is_success ($) { $_[0] >= 200 && $_[0] < 300; }
108sub is_redirect ($) { $_[0] >= 300 && $_[0] < 400; }
109sub is_error ($) { $_[0] >= 400 && $_[0] < 600; }
110sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
111sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
112
1131;
114
115
116__END__
117
118=head1 NAME
119
120HTTP::Status - HTTP Status code processing
121
122=head1 SYNOPSIS
123
124 use HTTP::Status qw(:constants :is status_message);
125
126 if ($rc != HTTP_OK) {
127 print status_message($rc), "\n";
128 }
129
130 if (is_success($rc)) { ... }
131 if (is_error($rc)) { ... }
132 if (is_redirect($rc)) { ... }
133
134=head1 DESCRIPTION
135
136I<HTTP::Status> is a library of routines for defining and
137classifying HTTP status codes for libwww-perl. Status codes are
138used to encode the overall outcome of an HTTP response message. Codes
139correspond to those defined in RFC 2616 and RFC 2518.
140
141=head1 CONSTANTS
142
143The following constant functions can be used as mnemonic status code
144names. None of these are exported by default. Use the C<:constants>
145tag to import them all.
146
147 HTTP_CONTINUE (100)
148 HTTP_SWITCHING_PROTOCOLS (101)
149 HTTP_PROCESSING (102)
150
151 HTTP_OK (200)
152 HTTP_CREATED (201)
153 HTTP_ACCEPTED (202)
154 HTTP_NON_AUTHORITATIVE_INFORMATION (203)
155 HTTP_NO_CONTENT (204)
156 HTTP_RESET_CONTENT (205)
157 HTTP_PARTIAL_CONTENT (206)
158 HTTP_MULTI_STATUS (207)
159 HTTP_ALREADY_REPORTED (208)
160
161 HTTP_MULTIPLE_CHOICES (300)
162 HTTP_MOVED_PERMANENTLY (301)
163 HTTP_FOUND (302)
164 HTTP_SEE_OTHER (303)
165 HTTP_NOT_MODIFIED (304)
166 HTTP_USE_PROXY (305)
167 HTTP_TEMPORARY_REDIRECT (307)
168
169 HTTP_BAD_REQUEST (400)
170 HTTP_UNAUTHORIZED (401)
171 HTTP_PAYMENT_REQUIRED (402)
172 HTTP_FORBIDDEN (403)
173 HTTP_NOT_FOUND (404)
174 HTTP_METHOD_NOT_ALLOWED (405)
175 HTTP_NOT_ACCEPTABLE (406)
176 HTTP_PROXY_AUTHENTICATION_REQUIRED (407)
177 HTTP_REQUEST_TIMEOUT (408)
178 HTTP_CONFLICT (409)
179 HTTP_GONE (410)
180 HTTP_LENGTH_REQUIRED (411)
181 HTTP_PRECONDITION_FAILED (412)
182 HTTP_REQUEST_ENTITY_TOO_LARGE (413)
183 HTTP_REQUEST_URI_TOO_LARGE (414)
184 HTTP_UNSUPPORTED_MEDIA_TYPE (415)
185 HTTP_REQUEST_RANGE_NOT_SATISFIABLE (416)
186 HTTP_EXPECTATION_FAILED (417)
187 HTTP_I_AM_A_TEAPOT (418)
188 HTTP_UNPROCESSABLE_ENTITY (422)
189 HTTP_LOCKED (423)
190 HTTP_FAILED_DEPENDENCY (424)
191 HTTP_NO_CODE (425)
192 HTTP_UPGRADE_REQUIRED (426)
193 HTTP_PRECONDITION_REQUIRED (428)
194 HTTP_TOO_MANY_REQUESTS (429)
195 HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE (431)
196 HTTP_RETRY_WITH (449)
197
198 HTTP_INTERNAL_SERVER_ERROR (500)
199 HTTP_NOT_IMPLEMENTED (501)
200 HTTP_BAD_GATEWAY (502)
201 HTTP_SERVICE_UNAVAILABLE (503)
202 HTTP_GATEWAY_TIMEOUT (504)
203 HTTP_HTTP_VERSION_NOT_SUPPORTED (505)
204 HTTP_VARIANT_ALSO_NEGOTIATES (506)
205 HTTP_INSUFFICIENT_STORAGE (507)
206 HTTP_BANDWIDTH_LIMIT_EXCEEDED (509)
207 HTTP_NOT_EXTENDED (510)
208 HTTP_NETWORK_AUTHENTICATION_REQUIRED (511)
209
210=head1 FUNCTIONS
211
212The following additional functions are provided. Most of them are
213exported by default. The C<:is> import tag can be used to import all
214the classification functions.
215
216=over 4
217
218=item status_message( $code )
219
220The status_message() function will translate status codes to human
221readable strings. The string is the same as found in the constant
222names above. If the $code is unknown, then C<undef> is returned.
223
224=item is_info( $code )
225
226Return TRUE if C<$code> is an I<Informational> status code (1xx). This
227class of status code indicates a provisional response which can't have
228any content.
229
230=item is_success( $code )
231
232Return TRUE if C<$code> is a I<Successful> status code (2xx).
233
234=item is_redirect( $code )
235
236Return TRUE if C<$code> is a I<Redirection> status code (3xx). This class of
237status code indicates that further action needs to be taken by the
238user agent in order to fulfill the request.
239
240=item is_error( $code )
241
242Return TRUE if C<$code> is an I<Error> status code (4xx or 5xx). The function
243returns TRUE for both client and server error status codes.
244
245=item is_client_error( $code )
246
247Return TRUE if C<$code> is a I<Client Error> status code (4xx). This class
248of status code is intended for cases in which the client seems to have
249erred.
250
251This function is B<not> exported by default.
252
253=item is_server_error( $code )
254
255Return TRUE if C<$code> is a I<Server Error> status code (5xx). This class
256of status codes is intended for cases in which the server is aware
257that it has erred or is incapable of performing the request.
258
259This function is B<not> exported by default.
260
261=back
262
263=head1 BUGS
264
265For legacy reasons all the C<HTTP_> constants are exported by default
266with the prefix C<RC_>. It's recommended to use explicit imports and
267the C<:constants> tag instead of relying on this.
Note: See TracBrowser for help on using the repository browser.