File Coverage

blib/lib/Net/WebSocket/Constants.pm
Criterion Covered Total %
statement 24 28 85.7
branch 2 4 50.0
condition n/a
subroutine 7 8 87.5
pod 0 3 0.0
total 33 43 76.7


line stmt bran cond sub pod time code
1             package Net::WebSocket::Constants;
2              
3 31     31   270327 use strict;
  31         139  
  31         948  
4 31     31   156 use warnings;
  31         62  
  31         1276  
5              
6 31         3322 use constant OPCODE => {
7             continuation => 0,
8             text => 1,
9             binary => 2,
10             close => 8,
11             ping => 9,
12             pong => 10,
13 31     31   200 };
  31         131  
14              
15             use constant {
16 31         3848 PROTOCOL_VERSION => 13,
17             REQUIRED_HTTP_METHOD => 'GET',
18             REQUIRED_HTTP_STATUS => 101,
19             REQUIRED_REQUEST_PROTOCOL => 'HTTP/1.1',
20 31     31   257 };
  31         82  
21              
22             #These names are taken from:
23             #https://msdn.microsoft.com/en-us/library/windows/desktop/hh449350(v=vs.85).aspx
24             # … however, the up-to-date list of canonical codes is at:
25             #https://www.iana.org/assignments/websocket/websocket.xml
26 31         8210 use constant STATUS => {
27             SUCCESS => 1000,
28             ENDPOINT_UNAVAILABLE => 1001,
29             PROTOCOL_ERROR => 1002,
30             INVALID_DATA_TYPE => 1003,
31              
32             #These are never actually sent.
33             #EMPTY_CLOSE => 1005,
34             #ABORTED_CLOSE => 1006,
35              
36             INVALID_PAYLOAD => 1007,
37             POLICY_VIOLATION => 1008,
38             MESSAGE_TOO_BIG => 1009,
39             UNSUPPORTED_EXTENSIONS => 1010,
40              
41             #Post-RFC, “server error” was changed to “internal error”.
42             #We accept both names; code-to-name conversion always returns this one.
43             INTERNAL_ERROR => 1011,
44             SERVER_ERROR => 1011,
45              
46             #These are part of the IANA registry but are not in Microsoft’s enum.
47             SERVICE_RESTART => 1012,
48             TRY_AGAIN_LATER => 1013,
49             BAD_GATEWAY => 1014,
50              
51             #RFC says not to use this one,
52             #but MS has it in their enum.
53             #SECURE_HANDSHAKE_ERROR => 1015,
54 31     31   229 };
  31         66  
55              
56             #----------------------------------------------------------------------
57              
58             my %status_code_name;
59              
60             sub status_name_to_code {
61 43     43 0 4243 my ($name) = @_;
62              
63 43         130 return STATUS()->{$name};
64             }
65              
66             sub status_code_to_name {
67 7     7 0 1043 my ($code) = @_;
68              
69 7 100       23 if (!%status_code_name) {
70 4         10 my %copy = %{ STATUS() };
  4         41  
71 4         16 delete $copy{'SERVER_ERROR'};
72 4         81 %status_code_name = reverse %copy;
73             }
74              
75 7         38 return $status_code_name{$code};
76             }
77              
78             #----------------------------------------------------------------------
79              
80             my %opcode_type;
81              
82             sub opcode_to_type {
83 0     0 0   my ($opcode) = @_;
84              
85 0 0         %opcode_type = reverse %{ OPCODE() } if !%opcode_type;
  0            
86              
87 0           return $opcode_type{$opcode};
88             }
89              
90             1;