Embedio: GetRequestBodyAsString() relies on HttpListenerRequest.ContentEncoding.Default

Created on 8 Apr 2020  路  2Comments  路  Source: unosquare/embedio

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

http bug v3.x v4.x wontfix

Most helpful comment

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.

All 2 comments

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.

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dewyer picture Dewyer  路  7Comments

madnik7 picture madnik7  路  5Comments

geoperez picture geoperez  路  8Comments

rocketraman picture rocketraman  路  7Comments

Splamy picture Splamy  路  8Comments