cleaned up authority handling

This commit is contained in:
Greg Wilkins 2014-06-17 19:40:09 +02:00
parent 3fcb910c1a
commit 7fa4f1e9f8
6 changed files with 39 additions and 89 deletions

View File

@ -115,13 +115,12 @@ public class MetaData implements Iterable<HttpField>
}
}
// TODO: review this constructor: host/port parameters are ignored, and code duplication should be avoided.
public Request(HttpVersion version, HttpScheme scheme, String method, String authority, String host, int port, String path, HttpFields fields)
public Request(HttpVersion version, HttpScheme scheme, String method, HostPortHttpField authority, String path, HttpFields fields)
{
super(version,fields);
_method=method;
_uri=new HttpURI(path); // TODO - this is not so efficient!
_hostPort = new HostPortHttpField(authority);
_hostPort = authority;
_scheme=scheme;
}
@ -149,12 +148,12 @@ public class MetaData implements Iterable<HttpField>
public String getHost()
{
return _hostPort.getHost();
return _hostPort==null?null:_hostPort.getHost();
}
public int getPort()
{
return _hostPort.getPort();
return _hostPort==null?0:_hostPort.getPort();
}
public HttpURI getURI()

View File

@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpScheme;
@ -49,7 +50,7 @@ public class HeadersGenerateParseTest
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", "localhost:8080", "localhost", 8080, "/path", fields);
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", new HostPortHttpField("localhost:8080"), "/path", fields);
final List<HeadersFrame> frames = new ArrayList<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter()
@ -115,7 +116,7 @@ public class HeadersGenerateParseTest
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", "localhost:8080", "localhost", 8080, "/path", fields);
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", new HostPortHttpField("localhost:8080"), "/path", fields);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.generateHeaders(lease, streamId, metaData, false);

View File

@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpScheme;
@ -61,7 +62,7 @@ public class PushPromiseGenerateParseTest
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", "localhost:8080", "localhost", 8080, "/path", fields);
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", new HostPortHttpField("localhost:8080"), "/path", fields);
// Iterate a few times to be sure generator and parser are properly reset.
for (int i = 0; i < 2; ++i)
@ -117,7 +118,7 @@ public class PushPromiseGenerateParseTest
HttpFields fields = new HttpFields();
fields.put("Accept", "text/html");
fields.put("User-Agent", "Jetty");
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", "localhost:8080", "localhost", 8080, "/path", fields);
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, "GET", new HostPortHttpField("localhost:8080"), "/path", fields);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.generatePushPromise(lease, streamId, promisedStreamId, metaData);

View File

@ -35,9 +35,7 @@ public class MetaDataBuilder
private int _status;
private String _method;
private HttpScheme _scheme;
private String _authority;
private String _host;
private int _port;
private HostPortHttpField _authority;
private String _path;
HttpFields _fields = new HttpFields(10);
@ -83,10 +81,7 @@ public class MetaDataBuilder
break;
case ":authority":
_authority=field.getValue();
HostPortHttpField afield=(field instanceof HostPortHttpField)?((HostPortHttpField)field):new AuthorityHttpField(field.getValue());
_host=afield.getHost();
_port=afield.getPort();
_authority=(field instanceof HostPortHttpField)?((HostPortHttpField)field):new AuthorityHttpField(field.getValue());
break;
case ":path":
@ -107,7 +102,7 @@ public class MetaDataBuilder
HttpFields fields = _fields;
_fields = new HttpFields(Math.max(10,fields.size()+5));
if (_method!=null)
return new MetaData.Request(HttpVersion.HTTP_2,_scheme,_method,_authority,_host,_port,_path,fields);
return new MetaData.Request(HttpVersion.HTTP_2,_scheme,_method,_authority,_path,fields);
if (_status!=0)
return new MetaData.Response(HttpVersion.HTTP_2,_status,fields);
return new MetaData(HttpVersion.HTTP_2,fields);
@ -119,8 +114,6 @@ public class MetaDataBuilder
_scheme=null;
_authority=null;
_path=null;
_host=null;
_port=0;
}
}
}

View File

