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 28     28   196997 use strict;
  28         66  
  28         645  
4 28     28   119 use warnings;
  28         46  
  28         965  
5              
6 28         2216 use constant OPCODE => {
7             continuation => 0,
8             text => 1,
9             binary => 2,
10             close => 8,
11             ping => 9,
12             pong => 10,
13 28     28   130 };
  28         62  
14              
15             use constant {
16 28         2735 PROTOCOL_VERSION => 13,
17             REQUIRED_HTTP_METHOD => 'GET',
18             REQUIRED_HTTP_STATUS => 101,
19             REQUIRED_REQUEST_PROTOCOL => 'HTTP/1.1',
20 28     28   159 };
  28         56  
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 28         5777 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 28     28   154 };
  28         59  
55              
56             #----------------------------------------------------------------------
57              
58             my %status_code_name;
59              
60             sub status_name_to_code {
61 8     8 0 3252 my ($name) = @_;
62              
63 8         33 return STATUS()->{$name};
64             }
65              
66             sub status_code_to_name {
67 3     3 0 696 my ($code) = @_;
68              
69 3 100       9 if (!%status_code_name) {
70 2         3 my %copy = %{ STATUS() };
  2         17  
71 2         5 delete $copy{'SERVER_ERROR'};
72 2         20 %status_code_name = reverse %copy;
73             }
74              
75 3         12 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;