src/java.net.http/share/classes/java/net/http/internal/websocket/StatusCodes.java
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56091 aedd6133e7a0
child 56093 22d94c4a3641
equal deleted inserted replaced
56091:aedd6133e7a0 56092:fd85b2bf2b0d
     1 /*
       
     2  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package java.net.http.internal.websocket;
       
    27 
       
    28 /*
       
    29  * Utilities for WebSocket status codes.
       
    30  *
       
    31  *     1. https://tools.ietf.org/html/rfc6455#section-7.4
       
    32  *     2. http://www.iana.org/assignments/websocket/websocket.xhtml#close-code-number
       
    33  */
       
    34 final class StatusCodes {
       
    35 
       
    36     static final int PROTOCOL_ERROR    = 1002;
       
    37     static final int NO_STATUS_CODE    = 1005;
       
    38     static final int CLOSED_ABNORMALLY = 1006;
       
    39     static final int NOT_CONSISTENT    = 1007;
       
    40 
       
    41     private StatusCodes() { }
       
    42 
       
    43     static boolean isLegalToSendFromClient(int code) {
       
    44         if (!isLegal(code)) {
       
    45             return false;
       
    46         }
       
    47         // Codes from unreserved range
       
    48         if (code > 4999) {
       
    49             return false;
       
    50         }
       
    51         // Codes below are not allowed to be sent using a WebSocket client API
       
    52         switch (code) {
       
    53             case PROTOCOL_ERROR:
       
    54             case NOT_CONSISTENT:
       
    55             case 1003:
       
    56             case 1009:
       
    57             case 1010:
       
    58             case 1012:  // code sent by servers
       
    59             case 1013:  // code sent by servers
       
    60             case 1014:  // code sent by servers
       
    61                 return false;
       
    62             default:
       
    63                 return true;
       
    64         }
       
    65     }
       
    66 
       
    67     static boolean isLegalToReceiveFromServer(int code) {
       
    68         if (!isLegal(code)) {
       
    69             return false;
       
    70         }
       
    71         return code != 1010;  // code sent by clients
       
    72     }
       
    73 
       
    74     private static boolean isLegal(int code) {
       
    75         // 2-byte unsigned integer excluding first 1000 numbers from the range
       
    76         // [0, 999] which are never used
       
    77         if (code < 1000 || code > 65535) {
       
    78             return false;
       
    79         }
       
    80         // Codes from the range below has no known meaning under the WebSocket
       
    81         // specification (i.e. unassigned/undefined)
       
    82         if ((code >= 1016 && code <= 2999) || code == 1004) {
       
    83             return false;
       
    84         }
       
    85         // Codes below cannot appear on the wire. It's always an error either
       
    86         // to send a frame with such a code or to receive one.
       
    87         switch (code) {
       
    88             case NO_STATUS_CODE:
       
    89             case CLOSED_ABNORMALLY:
       
    90             case 1015:
       
    91                 return false;
       
    92             default:
       
    93                 return true;
       
    94         }
       
    95     }
       
    96 }