View Javadoc
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.netty5.handler.codec.http;
18  
19  import io.netty5.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      /**
56       * Returns the class of the specified HTTP status code.
57       */
58      public static HttpStatusClass valueOf(int code) {
59          if (INFORMATIONAL.contains(code)) {
60              return INFORMATIONAL;
61          }
62          if (SUCCESS.contains(code)) {
63              return SUCCESS;
64          }
65          if (REDIRECTION.contains(code)) {
66              return REDIRECTION;
67          }
68          if (CLIENT_ERROR.contains(code)) {
69              return CLIENT_ERROR;
70          }
71          if (SERVER_ERROR.contains(code)) {
72              return SERVER_ERROR;
73          }
74          return UNKNOWN;
75      }
76  
77      /**
78       * Returns the class of the specified HTTP status code.
79       * @param code Just the numeric portion of the http status code.
80       */
81      public static HttpStatusClass valueOf(CharSequence code) {
82          if (code != null && code.length() == 3) {
83              char c0 = code.charAt(0);
84              return isDigit(c0) && isDigit(code.charAt(1)) && isDigit(code.charAt(2)) ? valueOf(digit(c0) * 100)
85                                                                                       : UNKNOWN;
86          }
87          return UNKNOWN;
88      }
89  
90      private static int digit(char c) {
91          return c - '0';
92      }
93  
94      private static boolean isDigit(char c) {
95          return c >= '0' && c <= '9';
96      }
97  
98      private final int min;
99      private final int max;
100     private final AsciiString defaultReasonPhrase;
101 
102     HttpStatusClass(int min, int max, String defaultReasonPhrase) {
103         this.min = min;
104         this.max = max;
105         this.defaultReasonPhrase = AsciiString.cached(defaultReasonPhrase);
106     }
107 
108     /**
109      * Returns {@code true} if and only if the specified HTTP status code falls into this class.
110      */
111     public boolean contains(int code) {
112         return code >= min && code < max;
113     }
114 
115     /**
116      * Returns the default reason phrase of this HTTP status class.
117      */
118     AsciiString defaultReasonPhrase() {
119         return defaultReasonPhrase;
120     }
121 }