Exception thrown when reading Base64 encoded RequestStream.

Open
#58 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Start with the RequestStream class and reproduce the JavaScript FileReader upload, then read the request input through StreamReader.ReadToEnd to observe the byteCount exception. Compare the implementation with the linked Mono RequestStream.cs version, accounting for the changed property names. Done means the Base64-encoded request stream can be read without the exception.

Written by the indexing model from the issue text.

Description

Use javascript to upload an image using a FileReader. For example:

        // Closure to capture the file information.
        reader.onload = function (e) {

            // Split by a comma to remove the url: prefix
            var data = e.target.result.split(',')[1];

            var url = self.getUrl("Users/" + userId + "/Images/" + imageType);

            self.ajax({
                type: "POST",
                url: url,
                data: data,
                contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
            });
        };

        // Read in the image file as a data URL.
        reader.readAsDataURL(file);

Then on the server, attempt to read the input stream using StreamReader.ReadToEnd. The following exception will be thrown:

Non-negative number required.
Parameter name: byteCount
System.ArgumentOutOfRangeException
   at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Boolean flush)
   at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)
   at System.IO.StreamReader.<ReadBufferAsync>d__28.MoveNext()

FYI - this can be fixed by replacing the entire contents of the RequestStream class with the version from mono (and adjusting property names that have changed):

https://github.com/mono/mono/blob/a31c107f59298053e4ff17fd09b2fa617b75c1ba/mcs/class/System/System.Net/RequestStream.cs

At the moment i do not have the time to hunt down the exact issue and submit a pull request, but i thought this information might be useful to you.

Dominant language
C#
Stars
6.1k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sta/websocket-sharp

All issues in sta/websocket-sharp

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.