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