2013-04-03 15:53:57 +02:00
|
|
|
package com.commafeed.backend;
|
|
|
|
|
|
2013-04-11 12:49:54 +02:00
|
|
|
import java.io.IOException;
|
2013-04-19 13:24:46 +02:00
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
|
import java.security.cert.X509Certificate;
|
|
|
|
|
|
2014-08-17 14:16:30 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
import javax.inject.Singleton;
|
2013-04-19 13:24:46 +02:00
|
|
|
import javax.net.ssl.KeyManager;
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
2013-04-11 12:49:54 +02:00
|
|
|
|
2014-08-19 00:56:21 +02:00
|
|
|
import lombok.Getter;
|
2014-08-17 14:16:30 +02:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2013-08-11 11:45:32 +02:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
2014-08-08 16:49:02 +02:00
|
|
|
import org.apache.commons.io.IOUtils;
|
2014-10-28 16:36:09 +01:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
2013-11-05 15:10:09 +01:00
|
|
|
import org.apache.http.Consts;
|
2013-04-17 12:49:03 +02:00
|
|
|
import org.apache.http.Header;
|
2013-04-03 15:53:57 +02:00
|
|
|
import org.apache.http.HttpEntity;
|
2013-04-17 12:49:03 +02:00
|
|
|
import org.apache.http.HttpHeaders;
|
2013-08-22 15:35:34 +02:00
|
|
|
import org.apache.http.HttpHost;
|
2014-04-04 11:41:52 +02:00
|
|
|
import org.apache.http.HttpResponseInterceptor;
|
2013-04-17 12:49:03 +02:00
|
|
|
import org.apache.http.HttpStatus;
|
2013-04-11 12:49:54 +02:00
|
|
|
import org.apache.http.client.ClientProtocolException;
|
2013-04-17 12:49:03 +02:00
|
|
|
import org.apache.http.client.HttpResponseException;
|
2013-11-05 15:10:09 +01:00
|
|
|
import org.apache.http.client.config.CookieSpecs;
|
|
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
2013-04-03 15:53:57 +02:00
|
|
|
import org.apache.http.client.methods.HttpGet;
|
2013-08-22 15:35:34 +02:00
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
2013-11-05 15:10:09 +01:00
|
|
|
import org.apache.http.client.protocol.HttpClientContext;
|
|
|
|
|
import org.apache.http.config.ConnectionConfig;
|
2015-03-19 11:22:56 +01:00
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
2013-11-05 15:10:09 +01:00
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
2013-04-03 15:53:57 +02:00
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
2014-08-19 12:49:31 +02:00
|
|
|
import com.commafeed.CommaFeedConfiguration;
|
|
|
|
|
|
2013-07-26 16:00:02 +02:00
|
|
|
/**
|
2013-08-13 12:15:30 +02:00
|
|
|
* Smart HTTP getter: handles gzip, ssl, last modified and etag headers
|
2013-07-26 16:00:02 +02:00
|
|
|
*
|
|
|
|
|
*/
|
2013-08-11 11:45:32 +02:00
|
|
|
@Slf4j
|
2014-08-17 14:16:30 +02:00
|
|
|
@Singleton
|
2013-04-03 15:53:57 +02:00
|
|
|
public class HttpGetter {
|
|
|
|
|
|
2013-07-02 16:17:34 +02:00
|
|
|
private static final String ACCEPT_LANGUAGE = "en";
|
|
|
|
|
private static final String PRAGMA_NO_CACHE = "No-cache";
|
|
|
|
|
private static final String CACHE_CONTROL_NO_CACHE = "no-cache";
|
2014-10-28 16:36:09 +01:00
|
|
|
|
2014-10-03 00:31:47 -04:00
|
|
|
private static final HttpResponseInterceptor REMOVE_INCORRECT_CONTENT_ENCODING = new ContentEncodingInterceptor();
|
2014-04-04 11:41:52 +02:00
|
|
|
|
2013-04-19 13:24:46 +02:00
|
|
|
private static SSLContext SSL_CONTEXT = null;
|
|
|
|
|
static {
|
|
|
|
|
try {
|
|
|
|
|
SSL_CONTEXT = SSLContext.getInstance("TLS");
|
2013-07-25 09:17:33 +02:00
|
|
|
SSL_CONTEXT.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
|
2013-04-19 13:24:46 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Could not configure ssl context");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 12:49:31 +02:00
|
|
|
private String userAgent;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public HttpGetter(CommaFeedConfiguration config) {
|
2018-07-31 15:21:45 +02:00
|
|
|
this.userAgent = config.getApplicationSettings().getUserAgent();
|
|
|
|
|
if (this.userAgent == null) {
|
|
|
|
|
this.userAgent = String.format("CommaFeed/%s (https://github.com/Athou/commafeed)", config.getVersion());
|
|
|
|
|
}
|
2014-08-19 12:49:31 +02:00
|
|
|
}
|
|
|
|
|
|
2013-07-25 09:17:33 +02:00
|
|
|
public HttpResult getBinary(String url, int timeout) throws ClientProtocolException, IOException, NotModifiedException {
|
2013-06-29 12:05:22 +02:00
|
|
|
return getBinary(url, null, null, timeout);
|
2013-04-03 15:53:57 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-17 12:49:03 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param url
|
|
|
|
|
* the url to retrive
|
|
|
|
|
* @param lastModified
|
|
|
|
|
* header we got last time we queried that url, or null
|
|
|
|
|
* @param eTag
|
|
|
|
|
* header we got last time we queried that url, or null
|
|
|
|
|
* @return
|
|
|
|
|
* @throws ClientProtocolException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws NotModifiedException
|
|
|
|
|
* if the url hasn't changed since we asked for it last time
|
|
|
|
|
*/
|
2013-07-25 09:17:33 +02:00
|
|
|
public HttpResult getBinary(String url, String lastModified, String eTag, int timeout) throws ClientProtocolException, IOException,
|
|
|
|
|
NotModifiedException {
|
2013-04-17 12:49:03 +02:00
|
|
|
HttpResult result = null;
|
2013-04-19 09:37:07 +02:00
|
|
|
long start = System.currentTimeMillis();
|
2013-04-03 15:53:57 +02:00
|
|
|
|
2013-11-05 15:10:09 +01:00
|
|
|
CloseableHttpClient client = newClient(timeout);
|
|
|
|
|
CloseableHttpResponse response = null;
|
2013-04-03 15:53:57 +02:00
|
|
|
try {
|
|
|
|
|
HttpGet httpget = new HttpGet(url);
|
2014-04-04 11:41:52 +02:00
|
|
|
HttpClientContext context = HttpClientContext.create();
|
2013-08-22 15:35:34 +02:00
|
|
|
|
2013-07-02 16:17:34 +02:00
|
|
|
httpget.addHeader(HttpHeaders.ACCEPT_LANGUAGE, ACCEPT_LANGUAGE);
|
|
|
|
|
httpget.addHeader(HttpHeaders.PRAGMA, PRAGMA_NO_CACHE);
|
|
|
|
|
httpget.addHeader(HttpHeaders.CACHE_CONTROL, CACHE_CONTROL_NO_CACHE);
|
2014-08-19 12:49:31 +02:00
|
|
|
httpget.addHeader(HttpHeaders.USER_AGENT, userAgent);
|
2013-04-17 12:49:03 +02:00
|
|
|
|
|
|
|
|
if (lastModified != null) {
|
|
|
|
|
httpget.addHeader(HttpHeaders.IF_MODIFIED_SINCE, lastModified);
|
|
|
|
|
}
|
|
|
|
|
if (eTag != null) {
|
|
|
|
|
httpget.addHeader(HttpHeaders.IF_NONE_MATCH, eTag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2013-08-22 15:35:34 +02:00
|
|
|
response = client.execute(httpget, context);
|
2013-04-23 09:14:18 +02:00
|
|
|
int code = response.getStatusLine().getStatusCode();
|
|
|
|
|
if (code == HttpStatus.SC_NOT_MODIFIED) {
|
2013-08-13 12:15:30 +02:00
|
|
|
throw new NotModifiedException("'304 - not modified' http code received");
|
2013-04-23 09:14:18 +02:00
|
|
|
} else if (code >= 300) {
|
2013-07-25 09:17:33 +02:00
|
|
|
throw new HttpResponseException(code, "Server returned HTTP error code " + code);
|
2013-04-17 12:49:03 +02:00
|
|
|
}
|
2013-04-23 09:14:18 +02:00
|
|
|
|
2013-04-17 12:49:03 +02:00
|
|
|
} catch (HttpResponseException e) {
|
|
|
|
|
if (e.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
|
2013-08-13 12:15:30 +02:00
|
|
|
throw new NotModifiedException("'304 - not modified' http code received");
|
2013-04-23 09:14:18 +02:00
|
|
|
} else {
|
|
|
|
|
throw e;
|
2013-04-17 12:49:03 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-07-25 09:17:33 +02:00
|
|
|
Header lastModifiedHeader = response.getFirstHeader(HttpHeaders.LAST_MODIFIED);
|
2013-08-22 15:35:34 +02:00
|
|
|
String lastModifiedHeaderValue = lastModifiedHeader == null ? null : StringUtils.trimToNull(lastModifiedHeader.getValue());
|
|
|
|
|
if (lastModifiedHeaderValue != null && StringUtils.equals(lastModified, lastModifiedHeaderValue)) {
|
2013-07-03 07:56:52 +02:00
|
|
|
throw new NotModifiedException("lastModifiedHeader is the same");
|
2013-04-17 12:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-22 15:35:34 +02:00
|
|
|
Header eTagHeader = response.getFirstHeader(HttpHeaders.ETAG);
|
|
|
|
|
String eTagHeaderValue = eTagHeader == null ? null : StringUtils.trimToNull(eTagHeader.getValue());
|
|
|
|
|
if (eTag != null && StringUtils.equals(eTag, eTagHeaderValue)) {
|
2013-07-03 07:56:52 +02:00
|
|
|
throw new NotModifiedException("eTagHeader is the same");
|
2013-04-17 12:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-19 11:49:22 +02:00
|
|
|
HttpEntity entity = response.getEntity();
|
2013-04-17 12:49:03 +02:00
|
|
|
byte[] content = null;
|
2013-07-26 15:34:02 +02:00
|
|
|
String contentType = null;
|
2013-04-03 15:53:57 +02:00
|
|
|
if (entity != null) {
|
|
|
|
|
content = EntityUtils.toByteArray(entity);
|
2013-07-26 15:34:02 +02:00
|
|
|
if (entity.getContentType() != null) {
|
|
|
|
|
contentType = entity.getContentType().getValue();
|
|
|
|
|
}
|
2013-04-03 15:53:57 +02:00
|
|
|
}
|
2017-10-12 10:21:11 +02:00
|
|
|
|
|
|
|
|
String urlAfterRedirect = url;
|
|
|
|
|
if (context.getRequest() instanceof HttpUriRequest) {
|
|
|
|
|
HttpUriRequest req = (HttpUriRequest) context.getRequest();
|
|
|
|
|
HttpHost host = context.getTargetHost();
|
|
|
|
|
urlAfterRedirect = req.getURI().isAbsolute() ? req.getURI().toString() : host.toURI() + req.getURI();
|
|
|
|
|
}
|
2013-04-19 13:24:46 +02:00
|
|
|
|
2013-04-19 09:37:07 +02:00
|
|
|
long duration = System.currentTimeMillis() - start;
|
2013-08-22 15:35:34 +02:00
|
|
|
result = new HttpResult(content, contentType, lastModifiedHeaderValue, eTagHeaderValue, duration, urlAfterRedirect);
|
2013-04-03 15:53:57 +02:00
|
|
|
} finally {
|
2013-11-05 15:10:09 +01:00
|
|
|
IOUtils.closeQuietly(response);
|
|
|
|
|
IOUtils.closeQuietly(client);
|
2013-04-03 15:53:57 +02:00
|
|
|
}
|
2013-04-17 12:49:03 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 00:56:21 +02:00
|
|
|
@Getter
|
|
|
|
|
@RequiredArgsConstructor
|
2013-04-17 12:49:03 +02:00
|
|
|
public static class HttpResult {
|
2014-08-19 00:56:21 +02:00
|
|
|
private final byte[] content;
|
|
|
|
|
private final String contentType;
|
|
|
|
|
private final String lastModifiedSince;
|
|
|
|
|
private final String eTag;
|
|
|
|
|
private final long duration;
|
|
|
|
|
private final String urlAfterRedirect;
|
2013-04-17 12:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
2013-11-05 15:10:09 +01:00
|
|
|
public static CloseableHttpClient newClient(int timeout) {
|
|
|
|
|
HttpClientBuilder builder = HttpClients.custom();
|
|
|
|
|
builder.useSystemProperties();
|
2014-04-04 11:41:52 +02:00
|
|
|
builder.addInterceptorFirst(REMOVE_INCORRECT_CONTENT_ENCODING);
|
2013-11-05 15:10:09 +01:00
|
|
|
builder.disableAutomaticRetries();
|
|
|
|
|
|
2015-06-04 14:55:36 +02:00
|
|
|
builder.setSSLContext(SSL_CONTEXT);
|
2015-03-19 11:22:56 +01:00
|
|
|
builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
|
2013-11-05 15:10:09 +01:00
|
|
|
|
|
|
|
|
RequestConfig.Builder configBuilder = RequestConfig.custom();
|
|
|
|
|
configBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
|
|
|
|
|
configBuilder.setSocketTimeout(timeout);
|
|
|
|
|
configBuilder.setConnectTimeout(timeout);
|
|
|
|
|
configBuilder.setConnectionRequestTimeout(timeout);
|
|
|
|
|
builder.setDefaultRequestConfig(configBuilder.build());
|
|
|
|
|
|
2014-04-09 16:21:33 +02:00
|
|
|
builder.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Consts.ISO_8859_1).build());
|
2013-11-05 15:10:09 +01:00
|
|
|
|
|
|
|
|
return builder.build();
|
2013-04-19 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
2013-04-17 12:49:03 +02:00
|
|
|
public static class NotModifiedException extends Exception {
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
2013-07-03 08:09:42 +02:00
|
|
|
|
|
|
|
|
public NotModifiedException(String message) {
|
2013-07-03 07:56:52 +02:00
|
|
|
super(message);
|
|
|
|
|
}
|
2013-04-17 12:49:03 +02:00
|
|
|
|
2013-04-03 15:53:57 +02:00
|
|
|
}
|
2013-04-19 13:24:46 +02:00
|
|
|
|
|
|
|
|
private static class DefaultTrustManager implements X509TrustManager {
|
|
|
|
|
@Override
|
2013-07-25 09:17:33 +02:00
|
|
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
2013-04-19 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2013-07-25 09:17:33 +02:00
|
|
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
2013-04-19 13:24:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-06 15:17:37 +01:00
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
CommaFeedConfiguration config = new CommaFeedConfiguration();
|
|
|
|
|
HttpGetter getter = new HttpGetter(config);
|
|
|
|
|
HttpResult result = getter.getBinary("https://sourceforge.net/projects/mpv-player-windows/rss", 30000);
|
|
|
|
|
System.out.println(new String(result.content));
|
|
|
|
|
}
|
2013-04-03 15:53:57 +02:00
|
|
|
}
|