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