1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.proxy.handlers.http;
21
22 import java.net.InetSocketAddress;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.mina.proxy.ProxyAuthException;
29 import org.apache.mina.proxy.handlers.ProxyRequest;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39 public class HttpProxyRequest extends ProxyRequest {
40 private static final Logger LOGGER = LoggerFactory.getLogger(HttpProxyRequest.class);
41
42
43
44
45 private final String httpVerb;
46
47
48
49
50 private final String httpURI;
51
52
53
54
55 private String httpVersion;
56
57
58
59
60 private String host;
61
62
63
64
65 private Map<String, List<String>> headers;
66
67
68
69
70
71 private Map<String, String> properties;
72
73
74
75
76
77
78
79 public HttpProxyRequest(final InetSocketAddress endpointAddress) {
80 this(endpointAddress, HttpProxyConstants.HTTP_1_0, null);
81 }
82
83
84
85
86
87
88
89
90 public HttpProxyRequest(final InetSocketAddress endpointAddress, final String httpVersion) {
91 this(endpointAddress, httpVersion, null);
92 }
93
94
95
96
97
98
99
100
101
102 public HttpProxyRequest(final InetSocketAddress endpointAddress, final String httpVersion,
103 final Map<String, List<String>> headers) {
104 this.httpVerb = HttpProxyConstants.CONNECT;
105 if (endpointAddress.isUnresolved()) {
106 this.httpURI = endpointAddress.getHostName() + ":" + endpointAddress.getPort();
107 } else {
108 this.httpURI = endpointAddress.getAddress().getHostAddress() + ":" + endpointAddress.getPort();
109 }
110
111 this.httpVersion = httpVersion;
112 this.headers = headers;
113 }
114
115
116
117
118
119
120
121 public HttpProxyRequest(final String httpURI) {
122 this(HttpProxyConstants.GET, httpURI, HttpProxyConstants.HTTP_1_0, null);
123 }
124
125
126
127
128
129
130
131
132 public HttpProxyRequest(final String httpURI, final String httpVersion) {
133 this(HttpProxyConstants.GET, httpURI, httpVersion, null);
134 }
135
136
137
138
139
140
141
142
143
144 public HttpProxyRequest(final String httpVerb, final String httpURI, final String httpVersion) {
145 this(httpVerb, httpURI, httpVersion, null);
146 }
147
148
149
150
151
152
153
154
155
156
157
158 public HttpProxyRequest(final String httpVerb, final String httpURI, final String httpVersion,
159 final Map<String, List<String>> headers) {
160 this.httpVerb = httpVerb;
161 this.httpURI = httpURI;
162 this.httpVersion = httpVersion;
163 this.headers = headers;
164 }
165
166
167
168
169 public final String getHttpVerb() {
170 return httpVerb;
171 }
172
173
174
175
176 public String getHttpVersion() {
177 return httpVersion;
178 }
179
180
181
182
183
184
185 public void setHttpVersion(String httpVersion) {
186 this.httpVersion = httpVersion;
187 }
188
189
190
191
192 public final synchronized String getHost() {
193 if (host == null) {
194 if (getEndpointAddress() != null && !getEndpointAddress().isUnresolved()) {
195 host = getEndpointAddress().getHostName();
196 }
197
198 if (host == null && httpURI != null) {
199 try {
200 host = (new URL(httpURI)).getHost();
201 } catch (MalformedURLException e) {
202 if (LOGGER.isDebugEnabled()) {
203 LOGGER.debug("Malformed URL", e);
204 }
205 }
206 }
207 }
208
209 return host;
210 }
211
212
213
214
215 public final String getHttpURI() {
216 return httpURI;
217 }
218
219
220
221
222 public final Map<String, List<String>> getHeaders() {
223 return headers;
224 }
225
226
227
228
229
230
231 public final void setHeaders(Map<String, List<String>> headers) {
232 this.headers = headers;
233 }
234
235
236
237
238 public Map<String, String> getProperties() {
239 return properties;
240 }
241
242
243
244
245
246
247 public void setProperties(Map<String, String> properties) {
248 this.properties = properties;
249 }
250
251
252
253
254
255
256
257
258 public void checkRequiredProperties(String... propNames) throws ProxyAuthException {
259 StringBuilder sb = new StringBuilder();
260 for (String propertyName : propNames) {
261 if (properties.get(propertyName) == null) {
262 sb.append(propertyName).append(' ');
263 }
264 }
265 if (sb.length() > 0) {
266 sb.append("property(ies) missing in request");
267 throw new ProxyAuthException(sb.toString());
268 }
269 }
270
271
272
273
274 public String toHttpString() {
275 StringBuilder sb = new StringBuilder();
276
277 sb.append(getHttpVerb()).append(' ').append(getHttpURI()).append(' ').append(getHttpVersion())
278 .append(HttpProxyConstants.CRLF);
279
280 boolean hostHeaderFound = false;
281
282 if (getHeaders() != null) {
283 for (Map.Entry<String, List<String>> header : getHeaders().entrySet()) {
284 if (!hostHeaderFound) {
285 hostHeaderFound = "host".equalsIgnoreCase(header.getKey());
286 }
287
288 for (String value : header.getValue()) {
289 sb.append(header.getKey()).append(": ").append(value).append(HttpProxyConstants.CRLF);
290 }
291 }
292
293 if (!hostHeaderFound && HttpProxyConstants.HTTP_1_1.equals(getHttpVersion())) {
294 sb.append("Host: ").append(getHost()).append(HttpProxyConstants.CRLF);
295 }
296 }
297
298 sb.append(HttpProxyConstants.CRLF);
299
300 return sb.toString();
301 }
302 }