@ -35,6 +35,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpScheme;
@ -106,7 +107,7 @@ public class HTTP2ServerTest
int port = connector.getLocalPort();
HttpFields fields = new HttpFields();
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, HttpMethod.GET.asString(),
host + ":" + port, host, port, path, fields);
new HostPortHttpField(host + ":" + port), path, fields);
HeadersFrame request = new HeadersFrame(1, metaData, null, true);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, request);
@ -155,7 +156,7 @@ public class HTTP2ServerTest
int port = connector.getLocalPort();
HttpFields fields = new HttpFields();
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2, HttpScheme.HTTP, HttpMethod.GET.asString(),
host + ":" + port, host, port, path, fields);
new HostPortHttpField(host + ":" + port), path, fields);
HeadersFrame request = new HeadersFrame(1, metaData, null, true);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, request);
@ -218,7 +219,7 @@ public class HTTP2ServerTest
int port = connector.getLocalPort();
HttpFields fields = new HttpFields();
MetaData.Request metaData = new MetaData.Request(HttpVersion.HTTP_2,HttpScheme.HTTP, HttpMethod.GET.asString(),
host + ":" + port, host, port, path, fields);
new HostPortHttpField(host + ":" + port), path, fields);
HeadersFrame request = new HeadersFrame(1, metaData, null, true);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, request);

View File

@ -60,7 +60,9 @@ import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpUpgradeHandler;
import javax.servlet.http.Part;
import org.eclipse.jetty.http.HostPortHttpField;
import org.eclipse.jetty.http.HttpCookie;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
@ -149,7 +151,7 @@ public class Request implements HttpServletRequest
private MultiMap<String> _contentParameters;
private MultiMap<String> _parameters;
private String _pathInfo;
private int _port;
private int _serverPort;
private HttpVersion _httpVersion = HttpVersion.HTTP_1_1;
private String _queryEncoding;
private String _queryString;
@ -1174,57 +1176,19 @@ public class Request implements HttpServletRequest
_serverName = _uri.getHost();
if (_serverName != null)
{
_port = _uri.getPort();
_serverPort = _uri.getPort();
return _serverName;
}
// Return host from header field
String hostPort = _fields.getStringField(HttpHeader.HOST);
_port=0;
if (hostPort != null)
HttpField host = _fields.getField(HttpHeader.HOST);
if (host!=null)
{
int len=hostPort.length();
loop: for (int i = len; i-- > 0;)
{
char c2 = (char)(0xff & hostPort.charAt(i));
switch (c2)
{
case ']':
break loop;
case ':':
try
{
len=i;
_port = StringUtil.toInt(hostPort,i+1);
}
catch (NumberFormatException e)
{
LOG.warn(e);
_serverName=hostPort;
_port=0;
return _serverName;
}
break loop;
}
}
if (hostPort.charAt(0)=='[')
{
if (hostPort.charAt(len-1)!=']')
{
LOG.warn("Bad IPv6 "+hostPort);
_serverName=hostPort;
_port=0;
return _serverName;
}
_serverName = hostPort.substring(1,len-1);
}
else if (len==hostPort.length())
_serverName=hostPort;
else
_serverName = hostPort.substring(0,len);
HostPortHttpField authority = (host instanceof HostPortHttpField)
?((HostPortHttpField)host)
:new HostPortHttpField(host.getValue());
_serverName=authority.getHost();
_serverPort=authority.getPort();
return _serverName;
}
@ -1232,7 +1196,7 @@ public class Request implements HttpServletRequest
if (_channel != null)
{
_serverName = getLocalName();
_port = getLocalPort();
_serverPort = getLocalPort();
if (_serverName != null && !StringUtil.ALL_INTERFACES.equals(_serverName))
return _serverName;
}
@ -1256,30 +1220,21 @@ public class Request implements HttpServletRequest
@Override
public int getServerPort()
{
if (_port <= 0)
{
if (_serverName == null)
// If we have no port and have not decoded a server name
if (_serverPort <= 0 && _serverName == null)
// decode a server name, which will set the port if it can
getServerName();
if (_port <= 0)
{
if (_serverName != null && _uri != null)
_port = _uri.getPort();
else
{
InetSocketAddress local = _channel.getLocalAddress();
_port = local == null?0:local.getPort();
}
}
}
if (_port <= 0)
// If no port specified, return the default port for the scheme
if (_serverPort <= 0)
{
if (getScheme().equalsIgnoreCase(URIUtil.HTTPS))
return 443;
return 80;
}
return _port;
// return a specific port
return _serverPort;
}
/* ------------------------------------------------------------ */
@ -1604,7 +1559,7 @@ public class Request implements HttpServletRequest
_serverName = null;
_httpMethod = null;
_pathInfo = null;
_port = 0;
_serverPort = 0;
_httpVersion = HttpVersion.HTTP_1_1;
_queryEncoding = null;
_queryString = null;
@ -1969,7 +1924,7 @@ public class Request implements HttpServletRequest
*/
public void setServerPort(int port)
{
_port = port;
_serverPort = port;
}
/* ------------------------------------------------------------ */