// hope that it's a server relative request, with a valid referrer:
String referer = httpRequest.getHeader("Referer");
if (referer == null) return null;
final UsableURI refuri = UsableURIFactory.getInstance(referer);
// Check that the Referer is our current wayback path
// before attempting to use referer as base archival url
if ((matchHost != null && !matchHost.equals(refuri.getHost())) ||
(matchPort != -1 && refuri.getPort() != -1 && matchPort != refuri
.getPort())) {
LOGGER.info("Server-Relative-Redirect: Skipping, Referer " +
refuri.getHost() + ":" + refuri.getPort() +
" not from matching wayback host:port\t");
return null;
}
String path = refuri.getPath();
int secondSlash = path.indexOf('/', 1);
if (secondSlash == -1) return null;
String collection = path.substring(0, secondSlash);
collection = modifyCollection(collection);
String remainder = path.substring(secondSlash + 1);
int thirdSlash = remainder.indexOf('/');
if (thirdSlash == -1) return null;
String datespec = remainder.substring(0, thirdSlash);
if (!datespec.isEmpty() &&
!Character.isDigit(datespec.charAt(0))) {
datespec = null;
}
String url = remainder.substring(thirdSlash + 1);
url = UrlOperations.fixupScheme(url);
url = ArchiveUtils.addImpliedHttpIfNecessary(url);
String thisPath = httpRequest.getRequestURI();
String queryString = httpRequest.getQueryString();
if (queryString != null) {
thisPath += "?" + queryString;
}
String resolved = UrlOperations.resolveUrl(url, thisPath);
String contextPath = httpRequest.getContextPath();
StringBuilder sb = new StringBuilder(refuri.getScheme());
sb.append("://");
sb.append(refuri.getAuthority());
sb.append(contextPath);
sb.append(collection);
sb.append("/");
if (datespec != null) {
sb.append(datespec);