Skip to content

Commit fd792bc

Browse files
committed
Merged branch 'jetty-9.4.x' into 'jetty-10.0.x'.
2 parents d2f714c + 862ac40 commit fd792bc

File tree

2 files changed

+42
-102
lines changed

2 files changed

+42
-102
lines changed

jetty-servlets/src/main/java/org/eclipse/jetty/servlets/DoSFilter.java

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
* </dl>
122122
* <p>
123123
* This filter should be configured for {@link DispatcherType#REQUEST} and {@link DispatcherType#ASYNC} and with
124-
* <code>&lt;async-supported&gt;true&lt;/async-supported&gt;</code>.
124+
* {@code <async-supported>true</async-supported>}.
125125
* </p>
126126
*/
127127
@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
146146
private static final long __DEFAULT_MAX_REQUEST_MS_INIT_PARAM = 30000L;
147147
private static final long __DEFAULT_MAX_IDLE_TRACKER_MS_INIT_PARAM = 30000L;
148148

149-
static final String NAME = "name";
150149
static final String MANAGED_ATTR_INIT_PARAM = "managedAttr";
151150
static final String MAX_REQUESTS_PER_S_INIT_PARAM = "maxRequestsPerSec";
152151
static final String DELAY_MS_INIT_PARAM = "delayMs";
@@ -384,14 +383,13 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
384383
long throttleMs = getThrottleMs();
385384
if (!Boolean.TRUE.equals(throttled) && throttleMs > 0)
386385
{
387-
final int priority = getPriority(request, tracker);
386+
int priority = getPriority(request, tracker);
388387
request.setAttribute(__THROTTLED, Boolean.TRUE);
389388
if (isInsertHeaders())
390389
response.addHeader("DoSFilter", "throttled");
391390
AsyncContext asyncContext = request.startAsync();
392391
request.setAttribute(_suspended, Boolean.TRUE);
393-
if (throttleMs > 0)
394-
asyncContext.setTimeout(throttleMs);
392+
asyncContext.setTimeout(throttleMs);
395393
asyncContext.addListener(_listeners[priority]);
396394
_queues[priority].add(asyncContext);
397395
if (LOG.isDebugEnabled())
@@ -467,14 +465,7 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
467465
protected void doFilterChain(FilterChain chain, final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException
468466
{
469467
final Thread thread = Thread.currentThread();
470-
Runnable requestTimeout = new Runnable()
471-
{
472-
@Override
473-
public void run()
474-
{
475-
onRequestTimeout(request, response, thread);
476-
}
477-
};
468+
Runnable requestTimeout = () -> onRequestTimeout(request, response, thread);
478469
Scheduler.Task task = _scheduler.schedule(requestTimeout, getMaxRequestMs(), TimeUnit.MILLISECONDS);
479470
try
480471
{
@@ -527,7 +518,7 @@ protected void onRequestTimeout(HttpServletRequest request, HttpServletResponse
527518
* @param tracker the rate tracker for this request
528519
* @return the priority for this request
529520
*/
530-
protected int getPriority(HttpServletRequest request, RateTracker tracker)
521+
private int getPriority(HttpServletRequest request, RateTracker tracker)
531522
{
532523
if (extractUserId(request) != null)
533524
return USER_AUTH;
@@ -544,7 +535,7 @@ protected int getMaxPriority()
544535
return USER_AUTH;
545536
}
546537

547-
public void schedule(RateTracker tracker)
538+
private void schedule(RateTracker tracker)
548539
{
549540
_scheduler.schedule(tracker, getMaxIdleTrackerMs(), TimeUnit.MILLISECONDS);
550541
}
@@ -565,7 +556,7 @@ public void schedule(RateTracker tracker)
565556
* @param request the current request
566557
* @return the request rate tracker for the current connection
567558
*/
568-
public RateTracker getRateTracker(ServletRequest request)
559+
RateTracker getRateTracker(ServletRequest request)
569560
{
570561
HttpSession session = ((HttpServletRequest)request).getSession(false);
571562

@@ -617,7 +608,7 @@ else if (session != null)
617608
return tracker;
618609
}
619610

620-
public void addToRateTracker(RateTracker tracker)
611+
private void addToRateTracker(RateTracker tracker)
621612
{
622613
_rateTrackers.put(tracker.getId(), tracker);
623614
}
@@ -700,7 +691,7 @@ private byte[] addressToBytes(String address)
700691
byte[] result = new byte[4];
701692
for (int i = 0; i < result.length; ++i)
702693
{
703-
result[i] = ((Integer)Integer.parseInt(ipv4Matcher.group(i + 1))).byteValue();
694+
result[i] = Integer.valueOf(ipv4Matcher.group(i + 1)).byteValue();
704695
}
705696
return result;
706697
}
@@ -1237,7 +1228,7 @@ protected void removeFromRateTrackers(DoSFilter filter, String id)
12371228
LOG.debug("Tracker removed: {}", getId());
12381229
}
12391230

1240-
protected void addToRateTrackers(DoSFilter filter, RateTracker tracker)
1231+
private void addToRateTrackers(DoSFilter filter, RateTracker tracker)
12411232
{
12421233
if (filter == null)
12431234
return;
@@ -1277,7 +1268,7 @@ public String toString()
12771268
}
12781269
}
12791270

1280-
class FixedRateTracker extends RateTracker
1271+
private static class FixedRateTracker extends RateTracker
12811272
{
12821273
public FixedRateTracker(ServletContext context, String filterName, String id, int type, int numRecentRequestsTracked)
12831274
{
@@ -1306,15 +1297,15 @@ public String toString()
13061297
}
13071298
}
13081299

1309-
private class DoSTimeoutAsyncListener implements AsyncListener
1300+
private static class DoSTimeoutAsyncListener implements AsyncListener
13101301
{
13111302
@Override
1312-
public void onStartAsync(AsyncEvent event) throws IOException
1303+
public void onStartAsync(AsyncEvent event)
13131304
{
13141305
}
13151306

13161307
@Override
1317-
public void onComplete(AsyncEvent event) throws IOException
1308+
public void onComplete(AsyncEvent event)
13181309
{
13191310
}
13201311

@@ -1325,7 +1316,7 @@ public void onTimeout(AsyncEvent event) throws IOException
13251316
}
13261317

13271318
@Override
1328-
public void onError(AsyncEvent event) throws IOException
1319+
public void onError(AsyncEvent event)
13291320
{
13301321
}
13311322
}

jetty-util/src/main/java/org/eclipse/jetty/util/Scanner.java

Lines changed: 27 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public class Scanner extends AbstractLifeCycle
4949
private static int __scannerId = 0;
5050
private int _scanInterval;
5151
private int _scanCount = 0;
52-
private final List<Listener> _listeners = new ArrayList<Listener>();
53-
private final Map<String, TimeNSize> _prevScan = new HashMap<String, TimeNSize>();
54-
private final Map<String, TimeNSize> _currentScan = new HashMap<String, TimeNSize>();
52+
private final List<Listener> _listeners = new ArrayList<>();
53+
private final Map<String, TimeNSize> _prevScan = new HashMap<>();
54+
private final Map<String, TimeNSize> _currentScan = new HashMap<>();
5555
private FilenameFilter _filter;
56-
private final List<File> _scanDirs = new ArrayList<File>();
56+
private final List<File> _scanDirs = new ArrayList<>();
5757
private volatile boolean _running = false;
5858
private boolean _reportExisting = true;
5959
private boolean _reportDirs = true;
@@ -66,8 +66,7 @@ public enum Notification
6666
ADDED, CHANGED, REMOVED
6767
}
6868

69-
;
70-
private final Map<String, Notification> _notifications = new HashMap<String, Notification>();
69+
private final Map<String, Notification> _notifications = new HashMap<>();
7170

7271
static class TimeNSize
7372
{
@@ -412,11 +411,7 @@ public synchronized void scan()
412411
if (l instanceof ScanListener)
413412
((ScanListener)l).scan();
414413
}
415-
catch (Exception e)
416-
{
417-
LOG.warn(e);
418-
}
419-
catch (Error e)
414+
catch (Throwable e)
420415
{
421416
LOG.warn(e);
422417
}
@@ -428,16 +423,11 @@ public synchronized void scan()
428423
*/
429424
public synchronized void scanFiles()
430425
{
431-
if (_scanDirs == null)
432-
return;
433-
434426
_currentScan.clear();
435-
Iterator<File> itor = _scanDirs.iterator();
436-
while (itor.hasNext())
427+
for (File dir : _scanDirs)
437428
{
438-
File dir = itor.next();
439-
440429
if ((dir != null) && (dir.exists()))
430+
{
441431
try
442432
{
443433
scanFile(dir.getCanonicalFile(), _currentScan, 0);
@@ -446,6 +436,7 @@ public synchronized void scanFiles()
446436
{
447437
LOG.warn("Error scanning files.", e);
448438
}
439+
}
449440
}
450441
}
451442

@@ -455,11 +446,11 @@ public synchronized void scanFiles()
455446
* @param currentScan the info from the most recent pass
456447
* @param oldScan info from the previous pass
457448
*/
458-
public synchronized void reportDifferences(Map<String, TimeNSize> currentScan, Map<String, TimeNSize> oldScan)
449+
private synchronized void reportDifferences(Map<String, TimeNSize> currentScan, Map<String, TimeNSize> oldScan)
459450
{
460451
// scan the differences and add what was found to the map of notifications:
461452

462-
Set<String> oldScanKeys = new HashSet<String>(oldScan.keySet());
453+
Set<String> oldScanKeys = new HashSet<>(oldScan.keySet());
463454

464455
// Look for new and changed files
465456
for (Map.Entry<String, TimeNSize> entry : currentScan.entrySet())
@@ -484,17 +475,8 @@ public synchronized void reportDifferences(Map<String, TimeNSize> currentScan, M
484475
else if (!oldScan.get(file).equals(currentScan.get(file)))
485476
{
486477
Notification old = _notifications.put(file, Notification.CHANGED);
487-
if (old != null)
488-
{
489-
switch (old)
490-
{
491-
case ADDED:
492-
_notifications.put(file, Notification.ADDED);
493-
break;
494-
default:
495-
break;
496-
}
497-
}
478+
if (old == Notification.ADDED)
479+
_notifications.put(file, Notification.ADDED);
498480
}
499481
}
500482

@@ -504,17 +486,8 @@ else if (!oldScan.get(file).equals(currentScan.get(file)))
504486
if (!currentScan.containsKey(file))
505487
{
506488
Notification old = _notifications.put(file, Notification.REMOVED);
507-
if (old != null)
508-
{
509-
switch (old)
510-
{
511-
case ADDED:
512-
_notifications.remove(file);
513-
break;
514-
default:
515-
break;
516-
}
517-
}
489+
if (old == Notification.ADDED)
490+
_notifications.remove(file);
518491
}
519492
}
520493

@@ -523,7 +496,7 @@ else if (!oldScan.get(file).equals(currentScan.get(file)))
523496

524497
// Process notifications
525498
// Only process notifications that are for stable files (ie same in old and current scan).
526-
List<String> bulkChanges = new ArrayList<String>();
499+
List<String> bulkChanges = new ArrayList<>();
527500
for (Iterator<Entry<String, Notification>> iter = _notifications.entrySet().iterator(); iter.hasNext(); )
528501
{
529502
Entry<String, Notification> entry = iter.next();
@@ -577,7 +550,7 @@ private void scanFile(File f, Map<String, TimeNSize> scanInfoMap, int depth)
577550

578551
if (f.isFile() || depth > 0 && _reportDirs && f.isDirectory())
579552
{
580-
if ((_filter == null) || ((_filter != null) && _filter.accept(f.getParentFile(), f.getName())))
553+
if (_filter == null || _filter.accept(f.getParentFile(), f.getName()))
581554
{
582555
if (LOG.isDebugEnabled())
583556
LOG.debug("scan accepted {}", f);
@@ -597,9 +570,9 @@ private void scanFile(File f, Map<String, TimeNSize> scanInfoMap, int depth)
597570
File[] files = f.listFiles();
598571
if (files != null)
599572
{
600-
for (int i = 0; i < files.length; i++)
573+
for (File file : files)
601574
{
602-
scanFile(files[i], scanInfoMap, depth + 1);
575+
scanFile(file, scanInfoMap, depth + 1);
603576
}
604577
}
605578
else
@@ -624,20 +597,14 @@ private void warn(Object listener, String filename, Throwable th)
624597
*/
625598
private void reportAddition(String filename)
626599
{
627-
Iterator<Listener> itor = _listeners.iterator();
628-
while (itor.hasNext())
600+
for (Listener l : _listeners)
629601
{
630-
Listener l = itor.next();
631602
try
632603
{
633604
if (l instanceof DiscreteListener)
634605
((DiscreteListener)l).fileAdded(filename);
635606
}
636-
catch (Exception e)
637-
{
638-
warn(l, filename, e);
639-
}
640-
catch (Error e)
607+
catch (Throwable e)
641608
{
642609
warn(l, filename, e);
643610
}
@@ -651,20 +618,14 @@ private void reportAddition(String filename)
651618
*/
652619
private void reportRemoval(String filename)
653620
{
654-
Iterator<Listener> itor = _listeners.iterator();
655-
while (itor.hasNext())
621+
for (Object l : _listeners)
656622
{
657-
Object l = itor.next();
658623
try
659624
{
660625
if (l instanceof DiscreteListener)
661626
((DiscreteListener)l).fileRemoved(filename);
662627
}
663-
catch (Exception e)
664-
{
665-
warn(l, filename, e);
666-
}
667-
catch (Error e)
628+
catch (Throwable e)
668629
{
669630
warn(l, filename, e);
670631
}
@@ -678,20 +639,14 @@ private void reportRemoval(String filename)
678639
*/
679640
private void reportChange(String filename)
680641
{
681-
Iterator<Listener> itor = _listeners.iterator();
682-
while (itor.hasNext())
642+
for (Listener l : _listeners)
683643
{
684-
Listener l = itor.next();
685644
try
686645
{
687646
if (l instanceof DiscreteListener)
688647
((DiscreteListener)l).fileChanged(filename);
689648
}
690-
catch (Exception e)
691-
{
692-
warn(l, filename, e);
693-
}
694-
catch (Error e)
649+
catch (Throwable e)
695650
{
696651
warn(l, filename, e);
697652
}
@@ -700,20 +655,14 @@ private void reportChange(String filename)
700655

701656
private void reportBulkChanges(List<String> filenames)
702657
{
703-
Iterator<Listener> itor = _listeners.iterator();
704-
while (itor.hasNext())
658+
for (Listener l : _listeners)
705659
{
706-
Listener l = itor.next();
707660
try
708661
{
709662
if (l instanceof BulkListener)
710663
((BulkListener)l).filesChanged(filenames);
711664
}
712-
catch (Exception e)
713-
{
714-
warn(l, filenames.toString(), e);
715-
}
716-
catch (Error e)
665+
catch (Throwable e)
717666
{
718667
warn(l, filenames.toString(), e);
719668
}

0 commit comments

Comments
 (0)