[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-2395?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18021191#comment-18021191
 ] 

Ahmed commented on HTTPCLIENT-2395:
-----------------------------------

Hi [~abernal] ,

sorry for late response. I pulled code with fix and it fixed the issue. Just 
for the reference, I will paste you my IT test bellow when test was failing on 
version 5.5.

Thank you
{code:java}
private val usAsciiFilename = "ติมเงินผิดเบอร์mPayเ.xlsx"
private val rfc2047EncodedFilename = "文字.pdf"
private val rfc2231EncodedFilename = "文件.txt"
private val incorrectlyEncodedFilename = "?UTF-8?B?5YWz5Lq655qE5YyW.pdf?="

@Test
fun `verify MIME`() {
    val mime = MimeMessage(
        getLocalSession(),
        
ByteArrayInputStream(File("src/test/resources/mime-encoded.eml").readBytes())
    )

    sendMessage(mime)

    val multiPartRequest = httpRequestCaptor.firstValue.entity.let {
        ByteArrayOutputStream().apply {
            it.writeTo(this)
        }.toString(StandardCharsets.UTF_8)
    }

    
assertThat(multiPartRequest).containsAnyOf(URLEncoder.encode(usAsciiFilename, 
StandardCharsets.UTF_8))
    
assertThat(multiPartRequest).containsAnyOf(URLEncoder.encode(rfc2047EncodedFilename,
 StandardCharsets.UTF_8))
    
assertThat(multiPartRequest).containsAnyOf(URLEncoder.encode(rfc2231EncodedFilename,
 StandardCharsets.UTF_8))
    
assertThat(multiPartRequest).containsAnyOf(URLEncoder.encode(incorrectlyEncodedFilename,
 StandardCharsets.UTF_8))
    // Apache HTTP client 5.5 adds a filename* param in body parts which is not 
well-supported in that version,
    // if the version is uplifted, this should fail
    assertThat(multiPartRequest).doesNotContain(
        
"filename*=\"UTF-8''UTF-8%27%27%25E0%25B8%2595%25E0%25B8%25B4%25E0%25B8%25A1%25E0%25B9%2580%25E0%25B8%2587%25E0%25B8%25B4%25E0%25B8%2599%25E0%25B8%259C%25E0%25B8%25B4%25E0%25B8%2594%25E0%25B9%2580%25E0%25B8%259A%25E0%25B8%25AD%25E0%25B8%25A3%25E0%25B9%258CmPay%25E0%25B9%2580.xlsx",
        "filename*=\"UTF-8''UTF-8%27%27%25E6%2596%2587%25E5%25AD%2597.pdf",
        "filename*=\"UTF-8''UTF-8%27%27%25E6%2596%2587%25E4%25BB%25B6.txt"
    )
} 


mime-encoded.eml
Content-Type: multipart/alternative;
 boundary="------------705ZF0wSwOSffEDi6dR6B0hC"
Message-ID: <[email protected]>
Date: Thu, 27 Jul 2025 10:03:31 +0200
From: [email protected]
To: [email protected]
Subject: Test

--------------705ZF0wSwOSffEDi6dR6B0hC
Content-Type: text/html

<p> HTML </p>
--------------705ZF0wSwOSffEDi6dR6B0hC
Content-Type: application/octet-stream; name="ติมเงินผิดเบอร์mPayเ.xlsx"
Content-Disposition: inline; filename="ติมเงินผิดเบอร์mPayเ.xlsx"
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
--------------705ZF0wSwOSffEDi6dR6B0hC
Content-Disposition: attachment
Content-Type: application/pdf; name="=?UTF-8?B?5paH5a2XLnBkZg==?="
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
--------------705ZF0wSwOSffEDi6dR6B0hC
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename*=UTF-8''%E6%96%87%E4%BB%B6.txt

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
--------------705ZF0wSwOSffEDi6dR6B0hC
Content-Disposition: attachment
Content-Type: application/pdf; name="?UTF-8?B?5YWz5Lq655qE5YyW.pdf?="
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
--------------705ZF0wSwOSffEDi6dR6B0hC--{code}

