"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CacheKeyGenerator.java" between
httpcomponents-client-4.5.7-src.tar.gz and httpcomponents-client-4.5.8-src.tar.gz

About: HttpComponents is an Apache project responsible for creating and maintaining a toolset of low level Java components focused on HTTP and associated protocols. Java sources.

CacheKeyGenerator.java  (httpcomponents-client-4.5.7-src):CacheKeyGenerator.java  (httpcomponents-client-4.5.8-src)
skipping to change at line 30 skipping to change at line 30
* *
* This software consists of voluntary contributions made by many * This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more * individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see * information on the Apache Software Foundation, please see
* <http://www.apache.org/>. * <http://www.apache.org/>.
* *
*/ */
package org.apache.http.impl.client.cache; package org.apache.http.impl.client.cache;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.apache.http.Consts; import org.apache.http.Consts;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HeaderElement; import org.apache.http.HeaderElement;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.HttpRequest; import org.apache.http.HttpRequest;
import org.apache.http.annotation.Contract; import org.apache.http.annotation.Contract;
import org.apache.http.annotation.ThreadingBehavior; import org.apache.http.annotation.ThreadingBehavior;
import org.apache.http.client.cache.HeaderConstants; import org.apache.http.client.cache.HeaderConstants;
import org.apache.http.client.cache.HttpCacheEntry; import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.utils.URIUtils; import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.util.Args;
/** /**
* @since 4.1 * @since 4.1
*/ */
@Contract(threading = ThreadingBehavior.IMMUTABLE) @Contract(threading = ThreadingBehavior.IMMUTABLE)
class CacheKeyGenerator { class CacheKeyGenerator {
private static final URI BASE_URI = URI.create("http://example.com/"); private static final URI BASE_URI = URI.create("http://example.com/");
static URIBuilder getRequestUriBuilder(final HttpRequest request) throws URI
SyntaxException {
if (request instanceof HttpUriRequest) {
final URI uri = ((HttpUriRequest) request).getURI();
if (uri != null) {
return new URIBuilder(uri);
}
}
return new URIBuilder(request.getRequestLine().getUri());
}
static URI getRequestUri(final HttpRequest request, final HttpHost target) t
hrows URISyntaxException {
Args.notNull(request, "HTTP request");
Args.notNull(target, "Target");
final URIBuilder uriBuilder = getRequestUriBuilder(request);
// Decode path segments to preserve original behavior for backward compa
tibility
final String path = uriBuilder.getPath();
if (path != null) {
uriBuilder.setPathSegments(URLEncodedUtils.parsePathSegments(path));
}
if (!uriBuilder.isAbsolute()) {
uriBuilder.setScheme(target.getSchemeName());
uriBuilder.setHost(target.getHostName());
uriBuilder.setPort(target.getPort());
}
return uriBuilder.build();
}
static URI normalize(final URI requestUri) throws URISyntaxException {
Args.notNull(requestUri, "URI");
final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIU
tils.resolve(BASE_URI, requestUri) : requestUri) ;
if (builder.getHost() != null) {
if (builder.getScheme() == null) {
builder.setScheme("http");
}
if (builder.getPort() <= -1) {
if ("http".equalsIgnoreCase(builder.getScheme())) {
builder.setPort(80);
} else if ("https".equalsIgnoreCase(builder.getScheme())) {
builder.setPort(443);
}
}
}
builder.setFragment(null);
return builder.build();
}
/** /**
* For a given {@link HttpHost} and {@link HttpRequest} get a URI from the * For a given {@link HttpHost} and {@link HttpRequest} get a URI from the
* pair that I can use as an identifier KEY into my HttpCache * pair that I can use as an identifier KEY into my HttpCache
* *
* @param host The host for this request * @param host The host for this request
* @param req the {@link HttpRequest} * @param req the {@link HttpRequest}
* @return String the extracted URI * @return String the extracted URI
*/ */
public String getURI(final HttpHost host, final HttpRequest req) { public String getURI(final HttpHost host, final HttpRequest req) {
if (isRelativeRequest(req)) { try {
return canonicalizeUri(String.format("%s%s", host.toString(), req.ge final URI uri = normalize(getRequestUri(req, host));
tRequestLine().getUri())); return uri.toASCIIString();
} catch (final URISyntaxException ex) {
return req.getRequestLine().getUri();
} }
return canonicalizeUri(req.getRequestLine().getUri());
} }
public String canonicalizeUri(final String uri) { public String canonicalizeUri(final String uri) {
try { try {
final URI normalized = URIUtils.resolve(BASE_URI, uri); final URI normalized = normalize(URIUtils.resolve(BASE_URI, uri));
final URL u = new URL(normalized.toASCIIString()); return normalized.toASCIIString();
final String protocol = u.getProtocol(); } catch (final URISyntaxException ex) {
final String hostname = u.getHost();
final int port = canonicalizePort(u.getPort(), protocol);
final String path = u.getPath();
final String query = u.getQuery();
final String file = (query != null) ? (path + "?" + query) : path;
final URL out = new URL(protocol, hostname, port, file);
return out.toString();
} catch (final IllegalArgumentException e) {
return uri;
} catch (final MalformedURLException e) {
return uri; return uri;
} }
} }
private int canonicalizePort(final int port, final String protocol) {
if (port == -1 && "http".equalsIgnoreCase(protocol)) {
return 80;
} else if (port == -1 && "https".equalsIgnoreCase(protocol)) {
return 443;
}
return port;
}
private boolean isRelativeRequest(final HttpRequest req) {
final String requestUri = req.getRequestLine().getUri();
return ("*".equals(requestUri) || requestUri.startsWith("/"));
}
protected String getFullHeaderValue(final Header[] headers) { protected String getFullHeaderValue(final Header[] headers) {
if (headers == null) { if (headers == null) {
return ""; return "";
} }
final StringBuilder buf = new StringBuilder(""); final StringBuilder buf = new StringBuilder("");
boolean first = true; boolean first = true;
for (final Header hdr : headers) { for (final Header hdr : headers) {
if (!first) { if (!first) {
buf.append(", "); buf.append(", ");
 End of changes. 9 change blocks. 
33 lines changed or deleted 65 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)