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 30     30   265010 use strict;
  30         133  
  30         915  
4 30     30   159 use warnings;
  30         51  
  30         1276  
5              
6 30         3306 use constant OPCODE => {
7             continuation => 0,
8             text => 1,
9             binary => 2,
10             close => 8,
11             ping => 9,
12             pong => 10,
13 30     30   173 };
  30         95  
14              
15             use constant {
16 30         4077 PROTOCOL_VERSION => 13,
17             REQUIRED_HTTP_METHOD => 'GET',
18             REQUIRED_HTTP_STATUS => 101,
19             REQUIRED_REQUEST_PROTOCOL => 'HTTP/1.1',
20 30     30   234 };
  30         74  
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 30         8197 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 30     30   218 };
  30         62  
55              
56             #----------------------------------------------------------------------
57              
58             my %status_code_name;
59              
60             sub status_name_to_code {
61 43     43 0 4308 my ($name) = @_;
62              
63 43         127 return STATUS()->{$name};
64             }
65              
66             sub status_code_to_name {
67 7     7 0 928 my ($code) = @_;
68              
69 7 100       22 if (!%status_code_name) {
70 4         9 my %copy = %{ STATUS() };
  4         41  
71 4         15 delete $copy{'SERVER_ERROR'};
72 4         62 %status_code_name = reverse %copy;
73             }
74              
75 7         33 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;