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

Self-hosted HttpListener gets empty file collection and input stream for multipart PUT or POST requests #6801

Closed
BenLubar opened this issue Feb 5, 2018 · 5 comments · Fixed by #6820

Comments

@BenLubar
Copy link
Contributor

BenLubar commented Feb 5, 2018

Steps to Reproduce

  1. Build MonoMultipartRepro in Visual Studio.
  2. Run MonoMultipartRepro.exe

Current Behavior

Program outputs:

files: 0

(on Mono)

Expected Behavior

Program outputs:

files: 1

(which it does on Microsoft's implementation of .NET)

On which platforms did you notice this

[ ] macOS
[x] Linux
[ ] Windows

(only tested on Linux Mono)

Version Used:

(Docker)

Mono JIT compiler version 5.4.1.6 (tarball Wed Nov  8 20:37:24 UTC 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug 
        LLVM:          supported, not enabled.
        GC:            sgen (concurrent by default)

Stacktrace

(no stack trace as there is no exception thrown)

@akoeplinger
Copy link
Member

@BenLubar can you reproduce this without System.Web as well, i.e. just a console app that uses an HttpListener?

@BenLubar
Copy link
Contributor Author

BenLubar commented Feb 6, 2018

using System;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace MonoMultipartRepro2
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var listener = new HttpListener())
            {
                listener.Prefixes.Add("http://127.0.0.1:8123/");
                listener.Start();

                var task = Task.Run(async () =>
                {
                    using (var client = new HttpClient())
                    using (var content = new MultipartFormDataContent())
                    {
                        content.Add(new StringContent("Hello, World!", Encoding.UTF8, "text/plain"), "foo", "foo.txt");

                        using (var response = await client.PostAsync("http://127.0.0.1:8123/", content))
                        {
                            return await response.Content.ReadAsStringAsync();
                        }
                    }
                });

                var context = listener.GetContext();
                context.Request.InputStream.CopyTo(context.Response.OutputStream);
                context.Response.OutputStream.Close();
                context.Response.Close();

                Console.WriteLine(task.Result);
            }
        }
    }
}

Output on Windows (Microsoft .NET 4.5.2 implementation):

--0579ebb2-17f2-40f5-9b97-258c0a82bd40
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=foo; filename=foo.txt; filename*=utf-8''foo.txt

Hello, World!
--0579ebb2-17f2-40f5-9b97-258c0a82bd40--

Output on Linux (Docker, mono version listed above):

--9106158a-072a-4012-b903-6d91a5919a6a
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=foo; filename=foo.txt; filename*=utf-8''foo.txt

Hello, World!
--9106158a-072a-4012-b903-6d91a5919a6a--

So this is a System.Web problem.

@akoeplinger
Copy link
Member

Ok good to know. Unfortunately we don't have a lot of resources to spend on System.Web so you might want to take a look at the sources yourself to figure out the problem.

@BenLubar
Copy link
Contributor Author

BenLubar commented Feb 6, 2018

The problem is here:

static string GetContentDispositionAttribute (string l, string name)
{
int idx = l.IndexOf (name + "=\"");
if (idx < 0)
return null;
int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf ('"', begin);
if (end < 0)
return null;
if (begin == end)
return "";
return l.Substring (begin, end - begin);
}
string GetContentDispositionAttributeWithEncoding (string l, string name)
{
int idx = l.IndexOf (name + "=\"");
if (idx < 0)
return null;
int begin = idx + name.Length + "=\"".Length;
int end = l.IndexOf ('"', begin);
if (end < 0)
return null;
if (begin == end)
return "";
string temp = l.Substring (begin, end - begin);
byte [] source = new byte [temp.Length];
for (int i = temp.Length - 1; i >= 0; i--)
source [i] = (byte) temp [i];
return encoding.GetString (source);
}

The code incorrectly assumes that Content-Disposition headers are double-quoted, so the header generated by System.Net.Http is considered invalid:

Content-Disposition: form-data; name=foo; filename=foo.txt; filename*=utf-8''foo.txt

The Microsoft reference source for the equivalent method is here: http://referencesource.microsoft.com/#System.Web/MultipartContentParser.cs,143

Removing the quote characters from the two "=\"" strings and replacing the int end = ... lines in the two functions above with this should work:

            int end;
            if (l.Length > begin && l[begin] == '"') {
                begin++;
                end = l.IndexOf ('"', begin);
            } else {
                end = l.IndexOf (';', begin);
                if (end == -1)
                    end = l.Length;
            }

@akoeplinger
Copy link
Member

awesome, would you mind sending a pull request?

@marek-safar marek-safar added this to the Community milestone Feb 6, 2018
jonpryor pushed a commit to dotnet/android that referenced this issue Aug 8, 2018
Fixes: #1130
Fixes: #1561 (comment)
Fixes: #1845
Fixes: #1951

