View Javadoc

1   /*
2    * Copyright 2012 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    *   http://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  package io.netty.handler.codec.http;
17  
18  import static io.netty.handler.codec.http.HttpConstants.SP;
19  import io.netty.buffer.ByteBuf;
20  import io.netty.util.AsciiString;
21  import io.netty.util.ByteProcessor;
22  import io.netty.util.CharsetUtil;
23  
24  /**
25   * The response code and its description of HTTP or its derived protocols, such as
26   * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
27   * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
28   */
29  public class HttpResponseStatus implements Comparable<HttpResponseStatus> {
30  
31      /**
32       * 100 Continue
33       */
34      public static final HttpResponseStatus CONTINUE = newStatus(100, "Continue");
35  
36      /**
37       * 101 Switching Protocols
38       */
39      public static final HttpResponseStatus SWITCHING_PROTOCOLS = newStatus(101, "Switching Protocols");
40  
41      /**
42       * 102 Processing (WebDAV, RFC2518)
43       */
44      public static final HttpResponseStatus PROCESSING = newStatus(102, "Processing");
45  
46      /**
47       * 200 OK
48       */
49      public static final HttpResponseStatus OK = newStatus(200, "OK");
50  
51      /**
52       * 201 Created
53       */
54      public static final HttpResponseStatus CREATED = newStatus(201, "Created");
55  
56      /**
57       * 202 Accepted
58       */
59      public static final HttpResponseStatus ACCEPTED = newStatus(202, "Accepted");
60  
61      /**
62       * 203 Non-Authoritative Information (since HTTP/1.1)
63       */
64      public static final HttpResponseStatus NON_AUTHORITATIVE_INFORMATION =
65              newStatus(203, "Non-Authoritative Information");
66  
67      /**
68       * 204 No Content
69       */
70      public static final HttpResponseStatus NO_CONTENT = newStatus(204, "No Content");
71  
72      /**
73       * 205 Reset Content
74       */
75      public static final HttpResponseStatus RESET_CONTENT = newStatus(205, "Reset Content");
76  
77      /**
78       * 206 Partial Content
79       */
80      public static final HttpResponseStatus PARTIAL_CONTENT = newStatus(206, "Partial Content");
81  
82      /**
83       * 207 Multi-Status (WebDAV, RFC2518)
84       */
85      public static final HttpResponseStatus MULTI_STATUS = newStatus(207, "Multi-Status");
86  
87      /**
88       * 300 Multiple Choices
89       */
90      public static final HttpResponseStatus MULTIPLE_CHOICES = newStatus(300, "Multiple Choices");
91  
92      /**
93       * 301 Moved Permanently
94       */
95      public static final HttpResponseStatus MOVED_PERMANENTLY = newStatus(301, "Moved Permanently");
96  
97      /**
98       * 302 Found
99       */
100     public static final HttpResponseStatus FOUND = newStatus(302, "Found");
101 
102     /**
103      * 303 See Other (since HTTP/1.1)
104      */
105     public static final HttpResponseStatus SEE_OTHER = newStatus(303, "See Other");
106 
107     /**
108      * 304 Not Modified
109      */
110     public static final HttpResponseStatus NOT_MODIFIED = newStatus(304, "Not Modified");
111 
112     /**
113      * 305 Use Proxy (since HTTP/1.1)
114      */
115     public static final HttpResponseStatus USE_PROXY = newStatus(305, "Use Proxy");
116 
117     /**
118      * 307 Temporary Redirect (since HTTP/1.1)
119      */
120     public static final HttpResponseStatus TEMPORARY_REDIRECT = newStatus(307, "Temporary Redirect");
121 
122     /**
123      * 308 Permanent Redirect (RFC7538)
124      */
125     public static final HttpResponseStatus PERMANENT_REDIRECT = newStatus(308, "Permanent Redirect");
126 
127     /**
128      * 400 Bad Request
129      */
130     public static final HttpResponseStatus BAD_REQUEST = newStatus(400, "Bad Request");
131 
132     /**
133      * 401 Unauthorized
134      */
135     public static final HttpResponseStatus UNAUTHORIZED = newStatus(401, "Unauthorized");
136 
137     /**
138      * 402 Payment Required
139      */
140     public static final HttpResponseStatus PAYMENT_REQUIRED = newStatus(402, "Payment Required");
141 
142     /**
143      * 403 Forbidden
144      */
145     public static final HttpResponseStatus FORBIDDEN = newStatus(403, "Forbidden");
146 
147     /**
148      * 404 Not Found
149      */
150     public static final HttpResponseStatus NOT_FOUND = newStatus(404, "Not Found");
151 
152     /**
153      * 405 Method Not Allowed
154      */
155     public static final HttpResponseStatus METHOD_NOT_ALLOWED = newStatus(405, "Method Not Allowed");
156 
157     /**
158      * 406 Not Acceptable
159      */
160     public static final HttpResponseStatus NOT_ACCEPTABLE = newStatus(406, "Not Acceptable");
161 
162     /**
163      * 407 Proxy Authentication Required
164      */
165     public static final HttpResponseStatus PROXY_AUTHENTICATION_REQUIRED =
166             newStatus(407, "Proxy Authentication Required");
167 
168     /**
169      * 408 Request Timeout
170      */
171     public static final HttpResponseStatus REQUEST_TIMEOUT = newStatus(408, "Request Timeout");
172 
173     /**
174      * 409 Conflict
175      */
176     public static final HttpResponseStatus CONFLICT = newStatus(409, "Conflict");
177 
178     /**
179      * 410 Gone
180      */
181     public static final HttpResponseStatus GONE = newStatus(410, "Gone");
182 
183     /**
184      * 411 Length Required
185      */
186     public static final HttpResponseStatus LENGTH_REQUIRED = newStatus(411, "Length Required");
187 
188     /**
189      * 412 Precondition Failed
190      */
191     public static final HttpResponseStatus PRECONDITION_FAILED = newStatus(412, "Precondition Failed");
192 
193     /**
194      * 413 Request Entity Too Large
195      */
196     public static final HttpResponseStatus REQUEST_ENTITY_TOO_LARGE =
197             newStatus(413, "Request Entity Too Large");
198 
199     /**
200      * 414 Request-URI Too Long
201      */
202     public static final HttpResponseStatus REQUEST_URI_TOO_LONG = newStatus(414, "Request-URI Too Long");
203 
204     /**
205      * 415 Unsupported Media Type
206      */
207     public static final HttpResponseStatus UNSUPPORTED_MEDIA_TYPE = newStatus(415, "Unsupported Media Type");
208 
209     /**
210      * 416 Requested Range Not Satisfiable
211      */
212     public static final HttpResponseStatus REQUESTED_RANGE_NOT_SATISFIABLE =
213             newStatus(416, "Requested Range Not Satisfiable");
214 
215     /**
216      * 417 Expectation Failed
217      */
218     public static final HttpResponseStatus EXPECTATION_FAILED = newStatus(417, "Expectation Failed");
219 
220     /**
221      * 421 Misdirected Request
222      *
223      * <a href="https://tools.ietf.org/html/draft-ietf-httpbis-http2-15#section-9.1.2">421 Status Code</a>
224      */
225     public static final HttpResponseStatus MISDIRECTED_REQUEST = newStatus(421, "Misdirected Request");
226 
227     /**
228      * 422 Unprocessable Entity (WebDAV, RFC4918)
229      */
230     public static final HttpResponseStatus UNPROCESSABLE_ENTITY = newStatus(422, "Unprocessable Entity");
231 
232     /**
233      * 423 Locked (WebDAV, RFC4918)
234      */
235     public static final HttpResponseStatus LOCKED = newStatus(423, "Locked");
236 
237     /**
238      * 424 Failed Dependency (WebDAV, RFC4918)
239      */
240     public static final HttpResponseStatus FAILED_DEPENDENCY = newStatus(424, "Failed Dependency");
241 
242     /**
243      * 425 Unordered Collection (WebDAV, RFC3648)
244      */
245     public static final HttpResponseStatus UNORDERED_COLLECTION = newStatus(425, "Unordered Collection");
246 
247     /**
248      * 426 Upgrade Required (RFC2817)
249      */
250     public static final HttpResponseStatus UPGRADE_REQUIRED = newStatus(426, "Upgrade Required");
251 
252     /**
253      * 428 Precondition Required (RFC6585)
254      */
255     public static final HttpResponseStatus PRECONDITION_REQUIRED = newStatus(428, "Precondition Required");
256 
257     /**
258      * 429 Too Many Requests (RFC6585)
259      */
260     public static final HttpResponseStatus TOO_MANY_REQUESTS = newStatus(429, "Too Many Requests");
261 
262     /**
263      * 431 Request Header Fields Too Large (RFC6585)
264      */
265     public static final HttpResponseStatus REQUEST_HEADER_FIELDS_TOO_LARGE =
266             newStatus(431, "Request Header Fields Too Large");
267 
268     /**
269      * 500 Internal Server Error
270      */
271     public static final HttpResponseStatus INTERNAL_SERVER_ERROR = newStatus(500, "Internal Server Error");
272 
273     /**
274      * 501 Not Implemented
275      */
276     public static final HttpResponseStatus NOT_IMPLEMENTED = newStatus(501, "Not Implemented");
277 
278     /**
279      * 502 Bad Gateway
280      */
281     public static final HttpResponseStatus BAD_GATEWAY = newStatus(502, "Bad Gateway");
282 
283     /**
284      * 503 Service Unavailable
285      */
286     public static final HttpResponseStatus SERVICE_UNAVAILABLE = newStatus(503, "Service Unavailable");
287 
288     /**
289      * 504 Gateway Timeout
290      */
291     public static final HttpResponseStatus GATEWAY_TIMEOUT = newStatus(504, "Gateway Timeout");
292 
293     /**
294      * 505 HTTP Version Not Supported
295      */
296     public static final HttpResponseStatus HTTP_VERSION_NOT_SUPPORTED =
297             newStatus(505, "HTTP Version Not Supported");
298 
299     /**
300      * 506 Variant Also Negotiates (RFC2295)
301      */
302     public static final HttpResponseStatus VARIANT_ALSO_NEGOTIATES = newStatus(506, "Variant Also Negotiates");
303 
304     /**
305      * 507 Insufficient Storage (WebDAV, RFC4918)
306      */
307     public static final HttpResponseStatus INSUFFICIENT_STORAGE = newStatus(507, "Insufficient Storage");
308 
309     /**
310      * 510 Not Extended (RFC2774)
311      */
312     public static final HttpResponseStatus NOT_EXTENDED = newStatus(510, "Not Extended");
313 
314     /**
315      * 511 Network Authentication Required (RFC6585)
316      */
317     public static final HttpResponseStatus NETWORK_AUTHENTICATION_REQUIRED =
318             newStatus(511, "Network Authentication Required");
319 
320     private static HttpResponseStatus newStatus(int statusCode, String reasonPhrase) {
321         return new HttpResponseStatus(statusCode, reasonPhrase, true);
322     }
323 
324     /**
325      * Returns the [email protected] HttpResponseStatus} represented by the specified code.
326      * If the specified code is a standard HTTP getStatus code, a cached instance
327      * will be returned.  Otherwise, a new instance will be returned.
328      */
329     public static HttpResponseStatus valueOf(int code) {
330         switch (code) {
331         case 100:
332             return CONTINUE;
333         case 101:
334             return SWITCHING_PROTOCOLS;
335         case 102:
336             return PROCESSING;
337         case 200:
338             return OK;
339         case 201:
340             return CREATED;
341         case 202:
342             return ACCEPTED;
343         case 203:
344             return NON_AUTHORITATIVE_INFORMATION;
345         case 204:
346             return NO_CONTENT;
347         case 205:
348             return RESET_CONTENT;
349         case 206:
350             return PARTIAL_CONTENT;
351         case 207:
352             return MULTI_STATUS;
353         case 300:
354             return MULTIPLE_CHOICES;
355         case 301:
356             return MOVED_PERMANENTLY;
357         case 302:
358             return FOUND;
359         case 303:
360             return SEE_OTHER;
361         case 304:
362             return NOT_MODIFIED;
363         case 305:
364             return USE_PROXY;
365         case 307:
366             return TEMPORARY_REDIRECT;
367         case 308:
368             return PERMANENT_REDIRECT;
369         case 400:
370             return BAD_REQUEST;
371         case 401:
372             return UNAUTHORIZED;
373         case 402:
374             return PAYMENT_REQUIRED;
375         case 403:
376             return FORBIDDEN;
377         case 404:
378             return NOT_FOUND;
379         case 405:
380             return METHOD_NOT_ALLOWED;
381         case 406:
382             return NOT_ACCEPTABLE;
383         case 407:
384             return PROXY_AUTHENTICATION_REQUIRED;
385         case 408:
386             return REQUEST_TIMEOUT;
387         case 409:
388             return CONFLICT;
389         case 410:
390             return GONE;
391         case 411:
392             return LENGTH_REQUIRED;
393         case 412:
394             return PRECONDITION_FAILED;
395         case 413:
396             return REQUEST_ENTITY_TOO_LARGE;
397         case 414:
398             return REQUEST_URI_TOO_LONG;
399         case 415:
400             return UNSUPPORTED_MEDIA_TYPE;
401         case 416:
402             return REQUESTED_RANGE_NOT_SATISFIABLE;
403         case 417:
404             return EXPECTATION_FAILED;
405         case 421:
406             return MISDIRECTED_REQUEST;
407         case 422:
408             return UNPROCESSABLE_ENTITY;
409         case 423:
410             return LOCKED;
411         case 424:
412             return FAILED_DEPENDENCY;
413         case 425:
414             return UNORDERED_COLLECTION;
415         case 426:
416             return UPGRADE_REQUIRED;
417         case 428:
418             return PRECONDITION_REQUIRED;
419         case 429:
420             return TOO_MANY_REQUESTS;
421         case 431:
422             return REQUEST_HEADER_FIELDS_TOO_LARGE;
423         case 500:
424             return INTERNAL_SERVER_ERROR;
425         case 501:
426             return NOT_IMPLEMENTED;
427         case 502:
428             return BAD_GATEWAY;
429         case 503:
430             return SERVICE_UNAVAILABLE;
431         case 504:
432             return GATEWAY_TIMEOUT;
433         case 505:
434             return HTTP_VERSION_NOT_SUPPORTED;
435         case 506:
436             return VARIANT_ALSO_NEGOTIATES;
437         case 507:
438             return INSUFFICIENT_STORAGE;
439         case 510:
440             return NOT_EXTENDED;
441         case 511:
442             return NETWORK_AUTHENTICATION_REQUIRED;
443         }
444 
445         return new HttpResponseStatus(code);
446     }
447 
448     /**
449      * Parses the specified HTTP status line into a [email protected] HttpResponseStatus}.  The expected formats of the line are:
450      * <ul>
451      * <li>[email protected] statusCode} (e.g. 200)</li>
452      * <li>[email protected] statusCode} [email protected] reasonPhrase} (e.g. 404 Not Found)</li>
453      * </ul>
454      *
455      * @throws IllegalArgumentException if the specified status line is malformed
456      */
457     public static HttpResponseStatus parseLine(CharSequence line) {
458         String status = line.toString();
459         try {
460             int space = status.indexOf(' ');
461             if (space == -1) {
462                 return valueOf(Integer.parseInt(status));
463             } else {
464                 int code = Integer.parseInt(status.substring(0, space));
465                 String reasonPhrase = status.substring(space + 1);
466                 HttpResponseStatus responseStatus = valueOf(code);
467                 if (responseStatus.reasonPhrase().contentEquals(reasonPhrase)) {
468                     return responseStatus;
469                 } else {
470                     return new HttpResponseStatus(code, reasonPhrase);
471                 }
472             }
473         } catch (Exception e) {
474             throw new IllegalArgumentException("malformed status line: " + status, e);
475         }
476     }
477 
478     private static final class HttpStatusLineProcessor implements ByteProcessor {
479         private static final byte ASCII_SPACE = (byte) ' ';
480         private final AsciiString string;
481         private int i;
482         /**
483          * 0 = New or havn't seen [email protected] #ASCII_SPACE}.
484          * 1 = Last byte was [email protected] #ASCII_SPACE}.
485          * 2 = Terminal State. Processed the byte after [email protected] #ASCII_SPACE}, and parsed the status line.
486          * 3 = Terminal State. There was no byte after [email protected] #ASCII_SPACE} but status has been parsed with what we saw.
487          */
488         private int state;
489         private HttpResponseStatus status;
490 
491         public HttpStatusLineProcessor(AsciiString string) {
492             this.string = string;
493         }
494 
495         @Override
496         public boolean process(byte value) {
497             switch (state) {
498             case 0:
499                 if (value == ASCII_SPACE) {
500                     state = 1;
501                 }
502                 break;
503             case 1:
504                 parseStatus(i);
505                 state = 2;
506                 return false;
507             default:
508                 break;
509             }
510             ++i;
511             return true;
512         }
513 
514         private void parseStatus(int codeEnd) {
515             int code = string.parseInt(0, codeEnd);
516             status = valueOf(code);
517             if (codeEnd < string.length()) {
518                 String actualReason = string.toString(codeEnd + 1, string.length());
519                 if (!status.reasonPhrase().contentEquals(actualReason)) {
520                     status = new HttpResponseStatus(code, actualReason);
521                 }
522             }
523         }
524 
525         public HttpResponseStatus status() {
526             if (state <= 1) {
527                 parseStatus(string.length());
528                 state = 3;
529             }
530             return status;
531         }
532     }
533 
534     /**
535      * Parses the specified HTTP status line into a [email protected] HttpResponseStatus}. The expected formats of the line are:
536      * <ul>
537      * <li>[email protected] statusCode} (e.g. 200)</li>
538      * <li>[email protected] statusCode} [email protected] reasonPhrase} (e.g. 404 Not Found)</li>
539      * </ul>
540      *
541      * @throws IllegalArgumentException if the specified status line is malformed
542      */
543     public static HttpResponseStatus parseLine(AsciiString line) {
544         try {
545             HttpStatusLineProcessor processor = new HttpStatusLineProcessor(line);
546             line.forEachByte(processor);
547             HttpResponseStatus status = processor.status();
548             if (status == null) {
549                 throw new IllegalArgumentException("unable to get status after parsing input");
550             }
551             return status;
552         } catch (Exception e) {
553             throw new IllegalArgumentException("malformed status line: " + line, e);
554         }
555     }
556 
557     private final int code;
558     private final AsciiString codeAsText;
559     private HttpStatusClass codeClass;
560 
561     private final String reasonPhrase;
562     private final byte[] bytes;
563 
564     /**
565      * Creates a new instance with the specified [email protected] code} and the auto-generated default reason phrase.
566      */
567     private HttpResponseStatus(int code) {
568         this(code, HttpStatusClass.valueOf(code).defaultReasonPhrase() + " (" + code + ')', false);
569     }
570 
571     /**
572      * Creates a new instance with the specified [email protected] code} and its [email protected] reasonPhrase}.
573      */
574     public HttpResponseStatus(int code, String reasonPhrase) {
575         this(code, reasonPhrase, false);
576     }
577 
578     private HttpResponseStatus(int code, String reasonPhrase, boolean bytes) {
579         if (code < 0) {
580             throw new IllegalArgumentException(
581                     "code: " + code + " (expected: 0+)");
582         }
583 
584         if (reasonPhrase == null) {
585             throw new NullPointerException("reasonPhrase");
586         }
587 
588         for (int i = 0; i < reasonPhrase.length(); i ++) {
589             char c = reasonPhrase.charAt(i);
590             // Check prohibited characters.
591             switch (c) {
592                 case '\n': case '\r':
593                     throw new IllegalArgumentException(
594                             "reasonPhrase contains one of the following prohibited characters: " +
595                                     "\\r\\n: " + reasonPhrase);
596             }
597         }
598 
599         this.code = code;
600         codeAsText = new AsciiString(Integer.toString(code));
601         this.reasonPhrase = reasonPhrase;
602         if (bytes) {
603             this.bytes = (code + " " + reasonPhrase).getBytes(CharsetUtil.US_ASCII);
604         } else {
605             this.bytes = null;
606         }
607     }
608 
609     /**
610      * Returns the code of this [email protected] HttpResponseStatus}.
611      */
612     public int code() {
613         return code;
614     }
615 
616     /**
617      * Returns the status code as [email protected] AsciiString}.
618      */
619     public AsciiString codeAsText() {
620         return codeAsText;
621     }
622 
623     /**
624      * Returns the reason phrase of this [email protected] HttpResponseStatus}.
625      */
626     public String reasonPhrase() {
627         return reasonPhrase;
628     }
629 
630     /**
631      * Returns the class of this [email protected] HttpResponseStatus}
632      */
633     public HttpStatusClass codeClass() {
634         HttpStatusClass type = this.codeClass;
635         if (type == null) {
636             this.codeClass = type = HttpStatusClass.valueOf(code);
637         }
638         return type;
639     }
640 
641     @Override
642     public int hashCode() {
643         return code();
644     }
645 
646     /**
647      * Equality of [email protected] HttpResponseStatus} only depends on [email protected] #code()}. The
648      * reason phrase is not considered for equality.
649      */
650     @Override
651     public boolean equals(Object o) {
652         if (!(o instanceof HttpResponseStatus)) {
653             return false;
654         }
655 
656         return code() == ((HttpResponseStatus) o).code();
657     }
658 
659     /**
660      * Equality of [email protected] HttpResponseStatus} only depends on [email protected] #code()}. The
661      * reason phrase is not considered for equality.
662      */
663     @Override
664     public int compareTo(HttpResponseStatus o) {
665         return code() - o.code();
666     }
667 
668     @Override
669     public String toString() {
670         return new StringBuilder(reasonPhrase.length() + 5)
671             .append(code)
672             .append(' ')
673             .append(reasonPhrase)
674             .toString();
675     }
676 
677     void encode(ByteBuf buf) {
678         if (bytes == null) {
679             HttpUtil.encodeAscii0(String.valueOf(code()), buf);
680             buf.writeByte(SP);
681             HttpUtil.encodeAscii0(String.valueOf(reasonPhrase()), buf);
682         } else {
683             buf.writeBytes(bytes);
684         }
685     }
686 }