-
-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Character encoding problem in Content-Disposition section in HTTP POST body (multipart/form-data requests) #95
Comments
Yeah, cool. This is likely an easy fix. Will be even easier if you could add a failing test, and I will debug it. |
OK, I will add the test. |
Here is pull request for the failing test: #96 |
Thanks: I should have time to take a look this weekend! |
@saliksaly, hey, thanks for the test! However, after lots of debugging, it looks like the proxy server is behaving properly. I think this has something to do with the postman endpoint you are using. I have verified that the More importantly, I have swapped out one line in your test. Instead of proxying the request through the proxy, I changed it to go straight to postman: [Fact]
public async Task CanProxyControllerPostWithFormAndFilesRequest()
{
var content = new MultipartFormDataContent();
content.Add(new StringContent("123"), "xyz");
content.Add(new StringContent("456"), "xyz");
content.Add(new StringContent("321"), "abc");
const string fileName = "Test こんにちは file.txt";
const string fileString = "This is a test file こんにちは with non-ascii content.";
var fileContent = new StreamContent(new System.IO.MemoryStream(Encoding.UTF8.GetBytes(fileString)));
content.Add(fileContent, "testFile", fileName);
//var response = await _client.PostAsync("api/multipart", content);
/// //////////////////////////////////////////
var response = await new HttpClient().PostAsync("https://postman-echo.com/post", content);
/// ///////////////////////
/// response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var json = JObject.Parse(responseString);
var form = Assert.IsAssignableFrom<JObject>(json["form"]);
Assert.Equal(2, form.Count);
var xyz = Assert.IsAssignableFrom<JArray>(form["xyz"]);
Assert.Equal(2, xyz.Count);
Assert.Equal("123", xyz[0]);
Assert.Equal("456", xyz[1]);
Assert.Equal("321", form["abc"]);
var files = Assert.IsAssignableFrom<JObject>(json["files"]);
Assert.Single(files);
var file = files.ToObject<Dictionary<string, string>>().Single();
Assert.Equal($"data:application/octet-stream;base64,{Convert.ToBase64String(Encoding.UTF8.GetBytes(fileString))}", file.Value);
Assert.Equal(fileName, file.Key);
} It fails in the exact same way, so it appears postman is giving back the wrong file name. |
Yes, it seems there is no problem in this test case. I tested the post request and filename is in correct format:
When the request is built so using MultipartFormDataContent object, the request is ok. But, there is still a problem, when forwarding file request. I will create some repo to show the problem. I am unable to show it in the tests... |
Here is simple ASPNET Core application which makes multipart/form-data http post request with file content to postman-echo.com/post to examine the request. There are three html forms, each sends file in different way:
Try to uload file with non-ascii characters in name (Test こんにちは file.txt for example), you will see broken characters in the filename sent through AspNetCore.Proxy: Side-noteDirect request from browser differs from request made in automaic tests (using MultipartFormDataContent class).
|
Thanks! |
Where are you capturing those files that you diffed? |
@saliksaly, thoughts? |
Hi, For comparison of behaviour, in all three cases the requests are sent to https://postman-echo.com/post (defined in https://github.com/saliksaly/proxy_http_post_multipard_bad_encoding_test/blob/master/Constants.cs). You cannot repro? Ty to upload the same file with unicode characters in name in all three forms and you will see the difference... |
I think the problem is with |
No no, it is not the postman's problem. Here are the differences:
Could you try it on your own, please, to see the difference? |
Finally figured it out. |
New release is here. |
It already works! :-) |
Hi,
I have encountered interesting problem with proxying http post requests - when uploading files.
Content-Type: multipart/form-data is used and form values are each in it's own Content-Disposition section (the format is described here: https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2).
In Content-Disposition header, characters in the filename value are malformed - different than in the original request.
Example from testing:
from the original request:
from the proxied request:
There is problem when using non-ascii characters in file name. Something with character encoding...
Before, I used ProxyKit (https://github.com/proxykit/ProxyKit). It's similar library - there was not this problem. It is not supported so I switched to AspNetCore.Proxy. It works without problems in my project (all traffic from one "light" application goes to other one, instead of some local endpoints).
Could you please throw an eye on this issue? :)
Thanks
The text was updated successfully, but these errors were encountered: