-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Disable undertow option to add Date response header and replace it …
…with DateHeaderInjector Undertow uses ThreadLocal<SimpleDateFormat> and this is not optimal for virtual threads
- Loading branch information
Showing
5 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/restheart/handlers/injectors/DateHeaderInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* restheart-core | ||
* %% | ||
* Copyright (C) 2014 - 2024 SoftInstigate | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
package org.restheart.handlers.injectors; | ||
|
||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Locale; | ||
|
||
import org.restheart.exchange.ServiceRequest; | ||
import org.restheart.exchange.ServiceResponse; | ||
import org.restheart.plugins.RegisterPlugin; | ||
import org.restheart.plugins.WildcardInterceptor; | ||
|
||
import com.google.common.net.HttpHeaders; | ||
|
||
import io.undertow.util.HttpString; | ||
|
||
/** | ||
* Author: Andrea Di Cesare <[email protected]> | ||
* | ||
* According to the HTTP specification, the `Date` header should be included in all responses, | ||
* except when the server lacks an accurate clock. | ||
* | ||
* In Undertow, the `Date` header is added via {@code ThreadLocal<SimpleDateFormat>}. | ||
* However, this approach is not optimal for virtual threads. | ||
*/ | ||
@RegisterPlugin(name="dateHeaderInjector", description="", enabledByDefault=true) | ||
public class DateHeaderInjector implements WildcardInterceptor { | ||
private static final HttpString DATE = HttpString.tryFromString(HttpHeaders.DATE); | ||
private static final String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; | ||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(RFC1123_PATTERN, Locale.US); | ||
private static final ZoneId GMT = ZoneId.of("GMT"); | ||
|
||
@Override | ||
public void handle(ServiceRequest<?> request, ServiceResponse<?> response) throws Exception { | ||
response.getHeaders().add(DATE, FORMATTER.format(ZonedDateTime.now(GMT))); | ||
} | ||
|
||
@Override | ||
public boolean resolve(ServiceRequest<?> request, ServiceResponse<?> response) { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,18 @@ | |
*/ | ||
package org.restheart.mongodb; | ||
|
||
import com.google.common.collect.Sets; | ||
import io.undertow.Undertow.Builder; | ||
import io.undertow.UndertowOptions; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.xnio.Option; | ||
|
||
import com.google.common.collect.Sets; | ||
|
||
import io.undertow.Undertow.Builder; | ||
import io.undertow.UndertowOptions; | ||
|
||
/** | ||
* | ||
* @author Andrea Di Cesare {@literal <[email protected]>} | ||
|
@@ -47,7 +50,6 @@ public class ConfigurationHelper { | |
UNDERTOW_OPTIONS.add(UndertowOptions.ALLOW_EQUALS_IN_COOKIE_VALUE); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.ALLOW_UNKNOWN_PROTOCOLS); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.ALLOW_UNESCAPED_CHARACTERS_IN_URL); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.ALWAYS_SET_DATE); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.ALWAYS_SET_KEEP_ALIVE); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.BUFFER_PIPELINED_DATA); | ||
UNDERTOW_OPTIONS.add(UndertowOptions.DECODE_URL); | ||
|
@@ -89,9 +91,7 @@ public class ConfigurationHelper { | |
* @param configuration | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static void setConnectionOptions( | ||
Builder builder, | ||
MongoServiceConfiguration configuration) { | ||
public static void setConnectionOptions(Builder builder, MongoServiceConfiguration configuration) { | ||
|
||
Map<String, Object> options = configuration.getConnectionOptions(); | ||
|
||
|
@@ -117,5 +117,10 @@ public static void setConnectionOptions( | |
} | ||
} | ||
}); | ||
|
||
// In Undertow, the `Date` header is added via {@code ThreadLocal<SimpleDateFormat>}. | ||
// * However, this approach is not optimal for virtual threads | ||
// we disable it and add the header with DateHeaderInjector | ||
builder.setServerOption(UndertowOptions.ALWAYS_SET_DATE, false); | ||
} | ||
} |