@@ -41,37 +41,6 @@ public boolean isHttps() {
41
41
}
42
42
}
43
43
44
- /**
45
- * Returns the host and port of this HTTPS request, including any modifications by other filters.
46
- *
47
- * @return host and port of this HTTPS request
48
- * @throws IllegalStateException if this is not an HTTPS request
49
- */
50
- public String getHttpsRequestHostAndPort () throws IllegalStateException {
51
- if (!isHttps ()) {
52
- throw new IllegalStateException ("Request is not HTTPS. Cannot get host and port on non-HTTPS request using this method." );
53
- }
54
-
55
- Attribute <String > hostnameAttr = ctx .attr (AttributeKey .<String >valueOf (HOST_ATTRIBUTE_NAME ));
56
- return hostnameAttr .get ();
57
- }
58
-
59
- /**
60
- * Returns the original host and port of this HTTPS request, as sent by the client. Does not reflect any modifications
61
- * by other filters.
62
- *
63
- * @return host and port of this HTTPS request
64
- * @throws IllegalStateException if this is not an HTTPS request
65
- */
66
- public String getHttpsOriginalRequestHostAndPort () throws IllegalStateException {
67
- if (!isHttps ()) {
68
- throw new IllegalStateException ("Request is not HTTPS. Cannot get original host and port on non-HTTPS request using this method." );
69
- }
70
-
71
- Attribute <String > hostnameAttr = ctx .attr (AttributeKey .<String >valueOf (ORIGINAL_HOST_ATTRIBUTE_NAME ));
72
- return hostnameAttr .get ();
73
- }
74
-
75
44
/**
76
45
* Returns the full, absolute URL of the specified request for both HTTP and HTTPS URLs. The request may reflect
77
46
* modifications from this or other filters. This filter instance must be currently handling the specified request;
@@ -91,19 +60,14 @@ public String getFullUrl(HttpRequest modifiedRequest) {
91
60
// To get the full URL, we need to retrieve the Scheme, Host + Port, Path, and Query Params from the request.
92
61
// Scheme: the scheme (HTTP/HTTPS) may or may not be part of the request, and so must be generated based on the
93
62
// type of connection.
94
- // Host and Port: for HTTP requests, the host and port can be read from the request itself using the URI and/or
95
- // Host header. for HTTPS requests, the host and port are not available in the request. by using the
96
- // getHttpsRequestHostAndPort() helper method in HttpsAwareFiltersAdapter, we can capture the host and port for
97
- // HTTPS requests.
98
- // Path + Query Params + Fragment: these elements are contained in the HTTP request
63
+ // Host and Port: available for HTTP and HTTPS requests using the getHostAndPort() helper method.
64
+ // Path + Query Params + Fragment: these elements are contained in the HTTP request for both HTTP and HTTPS
65
+ String hostAndPort = getHostAndPort (modifiedRequest );
66
+ String path = BrowserMobHttpUtil .getPathFromRequest (modifiedRequest );
99
67
String url ;
100
68
if (isHttps ()) {
101
- String hostAndPort = getHttpsRequestHostAndPort ();
102
- String path = BrowserMobHttpUtil .getPathFromRequest (modifiedRequest );
103
69
url = "https://" + hostAndPort + path ;
104
70
} else {
105
- String hostAndPort = BrowserMobHttpUtil .getHostAndPortFromRequest (modifiedRequest );
106
- String path = BrowserMobHttpUtil .getPathFromRequest (modifiedRequest );
107
71
url = "http://" + hostAndPort + path ;
108
72
}
109
73
return url ;
@@ -120,14 +84,14 @@ public String getOriginalUrl() {
120
84
}
121
85
122
86
/**
123
- * Returns the host and port of the specified request for both HTTP and HTTPS requests. The request may reflect
87
+ * Returns the hostname (but not the port) the specified request for both HTTP and HTTPS requests. The request may reflect
124
88
* modifications from this or other filters. This filter instance must be currently handling the specified request;
125
89
* otherwise the results are undefined.
126
90
*
127
91
* @param modifiedRequest a possibly-modified version of the request currently being processed
128
- * @return host and port of the specified request
92
+ * @return hostname of the specified request, without the port
129
93
*/
130
- public String getHostAndPort (HttpRequest modifiedRequest ) {
94
+ public String getHost (HttpRequest modifiedRequest ) {
131
95
String serverHost ;
132
96
if (isHttps ()) {
133
97
HostAndPort hostAndPort = HostAndPort .fromString (getHttpsRequestHostAndPort ());
@@ -137,4 +101,55 @@ public String getHostAndPort(HttpRequest modifiedRequest) {
137
101
}
138
102
return serverHost ;
139
103
}
104
+
105
+ /**
106
+ * Returns the host and port of the specified request for both HTTP and HTTPS requests. The request may reflect
107
+ * modifications from this or other filters. This filter instance must be currently handling the specified request;
108
+ * otherwise the results are undefined.
109
+ *
110
+ * @param modifiedRequest a possibly-modified version of the request currently being processed
111
+ * @return host and port of the specified request
112
+ */
113
+ public String getHostAndPort (HttpRequest modifiedRequest ) {
114
+ // For HTTP requests, the host and port can be read from the request itself using the URI and/or
115
+ // Host header. for HTTPS requests, the host and port are not available in the request. by using the
116
+ // getHttpsRequestHostAndPort() helper method, we can retrieve the host and port for HTTPS requests.
117
+ if (isHttps ()) {
118
+ return getHttpsRequestHostAndPort ();
119
+ } else {
120
+ return BrowserMobHttpUtil .getHostAndPortFromRequest (modifiedRequest );
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Returns the host and port of this HTTPS request, including any modifications by other filters.
126
+ *
127
+ * @return host and port of this HTTPS request
128
+ * @throws IllegalStateException if this is not an HTTPS request
129
+ */
130
+ private String getHttpsRequestHostAndPort () throws IllegalStateException {
131
+ if (!isHttps ()) {
132
+ throw new IllegalStateException ("Request is not HTTPS. Cannot get host and port on non-HTTPS request using this method." );
133
+ }
134
+
135
+ Attribute <String > hostnameAttr = ctx .attr (AttributeKey .<String >valueOf (HOST_ATTRIBUTE_NAME ));
136
+ return hostnameAttr .get ();
137
+ }
138
+
139
+ /**
140
+ * Returns the original host and port of this HTTPS request, as sent by the client. Does not reflect any modifications
141
+ * by other filters.
142
+ * TODO: evaluate this (unused) method and its capture mechanism in HttpsOriginalHostCaptureFilter; remove if not useful.
143
+ *
144
+ * @return host and port of this HTTPS request
145
+ * @throws IllegalStateException if this is not an HTTPS request
146
+ */
147
+ private String getHttpsOriginalRequestHostAndPort () throws IllegalStateException {
148
+ if (!isHttps ()) {
149
+ throw new IllegalStateException ("Request is not HTTPS. Cannot get original host and port on non-HTTPS request using this method." );
150
+ }
151
+
152
+ Attribute <String > hostnameAttr = ctx .attr (AttributeKey .<String >valueOf (ORIGINAL_HOST_ATTRIBUTE_NAME ));
153
+ return hostnameAttr .get ();
154
+ }
140
155
}
0 commit comments