unosquare / embedio

A tiny, cross-platform, module based web server for .NET
http://unosquare.github.io/embedio
Other
1.46k stars 176 forks source link

GetRequestBodyAsString() relies on HttpListenerRequest.ContentEncoding.Default #472

Closed ketodiet closed 4 years ago

ketodiet commented 4 years ago

Describe the bug I believe this is only an issue when using HttpListenerMode.Microsoft. In any case this is the config I have tested. Here's what happens:

GetRequestBodyAsString() reads from the input stream using the HttpListenerRequest.ContentEncoding encoding. In .Net FW this defaults to the Windows charset which is not UTF8. It should default to UTF8. As a side note, .Net Core defaults to UTF8.

To Reproduce Steps to reproduce the behaviour:

Start a server, send a UTF8 encoded request sing non-latin chars, GetRequestBodyAsString() returns garbage as it assumes request stream is non-UTF8

Expected behavior

We should be defaulting to UTF8 -and/or- allow the user to explicitly specify an encoding.

Screenshots

Desktop (please complete the following information):

Windows Server

Smartphone (please complete the following information):

Additional context See remarks section in the link below for more info: https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistenerrequest.contentencoding?view=netframework-4.8

rdeago commented 4 years ago

Hello @ketodiet, thanks for using EmbedIO!

GetRequestBodyAsStringAsync uses OpenRequestText, which in turn makes the wrong assumption that HttpListenerRequest.ContentEncoding will return null if no encoding is specified in request headers. This assumption, as you correctly pointed out, is wrong, as it will in fact return Encoding.Default, which on .NET Framework means the system's active code page.

(EmbedIO's own HttpListenerRequest doesn't ever return null either, but at least it defaults to UTF-8... after trying to read the encoding from Accept-Charset headers. 🤦‍♂ This deserves its own issue.)

To correct this bug, we could ~shamelessly copy~ take inspiration from .NET Core's implementation for both "flavors" of HTTP listener.

An encoding parameter can be added to OpenRequestText and consequently to GetRequestBodyAsStringAsync, but we need to clarify whether the given encoding should be used forcefully or as a default.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.