public void HandlePostRequest() { // this post data processing just reads everything into a memory stream. // this is fine for smallish things, but for large stuff we should really // hand an input stream to the request processor. However, the input stream // we hand him needs to let him see the "end of the stream" at this content // length, because otherwise he won't know when he's seen it all! int content_len = 0; MemoryStream ms = new MemoryStream(); if (HttpHeaders.ContainsKey("Content-Length")) { content_len = Convert.ToInt32(this.HttpHeaders["Content-Length"]); if (content_len > MAX_POST_SIZE) { throw new Exception( String.Format("POST Content-Length({0}) too big for this simple server", content_len)); } byte[] buf = new byte[BUF_SIZE]; int to_read = content_len; while (to_read > 0) { Console.WriteLine("starting Read, to_read={0}", to_read); int numread = this.InputStream.Read(buf, 0, Math.Min(BUF_SIZE, to_read)); Console.WriteLine("read finished, numread={0}", numread); if (numread == 0) { if (to_read == 0) { break; } else { throw new Exception("client disconnected during post"); } } to_read -= numread; ms.Write(buf, 0, numread); } ms.Seek(0, SeekOrigin.Begin); } Srv.HandlePostRequest(this, new StreamReader(ms)); }
public void HandlePostRequest() { HttpRequestEvent("Started: Get POST data"); int contentLength = 0; var memStream = new MemoryStream(); if (HttpHeaders.ContainsKey("Content-Length")) { contentLength = Convert.ToInt32(this.HttpHeaders["Content-Length"]); if (contentLength > MAX_POST_SIZE) { throw new Exception(String.Format("POST Content-Length({0}) to large for this server", contentLength)); } byte[] buf = new byte[BUF_SIZE]; int toRead = contentLength; while (toRead > 0) { HttpRequestEvent("starting Read, toRead=" + toRead); int numRead = this.m_InputStream.Read(buf, 0, Math.Min(BUF_SIZE, toRead)); HttpRequestEvent("read finished, numread=" + numRead); if (numRead == 0) { if (toRead == 0) { break; } else { throw new Exception("client disconnected during post"); } } toRead -= numRead; memStream.Write(buf, 0, numRead); } memStream.Seek(0, SeekOrigin.Begin); } HttpRequestEvent("Finished: Get POST data"); Srv.HandlePostRequest(this, new StreamReader(memStream)); }