4 require 5.002; # because we use prototypes
6 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION);
10 @EXPORT = qw(is_info is_success is_redirect is_error status_message);
11 @EXPORT_OK = qw(is_client_error is_server_error);
14 # Note also addition of mnemonics to @EXPORT below
16 # Unmarked codes are from RFC 2616
17 # See also: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
21 101 => 'Switching Protocols',
22 102 => 'Processing', # RFC 2518 (WebDAV)
26 203 => 'Non-Authoritative Information',
28 205 => 'Reset Content',
29 206 => 'Partial Content',
30 207 => 'Multi-Status', # RFC 2518 (WebDAV)
31 300 => 'Multiple Choices',
32 301 => 'Moved Permanently',
35 304 => 'Not Modified',
37 307 => 'Temporary Redirect',
39 401 => 'Unauthorized',
40 402 => 'Payment Required',
43 405 => 'Method Not Allowed',
44 406 => 'Not Acceptable',
45 407 => 'Proxy Authentication Required',
46 408 => 'Request Timeout',
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',
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
74 my $mnemonicCode = '';
76 while (($code, $message) = each %StatusCode) {
77 # create mnemonic subroutines
78 $message =~ tr/a-z \-/A-Z__/;
79 $mnemonicCode .= "sub HTTP_$message () { $code }\n";
80 $mnemonicCode .= "*RC_$message = \\&HTTP_$message;\n"; # legacy
81 $mnemonicCode .= "push(\@EXPORT_OK, 'HTTP_$message');\n";
82 $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
84 eval $mnemonicCode; # only one eval for speed
87 # backwards compatibility
88 *RC_MOVED_TEMPORARILY = \&RC_FOUND; # 302 was renamed in the standard
89 push(@EXPORT, "RC_MOVED_TEMPORARILY");
92 constants => [grep /^HTTP_/, @EXPORT_OK],
93 is => [grep /^is_/, @EXPORT, @EXPORT_OK],
97 sub status_message ($) { $StatusCode{$_[0]}; }
99 sub is_info ($) { $_[0] >= 100 && $_[0] < 200; }
100 sub is_success ($) { $_[0] >= 200 && $_[0] < 300; }
101 sub is_redirect ($) { $_[0] >= 300 && $_[0] < 400; }
102 sub is_error ($) { $_[0] >= 400 && $_[0] < 600; }
103 sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
104 sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
113 HTTP::Status - HTTP Status code processing
117 use HTTP::Status qw(:constants :is status_message);
119 if ($rc != HTTP_OK) {
120 print status_message($rc), "\n";
123 if (is_success($rc)) { ... }
124 if (is_error($rc)) { ... }
125 if (is_redirect($rc)) { ... }
129 I<HTTP::Status> is a library of routines for defining and
130 classifying HTTP status codes for libwww-perl. Status codes are
131 used to encode the overall outcome of a HTTP response message. Codes
132 correspond to those defined in RFC 2616 and RFC 2518.
136 The following constant functions can be used as mnemonic status code
137 names. None of these are exported by default. Use the C<:constants>
138 tag to import them all.
141 HTTP_SWITCHING_PROTOCOLS (101)
142 HTTP_PROCESSING (102)
147 HTTP_NON_AUTHORITATIVE_INFORMATION (203)
148 HTTP_NO_CONTENT (204)
149 HTTP_RESET_CONTENT (205)
150 HTTP_PARTIAL_CONTENT (206)
151 HTTP_MULTI_STATUS (207)
153 HTTP_MULTIPLE_CHOICES (300)
154 HTTP_MOVED_PERMANENTLY (301)
157 HTTP_NOT_MODIFIED (304)
159 HTTP_TEMPORARY_REDIRECT (307)
161 HTTP_BAD_REQUEST (400)
162 HTTP_UNAUTHORIZED (401)
163 HTTP_PAYMENT_REQUIRED (402)
166 HTTP_METHOD_NOT_ALLOWED (405)
167 HTTP_NOT_ACCEPTABLE (406)
168 HTTP_PROXY_AUTHENTICATION_REQUIRED (407)
169 HTTP_REQUEST_TIMEOUT (408)
172 HTTP_LENGTH_REQUIRED (411)
173 HTTP_PRECONDITION_FAILED (412)
174 HTTP_REQUEST_ENTITY_TOO_LARGE (413)
175 HTTP_REQUEST_URI_TOO_LARGE (414)
176 HTTP_UNSUPPORTED_MEDIA_TYPE (415)
177 HTTP_REQUEST_RANGE_NOT_SATISFIABLE (416)
178 HTTP_EXPECTATION_FAILED (417)
179 HTTP_UNPROCESSABLE_ENTITY (422)
181 HTTP_FAILED_DEPENDENCY (424)
183 HTTP_UPGRADE_REQUIRED (426)
184 HTTP_RETRY_WITH (449)
186 HTTP_INTERNAL_SERVER_ERROR (500)
187 HTTP_NOT_IMPLEMENTED (501)
188 HTTP_BAD_GATEWAY (502)
189 HTTP_SERVICE_UNAVAILABLE (503)
190 HTTP_GATEWAY_TIMEOUT (504)
191 HTTP_HTTP_VERSION_NOT_SUPPORTED (505)
192 HTTP_VARIANT_ALSO_NEGOTIATES (506)
193 HTTP_INSUFFICIENT_STORAGE (507)
194 HTTP_BANDWIDTH_LIMIT_EXCEEDED (509)
195 HTTP_NOT_EXTENDED (510)
199 The following additional functions are provided. Most of them are
200 exported by default. The C<:is> import tag can be used to import all
201 the classification functions.
205 =item status_message( $code )
207 The status_message() function will translate status codes to human
208 readable strings. The string is the same as found in the constant
209 names above. If the $code is unknown, then C<undef> is returned.
211 =item is_info( $code )
213 Return TRUE if C<$code> is an I<Informational> status code (1xx). This
214 class of status code indicates a provisional response which can't have
217 =item is_success( $code )
219 Return TRUE if C<$code> is a I<Successful> status code (2xx).
221 =item is_redirect( $code )
223 Return TRUE if C<$code> is a I<Redirection> status code (3xx). This class of
224 status code indicates that further action needs to be taken by the
225 user agent in order to fulfill the request.
227 =item is_error( $code )
229 Return TRUE if C<$code> is an I<Error> status code (4xx or 5xx). The function
230 return TRUE for both client error or a server error status codes.
232 =item is_client_error( $code )
234 Return TRUE if C<$code> is an I<Client Error> status code (4xx). This class
235 of status code is intended for cases in which the client seems to have
238 This function is B<not> exported by default.
240 =item is_server_error( $code )
242 Return TRUE if C<$code> is an I<Server Error> status code (5xx). This class
243 of status codes is intended for cases in which the server is aware
244 that it has erred or is incapable of performing the request.
246 This function is B<not> exported by default.
252 For legacy reasons all the C<HTTP_> constants are exported by default
253 with the prefix C<RC_>. It's recommended to use explict imports and
254 the C<:constants> tag instead of relying on this.