查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.handler.codec.http;
18  
19  import io.netty.util.AsciiString;
20  
21  /**
22   * The class of HTTP status.
23   */
24  public enum HttpStatusClass {
25      /**
26       * The informational class (1xx)
27       */
28      INFORMATIONAL(100, 200, "Informational"),
29      /**
30       * The success class (2xx)
31       */
32      SUCCESS(200, 300, "Success"),
33      /**
34       * The redirection class (3xx)
35       */
36      REDIRECTION(300, 400, "Redirection"),
37      /**
38       * The client error class (4xx)
39       */
40      CLIENT_ERROR(400, 500, "Client Error"),
41      /**
42       * The server error class (5xx)
43       */
44      SERVER_ERROR(500, 600, "Server Error"),
45      /**
46       * The unknown class
47       */
48      UNKNOWN(0, 0, "Unknown Status") {
49          @Override
50          public boolean contains(int code) {
51              return code < 100 || code >= 600;
52          }
53      };
54  
55      private static final HttpStatusClass[] statusArray = new HttpStatusClass[6];
56      static {
57          statusArray[1] = INFORMATIONAL;
58          statusArray[2] = SUCCESS;
59          statusArray[3] = REDIRECTION;
60          statusArray[4] = CLIENT_ERROR;
61          statusArray[5] = SERVER_ERROR;
62      }
63  
64      /**
65       * Returns the class of the specified HTTP status code.
66       */
67      public static HttpStatusClass valueOf(int code) {
68          if (UNKNOWN.contains(code)) {
69              return UNKNOWN;
70          }
71          return statusArray[fast_div100(code)];
72      }
73  
74      /**
75       * @param dividend Must >= 0
76       * @return dividend/100
77       */
78      private static int fast_div100(int dividend) {
79          return (int) ((dividend * 1374389535L) >> 37);
80      }
81  
82      /**
83       * Returns the class of the specified HTTP status code.
84       * @param code Just the numeric portion of the http status code.
85       */
86      public static HttpStatusClass valueOf(CharSequence code) {
87          if (code != null && code.length() == 3) {
88              char c0 = code.charAt(0);
89              return isDigit(c0) && isDigit(code.charAt(1)) && isDigit(code.charAt(2)) ? valueOf(digit(c0) * 100)
90                                                                                       : UNKNOWN;
91          }
92          return UNKNOWN;
93      }
94  
95      private static int digit(char c) {
96          return c - '0';
97      }
98  
99      private static boolean isDigit(char c) {
100         return c >= '0' && c <= '9';
101     }
102 
103     private final int min;
104     private final int max;
105     private final AsciiString defaultReasonPhrase;
106 
107     HttpStatusClass(int min, int max, String defaultReasonPhrase) {
108         this.min = min;
109         this.max = max;
110         this.defaultReasonPhrase = AsciiString.cached(defaultReasonPhrase);
111     }
112 
113     /**
114      * Returns {@code true} if and only if the specified HTTP status code falls into this class.
115      */
116     public boolean contains(int code) {
117         return code >= min && code < max;
118     }
119 
120     /**
121      * Returns the default reason phrase of this HTTP status class.
122      */
123     AsciiString defaultReasonPhrase() {
124         return defaultReasonPhrase;
125     }
126 }