Mercurial Hosting > luan
changeset 991:688c39c50ee3
simplify ContextHandler
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 00:22:46 -0600 |
parents | 83cc6e05a58f |
children | 0608a6664bee |
files | src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/Request.java src/org/eclipse/jetty/server/Response.java src/org/eclipse/jetty/server/Server.java src/org/eclipse/jetty/server/handler/ContextHandler.java src/org/eclipse/jetty/server/handler/DefaultHandler.java src/org/eclipse/jetty/server/handler/RequestLogHandler.java src/org/eclipse/jetty/server/handler/ResourceHandler.java |
diffstat | 8 files changed, 55 insertions(+), 1076 deletions(-) [+] |
line wrap: on
line diff
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/AbstractHttpConnection.java --- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java Tue Oct 18 00:22:46 2016 -0600 @@ -23,8 +23,6 @@ import java.io.PrintWriter; import java.io.Writer; -import javax.servlet.DispatcherType; -import javax.servlet.RequestDispatcher; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; @@ -292,7 +290,6 @@ if (_out!=null) _out.reopen(); - _request.setDispatcherType(DispatcherType.REQUEST); _connector.customize(_endp, _request); _server.handle(this); }
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/Request.java --- a/src/org/eclipse/jetty/server/Request.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/Request.java Tue Oct 18 00:22:46 2016 -0600 @@ -121,7 +121,7 @@ * * */ -public class Request implements HttpServletRequest +public final class Request implements HttpServletRequest { public static final String __MULTIPART_CONFIG_ELEMENT = "org.eclipse.multipartConfig"; public static final String __MULTIPART_INPUT_STREAM = "org.eclipse.multiPartInputStream"; @@ -137,11 +137,9 @@ private String _characterEncoding; protected AbstractHttpConnection _connection; private ContextHandler.Context _context; - private boolean _newContext; private String _contextPath; private CookieCutter _cookies; private boolean _cookiesExtracted = false; - private DispatcherType _dispatcherType; private EndPoint _endp; private boolean _handled = false; private int _inputState = __NONE; @@ -221,12 +219,6 @@ int maxFormContentSize = -1; int maxFormKeys = -1; - if (_context != null) - { - maxFormContentSize = _context.getContextHandler().getMaxFormContentSize(); - maxFormKeys = _context.getContextHandler().getMaxFormKeys(); - } - if (maxFormContentSize < 0) { maxFormContentSize = 200000; @@ -454,10 +446,10 @@ return _connection._requestFields.getDateField(name); } - /* ------------------------------------------------------------ */ + @Override public DispatcherType getDispatcherType() { - return _dispatcherType; + throw new UnsupportedOperationException(); } /* ------------------------------------------------------------ */ @@ -708,15 +700,10 @@ return _pathInfo; } - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.http.HttpServletRequest#getPathTranslated() - */ + @Override public String getPathTranslated() { - if (_pathInfo == null || _context == null) - return null; - return _context.getRealPath(_pathInfo); + return null; } /* ------------------------------------------------------------ */ @@ -774,15 +761,10 @@ return _reader; } - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletRequest#getRealPath(java.lang.String) - */ + @Override public String getRealPath(String path) { - if (_context == null) - return null; - return _context.getRealPath(path); + return null; } /* ------------------------------------------------------------ */ @@ -826,10 +808,7 @@ return p.getName(); } - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String) - */ + @Override public RequestDispatcher getRequestDispatcher(String path) { throw new UnsupportedOperationException(); @@ -1036,9 +1015,10 @@ return _port; } + @Override public ServletContext getServletContext() { - return _context; + throw new UnsupportedOperationException(); } /* ------------------------------------------------------------ */ @@ -1332,24 +1312,11 @@ */ public void setContext(Context context) { - _newContext = _context != context; _context = context; } /* ------------------------------------------------------------ */ /** - * @return True if this is the first call of {@link #takeNewContext()} since the last - * {@link #setContext(org.eclipse.jetty.server.handler.ContextHandler.Context)} call. - */ - public boolean takeNewContext() - { - boolean nc = _newContext; - _newContext = false; - return nc; - } - - /* ------------------------------------------------------------ */ - /** * Sets the "context path" for this request * * @see HttpServletRequest#getContextPath() @@ -1372,12 +1339,6 @@ } /* ------------------------------------------------------------ */ - public void setDispatcherType(DispatcherType type) - { - _dispatcherType = type; - } - - /* ------------------------------------------------------------ */ public void setHandled(boolean h) { _handled = h;
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/Response.java --- a/src/org/eclipse/jetty/server/Response.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/Response.java Tue Oct 18 00:22:46 2016 -0600 @@ -1021,40 +1021,6 @@ _locale = locale; _connection._responseFields.put(HttpHeaders.CONTENT_LANGUAGE_BUFFER,locale.toString().replace('_','-')); - - if (_explicitEncoding || _outputState!=0 ) - return; - - if (_connection.getRequest().getContext()==null) - return; - - String charset = _connection.getRequest().getContext().getContextHandler().getLocaleEncoding(locale); - - if (charset!=null && charset.length()>0) - { - _characterEncoding=charset; - - /* get current MIME type from Content-Type header */ - String type=getContentType(); - if (type!=null) - { - _characterEncoding=charset; - int semi=type.indexOf(';'); - if (semi<0) - { - _mimeType=type; - _contentType= type += ";charset="+charset; - } - else - { - _mimeType=type.substring(0,semi); - _contentType= _mimeType += ";charset="+charset; - } - - _cachedMimeType=MimeTypes.CACHE.get(_mimeType); - _connection._responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER,_contentType); - } - } } /* ------------------------------------------------------------ */
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/Server.java --- a/src/org/eclipse/jetty/server/Server.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/Server.java Tue Oct 18 00:22:46 2016 -0600 @@ -155,12 +155,12 @@ else handle(target, request, request, response); } - +/* public void join() throws InterruptedException { threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); } - +*/ @Override public String toString() {
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/handler/ContextHandler.java --- a/src/org/eclipse/jetty/server/handler/ContextHandler.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ContextHandler.java Tue Oct 18 00:22:46 2016 -0600 @@ -39,7 +39,6 @@ import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; -import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; import javax.servlet.ServletContext; @@ -100,80 +99,43 @@ private static final ThreadLocal<Context> __context = new ThreadLocal<Context>(); - /** - * If a context attribute with this name is set, it is interpreted as a comma separated list of attribute name. Any other context attributes that are set - * with a name from this list will result in a call to {@link #setManagedAttribute(String, Object)}, which typically initiates the creation of a JMX MBean - * for the attribute value. - */ - public static final String MANAGED_ATTRIBUTES = "org.eclipse.jetty.server.context.ManagedAttributes"; - /* ------------------------------------------------------------ */ /** * Get the current ServletContext implementation. * * @return ServletContext implementation */ - public static Context getCurrentContext() + public static ContextHandler getCurrentContext() { - return __context.get(); + Context context = __context.get(); + return context==null ? null : context.getContextHandler(); } - protected Context _scontext; + private Context _scontext; private final AttributesMap _contextAttributes; - private final Map<String, String> _initParams; - private ClassLoader _classLoader; private String _contextPath = "/"; - private String _displayName; private Resource _baseResource; - private MimeTypes _mimeTypes; - private Map<String, String> _localeEncodingMap; - private String[] _welcomeFiles; - private String[] _vhosts; private Logger _logger; - private boolean _allowNullPathInfo; - private int _maxFormKeys = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormKeys",-1).intValue(); - private int _maxFormContentSize = Integer.getInteger("org.eclipse.jetty.server.Request.maxFormContentSize",-1).intValue(); - private boolean _compactPath = false; - private boolean _aliasesAllowed = false; - - private Map<String, Object> _managedAttributes; - private String[] _protectedTargets; - private final CopyOnWriteArrayList<AliasCheck> _aliasChecks = new CopyOnWriteArrayList<ContextHandler.AliasCheck>(); private boolean _shutdown = false; - private boolean _available = true; private volatile int _availability; // 0=STOPPED, 1=AVAILABLE, 2=SHUTDOWN, 3=UNAVAILABLE - private final static int __STOPPED = 0, __AVAILABLE = 1, __SHUTDOWN = 2, __UNAVAILABLE = 3; + private final static int __STOPPED = 0, __AVAILABLE = 1, __SHUTDOWN = 2; - /* ------------------------------------------------------------ */ - /** - * - */ public ContextHandler() { super(); _scontext = new Context(); _contextAttributes = new AttributesMap(); - _initParams = new HashMap<String, String>(); - addAliasCheck(new ApproveNonExistentDirectoryAliases()); } - /* ------------------------------------------------------------ */ - /** - * - */ public ContextHandler(String contextPath) { this(); setContextPath(contextPath); } - /* ------------------------------------------------------------ */ - /** - * - */ public ContextHandler(HandlerContainer parent, String contextPath) { this(); @@ -184,269 +146,25 @@ ((HandlerCollection)parent).addHandler(this); } - /* ------------------------------------------------------------ */ @Override public void dump(Appendable out, String indent) throws IOException { dumpThis(out); - dump(out,indent,Collections.singletonList(new CLDump(getClassLoader())),TypeUtil.asList(getHandlers()),getBeans(),_initParams.entrySet(), + dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(), _contextAttributes.getAttributeEntrySet()); } - /* ------------------------------------------------------------ */ public Context getServletContext() { return _scontext; } - /* ------------------------------------------------------------ */ - /** - * @return the allowNullPathInfo true if /context is not redirected to /context/ - */ - public boolean getAllowNullPathInfo() - { - return _allowNullPathInfo; - } - - /* ------------------------------------------------------------ */ - /** - * @param allowNullPathInfo - * true if /context is not redirected to /context/ - */ - public void setAllowNullPathInfo(boolean allowNullPathInfo) - { - _allowNullPathInfo = allowNullPathInfo; - } - - /* ------------------------------------------------------------ */ - /** - * Set the virtual hosts for the context. Only requests that have a matching host header or fully qualified URL will be passed to that context with a - * virtual host name. A context with no virtual host names or a null virtual host name is available to all requests that are not served by a context with a - * matching virtual host name. - * - * @param vhosts - * Array of virtual hosts that this context responds to. A null host name or null/empty array means any hostname is acceptable. Host names may be - * String representation of IP addresses. Host names may start with '*.' to wildcard one level of names. - */ - public void setVirtualHosts(String[] vhosts) - { - if (vhosts == null) - { - _vhosts = vhosts; - } - else - { - _vhosts = new String[vhosts.length]; - for (int i = 0; i < vhosts.length; i++) - _vhosts[i] = normalizeHostname(vhosts[i]); - } - } - - /* ------------------------------------------------------------ */ - /** Either set virtual hosts or add to an existing set of virtual hosts. - * - * @param virtualHosts - * Array of virtual hosts that this context responds to. A null host name or null/empty array means any hostname is acceptable. Host names may be - * String representation of IP addresses. Host names may start with '*.' to wildcard one level of names. - */ - public void addVirtualHosts(String[] virtualHosts) - { - if (virtualHosts == null) // since this is add, we don't null the old ones - { - return; - } - else - { - List<String> currentVirtualHosts = null; - if (_vhosts != null) - { - currentVirtualHosts = new ArrayList<String>(Arrays.asList(_vhosts)); - } - else - { - currentVirtualHosts = new ArrayList<String>(); - } - - for (int i = 0; i < virtualHosts.length; i++) - { - String normVhost = normalizeHostname(virtualHosts[i]); - if (!currentVirtualHosts.contains(normVhost)) - { - currentVirtualHosts.add(normVhost); - } - } - _vhosts = currentVirtualHosts.toArray(new String[0]); - } - } - - /* ------------------------------------------------------------ */ - /** - * Removes an array of virtual host entries, if this removes all entries the _vhosts will be set to null - * - * @param virtualHosts - * Array of virtual hosts that this context responds to. A null host name or null/empty array means any hostname is acceptable. Host names may be - * String representation of IP addresses. Host names may start with '*.' to wildcard one level of names. - */ - public void removeVirtualHosts(String[] virtualHosts) - { - if (virtualHosts == null) - { - return; // do nothing - } - else if ( _vhosts == null || _vhosts.length == 0) - { - return; // do nothing - } - else - { - List<String> existingVirtualHosts = new ArrayList<String>(Arrays.asList(_vhosts)); - - for (int i = 0; i < virtualHosts.length; i++) - { - String toRemoveVirtualHost = normalizeHostname(virtualHosts[i]); - if (existingVirtualHosts.contains(toRemoveVirtualHost)) - { - existingVirtualHosts.remove(toRemoveVirtualHost); - } - } - - if (existingVirtualHosts.isEmpty()) - { - _vhosts = null; // if we ended up removing them all, just null out _vhosts - } - else - { - _vhosts = existingVirtualHosts.toArray(new String[0]); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * Get the virtual hosts for the context. Only requests that have a matching host header or fully qualified URL will be passed to that context with a - * virtual host name. A context with no virtual host names or a null virtual host name is available to all requests that are not served by a context with a - * matching virtual host name. - * - * @return Array of virtual hosts that this context responds to. A null host name or empty array means any hostname is acceptable. Host names may be String - * representation of IP addresses. Host names may start with '*.' to wildcard one level of names. - */ - public String[] getVirtualHosts() - { - return _vhosts; - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the classLoader. - */ - public ClassLoader getClassLoader() - { - return _classLoader; - } - - /* ------------------------------------------------------------ */ - /** - * Make best effort to extract a file classpath from the context classloader - * - * @return Returns the classLoader. - */ - public String getClassPath() - { - if (_classLoader == null || !(_classLoader instanceof URLClassLoader)) - return null; - URLClassLoader loader = (URLClassLoader)_classLoader; - URL[] urls = loader.getURLs(); - StringBuilder classpath = new StringBuilder(); - for (int i = 0; i < urls.length; i++) - { - try - { - Resource resource = newResource(urls[i]); - File file = resource.getFile(); - if (file != null && file.exists()) - { - if (classpath.length() > 0) - classpath.append(File.pathSeparatorChar); - classpath.append(file.getAbsolutePath()); - } - } - catch (IOException e) - { - LOG.debug("",e); - } - } - if (classpath.length() == 0) - return null; - return classpath.toString(); - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the _contextPath. - */ public String getContextPath() { return _contextPath; } /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getInitParameter(java.lang.String) - */ - public String getInitParameter(String name) - { - return _initParams.get(name); - } - - /* ------------------------------------------------------------ */ - /* - */ - public String setInitParameter(String name, String value) - { - return _initParams.put(name,value); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getInitParameterNames() - */ - @SuppressWarnings("rawtypes") - public Enumeration getInitParameterNames() - { - return Collections.enumeration(_initParams.keySet()); - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the initParams. - */ - public Map<String, String> getInitParams() - { - return _initParams; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServletContextName() - */ - public String getDisplayName() - { - return _displayName; - } - - /* ------------------------------------------------------------ */ - /** - * @return true if this context is accepting new requests - */ - public boolean isShutdown() - { - synchronized (this) - { - return !_shutdown; - } - } - - /* ------------------------------------------------------------ */ /** * Set shutdown status. This field allows for graceful shutdown of a context. A started context may be put into non accepting state so that existing * requests can complete, but no new requests are accepted. @@ -459,32 +177,7 @@ synchronized (this) { _shutdown = shutdown; - _availability = isRunning()?(_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE):__STOPPED; - } - } - - /* ------------------------------------------------------------ */ - /** - * @return false if this context is unavailable (sends 503) - */ - public boolean isAvailable() - { - synchronized (this) - { - return _available; - } - } - - /* ------------------------------------------------------------ */ - /** - * Set Available status. - */ - public void setAvailable(boolean available) - { - synchronized (this) - { - _available = available; - _availability = isRunning()?(_shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE):__STOPPED; + _availability = isRunning()?(_shutdown?__SHUTDOWN:__AVAILABLE):__STOPPED; } } @@ -512,24 +205,13 @@ if (_contextPath == null) throw new IllegalStateException("Null contextPath"); - _logger = LoggerFactory.getLogger(getDisplayName() == null?getContextPath():getDisplayName()); + _logger = LoggerFactory.getLogger(getContextPath()); ClassLoader old_classloader = null; Thread current_thread = null; Context old_context = null; try { - // Set the classloader - if (_classLoader != null) - { - current_thread = Thread.currentThread(); - old_classloader = current_thread.getContextClassLoader(); - current_thread.setContextClassLoader(_classLoader); - } - - if (_mimeTypes == null) - _mimeTypes = new MimeTypes(); - old_context = __context.get(); __context.set(_scontext); @@ -538,19 +220,12 @@ synchronized(this) { - _availability = _shutdown?__SHUTDOWN:_available?__AVAILABLE:__UNAVAILABLE; + _availability = _shutdown?__SHUTDOWN:__AVAILABLE; } } finally { __context.set(old_context); - - // reset the classloader - if (_classLoader != null) - { - current_thread.setContextClassLoader(old_classloader); - } - } } @@ -563,23 +238,6 @@ */ protected void startContext() throws Exception { - String managedAttributes = _initParams.get(MANAGED_ATTRIBUTES); - if (managedAttributes != null) - { - _managedAttributes = new HashMap<String, Object>(); - String[] attributes = managedAttributes.split(","); - for (String attribute : attributes) - _managedAttributes.put(attribute,null); - - Enumeration e = _scontext.getAttributeNames(); - while (e.hasMoreElements()) - { - String name = (String)e.nextElement(); - Object value = _scontext.getAttribute(name); - checkManagedAttribute(name,value); - } - } - super.doStart(); } @@ -611,30 +269,12 @@ __context.set(_scontext); try { - // Set the classloader - if (_classLoader != null) - { - current_thread = Thread.currentThread(); - old_classloader = current_thread.getContextClassLoader(); - current_thread.setContextClassLoader(_classLoader); - } - super.doStop(); - - Enumeration e = _scontext.getAttributeNames(); - while (e.hasMoreElements()) - { - String name = (String)e.nextElement(); - checkManagedAttribute(name,null); - } } finally { LOG.info("stopped {}",this); __context.set(old_context); - // reset the classloader - if (_classLoader != null) - current_thread.setContextClassLoader(old_classloader); } _contextAttributes.clearAttributes(); @@ -644,49 +284,18 @@ /* * @see org.eclipse.jetty.server.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ - public boolean checkContext(final String target, final Request baseRequest, final HttpServletResponse response) throws IOException, ServletException + private boolean checkContext(final String target, final Request baseRequest, final HttpServletResponse response) throws IOException, ServletException { - DispatcherType dispatch = baseRequest.getDispatcherType(); - switch (_availability) { case __STOPPED: case __SHUTDOWN: return false; - case __UNAVAILABLE: - baseRequest.setHandled(true); - response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); - return false; default: - if ((DispatcherType.REQUEST.equals(dispatch) && baseRequest.isHandled())) + if (baseRequest.isHandled()) return false; } - // Check the vhosts - if (_vhosts != null && _vhosts.length > 0) - { - String vhost = normalizeHostname(baseRequest.getServerName()); - - boolean match = false; - - // TODO non-linear lookup - for (int i = 0; !match && i < _vhosts.length; i++) - { - String contextVhost = _vhosts[i]; - if (contextVhost == null) - continue; - if (contextVhost.startsWith("*.")) - { - // wildcard only at the beginning, and only for one additional subdomain level - match = contextVhost.regionMatches(true,2,vhost,vhost.indexOf(".") + 1,contextVhost.length() - 2); - } - else - match = contextVhost.equalsIgnoreCase(vhost); - } - if (!match) - return false; - } - // Are we not the root context? if (_contextPath.length() > 1) { @@ -697,7 +306,7 @@ return false; // redirect null path infos - if (!_allowNullPathInfo && _contextPath.length() == target.length()) + if (_contextPath.length() == target.length()) { // context request must end with / baseRequest.setHandled(true); @@ -731,45 +340,29 @@ Thread current_thread = null; String pathInfo = target; - DispatcherType dispatch = baseRequest.getDispatcherType(); - old_context = baseRequest.getContext(); // Are we already in this context? if (old_context != _scontext) { - // check the target. - if (DispatcherType.REQUEST.equals(dispatch) || DispatcherType.ASYNC.equals(dispatch)) - { - if (_compactPath) - target = URIUtil.compactPath(target); - if (!checkContext(target,baseRequest,response)) - return; + if (!checkContext(target,baseRequest,response)) + return; - if (target.length() > _contextPath.length()) - { - if (_contextPath.length() > 1) - target = target.substring(_contextPath.length()); - pathInfo = target; - } - else if (_contextPath.length() == 1) - { - target = URIUtil.SLASH; - pathInfo = URIUtil.SLASH; - } - else - { - target = URIUtil.SLASH; - pathInfo = null; - } + if (target.length() > _contextPath.length()) + { + if (_contextPath.length() > 1) + target = target.substring(_contextPath.length()); + pathInfo = target; } - - // Set the classloader - if (_classLoader != null) + else if (_contextPath.length() == 1) { - current_thread = Thread.currentThread(); - old_classloader = current_thread.getContextClassLoader(); - current_thread.setContextClassLoader(_classLoader); + target = URIUtil.SLASH; + pathInfo = URIUtil.SLASH; + } + else + { + target = URIUtil.SLASH; + pathInfo = null; } } @@ -782,7 +375,7 @@ // Update the paths baseRequest.setContext(_scontext); __context.set(_scontext); - if (!DispatcherType.INCLUDE.equals(dispatch) && target.startsWith("/")) + if (target.startsWith("/")) { if (_contextPath.length() == 1) baseRequest.setContextPath(""); @@ -810,12 +403,6 @@ { if (old_context != _scontext) { - // reset the classloader - if (_classLoader != null) - { - current_thread.setContextClassLoader(old_classloader); - } - // reset the context and servlet path. baseRequest.setContext(old_context); __context.set(old_context); @@ -834,13 +421,8 @@ @Override public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - final DispatcherType dispatch = baseRequest.getDispatcherType(); - final boolean new_context = baseRequest.takeNewContext(); try { - if (DispatcherType.REQUEST.equals(dispatch) && isProtectedTarget(target)) - throw new HttpException(HttpServletResponse.SC_NOT_FOUND); - // start manual inline of nextHandle(target,baseRequest,request,response); // noinspection ConstantIfStatement if (never()) @@ -859,117 +441,6 @@ } } - /* ------------------------------------------------------------ */ - /* - * Handle a runnable in this context - */ - public void handle(Runnable runnable) - { - ClassLoader old_classloader = null; - Thread current_thread = null; - Context old_context = null; - try - { - old_context = __context.get(); - __context.set(_scontext); - - // Set the classloader - if (_classLoader != null) - { - current_thread = Thread.currentThread(); - old_classloader = current_thread.getContextClassLoader(); - current_thread.setContextClassLoader(_classLoader); - } - - runnable.run(); - } - finally - { - __context.set(old_context); - if (old_classloader != null) - { - current_thread.setContextClassLoader(old_classloader); - } - } - } - - /* ------------------------------------------------------------ */ - /** - * Check the target. Called by {@link #handle(String, Request, HttpServletRequest, HttpServletResponse)} when a target within a context is determined. If - * the target is protected, 404 is returned. - */ - /* ------------------------------------------------------------ */ - public boolean isProtectedTarget(String target) - { - if (target == null || _protectedTargets == null) - return false; - - while (target.startsWith("//")) - target=URIUtil.compactPath(target); - - boolean isProtected = false; - int i=0; - while (!isProtected && i<_protectedTargets.length) - { - isProtected = StringUtil.startsWithIgnoreCase(target, _protectedTargets[i++]); - } - return isProtected; - } - - - public void setProtectedTargets (String[] targets) - { - if (targets == null) - { - _protectedTargets = null; - return; - } - - _protectedTargets = new String[targets.length]; - System.arraycopy(targets, 0, _protectedTargets, 0, targets.length); - } - - public String[] getProtectedTargets () - { - if (_protectedTargets == null) - return null; - - String[] tmp = new String[_protectedTargets.length]; - System.arraycopy(_protectedTargets, 0, tmp, 0, _protectedTargets.length); - return tmp; - } - - - /* ------------------------------------------------------------ */ - public void checkManagedAttribute(String name, Object value) - { - if (_managedAttributes != null && _managedAttributes.containsKey(name)) - { - setManagedAttribute(name,value); - } - } - - /* ------------------------------------------------------------ */ - public void setManagedAttribute(String name, Object value) - { - Object old = _managedAttributes.put(name,value); - } - - /* ------------------------------------------------------------ */ - /** - * @param classLoader - * The classLoader to set. - */ - public void setClassLoader(ClassLoader classLoader) - { - _classLoader = classLoader; - } - - /* ------------------------------------------------------------ */ - /** - * @param contextPath - * The _contextPath to set. - */ public void setContextPath(String contextPath) { if (contextPath != null && contextPath.length() > 1 && contextPath.endsWith("/")) @@ -979,22 +450,10 @@ /* ------------------------------------------------------------ */ /** - * @param servletContextName - * The servletContextName to set. - */ - public void setDisplayName(String servletContextName) - { - _displayName = servletContextName; - } - - /* ------------------------------------------------------------ */ - /** * @return Returns the resourceBase. */ public Resource getBaseResource() { - if (_baseResource == null) - return null; return _baseResource; } @@ -1028,7 +487,7 @@ { try { - setBaseResource(newResource(resourceBase)); + setBaseResource(Resource.newResource(resourceBase)); } catch (Exception e) { @@ -1038,122 +497,9 @@ } } - /* ------------------------------------------------------------ */ - /** - * @return True if aliases are allowed - */ - public boolean isAliases() - { - return _aliasesAllowed; - } - - /* ------------------------------------------------------------ */ - /** - * @param aliases - * aliases are allowed - */ - public void setAliases(boolean aliases) - { - _aliasesAllowed = aliases; - } - - /* ------------------------------------------------------------ */ - /** - * @return Returns the mimeTypes. - */ - public MimeTypes getMimeTypes() - { - if (_mimeTypes == null) - _mimeTypes = new MimeTypes(); - return _mimeTypes; - } - - /* ------------------------------------------------------------ */ - /** - * @param mimeTypes - * The mimeTypes to set. - */ - public void setMimeTypes(MimeTypes mimeTypes) - { - _mimeTypes = mimeTypes; - } - - /* ------------------------------------------------------------ */ - /** - */ - public void setWelcomeFiles(String[] files) - { - _welcomeFiles = files; - } - - /* ------------------------------------------------------------ */ - /** - * @return The names of the files which the server should consider to be welcome files in this context. - * @see <a href="http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html">The Servlet Specification</a> - * @see #setWelcomeFiles - */ - public String[] getWelcomeFiles() - { - return _welcomeFiles; - } - - /* ------------------------------------------------------------ */ - public int getMaxFormContentSize() - { - return _maxFormContentSize; - } - - /* ------------------------------------------------------------ */ - /** - * Set the maximum size of a form post, to protect against DOS attacks from large forms. - * @param maxSize - */ - public void setMaxFormContentSize(int maxSize) - { - _maxFormContentSize = maxSize; - } - - /* ------------------------------------------------------------ */ - public int getMaxFormKeys() - { - return _maxFormKeys; - } - - /* ------------------------------------------------------------ */ - /** - * Set the maximum number of form Keys to protect against DOS attack from crafted hash keys. - * @param max - */ - public void setMaxFormKeys(int max) - { - _maxFormKeys = max; - } - - /* ------------------------------------------------------------ */ - /** - * @return True if URLs are compacted to replace multiple '/'s with a single '/' - */ - public boolean isCompactPath() - { - return _compactPath; - } - - /* ------------------------------------------------------------ */ - /** - * @param compactPath - * True if URLs are compacted to replace multiple '/'s with a single '/' - */ - public void setCompactPath(boolean compactPath) - { - _compactPath = compactPath; - } - - /* ------------------------------------------------------------ */ @Override public String toString() { - String[] vhosts = getVirtualHosts(); - StringBuilder b = new StringBuilder(); Package pkg = getClass().getPackage(); @@ -1170,50 +516,12 @@ b.append(getClass().getSimpleName()); b.append('{').append(getContextPath()).append(',').append(getBaseResource()); - if (vhosts != null && vhosts.length > 0) - b.append(',').append(vhosts[0]); b.append('}'); return b.toString(); } /* ------------------------------------------------------------ */ - public void addLocaleEncoding(String locale, String encoding) - { - if (_localeEncodingMap == null) - _localeEncodingMap = new HashMap<String, String>(); - _localeEncodingMap.put(locale,encoding); - } - - /* ------------------------------------------------------------ */ - public String getLocaleEncoding(String locale) - { - if (_localeEncodingMap == null) - return null; - String encoding = _localeEncodingMap.get(locale); - return encoding; - } - - /* ------------------------------------------------------------ */ - /** - * Get the character encoding for a locale. The full locale name is first looked up in the map of encodings. If no encoding is found, then the locale - * language is looked up. - * - * @param locale - * a <code>Locale</code> value - * @return a <code>String</code> representing the character encoding for the locale or null if none found. - */ - public String getLocaleEncoding(Locale locale) - { - if (_localeEncodingMap == null) - return null; - String encoding = _localeEncodingMap.get(locale.toString()); - if (encoding == null) - encoding = _localeEncodingMap.get(locale.getLanguage()); - return encoding; - } - - /* ------------------------------------------------------------ */ /* */ public Resource getResource(String path) throws MalformedURLException @@ -1229,7 +537,7 @@ path = URIUtil.canonicalPath(path); Resource resource = _baseResource.addPath(path); - if (checkAlias(path,resource)) + if (resource.getAlias() == null) // check not alias return resource; return null; } @@ -1240,59 +548,7 @@ return null; } - - /* ------------------------------------------------------------ */ - public boolean checkAlias(String path, Resource resource) - { - // Is the resource aliased? - if (!_aliasesAllowed && resource.getAlias() != null) - { - if (LOG.isDebugEnabled()) - LOG.debug("Aliased resource: " + resource + "~=" + resource.getAlias()); - - // alias checks - for (Iterator<AliasCheck> i=_aliasChecks.iterator();i.hasNext();) - { - AliasCheck check = i.next(); - if (check.check(path,resource)) - { - if (LOG.isDebugEnabled()) - LOG.debug("Aliased resource: " + resource + " approved by " + check); - return true; - } - } - return false; - } - return true; - } - /* ------------------------------------------------------------ */ - /** - * Convert URL to Resource wrapper for {@link Resource#newResource(URL)} enables extensions to provide alternate resource implementations. - */ - public Resource newResource(URL url) throws IOException - { - return Resource.newResource(url); - } - - /* ------------------------------------------------------------ */ - /** - * Convert a URL or path to a Resource. The default implementation is a wrapper for {@link Resource#newResource(String)}. - * - * @param urlOrPath - * The URL or path to convert - * @return The Resource for the URL/path - * @throws IOException - * The Resource could not be created. - */ - public Resource newResource(String urlOrPath) throws IOException - { - return Resource.newResource(urlOrPath); - } - - /* ------------------------------------------------------------ */ - /* - */ public Set<String> getResourcePaths(String path) { try @@ -1323,37 +579,6 @@ } /* ------------------------------------------------------------ */ - private String normalizeHostname(String host) - { - if (host == null) - return null; - - if (host.endsWith(".")) - return host.substring(0,host.length() - 1); - - return host; - } - - /* ------------------------------------------------------------ */ - /** - * Add an AliasCheck instance to possibly permit aliased resources - * @param check The alias checker - */ - public void addAliasCheck(AliasCheck check) - { - _aliasChecks.add(check); - } - - /* ------------------------------------------------------------ */ - /** - * @return Mutable list of Alias checks - */ - public List<AliasCheck> getAliasChecks() - { - return _aliasChecks; - } - - /* ------------------------------------------------------------ */ /** * Context. * <p> @@ -1400,37 +625,14 @@ if (uripath.equals(context_path) || (uripath.startsWith(context_path) && uripath.charAt(context_path.length()) == '/') || "/".equals(context_path)) { - // look first for vhost matching context only - if (getVirtualHosts() != null && getVirtualHosts().length > 0) + if (matched_path == null || context_path.length() > matched_path.length()) { - if (ch.getVirtualHosts() != null && ch.getVirtualHosts().length > 0) - { - for (String h1 : getVirtualHosts()) - for (String h2 : ch.getVirtualHosts()) - if (h1.equals(h2)) - { - if (matched_path == null || context_path.length() > matched_path.length()) - { - contexts.clear(); - matched_path = context_path; - } + contexts.clear(); + matched_path = context_path; + } - if (matched_path.equals(context_path)) - contexts.add(ch); - } - } - } - else - { - if (matched_path == null || context_path.length() > matched_path.length()) - { - contexts.clear(); - matched_path = context_path; - } - - if (matched_path.equals(context_path)) - contexts.add(ch); - } + if (matched_path.equals(context_path)) + contexts.add(ch); } } @@ -1483,11 +685,6 @@ @Override public String getMimeType(String file) { - if (_mimeTypes == null) - return null; - Buffer mime = _mimeTypes.getMimeByExtension(file); - if (mime != null) - return mime.toString(); return null; } @@ -1677,7 +874,7 @@ @Override public String getInitParameter(String name) { - return ContextHandler.this.getInitParameter(name); + return null; } /* ------------------------------------------------------------ */ @@ -1688,7 +885,7 @@ @Override public Enumeration getInitParameterNames() { - return ContextHandler.this.getInitParameterNames(); + return null; } /* ------------------------------------------------------------ */ @@ -1729,7 +926,6 @@ @Override public synchronized void setAttribute(String name, Object value) { - checkManagedAttribute(name,value); Object old_value = _contextAttributes.getAttribute(name); if (value == null) @@ -1745,8 +941,6 @@ @Override public synchronized void removeAttribute(String name) { - checkManagedAttribute(name,null); - if (_contextAttributes == null) { // Set it on the handler @@ -1763,10 +957,7 @@ @Override public String getServletContextName() { - String name = ContextHandler.this.getDisplayName(); - if (name == null) - name = ContextHandler.this.getContextPath(); - return name; + return ContextHandler.this.getContextPath(); } /* ------------------------------------------------------------ */ @@ -1790,10 +981,7 @@ @Override public boolean setInitParameter(String name, String value) { - if (ContextHandler.this.getInitParameter(name) != null) - return false; - ContextHandler.this.getInitParams().put(name,value); - return true; + return false; } /* ------------------------------------------------------------ */ @@ -1934,7 +1122,7 @@ public ClassLoader getClassLoader() { AccessController.checkPermission(new RuntimePermission("getClassLoader")); - return _classLoader; + return null; } @Override @@ -1977,122 +1165,4 @@ } } - private static class CLDump implements Dumpable - { - final ClassLoader _loader; - - CLDump(ClassLoader loader) - { - _loader = loader; - } - - public String dump() - { - return AggregateLifeCycle.dump(this); - } - - public void dump(Appendable out, String indent) throws IOException - { - out.append(String.valueOf(_loader)).append("\n"); - - if (_loader != null) - { - Object parent = _loader.getParent(); - if (parent != null) - { - if (!(parent instanceof Dumpable)) - parent = new CLDump((ClassLoader)parent); - - if (_loader instanceof URLClassLoader) - AggregateLifeCycle.dump(out,indent,TypeUtil.asList(((URLClassLoader)_loader).getURLs()),Collections.singleton(parent)); - else - AggregateLifeCycle.dump(out,indent,Collections.singleton(parent)); - } - } - } - } - - - /* ------------------------------------------------------------ */ - /** Interface to check aliases - */ - public interface AliasCheck - { - /* ------------------------------------------------------------ */ - /** Check an alias - * @param path The path the aliased resource was created for - * @param resource The aliased resourced - * @return True if the resource is OK to be served. - */ - boolean check(String path, Resource resource); - } - - - /* ------------------------------------------------------------ */ - /** Approve Aliases with same suffix. - * Eg. a symbolic link from /foobar.html to /somewhere/wibble.html would be - * approved because both the resource and alias end with ".html". - */ - @Deprecated - public static class ApproveSameSuffixAliases implements AliasCheck - { - { - LOG.warn("ApproveSameSuffixAlias is not safe for production"); - } - - public boolean check(String path, Resource resource) - { - int dot = path.lastIndexOf('.'); - if (dot<0) - return false; - String suffix=path.substring(dot); - return resource.toString().endsWith(suffix); - } - } - - - /* ------------------------------------------------------------ */ - /** Approve Aliases with a path prefix. - * Eg. a symbolic link from /dirA/foobar.html to /dirB/foobar.html would be - * approved because both the resource and alias end with "/foobar.html". - */ - @Deprecated - public static class ApprovePathPrefixAliases implements AliasCheck - { - { - LOG.warn("ApprovePathPrefixAliases is not safe for production"); - } - - public boolean check(String path, Resource resource) - { - int slash = path.lastIndexOf('/'); - if (slash<0 || slash==path.length()-1) - return false; - String suffix=path.substring(slash); - return resource.toString().endsWith(suffix); - } - } - - /* ------------------------------------------------------------ */ - /** Approve Aliases of a non existent directory. - * If a directory "/foobar/" does not exist, then the resource is - * aliased to "/foobar". Accept such aliases. - */ - public static class ApproveNonExistentDirectoryAliases implements AliasCheck - { - public boolean check(String path, Resource resource) - { - if (resource.exists()) - return false; - - String a=resource.getAlias().toString(); - String r=resource.getURL().toString(); - - if (a.length()>r.length()) - return a.startsWith(r) && a.length()==r.length()+1 && a.endsWith("/"); - else - return r.startsWith(a) && r.length()==a.length()+1 && r.endsWith("/"); - } - } - }
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/handler/DefaultHandler.java --- a/src/org/eclipse/jetty/server/handler/DefaultHandler.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/DefaultHandler.java Tue Oct 18 00:22:46 2016 -0600 @@ -98,15 +98,11 @@ if (context.isRunning()) { writer.write("<li><a href=\""); - if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) - writer.write("http://"+context.getVirtualHosts()[0]+":"+request.getLocalPort()); writer.write(context.getContextPath()); if (context.getContextPath().length()>1 && context.getContextPath().endsWith("/")) writer.write("/"); writer.write("\">"); writer.write(context.getContextPath()); - if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) - writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); writer.write(" ---> "); writer.write(context.toString()); writer.write("</a></li>\n"); @@ -115,8 +111,6 @@ { writer.write("<li>"); writer.write(context.getContextPath()); - if (context.getVirtualHosts()!=null && context.getVirtualHosts().length>0) - writer.write(" @ "+context.getVirtualHosts()[0]+":"+request.getLocalPort()); writer.write(" ---> "); writer.write(context.toString()); if (context.isFailed())
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/handler/RequestLogHandler.java --- a/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/RequestLogHandler.java Tue Oct 18 00:22:46 2016 -0600 @@ -29,7 +29,6 @@ import java.util.Locale; import java.util.TimeZone; -import javax.servlet.DispatcherType; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; @@ -70,10 +69,7 @@ } finally { - if (baseRequest.getDispatcherType().equals(DispatcherType.REQUEST)) - { - log(baseRequest, (Response)response); - } + log(baseRequest, (Response)response); } }
diff -r 83cc6e05a58f -r 688c39c50ee3 src/org/eclipse/jetty/server/handler/ResourceHandler.java --- a/src/org/eclipse/jetty/server/handler/ResourceHandler.java Mon Oct 17 05:50:47 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ResourceHandler.java Tue Oct 18 00:22:46 2016 -0600 @@ -38,7 +38,6 @@ import javax.servlet.RequestDispatcher; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.server.handler.ContextHandler.Context; import org.eclipse.jetty.util.URIUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -133,11 +132,7 @@ public void doStart() throws Exception { - Context scontext = ContextHandler.getCurrentContext(); - _context = (scontext==null?null:scontext.getContextHandler()); - - if (_context!=null) - _aliases=_context.isAliases(); + _context = ContextHandler.getCurrentContext(); if (!_aliases && !FileResource.getCheckAliases()) throw new IllegalStateException("Alias checking disabled");