Skip to content

Commit

Permalink
Use en locale for uppercase/lowercase (fixes #156)
Browse files Browse the repository at this point in the history
  • Loading branch information
shred committed Mar 9, 2024
1 parent bbc057b commit 5119541
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion acme4j-client/src/main/java/org/shredzone/acme4j/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.shredzone.acme4j;

import java.util.Arrays;
import java.util.Locale;

/**
* An enumeration of status codes of challenges and authorizations.
Expand Down Expand Up @@ -84,7 +85,7 @@ public enum Status {
* no match
*/
public static Status parse(String str) {
var check = str.toUpperCase();
var check = str.toUpperCase(Locale.ENGLISH);
return Arrays.stream(values())
.filter(s -> s.name().equals(check))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static boolean isValidBase64Url(@Nullable String base64) {
*/
public static String toAce(String domain) {
Objects.requireNonNull(domain, "domain");
return IDN.toASCII(domain.trim()).toLowerCase();
return IDN.toASCII(domain.trim()).toLowerCase(Locale.ENGLISH);
}

/**
Expand Down Expand Up @@ -307,7 +307,7 @@ public static String getContentType(@Nullable String header) {
if (charset != null && !"utf-8".equalsIgnoreCase(charset)) {
throw new AcmeProtocolException("Unsupported charset " + charset);
}
return m.group(1).trim().toLowerCase();
return m.group(1).trim().toLowerCase(Locale.ENGLISH);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Locale;

import org.junit.jupiter.api.Test;

/**
Expand All @@ -27,8 +29,11 @@ public class StatusTest {
*/
@Test
public void testParse() {
// Would break toUpperCase() if English locale is not set, see #156.
Locale.setDefault(new Locale("tr"));

for (var s : Status.values()) {
var parsed = Status.parse(s.name().toLowerCase());
var parsed = Status.parse(s.name().toLowerCase(Locale.ENGLISH));
assertThat(parsed).isEqualTo(s);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void importUntrustedHeaders(Enumeration<Header> en) {
while (en.hasMoreElements()) {
var h = en.nextElement();
var name = h.getName();
if (IGNORE_HEADERS.contains(name.toUpperCase())) {
if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) {
continue;
}

Expand All @@ -95,7 +96,7 @@ public void importTrustedHeaders(Enumeration<Header> en) throws AcmeInvalidMessa
while (en.hasMoreElements()) {
var h = en.nextElement();
var name = h.getName();
if (IGNORE_HEADERS.contains(name.toUpperCase())) {
if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) {
continue;
}

Expand All @@ -122,7 +123,7 @@ public void importTrustedHeadersRelaxed(Enumeration<Header> en) {
while (en.hasMoreElements()) {
var h = en.nextElement();
var name = h.getName();
if (IGNORE_HEADERS.contains(name.toUpperCase())) {
if (IGNORE_HEADERS.contains(name.toUpperCase(Locale.ENGLISH))) {
continue;
}

Expand Down Expand Up @@ -252,7 +253,7 @@ public boolean isAutoSubmitted() {
.filter(mh -> "AUTO-SUBMITTED".equalsIgnoreCase(mh.name))
.map(mh -> mh.value)
.map(String::trim)
.map(String::toLowerCase)
.map(mh -> mh.toLowerCase(Locale.ENGLISH))
.anyMatch(h -> h.equals("auto-generated") || h.startsWith("auto-generated;"));
}

Expand All @@ -266,7 +267,7 @@ public Set<String> getMissingSecuredHeaders() {
headers.stream()
.filter(mh -> mh.trusted)
.map(mh -> mh.name)
.map(String::toUpperCase)
.map(mh -> mh.toUpperCase(Locale.ENGLISH))
.forEach(missing::remove);
return missing;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Optional;

import jakarta.mail.Message;
Expand Down Expand Up @@ -136,7 +137,7 @@ public boolean isAutoSubmitted() throws AcmeInvalidMessageException {
}
return Arrays.stream(autoSubmitted)
.map(String::trim)
.map(String::toLowerCase)
.map(as -> as.toLowerCase(Locale.ENGLISH))
.anyMatch(h -> h.equals("auto-generated") || h.startsWith("auto-generated;"));
} catch (MessagingException ex) {
throw new AcmeInvalidMessageException("Could not read '" + HEADER_AUTO_SUBMITTED + "' header", ex);
Expand Down

0 comments on commit 5119541

Please sign in to comment.