mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-12-02 12:36:53 +00:00
Include raw "Accept-Language" header value in RequestAttributes
This commit is contained in:
1 parent
11df65b8d8
commit
a320504fdc
6 files changed
+45
-27
No files matched your search
@@ -5,12 +5,37 @@
|
||||
|
||||
package org.whispersystems.textsecuregcm.grpc;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public record RequestAttributes(InetAddress remoteAddress,
|
||||
@Nullable String userAgent,
|
||||
@Nullable String acceptLanguageRaw,
|
||||
List<Locale.LanguageRange> acceptLanguage) {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RequestAttributes.class);
|
||||
|
||||
public RequestAttributes(InetAddress remoteAddress,
|
||||
@Nullable String userAgent,
|
||||
@Nullable String acceptLanguageRaw) {
|
||||
this(remoteAddress, userAgent, acceptLanguageRaw, parseAcceptLanguage(acceptLanguageRaw, userAgent));
|
||||
}
|
||||
|
||||
private static List<Locale.LanguageRange> parseAcceptLanguage(final String acceptLanguageRaw, final String userAgent) {
|
||||
List<Locale.LanguageRange> acceptLanguages = Collections.emptyList();
|
||||
if (StringUtils.isNotBlank(acceptLanguageRaw)) {
|
||||
try {
|
||||
acceptLanguages = Locale.LanguageRange.parse(acceptLanguageRaw);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
LOGGER.debug("Invalid Accept-Language header from User-Agent {}: {}", userAgent, acceptLanguageRaw, e);
|
||||
}
|
||||
}
|
||||
return acceptLanguages;
|
||||
}
|
||||
}
|
||||
+1
-10
@@ -70,17 +70,8 @@ public class RequestAttributesInterceptor implements ServerInterceptor {
|
||||
return ServerInterceptorUtil.closeWithStatus(call, Status.UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Nullable List<Locale.LanguageRange> acceptLanguages = Collections.emptyList();
|
||||
if (StringUtils.isNotBlank(acceptLanguageHeader)) {
|
||||
try {
|
||||
acceptLanguages = Locale.LanguageRange.parse(acceptLanguageHeader);
|
||||
} catch (final IllegalArgumentException e) {
|
||||
log.debug("Invalid Accept-Language header from User-Agent {}: {}", userAgentHeader, acceptLanguageHeader, e);
|
||||
}
|
||||
}
|
||||
|
||||
final RequestAttributes requestAttributes =
|
||||
new RequestAttributes(remoteAddress.get(), userAgentHeader, acceptLanguages);
|
||||
new RequestAttributes(remoteAddress.get(), userAgentHeader, acceptLanguageHeader);
|
||||
return Contexts.interceptCall(
|
||||
Context.current().withValue(RequestAttributesUtil.REQUEST_ATTRIBUTES_CONTEXT_KEY, requestAttributes),
|
||||
call, headers, next);
|
||||
|
||||
+10
@@ -1,6 +1,7 @@
|
||||
package org.whispersystems.textsecuregcm.grpc;
|
||||
|
||||
import io.grpc.Context;
|
||||
import javax.annotation.Nullable;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -22,6 +23,15 @@ public class RequestAttributesUtil {
|
||||
return REQUEST_ATTRIBUTES_CONTEXT_KEY.get().acceptLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the raw "Accept-Language" header string from the remote client in the current gRPC request context
|
||||
*
|
||||
* @return the raw "Accept-Language" header listed by the remote client; may be null if not specified
|
||||
*/
|
||||
public static @Nullable String getAcceptLanguageRaw() {
|
||||
return REQUEST_ATTRIBUTES_CONTEXT_KEY.get().acceptLanguageRaw();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of distinct locales supported by the JVM and accepted by the remote client in the current gRPC
|
||||
* context. May be empty if the client did not supply a list of acceptable languages, if the list of acceptable
|
||||
|
||||
Reference in new issue
Block a user