Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Unable to read big json file from Request.Body #234

Closed
Tratcher opened this issue Sep 30, 2015 · 15 comments
Closed

Unable to read big json file from Request.Body #234

Tratcher opened this issue Sep 30, 2015 · 15 comments
Assignees
Milestone

Comments

@Tratcher
Copy link
Member

From @anfomin on September 30, 2015 15:1

I'm trying to read big JSON file (UTF8) from Request.Body stream:

string content;
using (var sr = new StreamReader(Request.Body, Encoding.Default))
    content = sr.ReadToEnd();

At the beginning it's ok, but content variable contains invalid character sequence after some offset.

Sample repo: https://github.com/anfomin/DNX-MVC-bigfile. There is file sample.json. Just run dnx kestrel and make:

POST http://localhost:5000

with body = sample.json file.

You will find mismatches since line 84.

Source:
2015-09-30 19 58 00

Result:
2015-09-30 19 58 18

I've tried latest DNX 1.0.0-beta8-15736 from https://www.myget.org/F/aspnetvnext/api/v2 and 1.0.0-rc1-15775 from https://www.myget.org/F/aspnetcidev/api/v2.

Copied from original issue: aspnet/Mvc#3235

@Tratcher
Copy link
Member Author

From @Eilon on September 30, 2015 18:49

@kichalla can you take a look?

@Tratcher
Copy link
Member Author

From @kichalla on September 30, 2015 19:51

@anfomin I suppose you are running on Mono on Linux? was curious if Encoding.Default was doing something here but looks like some content is entirely missing based on your snapshot above...just to isolate the issue, can you try saving the read content to a file and see if the contents are any different? also does this issue repro consistently for you?

@Tratcher
Copy link
Member Author

From @anfomin on September 30, 2015 20:38

@kichalla I tried this sample both on Windows and Mac, result is the same.

Also I tried to save Request.Body to a text file via File.CreateText("..."). Result file contains the same corruptions. You can remove Encoding.Default, the result does not change.

The problem is Request.Body because when I change input stream to File.Open(...) then output is correct.

@Tratcher
Copy link
Member Author

From @kichalla on September 30, 2015 21:2

@anfomin you are right, I was able to repro it on a Windows machine now..investigating...

left -> actual file content
right -> content read from request body and saved to a file
image

@Tratcher
Copy link
Member Author

From @Eilon on September 30, 2015 21:5

Weird... putting this on RC. Please enlist @halter73 @lodejard @Tratcher @moozzyk if needed because this might even be a Kestrel issue.

@Tratcher
Copy link
Member Author

From @kichalla on September 30, 2015 21:13

Yeah, this seems like a Kestrel issue as I cannot repro it with weblistener

@lodejard
Copy link
Contributor

I'll take a look also

@kichalla
Copy link
Member

kichalla commented Oct 1, 2015

@loudej Probably you are the best person to take a look at it...I tried to figure it out but I got lost 😄 ..following are some details:

  • This issue seems to repro only for specific request sizes and characters...for example, the below repro works fine if the character is ASCII...so my wild guess is that it has something to do with decoder and its flushing...

  • You can use the following repro code
    I used the LargeResponseApp in Kestrel repo for this

    app.Run(async (context) =>
            {
                var reader = new StreamReader(context.Request.Body);
                var content = await reader.ReadToEndAsync();
    
                using (var sw = new StreamWriter(File.Create(@"C:\Users\kichalla\Documents\files\actual.txt")))
                {
                    await sw.WriteAsync(content);
                }
            });

    Client app:

    public class Program
    {
        public void Main(string[] args)
        {
            var filePath = @"C:\users\kichalla\documents\files\expected.txt";
            using (var streamWriter = File.CreateText(filePath))
            {
                streamWriter.Write((new string('й', 5 * 1024 * 1024)));
            }
            //--------
            var httpClient = new HttpClient();
            var response = httpClient.PostAsync("http://localhost:5000", new StreamContent(File.OpenRead(filePath))).Result;
            Console.WriteLine(response.ToString());
            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
        }
    }

@lodejard
Copy link
Contributor

lodejard commented Oct 1, 2015

Perfect! Thanks @Tratcher, @kichalla those should repro very well in a unit test also

@MJomaa
Copy link

MJomaa commented Oct 2, 2015

I have the same issue with 1.0.0-beta8-15736. With beta8-15613 it worked fine.

Reproduce:

https://github.com/MJomaa/MalformingDataBug

This Repo just uploads a bunch of encoded HTML.
Malform

@muratg muratg added this to the 1.0.0-rc1 milestone Oct 2, 2015
@muratg
Copy link
Contributor

muratg commented Oct 2, 2015

@MJomaa thanks for the info when it started to fail. This should give @lodejard some pointers in investigation.

@Tratcher
Copy link
Member Author

Tratcher commented Oct 5, 2015

halter73 added a commit that referenced this issue Oct 8, 2015
WIP Still needs tests
@halter73 halter73 modified the milestones: 1.0.0-beta8, 1.0.0-rc1 Oct 8, 2015
@halter73 halter73 assigned halter73 and unassigned lodejard Oct 8, 2015
halter73 added a commit that referenced this issue Oct 8, 2015
- This fix prevents large request streams from being corrupted

#234
halter73 added a commit that referenced this issue Oct 8, 2015
- This fix prevents large request streams from being corrupted

#234
halter73 added a commit that referenced this issue Oct 8, 2015
- This fix prevents large request streams from being corrupted

#234
@halter73 halter73 removed the 1 - Ready label Oct 8, 2015
halter73 added a commit that referenced this issue Oct 8, 2015
- This fix prevents large request streams from being corrupted

#234
@halter73 halter73 closed this as completed Oct 8, 2015
@edurdias
Copy link

edurdias commented Nov 9, 2015

hi @halter73, thanks for working on this issue.

Would you be able to confirm which is the package version that contains the fix?

The last version I was able to use was 1.0.0-beta8-15674. If I try to use 1.0.0-beta8-15697 or 1.0.0-beta8-15702 it doesn't seems to work anymore.

@halter73
Copy link
Member

@edurdias This should be fixed in the final beat8 package and all the rc1 prerelease packages.

It will probably take a little while digging through the CI logs to find the specific build number the fix was introduced in. Do you really need that?

@edurdias
Copy link

We were using older build version in some of our microservices but I did an experiment and we should fine upgrading them to final beta8 version.

Thanks for the quick feedback.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants