"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "httpclient/src/main/java/org/apache/http/client/utils/URIUtils.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.

URIUtils.java  (httpcomponents-client-4.5.7-src):URIUtils.java  (httpcomponents-client-4.5.8-src)
skipping to change at line 31 skipping to change at line 31
* 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.client.utils; package org.apache.http.client.utils;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Stack; import java.util.Stack;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.conn.routing.RouteInfo; import org.apache.http.conn.routing.RouteInfo;
import org.apache.http.util.Args; import org.apache.http.util.Args;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
/** /**
* A collection of utilities for {@link URI URIs}, to workaround * A collection of utilities for {@link URI URIs}, to workaround
* bugs within the class or for ease-of-use features. * bugs within the class or for ease-of-use features.
* *
* @since 4.0 * @since 4.0
*/ */
public class URIUtils { public class URIUtils {
/**
* Flags that control how URI is being rewritten.
*
* @since 4.5.8
*/
public enum UriFlag {
DROP_FRAGMENT,
NORMALIZE
}
/**
* Empty set of uri flags.
*
* @since 4.5.8
*/
public static final EnumSet<UriFlag> NO_FLAGS = EnumSet.noneOf(UriFlag.class
);
/**
* Set of uri flags containing {@link UriFlag#DROP_FRAGMENT}.
*
* @since 4.5.8
*/
public static final EnumSet<UriFlag> DROP_FRAGMENT = EnumSet.of(UriFlag.DROP
_FRAGMENT);
/**
* Set of uri flags containing {@link UriFlag#NORMALIZE}.
*
* @since 4.5.8
*/
public static final EnumSet<UriFlag> NORMALIZE = EnumSet.of(UriFlag.NORMALIZ
E);
/**
* Set of uri flags containing {@link UriFlag#DROP_FRAGMENT} and {@link UriF
lag#NORMALIZE}.
*
* @since 4.5.8
*/
public static final EnumSet<UriFlag> DROP_FRAGMENT_AND_NORMALIZE = EnumSet.o
f(UriFlag.DROP_FRAGMENT, UriFlag.NORMALIZE);
/** /**
* Constructs a {@link URI} using all the parameters. This should be * Constructs a {@link URI} using all the parameters. This should be
* used instead of * used instead of
* {@link URI#URI(String, String, String, int, String, String, String)} * {@link URI#URI(String, String, String, int, String, String, String)}
* or any of the other URI multi-argument URI constructors. * or any of the other URI multi-argument URI constructors.
* *
* @param scheme * @param scheme
* Scheme name * Scheme name
* @param host * @param host
* Host name * Host name
skipping to change at line 128 skipping to change at line 169
* *
* @param uri * @param uri
* Contains the path, query and fragment to use. * Contains the path, query and fragment to use.
* @param target * @param target
* Contains the scheme, host and port to use. * Contains the scheme, host and port to use.
* @param dropFragment * @param dropFragment
* True if the fragment should not be copied. * True if the fragment should not be copied.
* *
* @throws URISyntaxException * @throws URISyntaxException
* If the resulting URI is invalid. * If the resulting URI is invalid.
* @deprecated (4.5.8) Use {@link #rewriteURI(URI, HttpHost, EnumSet)}
*/
@Deprecated
public static URI rewriteURI(
final URI uri,
final HttpHost target,
final boolean dropFragment) throws URISyntaxException
{
return rewriteURI(uri, target, dropFragment ? DROP_FRAGMENT : NO_FLAGS);
}
/**
* A convenience method for creating a new {@link URI} whose scheme, host
* and port are taken from the target host, but whose path, query and
* fragment are taken from the existing URI. What exactly is used and how
* is driven by the passed in flags. The path is set to "/" if not explicitl
y specified.
*
* @param uri
* Contains the path, query and fragment to use.
* @param target
* Contains the scheme, host and port to use.
* @param flags
* True if the fragment should not be copied.
*
* @throws URISyntaxException
* If the resulting URI is invalid.
* @since 4.5.8
*/ */
public static URI rewriteURI( public static URI rewriteURI(
final URI uri, final URI uri,
final HttpHost target, final HttpHost target,
final boolean dropFragment) throws URISyntaxException { final EnumSet<UriFlag> flags) throws URISyntaxException {
Args.notNull(uri, "URI"); Args.notNull(uri, "URI");
Args.notNull(flags, "URI flags");
if (uri.isOpaque()) { if (uri.isOpaque()) {
return uri; return uri;
} }
final URIBuilder uribuilder = new URIBuilder(uri); final URIBuilder uribuilder = new URIBuilder(uri);
if (target != null) { if (target != null) {
uribuilder.setScheme(target.getSchemeName()); uribuilder.setScheme(target.getSchemeName());
uribuilder.setHost(target.getHostName()); uribuilder.setHost(target.getHostName());
uribuilder.setPort(target.getPort()); uribuilder.setPort(target.getPort());
} else { } else {
uribuilder.setScheme(null); uribuilder.setScheme(null);
uribuilder.setHost(null); uribuilder.setHost(null);
uribuilder.setPort(-1); uribuilder.setPort(-1);
} }
if (dropFragment) { if (flags.contains(UriFlag.DROP_FRAGMENT)) {
uribuilder.setFragment(null); uribuilder.setFragment(null);
} }
final String path = uribuilder.getPath(); if (flags.contains(UriFlag.NORMALIZE)) {
if (TextUtils.isEmpty(path)) { final List<String> pathSegments = new ArrayList<String>(uribuilder.g
uribuilder.setPath("/"); etPathSegments());
} else { for (final Iterator<String> it = pathSegments.iterator(); it.hasNext
final StringBuilder buf = new StringBuilder(path.length()); (); ) {
boolean foundSlash = false; final String pathSegment = it.next();
for (int i = 0; i < path.length(); i++) { if (pathSegment.isEmpty() && it.hasNext()) {
final char ch = path.charAt(i); it.remove();
if (ch != '/' || !foundSlash) {
buf.append(ch);
} }
foundSlash = ch == '/';
} }
uribuilder.setPath(buf.toString()); uribuilder.setPathSegments(pathSegments);
}
if (uribuilder.isPathEmpty()) {
uribuilder.setPathSegments("");
} }
return uribuilder.build(); return uribuilder.build();
} }
/** /**
* A convenience method for * A convenience method for
* {@link URIUtils#rewriteURI(URI, HttpHost, boolean)} that always keeps the * {@link URIUtils#rewriteURI(URI, HttpHost, EnumSet)} that always keeps the
* fragment. * fragment.
*/ */
public static URI rewriteURI( public static URI rewriteURI(
final URI uri, final URI uri,
final HttpHost target) throws URISyntaxException { final HttpHost target) throws URISyntaxException {
return rewriteURI(uri, target, false); return rewriteURI(uri, target, NORMALIZE);
} }
/** /**
* A convenience method that creates a new {@link URI} whose scheme, host, p ort, path, * A convenience method that creates a new {@link URI} whose scheme, host, p ort, path,
* query are taken from the existing URI, dropping any fragment or user-info rmation. * query are taken from the existing URI, dropping any fragment or user-info rmation.
* The path is set to "/" if not explicitly specified. The existing URI is r eturned * The path is set to "/" if not explicitly specified. The existing URI is r eturned
* unmodified if it has no fragment or user-information and has a path. * unmodified if it has no fragment or user-information and has a path.
* *
* @param uri * @param uri
* original URI. * original URI.
skipping to change at line 199 skipping to change at line 266
*/ */
public static URI rewriteURI(final URI uri) throws URISyntaxException { public static URI rewriteURI(final URI uri) throws URISyntaxException {
Args.notNull(uri, "URI"); Args.notNull(uri, "URI");
if (uri.isOpaque()) { if (uri.isOpaque()) {
return uri; return uri;
} }
final URIBuilder uribuilder = new URIBuilder(uri); final URIBuilder uribuilder = new URIBuilder(uri);
if (uribuilder.getUserInfo() != null) { if (uribuilder.getUserInfo() != null) {
uribuilder.setUserInfo(null); uribuilder.setUserInfo(null);
} }
if (uribuilder.getPathSegments().isEmpty()) {
uribuilder.setPathSegments("");
}
if (TextUtils.isEmpty(uribuilder.getPath())) { if (TextUtils.isEmpty(uribuilder.getPath())) {
uribuilder.setPath("/"); uribuilder.setPath("/");
} }
if (uribuilder.getHost() != null) { if (uribuilder.getHost() != null) {
uribuilder.setHost(uribuilder.getHost().toLowerCase(Locale.ROOT)); uribuilder.setHost(uribuilder.getHost().toLowerCase(Locale.ROOT));
} }
uribuilder.setFragment(null); uribuilder.setFragment(null);
return uribuilder.build(); return uribuilder.build();
} }
skipping to change at line 221 skipping to change at line 291
* to a relative or an absolute form as required by the specified route. * to a relative or an absolute form as required by the specified route.
* *
* @param uri * @param uri
* original URI. * original URI.
* @throws URISyntaxException * @throws URISyntaxException
* If the resulting URI is invalid. * If the resulting URI is invalid.
* *
* @since 4.4 * @since 4.4
*/ */
public static URI rewriteURIForRoute(final URI uri, final RouteInfo route) t hrows URISyntaxException { public static URI rewriteURIForRoute(final URI uri, final RouteInfo route) t hrows URISyntaxException {
return rewriteURIForRoute(uri, route, true);
}
/**
* A convenience method that optionally converts the original {@link java.ne
t.URI} either
* to a relative or an absolute form as required by the specified route.
*
* @param uri
* original URI.
* @throws URISyntaxException
* If the resulting URI is invalid.
*
* @since 4.5.8
*/
public static URI rewriteURIForRoute(final URI uri, final RouteInfo route, f
inal boolean normalizeUri) throws URISyntaxException {
if (uri == null) { if (uri == null) {
return null; return null;
} }
if (route.getProxyHost() != null && !route.isTunnelled()) { if (route.getProxyHost() != null && !route.isTunnelled()) {
// Make sure the request URI is absolute // Make sure the request URI is absolute
return uri.isAbsolute() return uri.isAbsolute()
? rewriteURI(uri) ? rewriteURI(uri)
: rewriteURI(uri, route.getTargetHost(), true); : rewriteURI(uri, route.getTargetHost(), normalizeUri ? DROP
_FRAGMENT_AND_NORMALIZE : DROP_FRAGMENT);
} }
// Make sure the request URI is relative // Make sure the request URI is relative
return uri.isAbsolute() ? rewriteURI(uri, null, true) : rewriteURI(uri); return uri.isAbsolute() ? rewriteURI(uri, null, normalizeUri ? DROP_FRAG MENT_AND_NORMALIZE : DROP_FRAGMENT) : rewriteURI(uri);
} }
/** /**
* Resolves a URI reference against a base URI. Work-around for bug in * Resolves a URI reference against a base URI. Work-around for bug in
* java.net.URI (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535) * java.net.URI (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4708535)
* *
* @param baseURI the base URI * @param baseURI the base URI
* @param reference the URI reference * @param reference the URI reference
* @return the resulting URI * @return the resulting URI
*/ */
skipping to change at line 286 skipping to change at line 371
throw new IllegalArgumentException(ex); throw new IllegalArgumentException(ex);
} }
} }
/** /**
* Removes dot segments according to RFC 3986, section 5.2.4 and * Removes dot segments according to RFC 3986, section 5.2.4 and
* Syntax-Based Normalization according to RFC 3986, section 6.2.2. * Syntax-Based Normalization according to RFC 3986, section 6.2.2.
* *
* @param uri the original URI * @param uri the original URI
* @return the URI without dot segments * @return the URI without dot segments
*
* @since 4.5
*/ */
static URI normalizeSyntax(final URI uri) throws URISyntaxException { public static URI normalizeSyntax(final URI uri) throws URISyntaxException {
if (uri.isOpaque() || uri.getAuthority() == null) { if (uri.isOpaque() || uri.getAuthority() == null) {
// opaque and file: URIs // opaque and file: URIs
return uri; return uri;
} }
Args.check(uri.isAbsolute(), "Base URI must be absolute");
final URIBuilder builder = new URIBuilder(uri); final URIBuilder builder = new URIBuilder(uri);
final String path = builder.getPath(); final List<String> inputSegments = builder.getPathSegments();
if (path != null && !path.equals("/")) { final Stack<String> outputSegments = new Stack<String>();
final String[] inputSegments = path.split("/"); for (final String inputSegment : inputSegments) {
final Stack<String> outputSegments = new Stack<String>(); if (".".equals(inputSegment)) {
for (final String inputSegment : inputSegments) { // Do nothing
if ((inputSegment.isEmpty()) || (".".equals(inputSegment))) { } else if ("..".equals(inputSegment)) {
// Do nothing if (!outputSegments.isEmpty()) {
} else if ("..".equals(inputSegment)) { outputSegments.pop();
if (!outputSegments.isEmpty()) {
outputSegments.pop();
}
} else {
outputSegments.push(inputSegment);
} }
} else {
outputSegments.push(inputSegment);
} }
final StringBuilder outputBuffer = new StringBuilder();
for (final String outputSegment : outputSegments) {
outputBuffer.append('/').append(outputSegment);
}
if (path.lastIndexOf('/') == path.length() - 1) {
// path.endsWith("/") || path.equals("")
outputBuffer.append('/');
}
builder.setPath(outputBuffer.toString());
} }
if (outputSegments.size() == 0) {
outputSegments.add("");
}
builder.setPathSegments(outputSegments);
if (builder.getScheme() != null) { if (builder.getScheme() != null) {
builder.setScheme(builder.getScheme().toLowerCase(Locale.ROOT)); builder.setScheme(builder.getScheme().toLowerCase(Locale.ROOT));
} }
if (builder.getHost() != null) { if (builder.getHost() != null) {
builder.setHost(builder.getHost().toLowerCase(Locale.ROOT)); builder.setHost(builder.getHost().toLowerCase(Locale.ROOT));
} }
return builder.build(); return builder.build();
} }
/** /**
 End of changes. 22 change blocks. 
43 lines changed or deleted 132 lines changed or added

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