260806_03_ENH: mutipart 한글 꺠짐 개선
This commit is contained in:
@@ -10,8 +10,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@@ -133,33 +131,25 @@ public class ExternalApiClient {
|
||||
}
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
Set<String> usedAsciiFilenames = collectAsciiFilenames(parts);
|
||||
for (ExternalApiMultipartPart part : parts) {
|
||||
if (part == null) {
|
||||
throw new IllegalArgumentException("parts must not contain null");
|
||||
}
|
||||
|
||||
writeAscii(out, "--" + boundary + CRLF);
|
||||
writeAscii(out, "Content-Disposition: form-data; name=\"" + headerValue(part.getName()) + "\"");
|
||||
writeUtf8(out, "--" + boundary + CRLF);
|
||||
writeUtf8(out, "Content-Disposition: form-data; name=\"" + headerValue(part.getName()) + "\"");
|
||||
if (part.getFilename() != null) {
|
||||
String filename = safeHeader(part.getFilename());
|
||||
if (isAscii(filename)) {
|
||||
writeAscii(out, "; filename=\"" + headerValue(filename) + "\"");
|
||||
} else {
|
||||
String fallbackFilename = uniqueAsciiFallback(filename, usedAsciiFilenames);
|
||||
writeAscii(out, "; filename=\"" + headerValue(fallbackFilename) + "\"");
|
||||
writeAscii(out, "; filename*=UTF-8''" + percentEncodeUtf8(filename));
|
||||
}
|
||||
writeUtf8(out, "; filename=\"" + headerValue(part.getFilename()) + "\"");
|
||||
}
|
||||
writeAscii(out, CRLF);
|
||||
writeUtf8(out, CRLF);
|
||||
if (part.getContentType() != null && !part.getContentType().isBlank()) {
|
||||
writeAscii(out, "Content-Type: " + safeHeader(part.getContentType()) + CRLF);
|
||||
writeUtf8(out, "Content-Type: " + safeHeader(part.getContentType()) + CRLF);
|
||||
}
|
||||
writeAscii(out, CRLF);
|
||||
writeUtf8(out, CRLF);
|
||||
out.write(part.content());
|
||||
writeAscii(out, CRLF);
|
||||
writeUtf8(out, CRLF);
|
||||
}
|
||||
writeAscii(out, "--" + boundary + "--" + CRLF);
|
||||
writeUtf8(out, "--" + boundary + "--" + CRLF);
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
@@ -175,85 +165,10 @@ public class ExternalApiClient {
|
||||
});
|
||||
}
|
||||
|
||||
private static void writeAscii(ByteArrayOutputStream out, String value) throws IOException {
|
||||
out.write(value.getBytes(StandardCharsets.US_ASCII));
|
||||
}
|
||||
|
||||
private static Set<String> collectAsciiFilenames(List<ExternalApiMultipartPart> parts) {
|
||||
Set<String> filenames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
|
||||
for (ExternalApiMultipartPart part : parts) {
|
||||
if (part != null && part.getFilename() != null) {
|
||||
String filename = safeHeader(part.getFilename());
|
||||
if (isAscii(filename)) {
|
||||
filenames.add(filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
return filenames;
|
||||
}
|
||||
|
||||
private static String uniqueAsciiFallback(String filename, Set<String> usedFilenames) {
|
||||
String extension = asciiExtension(filename);
|
||||
String candidate = "attachment" + extension;
|
||||
int sequence = 2;
|
||||
while (!usedFilenames.add(candidate)) {
|
||||
candidate = "attachment-" + sequence++ + extension;
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
private static String asciiExtension(String filename) {
|
||||
int lastSeparator = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
||||
int lastDot = filename.lastIndexOf('.');
|
||||
if (lastDot <= lastSeparator || lastDot == filename.length() - 1) {
|
||||
return "";
|
||||
}
|
||||
|
||||
String extension = filename.substring(lastDot + 1);
|
||||
if (extension.length() > 16 || !extension.chars().allMatch(Character::isLetterOrDigit)
|
||||
|| !isAscii(extension)) {
|
||||
return "";
|
||||
}
|
||||
return "." + extension;
|
||||
}
|
||||
|
||||
private static boolean isAscii(String value) {
|
||||
return value.chars().allMatch(ch -> ch <= 0x7F);
|
||||
}
|
||||
|
||||
private static String percentEncodeUtf8(String value) {
|
||||
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
|
||||
StringBuilder encoded = new StringBuilder(bytes.length * 3);
|
||||
char[] hex = "0123456789ABCDEF".toCharArray();
|
||||
for (byte current : bytes) {
|
||||
int unsigned = current & 0xFF;
|
||||
if (isRfc5987AttrChar(unsigned)) {
|
||||
encoded.append((char) unsigned);
|
||||
} else {
|
||||
encoded.append('%')
|
||||
.append(hex[unsigned >>> 4])
|
||||
.append(hex[unsigned & 0x0F]);
|
||||
}
|
||||
}
|
||||
return encoded.toString();
|
||||
}
|
||||
|
||||
private static boolean isRfc5987AttrChar(int value) {
|
||||
return value >= 'A' && value <= 'Z'
|
||||
|| value >= 'a' && value <= 'z'
|
||||
|| value >= '0' && value <= '9'
|
||||
|| value == '!'
|
||||
|| value == '#'
|
||||
|| value == '$'
|
||||
|| value == '&'
|
||||
|| value == '+'
|
||||
|| value == '-'
|
||||
|| value == '.'
|
||||
|| value == '^'
|
||||
|| value == '_'
|
||||
|| value == '`'
|
||||
|| value == '|'
|
||||
|| value == '~';
|
||||
private static void writeUtf8(ByteArrayOutputStream out, String value) throws IOException {
|
||||
// Multipart delimiters remain byte-identical because UTF-8 is ASCII-compatible, while
|
||||
// user-visible field names and filenames retain non-ASCII characters.
|
||||
out.write(value.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static String headerValue(String value) {
|
||||
|
||||
Reference in New Issue
Block a user