1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html;
16
17 import static org.htmlunit.BrowserVersionFeatures.ANCHOR_SEND_PING_REQUEST;
18 import static org.htmlunit.BrowserVersionFeatures.FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE;
19 import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
20
21 import java.io.IOException;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.Locale;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.htmlunit.BrowserVersion;
29 import org.htmlunit.FormEncodingType;
30 import org.htmlunit.HttpHeader;
31 import org.htmlunit.HttpMethod;
32 import org.htmlunit.Page;
33 import org.htmlunit.WebClient;
34 import org.htmlunit.WebRequest;
35 import org.htmlunit.WebWindow;
36 import org.htmlunit.html.csp.ContentSecurityPolicy;
37 import org.htmlunit.protocol.javascript.JavaScriptURLConnection;
38 import org.htmlunit.util.ArrayUtils;
39 import org.htmlunit.util.StringUtils;
40 import org.htmlunit.util.UrlUtils;
41
42
43
44
45
46
47
48
49
50
51 public final class HyperlinkElementSupport {
52
53 private static final Log LOG = LogFactory.getLog(HyperlinkElementSupport.class);
54
55 private HyperlinkElementSupport() {
56
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public static void doClickStateUpdate(final HyperlinkElement element, final boolean shiftKey,
73 final boolean ctrlKey, final String hrefSuffix) throws IOException {
74 final HtmlElement htmlElement = (HtmlElement) element;
75
76 final String href = (element.getHrefAttribute() + hrefSuffix).trim();
77 if (LOG.isDebugEnabled()) {
78 final String w = htmlElement.getPage().getEnclosingWindow().getName();
79 LOG.debug("do click action in window '" + w + "', using href '" + href + "'");
80 }
81 if (ATTRIBUTE_NOT_DEFINED == element.getHrefAttribute()) {
82 return;
83 }
84 final String downloadAttribute = element.getDownloadAttribute();
85 HtmlPage page = (HtmlPage) htmlElement.getPage();
86 if (StringUtils.startsWithIgnoreCase(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
87 if (!ContentSecurityPolicy.allowsJavaScriptUrl(page, htmlElement, href)) {
88 return;
89 }
90
91 final StringBuilder builder = new StringBuilder(href.length());
92 builder.append(JavaScriptURLConnection.JAVASCRIPT_PREFIX);
93 for (int i = JavaScriptURLConnection.JAVASCRIPT_PREFIX.length(); i < href.length(); i++) {
94 final char ch = href.charAt(i);
95 if (ch == '%' && i + 2 < href.length()) {
96 final char ch1 = Character.toUpperCase(href.charAt(i + 1));
97 final char ch2 = Character.toUpperCase(href.charAt(i + 2));
98 if ((Character.isDigit(ch1) || ch1 >= 'A' && ch1 <= 'F')
99 && (Character.isDigit(ch2) || ch2 >= 'A' && ch2 <= 'F')) {
100 builder.append((char) Integer.parseInt(href.substring(i + 1, i + 3), 16));
101 i += 2;
102 continue;
103 }
104 }
105 builder.append(ch);
106 }
107
108 final String target;
109 if (shiftKey || ctrlKey || ATTRIBUTE_NOT_DEFINED != downloadAttribute) {
110 target = WebClient.TARGET_BLANK;
111 }
112 else {
113 target = page.getResolvedTarget(element.getTargetAttribute());
114 }
115 final WebWindow win = page.getWebClient().openTargetWindow(page.getEnclosingWindow(),
116 target, WebClient.TARGET_SELF);
117 Page enclosedPage = win.getEnclosedPage();
118 if (enclosedPage == null) {
119 win.getWebClient().getPage(win, WebRequest.newAboutBlankRequest());
120 enclosedPage = win.getEnclosedPage();
121 }
122 if (enclosedPage != null && enclosedPage.isHtmlPage()) {
123 page = (HtmlPage) enclosedPage;
124 page.executeJavaScript(builder.toString(), "javascript url", htmlElement.getStartLineNumber());
125 }
126 return;
127 }
128
129 final URL url = getTargetUrl(href, page);
130
131 final URL pageUrl = page.getUrl();
132
133 final WebClient webClient = page.getWebClient();
134 final BrowserVersion browser = webClient.getBrowserVersion();
135 if (ATTRIBUTE_NOT_DEFINED != element.getPingAttribute() && browser.hasFeature(ANCHOR_SEND_PING_REQUEST)) {
136 final URL pingUrl = getTargetUrl(element.getPingAttribute(), page);
137 final WebRequest pingRequest = new WebRequest(pingUrl, HttpMethod.POST);
138 pingRequest.setAdditionalHeader(HttpHeader.ACCEPT_ENCODING, browser.getAcceptEncodingHeader());
139 pingRequest.setAdditionalHeader(HttpHeader.PING_FROM, pageUrl.toExternalForm());
140 pingRequest.setAdditionalHeader(HttpHeader.PING_TO, url.toExternalForm());
141 pingRequest.setEncodingType(FormEncodingType.TEXT_PLAIN);
142 try {
143 pingRequest.setAdditionalHeader(HttpHeader.ORIGIN,
144 UrlUtils.getUrlWithProtocolAndAuthority(pageUrl).toExternalForm());
145 }
146 catch (final MalformedURLException e) {
147 if (LOG.isInfoEnabled()) {
148 LOG.info("Invalid origin url '" + pageUrl + "'");
149 }
150 }
151 if (browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE)) {
152 pingRequest.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "max-age=0");
153 }
154 pingRequest.setRequestBody("PING");
155
156
157
158 pingRequest.setFetchDestination(WebRequest.FetchDestination.EMPTY);
159 pingRequest.setFetchModeOverride(WebRequest.FetchMode.NO_CORS);
160 pingRequest.setRequestingUrl(pageUrl);
161
162 try {
163 webClient.loadWebResponse(pingRequest);
164 }
165 catch (final Exception e) {
166
167 }
168 }
169
170
171 final WebRequest webRequest = new WebRequest(ContentSecurityPolicy.upgradeInsecureNavigation(page, url),
172 browser.getHtmlAcceptHeader(),
173 browser.getAcceptEncodingHeader());
174 ContentSecurityPolicy.upgradeInsecureNavigationRedirects(page, webRequest);
175
176 webRequest.setCharset(page.getCharset());
177
178
179
180
181
182
183
184
185
186
187
188 webRequest.markAsNavigation(page.getUrl(), true);
189
190 if (!relContainsNoreferrer(element)) {
191 webRequest.setRefererHeader(page.getUrl());
192 }
193
194 if (LOG.isDebugEnabled()) {
195 LOG.debug(
196 "Getting page for " + url.toExternalForm()
197 + ", derived from href '" + href
198 + "', using the originating URL "
199 + page.getUrl());
200 }
201
202 final String target;
203 if (shiftKey || ctrlKey
204 || (webClient.getAttachmentHandler() == null
205 && ATTRIBUTE_NOT_DEFINED != downloadAttribute)) {
206 target = WebClient.TARGET_BLANK;
207 }
208 else {
209 target = page.getResolvedTarget(element.getTargetAttribute());
210 }
211 page.getWebClient().download(page.getEnclosingWindow(), target, webRequest,
212 true, (ATTRIBUTE_NOT_DEFINED == downloadAttribute) ? null : downloadAttribute,
213 htmlElement.getClass().getSimpleName() + " click");
214 }
215
216
217
218
219
220
221
222 public static boolean relContainsNoreferrer(final HyperlinkElement element) {
223 String rel = element.getRelAttribute();
224 if (rel != null) {
225 rel = rel.toLowerCase(Locale.ROOT);
226 return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "noreferrer");
227 }
228 return false;
229 }
230
231
232
233
234
235
236
237
238
239 public static URL getTargetUrl(final String href, final HtmlPage page) throws MalformedURLException {
240 URL url = page.getFullyQualifiedUrl(href);
241
242 if (StringUtils.isEmptyOrNull(href)) {
243 url = UrlUtils.getUrlWithNewRef(url, null);
244 }
245 return url;
246 }
247 }