> Non-ASCII filename corrupted in HTTP request
> --------------------------------------------
>
>                 Key: HTTPCLIENT-2395
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2395
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpClient (classic)
>    Affects Versions: 5.5
>         Environment: Ubuntu 24.04
>            Reporter: Ahmed
>            Priority: Minor
>              Labels: bug
>             Fix For: 5.4.4
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> Hi team,
> I recently upgraded Apache HTTP Client to newest version (5.5) from 5.3.1 and 
> one of the tests in my client side service detected an issue. Issue is 
> presented while forming HTTP multipart request with attachments/inlines that 
> contains non-ascii characters in filename.
> Example:
>  
> {code:java}
> val attachment : Part? = mimeMessage.attachments.firstOrNull()
> val multipart = MultipartEntityBuilder.create()
> multipart.setMode(HttpMultipartMode.EXTENDED)
> multipart.addBinaryBody(
>   "attachments",
>   attachment?.openDataStream()?.use { it.readBytes()},
>   ContentType.parse(attachment?.contentType),
>   attachment?.name)
> .build() 
> val httpPost = HttpPost(url())
> httpPost.entity = multipart.build()
> httpClient.execute(httpPost) { it.handleResponse() }{code}
>  
> From given MIME message:
>  
> {code:java}
> Content-Type: multipart/alternative;
>  boundary="------------705ZF0wSwOSffEDi6dR6B0hC"
> Message-ID: <[email protected]>
> From: "🌪️ R@nd0M ユーザー" <[email protected]>
> To: "Tēst 🎯 Üser" <[email protected]>
> Subject: =?UTF-8?B?Rml4IG1l?=
> --------------705ZF0wSwOSffEDi6dR6B0hC
> Content-Type: text/html
> <p> HTML </p>
> --------------705ZF0wSwOSffEDi6dR6B0hC
> Content-Type: application/octet-stream; name="ติมเงินผิดเบอร์mPayเ.xlsx"
> Content-Disposition: inline; filename="ติมเงินผิดเบอร์mPayเ.xlsx"
> Content-Transfer-Encoding: base64
> iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==
> --------------705ZF0wSwOSffEDi6dR6B0hC--
> {code}
>  
> This generates HTTP request with following problematic URL encoded part:
>  
> {code:java}
> Content-Disposition: form-data; name="attachments"; 
> filename="%F0%9F%90%99_inline-%E5%9B%BE%E5%83%8F_%E6%96%87%E4%BB%B6.png"; 
> filename*="UTF-8''UTF-8%27%27%25F0%259F%2590%2599_inline-%25E5%259B%25BE%25E5%2583%258F_%25E6%2596%2587%25E4%25BB%25B6.png"Content-Type:
>  image/png  {code}
> filename* gets UTF-8 encoded two times resulting in filename with UTF-8'' 
> prefix where actual value should be:
>  
> {code:java}
> Content-Disposition: form-data; name="attachments"; 
> filename="%F0%9F%90%99_inline-%E5%9B%BE%E5%83%8F_%E6%96%87%E4%BB%B6.png"; 
> filename*="UTF-8''UTF-8%27%27%25F0%259F%2590%2599_inline-%25E5%259B%25BE%25E5%2583%258F_%25E6%2596%2587%25E4%25BB%25B6.png"Content-Type:
>  image/png  {code}
>  
> I suspect that problem lies 
> [here|https://github.com/apache/httpcomponents-client/blob/3eda5098f82c0d5cf1ceaa72afb1c24d9836ff56/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/HttpRFC7578Multipart.java#L104],
>  where additional UTF-8'' char is appended on filename along with original 
> appending while generating multipart itself 
> [here|https://github.com/apache/httpcomponents-client/blob/3eda5098f82c0d5cf1ceaa72afb1c24d9836ff56/httpclient5/src/main/java/org/apache/hc/client5/http/entity/mime/FormBodyPartBuilder.java#L164].
>  
> Problem can be avoided using LEGACY mode which doesn't look as ideal solution 
> to me as it doesn't support UTF-8 headers like in From or To MIME headers for 
> example.
> Related JIRA: https://issues.apache.org/jira/browse/HTTPCLIENT-2360



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to