1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate.util;
17
18 import java.util.List;
19
20 import org.apache.http.Header;
21 import org.apache.http.HttpHeaders;
22 import org.apache.http.HttpHost;
23 import org.apache.http.HttpRequest;
24 import org.apache.http.NameValuePair;
25 import org.esigate.impl.DriverRequest;
26
27 public final class HttpRequestHelper {
28
29 private HttpRequestHelper() {
30 }
31
32 public static String getFirstHeader(String name, HttpRequest request) {
33 final Header header = request.getFirstHeader(name);
34 String headerValue = null;
35 if (header != null) {
36 headerValue = header.getValue();
37 }
38 return headerValue;
39 }
40
41 public static String getParameter(DriverRequest request, String name) {
42 String characterEncoding = request.getCharacterEncoding();
43 if (characterEncoding == null) {
44 characterEncoding = "ISO-8859-1";
45 }
46 List<NameValuePair> parameters =
47 UriUtils.parse(request.getOriginalRequest().getRequestLine().getUri(), characterEncoding);
48 for (NameValuePair nameValuePair : parameters) {
49 if (nameValuePair.getName().equals(name)) {
50 return nameValuePair.getValue();
51 }
52 }
53 return null;
54 }
55
56
57
58
59
60
61
62
63
64 public static HttpHost getHost(HttpRequest request) {
65 HttpHost httpHost = UriUtils.extractHost(request.getRequestLine().getUri());
66 String scheme = httpHost.getSchemeName();
67 String host = httpHost.getHostName();
68 int port = httpHost.getPort();
69 Header[] headers = request.getHeaders(HttpHeaders.HOST);
70 if (headers != null && headers.length != 0) {
71 String headerValue = headers[0].getValue();
72 String[] splitted = headerValue.split(":");
73 host = splitted[0];
74 if (splitted.length > 1) {
75 port = Integer.parseInt(splitted[1]);
76 } else {
77 port = -1;
78 }
79 }
80 return new HttpHost(host, port, scheme);
81 }
82
83 }