File Coverage

blib/lib/Net/WebSocket/Constants.pm
Criterion Covered Total %
statement 22 28 78.5
branch 1 4 25.0
condition n/a
subroutine 6 8 75.0
pod 0 3 0.0
total 29 43 67.4


line stmt bran cond sub pod time code
1             package Net::WebSocket::Constants;
2              
3 15     15   116511 use strict;
  15         33  
  15         354  
4 15     15   62 use warnings;
  15         41  
  15         487  
5              
6 15         1186 use constant OPCODE => {
7             continuation => 0,
8             text => 1,
9             binary => 2,
10             close => 8,
11             ping => 9,
12             pong => 10,
13 15     15   73 };
  15         26  
14              
15             use constant {
16 15         1242 PROTOCOL_VERSION => 13,
17             REQUIRED_HTTP_METHOD => 'GET',
18             REQUIRED_HTTP_STATUS => 101,
19             REQUIRED_REQUEST_PROTOCOL => 'HTTP/1.1',
20 15     15   83 };
  15         25  
21              
22             #These names are taken from:
23             #https://msdn.microsoft.com/en-us/library/windows/desktop/hh449350(v=vs.85).aspx
24 15         2804 use constant STATUS => {
25             SUCCESS => 1000,
26             ENDPOINT_UNAVAILABLE => 1001,
27             PROTOCOL_ERROR => 1002,
28             INVALID_DATA_TYPE => 1003,
29              
30             #These are never actually sent.
31             #EMPTY_CLOSE => 1005,
32             #ABORTED_CLOSE => 1006,
33              
34             INVALID_PAYLOAD => 1007,
35             POLICY_VIOLATION => 1008,
36             MESSAGE_TOO_BIG => 1009,
37             UNSUPPORTED_EXTENSIONS => 1010,
38              
39             #Post-RFC, “server error” was changed to “internal error”.
40             #We accept both names; code-to-name conversion always returns this one.
41             INTERNAL_ERROR => 1011,
42             SERVER_ERROR => 1011,
43              
44             #RFC says not to use this one,
45             #but MS has it in their enum.
46             #SECURE_HANDSHAKE_ERROR => 1015,
47 15     15   81 };
  15         22  
48              
49             #----------------------------------------------------------------------
50              
51             my %status_code_name;
52              
53             sub status_name_to_code {
54 0     0 0 0 my ($name) = @_;
55              
56 0         0 return STATUS()->{$name};
57             }
58              
59             sub status_code_to_name {
60 1     1 0 827 my ($code) = @_;
61              
62 1 50       4 if (!%status_code_name) {
63 1         2 my %copy = %{ STATUS() };
  1         8  
64 1         3 delete $copy{'SERVER_ERROR'};
65 1         9 %status_code_name = reverse %copy;
66             }
67              
68 1         6 return $status_code_name{$code};
69             }
70              
71             #----------------------------------------------------------------------
72              
73             my %opcode_type;
74              
75             sub opcode_to_type {
76 0     0 0   my ($opcode) = @_;
77              
78 0 0         %opcode_type = reverse %{ OPCODE() } if !%opcode_type;
  0            
79              
80 0           return $opcode_type{$opcode};
81             }
82              
83             1;