Skip to content

Commit

Permalink
PushBuilder uses full URI
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Feb 5, 2015
1 parent 4ed6069 commit ab8b7f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 41 deletions.
Expand Up @@ -137,27 +137,25 @@ public void push(String uriInContext)
* will be generated. If the builder has a session ID, then the pushed request
* will include the session ID either as a Cookie or as a URI parameter as appropriate.The builders
* query string is merged with any passed query string.
* @param uriInContext The URI within the current context of the resource to push.
* @param path The URI of the resource to push.
* @param etag The etag for the resource or null if not available
* @param lastModified The last modified date of the resource or null if not available
* @throws IllegalArgumentException if the method set expects a request
* body (eg POST)
*/
public void push(String uriInContext,String etag,String lastModified)
public void push(String path,String etag,String lastModified)
{
if (HttpMethod.POST.is(_method) || HttpMethod.PUT.is(_method))
throw new IllegalStateException("Bad Method "+_method);

String query=_queryString;
int q=uriInContext.indexOf('?');
int q=path.indexOf('?');
if (q>=0)
{
query=uriInContext.substring(q+1)+'&'+query;
uriInContext=uriInContext.substring(0,q);
query=path.substring(q+1)+'&'+query;
path=path.substring(0,q);
}

String path = URIUtil.addPaths(_request.getContextPath(),uriInContext);


String param=null;
if (_sessionId!=null && _request.isRequestedSessionIdFromURL())
param="jsessionid="+_sessionId;
Expand All @@ -173,6 +171,5 @@ else if (lastModified!=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);
_request.getHttpChannel().getHttpTransport().push(push);

}
}
Expand Up @@ -33,13 +33,11 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;

import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.PushBuilder;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

Expand Down Expand Up @@ -86,24 +84,26 @@ public void requestDestroyed(ServletRequestEvent sre)

// Does this request have a referer?
String referer = request.getHttpFields().get(HttpHeader.REFERER);

if (referer!=null)
{
// Is the referer from this contexts?
HttpURI uri = new HttpURI(referer);
String path = uri.getPath();
if (request.getServerName().equals(uri.getHost()) && path.startsWith(request.getContextPath()))
HttpURI referer_uri = new HttpURI(referer);
if (request.getServerName().equals(referer_uri.getHost()))
{
String path_in_ctx = path.substring(request.getContextPath().length());
Target referer_target = _cache.get(path_in_ctx);
Target referer_target = _cache.get(referer_uri.getPath());
if (referer_target!=null)
{
HttpSession session = request.getSession();
ConcurrentHashMap<String, Long> timestamps = (ConcurrentHashMap<String, Long>)session.getAttribute(TIMESTAMP_ATTR);
Long last = timestamps.get(referer_target._path);
if (last!=null && (System.currentTimeMillis()-last)<_associateDelay && !referer_target._associated.containsKey(path))
if (last!=null && (System.currentTimeMillis()-last)<_associateDelay)
{
if (referer_target._associated.putIfAbsent(path,target)==null)
LOG.info("ASSOCIATE {}->{}",path_in_ctx,target._path);
if (referer_target._associated.putIfAbsent(target._path,target)==null)
{
if (LOG.isDebugEnabled())
LOG.debug("ASSOCIATE {}->{}",referer_target._path,target._path);
}
}
}
}
Expand All @@ -128,42 +128,31 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
{
// Get Jetty request as these APIs are not yet standard
Request baseRequest = Request.getBaseRequest(request);

if (baseRequest.isPush())
{
LOG.info("PUSH {} if modified since {}",baseRequest,baseRequest.getHttpFields().get("If-Modified-Since"));
}


// Iterating over fields is more efficient than multiple gets
HttpFields fields = baseRequest.getHttpFields();
String referer=fields.get(HttpHeader.REFERER);

String uri=baseRequest.getRequestURI();

if (LOG.isDebugEnabled())
LOG.debug("{} {} referer={}%n",baseRequest.getMethod(),baseRequest.getRequestURI(),referer);
LOG.debug("{} {} push={}",baseRequest.getMethod(),uri,baseRequest.isPush());

HttpSession session = baseRequest.getSession(true);
String sessionId = session.getId();
String path = URIUtil.addPaths(baseRequest.getServletPath(),baseRequest.getPathInfo());

// find the target for this resource
Target target = _cache.get(path);
Target target = _cache.get(uri);
if (target == null)
{
Target t=new Target(path);
target = _cache.putIfAbsent(path,t);
Target t=new Target(uri);
target = _cache.putIfAbsent(uri,t);
target = target==null?t:target;
}
request.setAttribute(TARGET_ATTR,target);

// Set the timestamp for this resource in this session
ConcurrentHashMap<String, Long> timestamps = (ConcurrentHashMap<String, Long>)session.getAttribute(TIMESTAMP_ATTR);
if (timestamps==null)
{
timestamps=new ConcurrentHashMap<>();
session.setAttribute(TIMESTAMP_ATTR,timestamps);
}

timestamps.put(path,System.currentTimeMillis());
request.setAttribute(TARGET_ATTR,target);
timestamps.put(uri,System.currentTimeMillis());

// push any associated resources
if (baseRequest.isPushSupported() && target._associated.size()>0)
Expand All @@ -173,7 +162,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
builder.setConditional(true);
for (Target associated : target._associated.values())
{
LOG.info("PUSH {}->{}",path,associated);
if (LOG.isDebugEnabled())
LOG.debug("PUSH {}->{}",uri,associated);
builder.push(associated._path,associated._etag,associated._lastModified);
}
}
Expand Down Expand Up @@ -207,7 +197,7 @@ public Target(String path)
@Override
public String toString()
{
return String.format("Target(p=%s,e=%s,m=%s)->%s",_path,_etag,_lastModified,_associated);
return String.format("Target{p=%s,e=%s,m=%s,a=%d}",_path,_etag,_lastModified,_associated.size());
}
}
}

0 comments on commit ab8b7f8

Please sign in to comment.