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    *   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  package io.netty.handler.codec.http;
17  
18  import static io.netty.handler.codec.http.DefaultHttpHeadersFactory.headersFactory;
19  import static io.netty.util.internal.ObjectUtil.checkNotNull;
20  
21  /**
22   * The default {@link HttpRequest} implementation.
23   */
24  public class DefaultHttpRequest extends DefaultHttpMessage implements HttpRequest {
25      private static final int HASH_CODE_PRIME = 31;
26      private HttpMethod method;
27      private String uri;
28      private final boolean validateRequestLine;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param httpVersion the HTTP version of the request
34       * @param method      the HTTP method of the request
35       * @param uri         the URI or path of the request
36       */
37      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri) {
38          this(httpVersion, method, uri, headersFactory().newHeaders());
39      }
40  
41      /**
42       * Creates a new instance.
43       *
44       * @param httpVersion       the HTTP version of the request
45       * @param method            the HTTP method of the request
46       * @param uri               the URI or path of the request
47       * @param validateHeaders   validate the header names and values when adding them to the {@link HttpHeaders}
48       * @deprecated Prefer the {@link #DefaultHttpRequest(HttpVersion, HttpMethod, String)} constructor instead,
49       * to always have header validation enabled.
50       */
51      @Deprecated
52      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, boolean validateHeaders) {
53          this(httpVersion, method, uri, headersFactory().withValidation(validateHeaders));
54      }
55  
56      /**
57       * Creates a new instance.
58       *
59       * @param httpVersion       the HTTP version of the request
60       * @param method            the HTTP method of the request
61       * @param uri               the URI or path of the request
62       * @param headersFactory    the {@link HttpHeadersFactory} used to create the headers for this Request.
63       * The recommended default is {@link DefaultHttpHeadersFactory#headersFactory()}.
64       */
65      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri,
66                                HttpHeadersFactory headersFactory) {
67          this(httpVersion, method, uri, headersFactory.newHeaders());
68      }
69  
70      /**
71       * Creates a new instance.
72       *
73       * @param httpVersion       the HTTP version of the request
74       * @param method            the HTTP method of the request
75       * @param uri               the URI or path of the request
76       * @param headers           the Headers for this Request
77       */
78      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, HttpHeaders headers) {
79          this(httpVersion, method, uri, headers, true);
80      }
81  
82      /**
83       * Creates a new instance.
84       *
85       * @param httpVersion       the HTTP version of the request
86       * @param method            the HTTP method of the request
87       * @param uri               the URI or path of the request
88       * @param headers           the Headers for this Request
89       */
90      public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri, HttpHeaders headers,
91                                boolean validateRequestLine) {
92          super(httpVersion, headers);
93          this.method = checkNotNull(method, "method");
94          this.uri = checkNotNull(uri, "uri");
95          this.validateRequestLine = validateRequestLine;
96          if (validateRequestLine) {
97              HttpUtil.validateRequestLineTokens(method, uri);
98          }
99      }
100 
101     @Override
102     @Deprecated
103     public HttpMethod getMethod() {
104         return method();
105     }
106 
107     @Override
108     public HttpMethod method() {
109         return method;
110     }
111 
112     @Override
113     @Deprecated
114     public String getUri() {
115         return uri();
116     }
117 
118     @Override
119     public String uri() {
120         return uri;
121     }
122 
123     @Override
124     public HttpRequest setMethod(HttpMethod method) {
125         checkNotNull(method, "method");
126         if (validateRequestLine) {
127             HttpUtil.validateRequestLineTokens(method, uri);
128         }
129         this.method = method;
130         return this;
131     }
132 
133     @Override
134     public HttpRequest setUri(String uri) {
135         checkNotNull(uri, "uri");
136         if (validateRequestLine) {
137             HttpUtil.validateRequestLineTokens(method, uri);
138         }
139         this.uri = uri;
140         return this;
141     }
142 
143     @Override
144     public HttpRequest setProtocolVersion(HttpVersion version) {
145         super.setProtocolVersion(version);
146         return this;
147     }
148 
149     @Override
150     public int hashCode() {
151         int result = 1;
152         result = HASH_CODE_PRIME * result + method.hashCode();
153         result = HASH_CODE_PRIME * result + uri.hashCode();
154         result = HASH_CODE_PRIME * result + super.hashCode();
155         return result;
156     }
157 
158     @Override
159     public boolean equals(Object o) {
160         if (!(o instanceof DefaultHttpRequest)) {
161             return false;
162         }
163 
164         DefaultHttpRequest other = (DefaultHttpRequest) o;
165 
166         return method().equals(other.method()) &&
167                uri().equalsIgnoreCase(other.uri()) &&
168                super.equals(o);
169     }
170 
171     @Override
172     public String toString() {
173         return HttpMessageUtil.appendRequest(new StringBuilder(256), this).toString();
174     }
175 }