public void ProcessRequest(HttpContext aspNetContext) { Connection con = new Connection(aspNetContext.Response.OutputStream, aspNetContext.Request.UserHostAddress, 0); // Construct a copy of the HttpRequest.Headers collection, because the one created by ASP.NET // is automagically modified whenever the response collection created by ASP.NET is modified. // There is no good way to mimic this behavior when running in our own web server, so to // maintain compatibility with our own server we use a copy of the ASP.NET request headers // so they won't be modified when we add to the response headers. NameValueCollection requestHeaders = new NameValueCollection(); NameValueCollection aspNetRequestHeaders = aspNetContext.Request.Headers; foreach (string headerName in aspNetRequestHeaders) { foreach (string value in aspNetRequestHeaders.GetValues(headerName)) { requestHeaders.Add(headerName, value); } } WebRequest request = new WebRequest(aspNetContext.Request.RequestType, requestHeaders, aspNetContext.Request.RawUrl, aspNetContext.Request.Path, aspNetContext.Request.QueryString, con.RemoteAddress); for (int fileIndex = 0; fileIndex < aspNetContext.Request.Files.Count; fileIndex++) { string fieldName = aspNetContext.Request.Files.AllKeys[fileIndex]; HttpPostedFile aspNetFile = aspNetContext.Request.Files[fileIndex]; WebUploadedFile file = new WebUploadedFile(aspNetFile.FileName, aspNetFile.InputStream, aspNetFile.ContentLength, aspNetFile.ContentType); request.UploadedFiles.Add(fieldName, file); } WebResponse response = new WebResponse(con, aspNetContext.Response.Headers, aspNetContext.Response.Cookies, false); TSiteData siteData = (TSiteData)aspNetContext.Application[ AspNetHttpModule <TSiteData, TSession> .SiteDataKey]; WebSessionContainer <TSession> sessionContainer = siteData.GetSessionContainer(request, response); WebContext <TSiteData, TSession> webContext = new WebContext <TSiteData, TSession>( request, response, new ServerUtilities(new NullDiagOutput()), new SiteUtilities(aspNetContext.Server), siteData, sessionContainer.Session); WebPage <TSiteData, TSession> page = PageFactory.GetInstance(webContext); page.Process(webContext); }
private static void ReadMultiPartFormData(Connection con, WebRequest request, string contentType, Stream bodyStream) { request.PostVars = new NameValueCollection(); int boundaryOffset = contentType.IndexOf("boundary="); if (boundaryOffset < 0) { return; } string boundary = contentType.Substring(boundaryOffset + 9); LookAheadStream lookAheadStream = new LookAheadStream(bodyStream, 200); // Read and discard the preamble. MultiPartMIMEStream multiPartStream = new MultiPartMIMEStream(lookAheadStream, "--" + boundary); for (; ;) { byte[] discardBuffer = new byte[100]; if (multiPartStream.Read(discardBuffer, 0, discardBuffer.Length) == 0) { break; } } multiPartStream.SkipBoundary(); // Read and save any following parts. for (; ;) { if (multiPartStream.LastPartRead || multiPartStream.TotalBytesRead == 0) { break; } multiPartStream = new MultiPartMIMEStream(lookAheadStream, "\r\n--" + boundary); bool connectionBroken; NameValueCollection headers = ReadAllHeaders(multiPartStream, out connectionBroken); if (connectionBroken) { con.ConnectionBroken = true; } using (MemoryStream memStream = new MemoryStream()) { using (BufferedStream partBody = new BufferedStream(memStream, 1024)) { multiPartStream.CopyTo(partBody, 1024); multiPartStream.SkipBoundary(); partBody.Flush(); partBody.Seek(0, SeekOrigin.Begin); string contentDisposition = headers.Get(HttpHeaders.ContentDisposition); if (contentDisposition != null) { // save uploaded files in data structures compatible with the way ASP.NET exposes them string[] parts = contentDisposition.Split(';'); bool formDataFound = false; string name = null; string filename = null; foreach (string part in parts) { string trimmedPart = part.Trim(); if (trimmedPart == "form-data") { formDataFound = true; } else if (trimmedPart.StartsWith("name=\"")) { name = trimmedPart.Substring(6, trimmedPart.Length - 7); } else if (trimmedPart.StartsWith("filename=\"")) { filename = trimmedPart.Substring(10, trimmedPart.Length - 11); } } if (!formDataFound) { throw new InvalidDataException(); } if (filename != null) { AddNameValuePair(request.PostVars, name, filename); partBody.Seek(0, SeekOrigin.Begin); string fileContentType = headers.Get(HttpHeaders.ContentType); if (string.IsNullOrEmpty(fileContentType)) { fileContentType = HttpHeaders.ContentType_TextPlain; } WebUploadedFile file = new WebUploadedFile(filename, partBody, partBody.Length, fileContentType); request.UploadedFiles.Add(name, file); } else { string value; bool ignoreConnectionBroken; Connection.ReadAsciiUntilEnd(partBody, out value, out ignoreConnectionBroken); AddNameValuePair(request.PostVars, name, value); } } } } } // Discard the epilogue for (; ;) { byte[] discardBuffer = new byte[100]; if (lookAheadStream.Read(discardBuffer, 0, discardBuffer.Length) == 0) { break; } } }