Context: https://bugzilla.xamarin.com/show_bug.cgi?id=10087
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=11771
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=12850
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=18941
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=19436
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=25444
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=33208
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=58413
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59184
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59400
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59779
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=60065
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=60843
Context: mono/mono#6174
Context: mono/mono#6178
Context: mono/mono#6180
Context: mono/mono#6181
Context: mono/mono#6186
Context: mono/mono#6187
Context: mono/mono#6211
Context: mono/mono#6266
Context: mono/mono#6579
Context: mono/mono#6666
Context: mono/mono#6752
Context: mono/mono#6801
Context: mono/mono#6812
Context: mono/mono#6848
Context: mono/mono#6940
Context: mono/mono#6948
Context: mono/mono#6998
Context: mono/mono#6999
Context: mono/mono#7016
Context: mono/mono#7085
Context: mono/mono#7086
Context: mono/mono#7095
Context: mono/mono#7134
Context: mono/mono#7137
Context: mono/mono#7145
Context: mono/mono#7184
Context: mono/mono#7240
Context: mono/mono#7262
Context: mono/mono#7289
Context: mono/mono#7338
Context: mono/mono#7356
Context: mono/mono#7364
Context: mono/mono#7378
Context: mono/mono#7389
Context: mono/mono#7449
Context: mono/mono#7460
Context: mono/mono#7535
Context: mono/mono#7536
Context: mono/mono#7537
Context: mono/mono#7565
Context: mono/mono#7588
Context: mono/mono#7596
Context: mono/mono#7610
Context: mono/mono#7613
Context: mono/mono#7620
Context: mono/mono#7624
Context: mono/mono#7637
Context: mono/mono#7655
Context: mono/mono#7657
Context: mono/mono#7661
Context: mono/mono#7685
Context: mono/mono#7696
Context: mono/mono#7729
Context: mono/mono#7786
Context: mono/mono#7792
Context: mono/mono#7805
Context: mono/mono#7822
Context: mono/mono#7828
Context: mono/mono#7860
Context: mono/mono#7864
Context: mono/mono#7903
Context: mono/mono#7920
Context: mono/mono#8089
Context: mono/mono#8143
Context: mono/mono#8267
Context: mono/mono#8311
Context: mono/mono#8340
Context: mono/mono#8409
Context: mono/mono#8417
Context: mono/mono#8430
Context: mono/mono#8698
Context: mono/mono#8701
Context: mono/mono#8712
Context: mono/mono#8721
Context: mono/mono#8726
Context: mono/mono#8866
Context: mono/mono#9023
Context: mono/mono#9031
Context: mono/mono#9033
Context: mono/mono#9044
Context: mono/mono#9179
Context: mono/mono#9318
Context: mono/mono#9318
Context: xamarin/maccore#628
Context: xamarin/maccore#629
Context: xamarin/maccore#673
jonpryor pushed a commit to dotnet/android that referenced this issue Aug 13, 2018
Fixes: #1130
Fixes: #1561 (comment)
Fixes: #1845
Fixes: #1951

Context: https://bugzilla.xamarin.com/show_bug.cgi?id=10087
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=11771
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=12850
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=18941
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=19436
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=25444
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=33208
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=58413
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59184
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59400
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=59779
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=60065
Context: https://bugzilla.xamarin.com/show_bug.cgi?id=60843
Context: mono/mono#6174
Context: mono/mono#6178
Context: mono/mono#6180
Context: mono/mono#6181
Context: mono/mono#6186
Context: mono/mono#6187
Context: mono/mono#6211
Context: mono/mono#6266
Context: mono/mono#6579
Context: mono/mono#6666
Context: mono/mono#6752
Context: mono/mono#6801
Context: mono/mono#6812
Context: mono/mono#6848
Context: mono/mono#6940
Context: mono/mono#6948
Context: mono/mono#6998
Context: mono/mono#6999
Context: mono/mono#7016
Context: mono/mono#7085
Context: mono/mono#7086
Context: mono/mono#7095
Context: mono/mono#7134
Context: mono/mono#7137
Context: mono/mono#7145
Context: mono/mono#7184
Context: mono/mono#7240
Context: mono/mono#7262
Context: mono/mono#7289
Context: mono/mono#7338
Context: mono/mono#7356
Context: mono/mono#7364
Context: mono/mono#7378
Context: mono/mono#7389
Context: mono/mono#7449
Context: mono/mono#7460
Context: mono/mono#7535
Context: mono/mono#7536
Context: mono/mono#7537
Context: mono/mono#7565
Context: mono/mono#7588
Context: mono/mono#7596
Context: mono/mono#7610
Context: mono/mono#7613
Context: mono/mono#7620
Context: mono/mono#7624
Context: mono/mono#7637
Context: mono/mono#7655
Context: mono/mono#7657
Context: mono/mono#7661
Context: mono/mono#7685
Context: mono/mono#7696
Context: mono/mono#7729
Context: mono/mono#7786
Context: mono/mono#7792
Context: mono/mono#7805
Context: mono/mono#7822
Context: mono/mono#7828
Context: mono/mono#7860
Context: mono/mono#7864
Context: mono/mono#7903
Context: mono/mono#7920
Context: mono/mono#8089
Context: mono/mono#8143
Context: mono/mono#8267
Context: mono/mono#8311
Context: mono/mono#8340
Context: mono/mono#8409
Context: mono/mono#8417
Context: mono/mono#8430
Context: mono/mono#8698
Context: mono/mono#8701
Context: mono/mono#8712
Context: mono/mono#8721
Context: mono/mono#8726
Context: mono/mono#8866
Context: mono/mono#9023
Context: mono/mono#9031
Context: mono/mono#9033
Context: mono/mono#9044
Context: mono/mono#9179
Context: mono/mono#9318
Context: mono/mono#9318
Context: xamarin/maccore#628
Context: xamarin/maccore#629
Context: xamarin/maccore#673
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants