Skip to content
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

Unable to access twice to request.InputStream #33

Closed
bufferUnderrun opened this issue Apr 12, 2016 · 4 comments
Closed

Unable to access twice to request.InputStream #33

bufferUnderrun opened this issue Apr 12, 2016 · 4 comments
Assignees
Milestone

Comments

@bufferUnderrun
Copy link
Contributor

How to reproduce (in the ResApiSample.cs) :

            [WebApiHandler(HttpVerbs.Post, RelativePath + "people/*")]
            public async Task<bool> PostPeople(WebServer server, HttpListenerContext context)
            {
                    ...
                    var post = context.RequestFormData(); // OK
                    var model = context.ParseJson<GridDataRequest>(); // will be null
                    ...
            }

OR

            [WebApiHandler(HttpVerbs.Post, RelativePath + "people/*")]
            public async Task<bool> PostPeople(WebServer server, HttpListenerContext context)
            {
                    ...
                    var model = context.ParseJson<GridDataRequest>(); // OK
                    var post = context.RequestFormData(); // will be null
                    ...
            }

The problem come from the underlying inputStream : when StreamReader is disposed, it lets the cursor position to the end. So the next call to StreamReader(inputStream) will be null.

         using (var body = request.InputStream) {
                using (var reader = new StreamReader(body, request.ContentEncoding)) {
                    var stringData = reader.ReadToEnd();

                    if (string.IsNullOrWhiteSpace(stringData)) return null;
                    return stringData.Split('&')
                        .ToDictionary(c => c.Split('=')[0],
                            c => WebUtility.UrlDecode(c.Split('=')[1]));
                }
            }

I've tried to use Read(pos, len) or force body.Position = 0 but this stream cannot be seek or written. Same problem using a MemoryStream and CopyTo... Do you have an idea to fix it ?

@geoperez geoperez self-assigned this Apr 12, 2016
@geoperez
Copy link
Member

Let me take a look in your sample.

@mariodivece
Copy link
Member

This is most likely the underlying design of the http.sys implementation as the backing http stream is not rewindable for obvious reasons but thanks for checking @geoperez

@bufferUnderrun
Copy link
Contributor Author

@mariodivece agree. Do you plan to cache context.Request.InputStream in order multiple reads works or keep the actual implementation ?

For instance, i've made a temporary workaround, so i can :

            [WebApiHandler(HttpVerbs.Post, RelativePath + "people/*")]
            public async Task<bool> PostPeople(WebServer server, HttpListenerContext context)
            {
                    ...
                    var body = context.RequestBody(); // just one call to inputStream
                    var model = context.ParseJson<GridDataRequest>(body); // ok
                    var post = body.FormData(); // ok
                    ...
            }

A bit verbose but solve my problem...

mariodivece added a commit that referenced this issue Apr 12, 2016
@mariodivece
Copy link
Member

I have added the overloads. Hope these help. We will be releasing a new NuGet package if no more issues arise by the EOD.

@mariodivece mariodivece added this to the 1.0.17 milestone Apr 12, 2016
@mariodivece mariodivece assigned mariodivece and unassigned geoperez Apr 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants