refactoring: use constants for header names

This commit is contained in:
Sergey Skrobotov
2022-11-15 10:33:30 -08:00
parent 7fb7abb593
commit c32067759c
29 changed files with 166 additions and 163 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2021 Signal Messenger, LLC
* Copyright 2022 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
@@ -10,17 +10,23 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
/**
* Tools for working with chains of IP addresses in forwarding lists in HTTP headers.
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For">X-Forwarded-For - HTTP | MDN</a>
*/
public final class ForwardedIpUtil {
public final class HeaderUtils {
private ForwardedIpUtil() {
public static final String X_SIGNAL_AGENT = "X-Signal-Agent";
public static final String X_SIGNAL_KEY = "X-Signal-Key";
public static final String TIMESTAMP_HEADER = "X-Signal-Timestamp";
private HeaderUtils() {
// utility class
}
@Nonnull
public static String getTimestampHeader() {
return TIMESTAMP_HEADER + ":" + System.currentTimeMillis();
}
/**
* Returns the most recent proxy in a chain described by an {@code X-Forwarded-For} header.
*
@@ -28,6 +34,8 @@ public final class ForwardedIpUtil {
*
* @return the IP address of the most recent proxy in the forwarding chain, or empty if none was found or
* {@code forwardedFor} was null
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For">X-Forwarded-For - HTTP | MDN</a>
*/
@Nonnull
public static Optional<String> getMostRecentProxy(@Nullable final String forwardedFor) {

View File

@@ -1,18 +0,0 @@
/*
* Copyright 2013-2020 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.util;
public class TimestampHeaderUtil {
public static final String TIMESTAMP_HEADER = "X-Signal-Timestamp";
private TimestampHeaderUtil() {
}
public static String getTimestampHeader() {
return TIMESTAMP_HEADER + ":" + System.currentTimeMillis();
}
}

View File

@@ -6,6 +6,7 @@
package org.whispersystems.textsecuregcm.util.logging;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.HttpHeaders;
import io.dropwizard.jersey.errors.LoggingExceptionMapper;
import javax.inject.Provider;
import javax.ws.rs.core.Context;
@@ -38,7 +39,7 @@ public class LoggingUnhandledExceptionMapper extends LoggingExceptionMapper<Thro
// request shouldnt be `null`, but it is technically possible
requestMethod = request.get().getMethod();
requestPath = UriInfoUtil.getPathTemplate(request.get().getUriInfo());
userAgent = request.get().getHeaderString("user-agent");
userAgent = request.get().getHeaderString(HttpHeaders.USER_AGENT);
// streamline the user-agent if it is recognized
final UserAgent ua = UserAgentUtil.parseUserAgentString(userAgent);