views:

160

answers:

1

I have a very simple class using HttpListener to receive an HTTP POST from a browser via an AJAX request. I've included a demo that highlights this issue:

private static void HandleRequest(HttpListenerContext context)
{
    using (var inputStream = context.Request.InputStream)
    {
        for (int i = 0; i < context.Request.ContentLength64; i++)
        {
            Console.WriteLine("{0}:\t{1}", i, inputStream.ReadByte());
        }
    }
 }

I also use the following Javascript in Firefox (using jQuery):

upload_html: function(html){
   jQuery.ajax({
            type: 'POST',
            data: html,
            url: 'http://localhost:8080/api/html?url=' + escape(content.document.location),
            success: function(data) {
                 // do stuff
            }
   });
}

The problem I'm having is that InputStream only ever contains around 900 bytes when sent from Firefox. If I place Fiddler between Firefox and my app, it correctly sends all 9,900 bytes. But when sending directly from Firefox to my app, only the first 900 chars (encoded as UTF8, so 900 bytes) are sent.

If I use curl to post data to my app, it works perfectly. Fiddler is also able to post data to my app and have it read correctly. Fiddler is able to read the data posted to it from Firefox, but my app can't.

With different sizes of html, different amounts are sent, but it's consistently the same size for the same pages. Regardless of size, it's always between 899 and 999 bytes sent. If a page is under 900 bytes, it works and correctly reads all the data from the InputStream.

One theory I had was that it might be sending 1024 bytes, but the http headers were consuming some of those bytes, but the http headers are considerably larger than ~120 bytes.

One thing I should mention is that this JS is running from within a Firefox extension with jquery imported. I don't think that's causing the problem though, as I run into this same issue when running the same Javascript from within the page.

A: 

ReadToEnd works for me

_RequestBody  = new StreamReader(_Context.Request.InputStream).ReadToEnd();

and so does this for binary content

var memoryStream = new MemoryStream();
var buffer = new byte[0xFFFF];
Stream stream = _Context.Request.InputStream;

int i = 0;
do {
  i = stream.Read(buffer, 0, 0xFFFF);
  memoryStream.Write(buffer, 0, i);
} while (i > 0);
memoryStream.Flush();
memoryStream.Position = 0;
return memoryStream;
Darrel Miller