File Coverage

blib/lib/Dancer2/Core/HTTP.pm
Criterion Covered Total %
statement 24 25 96.0
branch 11 12 91.6
condition 2 3 66.6
subroutine 9 9 100.0
pod 5 5 100.0
total 51 54 94.4


line stmt bran cond sub pod time code
1             # ABSTRACT: helper for rendering HTTP status codes for Dancer2
2              
3             package Dancer2::Core::HTTP;
4             $Dancer2::Core::HTTP::VERSION = '2.0.1';
5 164     164   393531 use strict;
  164         398  
  164         7906  
6 164     164   937 use warnings;
  164         324  
  164         11898  
7              
8 164     164   1089 use List::Util qw/ pairmap pairgrep /;
  164         347  
  164         126474  
9              
10             my $HTTP_CODES = {
11              
12             # informational
13             100 => 'Continue', # only on HTTP 1.1
14             101 => 'Switching Protocols', # only on HTTP 1.1
15             102 => 'Processing', # WebDAV; RFC 2518
16              
17             # processed
18             200 => 'OK',
19             201 => 'Created',
20             202 => 'Accepted',
21             203 => 'Non-Authoritative Information', # only on HTTP 1.1
22             204 => 'No Content',
23             205 => 'Reset Content',
24             206 => 'Partial Content',
25             207 => 'Multi-Status', # WebDAV; RFC 4918
26             208 => 'Already Reported', # WebDAV; RFC 5842
27             # 226 => 'IM Used' # RFC 3229
28              
29             # redirections
30             301 => 'Moved Permanently',
31             302 => 'Found',
32             303 => 'See Other', # only on HTTP 1.1
33             304 => 'Not Modified',
34             305 => 'Use Proxy', # only on HTTP 1.1
35             306 => 'Switch Proxy',
36             307 => 'Temporary Redirect', # only on HTTP 1.1
37             # 308 => 'Permanent Redirect' # approved as experimental RFC
38              
39             # problems with request
40             400 => 'Bad Request',
41             401 => 'Unauthorized',
42             402 => 'Payment Required',
43             403 => 'Forbidden',
44             404 => 'Not Found',
45             405 => 'Method Not Allowed',
46             406 => 'Not Acceptable',
47             407 => 'Proxy Authentication Required',
48             408 => 'Request Timeout',
49             409 => 'Conflict',
50             410 => 'Gone',
51             411 => 'Length Required',
52             412 => 'Precondition Failed',
53             413 => 'Request Entity Too Large',
54             414 => 'Request-URI Too Long',
55             415 => 'Unsupported Media Type',
56             416 => 'Requested Range Not Satisfiable',
57             417 => 'Expectation Failed',
58             418 => "I'm a teapot", # RFC 2324
59             # 419 => 'Authentication Timeout', # not in RFC 2616
60             420 => 'Enhance Your Calm',
61             422 => 'Unprocessable Entity',
62             423 => 'Locked',
63             424 => 'Failed Dependency', # Also used for 'Method Failure'
64             425 => 'Unordered Collection',
65             426 => 'Upgrade Required',
66             428 => 'Precondition Required',
67             429 => 'Too Many Requests',
68             431 => 'Request Header Fields Too Large',
69             444 => 'No Response',
70             449 => 'Retry With',
71             450 => 'Blocked by Windows Parental Controls',
72             451 => 'Unavailable For Legal Reasons',
73             494 => 'Request Header Too Large',
74             495 => 'Cert Error',
75             496 => 'No Cert',
76             497 => 'HTTP to HTTPS',
77             499 => 'Client Closed Request',
78              
79             # problems with server
80             500 => 'Internal Server Error',
81             501 => 'Not Implemented',
82             502 => 'Bad Gateway',
83             503 => 'Service Unavailable',
84             504 => 'Gateway Timeout',
85             505 => 'HTTP Version Not Supported',
86             506 => 'Variant Also Negotiates',
87             507 => 'Insufficient Storage',
88             508 => 'Loop Detected',
89             509 => 'Bandwidth Limit Exceeded',
90             510 => 'Not Extended',
91             511 => 'Network Authentication Required',
92             598 => 'Network read timeout error',
93             599 => 'Network connect timeout error',
94             };
95              
96             $HTTP_CODES = {
97             %$HTTP_CODES,
98             ( reverse %$HTTP_CODES ),
99             pairmap { join( '_', split /\W/, lc $a ) => $b } reverse %$HTTP_CODES
100             };
101              
102             $HTTP_CODES->{error} = $HTTP_CODES->{internal_server_error};
103              
104             sub status {
105 827     827 1 298130 my ( $class, $status ) = @_;
106 827 100       2869 return if ! defined $status;
107 826 100       21169 return $status if $status =~ /^\d+$/;
108 7 50       28 if ( exists $HTTP_CODES->{$status} ) {
109 7         47 return $HTTP_CODES->{$status};
110             }
111 0         0 return;
112             }
113              
114             sub status_message {
115 137     137 1 11398 my ( $class, $status ) = @_;
116 137 100       588 return if ! defined $status;
117 136         551 my $code = $class->status($status);
118 136 100 66     1206 return if ! defined $code || ! exists $HTTP_CODES->{$code};
119 134         811 return $HTTP_CODES->{ $code };
120             }
121              
122             sub status_mapping {
123 422 100   422 1 1683 pairgrep { $b =~ /^\d+$/ and $a !~ /_/ } %$HTTP_CODES;
  2     2   3780  
124             }
125              
126             sub code_mapping {
127 1     1 1 4 my @result = reverse status_mapping();
128 1         91 return @result;
129             }
130              
131 1     1 1 1457 sub all_mappings { %$HTTP_CODES }
132              
133             1;
134              
135             __END__