parent
7669ea0365
commit
162d6fc722
|
@ -33,15 +33,13 @@ import org.eclipse.jetty.util.UrlEncoded;
|
|||
/* ------------------------------------------------------------ */
|
||||
/** Http URI.
|
||||
* Parse a HTTP URI from a string or byte array. Given a URI
|
||||
* <code>http://user:password@host:port/path;ignored/%69nfo;param?query#fragment</code>
|
||||
* <code>http://user@host:port/path/info;param?query#fragment</code>
|
||||
* this class will split it into the following undecoded optional elements:<ul>
|
||||
* <li>{@link #getScheme()} - http:</li>
|
||||
* <li>{@link #getUser()} - user:password</li>
|
||||
* <li>{@link #getAuthority()} - host:port</li>
|
||||
* <li>{@link #getAuthority()} - //name@host:port</li>
|
||||
* <li>{@link #getHost()} - host</li>
|
||||
* <li>{@link #getPort()} - port</li>
|
||||
* <li>{@link #getPath()} - /path;ignored/%69nfo;param</li>
|
||||
* <li>{@link #getDecodedPath()} - /path/info</li>
|
||||
* <li>{@link #getPath()} - /path/info</li>
|
||||
* <li>{@link #getParam()} - param</li>
|
||||
* <li>{@link #getQuery()} - query</li>
|
||||
* <li>{@link #getFragment()} - fragment</li>
|
||||
|
@ -77,6 +75,28 @@ public class HttpURI
|
|||
|
||||
String _uri;
|
||||
String _decodedPath;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* Construct a normalized URI.
|
||||
* Port is not set if it is the default port.
|
||||
* @param scheme the URI scheme
|
||||
* @param host the URI hose
|
||||
* @param port the URI port
|
||||
* @param path the URI path
|
||||
* @param param the URI param
|
||||
* @param query the URI query
|
||||
* @param fragment the URI fragment
|
||||
* @return the normalized URI
|
||||
*/
|
||||
public static HttpURI createHttpURI(String scheme, String host, int port, String path, String param, String query, String fragment)
|
||||
{
|
||||
if (port==80 && HttpScheme.HTTP.is(scheme))
|
||||
port=0;
|
||||
if (port==443 && HttpScheme.HTTPS.is(scheme))
|
||||
port=0;
|
||||
return new HttpURI(scheme,host,port,path,param,query,fragment);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public HttpURI()
|
||||
|
@ -88,18 +108,17 @@ public class HttpURI
|
|||
{
|
||||
_scheme = scheme;
|
||||
_host = host;
|
||||
_port = (port<0 && (HttpScheme.HTTP.is(_scheme)||HttpScheme.HTTPS.is(_scheme)))?0:port;
|
||||
_port = port;
|
||||
_path = path;
|
||||
_param = param;
|
||||
_query = query;
|
||||
_fragment = fragment;
|
||||
_fragment = fragment;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public HttpURI(HttpURI uri)
|
||||
{
|
||||
this(uri._scheme,uri._host,uri._port,uri._path,uri._param,uri._query,uri._fragment);
|
||||
_user=uri._user;
|
||||
_uri=uri._uri;
|
||||
}
|
||||
|
||||
|
@ -107,7 +126,7 @@ public class HttpURI
|
|||
public HttpURI(String uri)
|
||||
{
|
||||
_port=-1;
|
||||
parse(State.START,uri);
|
||||
parse(State.START,uri,0,uri.length());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -139,11 +158,22 @@ public class HttpURI
|
|||
/* ------------------------------------------------------------ */
|
||||
public HttpURI(String scheme, String host, int port, String pathQuery)
|
||||
{
|
||||
_uri=null;
|
||||
|
||||
_scheme=scheme;
|
||||
_host=host;
|
||||
_port = (port<0 && (HttpScheme.HTTP.is(_scheme)||HttpScheme.HTTPS.is(_scheme)))?0:port;
|
||||
parse(State.PATH,pathQuery);
|
||||
_uri=null;
|
||||
_port=port;
|
||||
|
||||
parse(State.PATH,pathQuery,0,pathQuery.length());
|
||||
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void parse(String uri)
|
||||
{
|
||||
clear();
|
||||
_uri=uri;
|
||||
parse(State.START,uri,0,uri.length());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -160,18 +190,35 @@ public class HttpURI
|
|||
if (HttpMethod.CONNECT.is(method))
|
||||
_path=uri;
|
||||
else
|
||||
parse(uri.startsWith("/")?State.PATH:State.START,uri);
|
||||
parse(uri.startsWith("/")?State.PATH:State.START,uri,0,uri.length());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private void parse(State state, final String uri)
|
||||
@Deprecated
|
||||
public void parseConnect(String uri)
|
||||
{
|
||||
clear();
|
||||
_uri=uri;
|
||||
_path=uri;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public void parse(String uri, int offset, int length)
|
||||
{
|
||||
clear();
|
||||
int end=offset+length;
|
||||
_uri=uri.substring(offset,end);
|
||||
parse(State.START,uri,offset,end);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
private void parse(State state, final String uri, final int offset, final int end)
|
||||
{
|
||||
boolean encoded=false;
|
||||
int mark=0;
|
||||
int end=uri.length();
|
||||
int mark=offset;
|
||||
int path_mark=0;
|
||||
|
||||
for (int i=0; i<end; i++)
|
||||
for (int i=offset; i<end; i++)
|
||||
{
|
||||
char c=uri.charAt(i);
|
||||
|
||||
|
@ -295,13 +342,12 @@ public class HttpURI
|
|||
{
|
||||
case '/':
|
||||
_host = uri.substring(mark,i);
|
||||
_port = 0;
|
||||
path_mark=mark=i;
|
||||
state=State.PATH;
|
||||
break;
|
||||
case ':':
|
||||
_host=(i > mark)?uri.substring(mark,i):"";
|
||||
_port = 0;
|
||||
if (i > mark)
|
||||
_host=uri.substring(mark,i);
|
||||
mark=i+1;
|
||||
state=State.PORT;
|
||||
break;
|
||||
|
@ -515,13 +561,6 @@ public class HttpURI
|
|||
/* ------------------------------------------------------------ */
|
||||
public int getPort()
|
||||
{
|
||||
if (_port==0)
|
||||
{
|
||||
if (HttpScheme.HTTP.is(_scheme))
|
||||
return 80;
|
||||
if (HttpScheme.HTTPS.is(_scheme))
|
||||
return 443;
|
||||
}
|
||||
return _port;
|
||||
}
|
||||
|
||||
|
@ -635,7 +674,7 @@ public class HttpURI
|
|||
out.append(_host);
|
||||
}
|
||||
|
||||
if (_port>0 && !(_port==80&&HttpScheme.HTTP.is(_scheme)) && !(_port==443&&HttpScheme.HTTPS.is(_scheme)))
|
||||
if (_port>0)
|
||||
out.append(':').append(_port);
|
||||
|
||||
if (_path!=null)
|
||||
|
@ -704,7 +743,7 @@ public class HttpURI
|
|||
_param=null;
|
||||
_fragment=null;
|
||||
if (path!=null)
|
||||
parse(State.PATH,path);
|
||||
parse(State.PATH,path,0,path.length());
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -741,4 +780,6 @@ public class HttpURI
|
|||
{
|
||||
return _user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -45,9 +45,10 @@ public class HttpURITest
|
|||
|
||||
private void assertInvalidURI(String invalidURI, String message)
|
||||
{
|
||||
HttpURI uri = new HttpURI();
|
||||
try
|
||||
{
|
||||
new HttpURI(invalidURI);
|
||||
uri.parse(invalidURI);
|
||||
fail(message);
|
||||
}
|
||||
catch (IllegalArgumentException e)
|
||||
|
@ -56,6 +57,28 @@ public class HttpURITest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse()
|
||||
{
|
||||
HttpURI uri = new HttpURI();
|
||||
|
||||
uri.parse("*");
|
||||
assertThat(uri.getHost(),nullValue());
|
||||
assertThat(uri.getPath(),is("*"));
|
||||
|
||||
uri.parse("/foo/bar");
|
||||
assertThat(uri.getHost(),nullValue());
|
||||
assertThat(uri.getPath(),is("/foo/bar"));
|
||||
|
||||
uri.parse("//foo/bar");
|
||||
assertThat(uri.getHost(),is("foo"));
|
||||
assertThat(uri.getPath(),is("/bar"));
|
||||
|
||||
uri.parse("http://foo/bar");
|
||||
assertThat(uri.getHost(),is("foo"));
|
||||
assertThat(uri.getPath(),is("/bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseRequestTarget()
|
||||
{
|
||||
|
@ -227,47 +250,4 @@ public class HttpURITest
|
|||
assertEquals(uri.getAuthority(), "example.com:8888");
|
||||
assertEquals(uri.getUser(), "user:password");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testComplete() throws Exception
|
||||
{
|
||||
HttpURI uri = new HttpURI("scheme://user:password@host:99/path;ignored/%69nfo;param?query+string#fragment");
|
||||
|
||||
assertEquals("scheme",uri.getScheme());
|
||||
assertEquals("user:password",uri.getUser());
|
||||
assertEquals("host",uri.getHost());
|
||||
assertEquals(99,uri.getPort());
|
||||
assertEquals("host:99",uri.getAuthority());
|
||||
assertEquals("/path;ignored/%69nfo;param",uri.getPath());
|
||||
assertEquals("/path/info",uri.getDecodedPath());
|
||||
assertEquals("param",uri.getParam());
|
||||
assertEquals("query+string",uri.getQuery());
|
||||
assertEquals("fragment",uri.getFragment());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultPorts() throws Exception
|
||||
{
|
||||
HttpURI uri = new HttpURI("/path/info");
|
||||
assertEquals(-1,uri.getPort());
|
||||
|
||||
uri = new HttpURI("http:/path/info");
|
||||
assertEquals(-1,uri.getPort());
|
||||
|
||||
uri = new HttpURI("http://host/path/info");
|
||||
assertEquals(80,uri.getPort());
|
||||
|
||||
uri = new HttpURI("https://host/path/info");
|
||||
assertEquals(443,uri.getPort());
|
||||
|
||||
uri = new HttpURI("http://host:8080/path/info");
|
||||
assertEquals(8080,uri.getPort());
|
||||
|
||||
uri = new HttpURI("http","host",-1,"/path?query");
|
||||
assertEquals(80,uri.getPort());
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -248,7 +248,7 @@ public class PushBuilderImpl implements PushBuilder
|
|||
_fields.add(HttpHeader.IF_MODIFIED_SINCE,_lastModified);
|
||||
}
|
||||
|
||||
HttpURI uri = new HttpURI(_request.getScheme(),_request.getServerName(),_request.getServerPort(),path,param,query,null);
|
||||
HttpURI uri = HttpURI.createHttpURI(_request.getScheme(),_request.getServerName(),_request.getServerPort(),path,param,query,null);
|
||||
MetaData.Request push = new MetaData.Request(_method,uri,_request.getHttpVersion(),_fields);
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
|
|
Loading…
Reference in New Issue