Merged branch 'jetty-9.4.x' into 'jetty-10.0.x'.

This commit is contained in:
Simone Bordet 2019-10-20 17:03:21 +02:00
commit fd792bc637
2 changed files with 42 additions and 102 deletions

View File

@ -121,7 +121,7 @@ import org.eclipse.jetty.util.thread.Scheduler;
* </dl>
* <p>
* This filter should be configured for {@link DispatcherType#REQUEST} and {@link DispatcherType#ASYNC} and with
* <code>&lt;async-supported&gt;true&lt;/async-supported&gt;</code>.
* {@code <async-supported>true</async-supported>}.
* </p>
*/
@ManagedObject("limits exposure to abuse from request flooding, whether malicious, or as a result of a misconfigured client")
@ -146,7 +146,6 @@ public class DoSFilter implements Filter
private static final long __DEFAULT_MAX_REQUEST_MS_INIT_PARAM = 30000L;
private static final long __DEFAULT_MAX_IDLE_TRACKER_MS_INIT_PARAM = 30000L;
static final String NAME = "name";
static final String MANAGED_ATTR_INIT_PARAM = "managedAttr";
static final String MAX_REQUESTS_PER_S_INIT_PARAM = "maxRequestsPerSec";
static final String DELAY_MS_INIT_PARAM = "delayMs";
@ -384,14 +383,13 @@ public class DoSFilter implements Filter
long throttleMs = getThrottleMs();
if (!Boolean.TRUE.equals(throttled) && throttleMs > 0)
{
final int priority = getPriority(request, tracker);
int priority = getPriority(request, tracker);
request.setAttribute(__THROTTLED, Boolean.TRUE);
if (isInsertHeaders())
response.addHeader("DoSFilter", "throttled");
AsyncContext asyncContext = request.startAsync();
request.setAttribute(_suspended, Boolean.TRUE);
if (throttleMs > 0)
asyncContext.setTimeout(throttleMs);
asyncContext.setTimeout(throttleMs);
asyncContext.addListener(_listeners[priority]);
_queues[priority].add(asyncContext);
if (LOG.isDebugEnabled())
@ -467,14 +465,7 @@ public class DoSFilter implements Filter
protected void doFilterChain(FilterChain chain, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
{
final Thread thread = Thread.currentThread();
Runnable requestTimeout = new Runnable()
{
@Override
public void run()
{
onRequestTimeout(request, response, thread);
}
};
Runnable requestTimeout = () -> onRequestTimeout(request, response, thread);
Scheduler.Task task = _scheduler.schedule(requestTimeout, getMaxRequestMs(), TimeUnit.MILLISECONDS);
try
{
@ -527,7 +518,7 @@ public class DoSFilter implements Filter
* @param tracker the rate tracker for this request
* @return the priority for this request
*/
protected int getPriority(HttpServletRequest request, RateTracker tracker)
private int getPriority(HttpServletRequest request, RateTracker tracker)
{
if (extractUserId(request) != null)
return USER_AUTH;
@ -544,7 +535,7 @@ public class DoSFilter implements Filter
return USER_AUTH;
}
public void schedule(RateTracker tracker)
private void schedule(RateTracker tracker)
{
_scheduler.schedule(tracker, getMaxIdleTrackerMs(), TimeUnit.MILLISECONDS);
}
@ -565,7 +556,7 @@ public class DoSFilter implements Filter
* @param request the current request
* @return the request rate tracker for the current connection
*/
public RateTracker getRateTracker(ServletRequest request)
RateTracker getRateTracker(ServletRequest request)
{
HttpSession session = ((HttpServletRequest)request).getSession(false);
@ -617,7 +608,7 @@ public class DoSFilter implements Filter
return tracker;
}
public void addToRateTracker(RateTracker tracker)
private void addToRateTracker(RateTracker tracker)
{
_rateTrackers.put(tracker.getId(), tracker);
}
@ -700,7 +691,7 @@ public class DoSFilter implements Filter
byte[] result = new byte[4];
for (int i = 0; i < result.length; ++i)
{
result[i] = ((Integer)Integer.parseInt(ipv4Matcher.group(i + 1))).byteValue();
result[i] = Integer.valueOf(ipv4Matcher.group(i + 1)).byteValue();
}
return result;
}
@ -1237,7 +1228,7 @@ public class DoSFilter implements Filter
LOG.debug("Tracker removed: {}", getId());
}
protected void addToRateTrackers(DoSFilter filter, RateTracker tracker)
private void addToRateTrackers(DoSFilter filter, RateTracker tracker)
{
if (filter == null)
return;
@ -1277,7 +1268,7 @@ public class DoSFilter implements Filter
}
}
class FixedRateTracker extends RateTracker
private static class FixedRateTracker extends RateTracker
{
public FixedRateTracker(ServletContext context, String filterName, String id, int type, int numRecentRequestsTracked)
{
@ -1306,15 +1297,15 @@ public class DoSFilter implements Filter
}
}
private class DoSTimeoutAsyncListener implements AsyncListener
private static class DoSTimeoutAsyncListener implements AsyncListener
{
@Override
public void onStartAsync(AsyncEvent event) throws IOException
public void onStartAsync(AsyncEvent event)
{
}
@Override
public void onComplete(AsyncEvent event) throws IOException
public void onComplete(AsyncEvent event)
{
}
@ -1325,7 +1316,7 @@ public class DoSFilter implements Filter
}
@Override
public void onError(AsyncEvent event) throws IOException
public void onError(AsyncEvent event)
{
}
}

View File

@ -49,11 +49,11 @@ public class Scanner extends AbstractLifeCycle
private static int __scannerId = 0;
private int _scanInterval;
private int _scanCount = 0;
private final List<Listener> _listeners = new ArrayList<Listener>();
private final Map<String, TimeNSize> _prevScan = new HashMap<String, TimeNSize>();
private final Map<String, TimeNSize> _currentScan = new HashMap<String, TimeNSize>();
private final List<Listener> _listeners = new ArrayList<>();
private final Map<String, TimeNSize> _prevScan = new HashMap<>();
private final Map<String, TimeNSize> _currentScan = new HashMap<>();
private FilenameFilter _filter;
private final List<File> _scanDirs = new ArrayList<File>();
private final List<File> _scanDirs = new ArrayList<>();
private volatile boolean _running = false;
private boolean _reportExisting = true;
private boolean _reportDirs = true;
@ -66,8 +66,7 @@ public class Scanner extends AbstractLifeCycle
ADDED, CHANGED, REMOVED
}
;
private final Map<String, Notification> _notifications = new HashMap<String, Notification>();
private final Map<String, Notification> _notifications = new HashMap<>();
static class TimeNSize
{
@ -412,11 +411,7 @@ public class Scanner extends AbstractLifeCycle
if (l instanceof ScanListener)
((ScanListener)l).scan();
}
catch (Exception e)
{
LOG.warn(e);
}
catch (Error e)
catch (Throwable e)
{
LOG.warn(e);
}
@ -428,16 +423,11 @@ public class Scanner extends AbstractLifeCycle
*/
public synchronized void scanFiles()
{
if (_scanDirs == null)
return;
_currentScan.clear();
Iterator<File> itor = _scanDirs.iterator();
while (itor.hasNext())
for (File dir : _scanDirs)
{
File dir = itor.next();
if ((dir != null) && (dir.exists()))
{
try
{
scanFile(dir.getCanonicalFile(), _currentScan, 0);
@ -446,6 +436,7 @@ public class Scanner extends AbstractLifeCycle
{
LOG.warn("Error scanning files.", e);
}
}
}
}
@ -455,11 +446,11 @@ public class Scanner extends AbstractLifeCycle
* @param currentScan the info from the most recent pass
* @param oldScan info from the previous pass
*/
public synchronized void reportDifferences(Map<String, TimeNSize> currentScan, Map<String, TimeNSize> oldScan)
private synchronized void reportDifferences(Map<String, TimeNSize> currentScan, Map<String, TimeNSize> oldScan)
{
// scan the differences and add what was found to the map of notifications:
Set<String> oldScanKeys = new HashSet<String>(oldScan.keySet());
Set<String> oldScanKeys = new HashSet<>(oldScan.keySet());
// Look for new and changed files
for (Map.Entry<String, TimeNSize> entry : currentScan.entrySet())
@ -484,17 +475,8 @@ public class Scanner extends AbstractLifeCycle
else if (!oldScan.get(file).equals(currentScan.get(file)))
{
Notification old = _notifications.put(file, Notification.CHANGED);
if (old != null)
{
switch (old)
{
case ADDED:
_notifications.put(file, Notification.ADDED);
break;
default:
break;
}
}
if (old == Notification.ADDED)
_notifications.put(file, Notification.ADDED);
}
}
@ -504,17 +486,8 @@ public class Scanner extends AbstractLifeCycle
if (!currentScan.containsKey(file))
{
Notification old = _notifications.put(file, Notification.REMOVED);
if (old != null)
{
switch (old)
{
case ADDED:
_notifications.remove(file);
break;
default:
break;
}
}
if (old == Notification.ADDED)
_notifications.remove(file);
}
}
@ -523,7 +496,7 @@ public class Scanner extends AbstractLifeCycle
// Process notifications
// Only process notifications that are for stable files (ie same in old and current scan).
List<String> bulkChanges = new ArrayList<String>();
List<String> bulkChanges = new ArrayList<>();
for (Iterator<Entry<String, Notification>> iter = _notifications.entrySet().iterator(); iter.hasNext(); )
{
Entry<String, Notification> entry = iter.next();
@ -577,7 +550,7 @@ public class Scanner extends AbstractLifeCycle
if (f.isFile() || depth > 0 && _reportDirs && f.isDirectory())
{
if ((_filter == null) || ((_filter != null) && _filter.accept(f.getParentFile(), f.getName())))
if (_filter == null || _filter.accept(f.getParentFile(), f.getName()))
{
if (LOG.isDebugEnabled())
LOG.debug("scan accepted {}", f);
@ -597,9 +570,9 @@ public class Scanner extends AbstractLifeCycle
File[] files = f.listFiles();
if (files != null)
{
for (int i = 0; i < files.length; i++)
for (File file : files)
{
scanFile(files[i], scanInfoMap, depth + 1);
scanFile(file, scanInfoMap, depth + 1);
}
}
else
@ -624,20 +597,14 @@ public class Scanner extends AbstractLifeCycle
*/
private void reportAddition(String filename)
{
Iterator<Listener> itor = _listeners.iterator();
while (itor.hasNext())
for (Listener l : _listeners)
{
Listener l = itor.next();
try
{
if (l instanceof DiscreteListener)
((DiscreteListener)l).fileAdded(filename);
}
catch (Exception e)
{
warn(l, filename, e);
}
catch (Error e)
catch (Throwable e)
{
warn(l, filename, e);
}
@ -651,20 +618,14 @@ public class Scanner extends AbstractLifeCycle
*/
private void reportRemoval(String filename)
{
Iterator<Listener> itor = _listeners.iterator();
while (itor.hasNext())
for (Object l : _listeners)
{
Object l = itor.next();
try
{
if (l instanceof DiscreteListener)
((DiscreteListener)l).fileRemoved(filename);
}
catch (Exception e)
{
warn(l, filename, e);
}
catch (Error e)
catch (Throwable e)
{
warn(l, filename, e);
}
@ -678,20 +639,14 @@ public class Scanner extends AbstractLifeCycle
*/
private void reportChange(String filename)
{
Iterator<Listener> itor = _listeners.iterator();
while (itor.hasNext())
for (Listener l : _listeners)
{
Listener l = itor.next();
try
{
if (l instanceof DiscreteListener)
((DiscreteListener)l).fileChanged(filename);
}
catch (Exception e)
{
warn(l, filename, e);
}
catch (Error e)
catch (Throwable e)
{
warn(l, filename, e);
}
@ -700,20 +655,14 @@ public class Scanner extends AbstractLifeCycle
private void reportBulkChanges(List<String> filenames)
{
Iterator<Listener> itor = _listeners.iterator();
while (itor.hasNext())
for (Listener l : _listeners)
{
Listener l = itor.next();
try
{
if (l instanceof BulkListener)
((BulkListener)l).filesChanged(filenames);
}
catch (Exception e)
{
warn(l, filenames.toString(), e);
}
catch (Error e)
catch (Throwable e)
{
warn(l, filenames.toString(), e);
}