/// <summary> /// Create the page requested and send the context information to the page. /// </summary> /// <param name="baseNameSpace">The base namespace of the code behind resource.</param> /// <param name="assemblyName">The assembly name,from where the namespace belongs.</param> /// <param name="urlFilePath">The path and file name of the resource.</param> /// <param name="context">Provides access to the request and response objects.</param> /// <returns>True if the page code behind is found; else false;</returns> public static bool PageInstance(string baseNameSpace, string assemblyName, string urlFilePath, IHttpContext context) { // Page base. HttpPageBase page = null; bool foundPage = true; try { // Get the name of the file without extension. string fileNameWithoutEx = System.IO.Path.GetFileNameWithoutExtension(urlFilePath); try { // Attempt to create the instnce of the page. Type pageType = Type.GetType(baseNameSpace.TrimEnd('.') + "." + fileNameWithoutEx + ", " + assemblyName, true, true); page = (HttpPageBase)Activator.CreateInstance(pageType); } catch (Exception) { // If error foundPage = false; page = null; } // The page hierachy initialiser. HttpPageBase.PageInitialiser(page, urlFilePath, context); return(foundPage); } catch (Exception) { throw; } }
/// <summary> /// The page hierachy initialiser. /// </summary> /// <param name="page">The current page instance.</param> /// <param name="urlFilePath">The local file name and full path of the resource.</param> /// <param name="context">Provides access to the request and response objects.</param> public static void PageInitialiser(HttpPageBase page, string urlFilePath, IHttpContext context) { // Make sure that the page exists. if (page != null) { System.IO.Stream outputResponse = null; System.IO.Stream inputRequest = null; try { // Create a new page context. HttpPageContext pageContext = new HttpPageContext(); pageContext.IsValid = true; pageContext.OverrideRequestResponse = false; pageContext.ProcessOnPostBack = true; // Assign the initial values. page.Request = context.HttpContext.Request; page.Response = context.HttpContext.Response; page.IsPostBack = context.ActiveProcess.IsPostBack; page.UrlFilePath = urlFilePath; page.User = context.HttpContext.User; // Execute the page hierachy. page.OnInit(pageContext); // If true then continue if (pageContext.IsValid) { // Assign the current active processing. page.ActiveProcessing = context.ActiveProcess; // Execute the pre-processing event. page.OnPreProcess(pageContext); if (pageContext.ProcessOnPostBack) { // If post back. if (context.ActiveProcess.IsPostBack) { // Get all the post back data. context.ActiveProcess.ProcessPostBack(page.Request, page.UploadDirectory); page.Form = context.ActiveProcess.Form; page.UploadFiles = context.ActiveProcess.UploadFiles; } } // Execute the page load event. page.OnLoad(pageContext); if (!pageContext.OverrideRequestResponse) { // If alternative content is to be sent. if (page.AlternativeContent != null) { // Get the request, response stream. outputResponse = context.HttpContext.Response.OutputStream; // Transfer the data to the client. inputRequest = HttpResponseContent.CreateResponseStream(page.AlternativeContent); HttpResponseContent.TransferResponse(inputRequest, outputResponse); } else { // Get the request, response stream. outputResponse = context.HttpContext.Response.OutputStream; // Get the response html. Create the response. Transfer the data. byte[] responseResourceHtmlData = HttpResponseContent.ResourceFound(context.HttpContext.Response, context.ActiveProcess, urlFilePath, System.IO.Path.GetExtension(urlFilePath).TrimStart('.')); // Transfer the data to the client. inputRequest = HttpResponseContent.CreateResponseStream(responseResourceHtmlData); HttpResponseContent.TransferResponse(inputRequest, outputResponse); } } } } catch (Exception) { throw; } finally { try { if (inputRequest != null) { inputRequest.Close(); } } catch (Exception ex) { page.Exception = ex; } try { if (outputResponse != null) { outputResponse.Close(); } } catch (Exception ex) { page.Exception = ex; } } } }
/// <summary> /// Process the http server resource request. /// </summary> /// <param name="context">Provides access to the request and response objects.</param> public virtual void HttpServiceProcessRequest(IHttpContext context) { System.IO.Stream outputResponse = null; System.IO.Stream inputRequest = null; System.IO.MemoryStream memoryOutput = null; System.IO.MemoryStream memoryInput = null; HttpListenerRequest httpRequest = null; HttpListenerResponse httpResponse = null; try { // Get the upload file directory. string serviceDirectory = BaseServicePath.TrimEnd('\\') + "\\"; string uploadFilePath = null; bool foundInDownload = false; // Get the request and response context. httpRequest = context.HttpContext.Request; httpResponse = context.HttpContext.Response; // Get the current resposne stream. outputResponse = httpResponse.OutputStream; // Get the current request filename and directory information. string absolutePath = HttpUtility.UrlDecode(httpRequest.Url.AbsolutePath.TrimStart('/').Replace("/", "\\")); string urlFilePath = serviceDirectory + absolutePath; bool fileExists = System.IO.File.Exists(urlFilePath); // Look in the base download path for the file. if (!fileExists) { // If an upload path has been supplied. if (!string.IsNullOrEmpty(BaseUploadPath)) { // Get the upload directory. string uploadDirectory = BaseUploadPath.TrimEnd('\\') + "\\"; string[] directories = Path.GetDirectoryName(absolutePath).Split(new char[] { '\\' }); // Get the directory query string. string directory = ""; bool foundStart = false; foreach (string item in directories) { // Find the starting virtual directory name associated // with the current service name. if (item.ToLower() == _serviceVirtualName.ToLower()) { foundStart = true; } // Build the directory. if (foundStart) { directory += item + "\\"; } } // Get the download file path. uploadFilePath = uploadDirectory + (string.IsNullOrEmpty(directory) ? "" : directory.ToLower().Replace(_serviceVirtualName.ToLower() + "\\", "").TrimEnd('\\') + "\\") + System.IO.Path.GetFileName(urlFilePath); // Does the file exist. fileExists = System.IO.File.Exists(uploadFilePath); // If the file is found in the download path. if (fileExists) { foundInDownload = true; } } } // If the file does not exists then try to load // the default.htm file. if (!fileExists) { string newUrlFilePath = urlFilePath.TrimEnd('\\') + "\\"; string newFileName = System.IO.Path.GetFileName(newUrlFilePath); // Create the new default url file name. if (String.IsNullOrEmpty(newFileName)) { urlFilePath = newUrlFilePath + "default.htm"; fileExists = System.IO.File.Exists(urlFilePath); } } // Does the resource exits on the server. if (fileExists) { // Get the extension of the resource. string extension = System.IO.Path.GetExtension(urlFilePath).TrimStart(new char[] { '.' }); string fileName = System.IO.Path.GetFileName(urlFilePath); string[] extensions = context.ActiveProcess.AllowedExtensions(); // Get the maximum upload file size. int maxUploadFileSize = MaxBaseContentSize; // If the file is not too large. if (httpRequest.ContentLength64 >= maxUploadFileSize) { // Get the response html. Create the response. Transfer the data. byte[] responseMaxHtmlData = HttpResponseContent.MaxContentLength(httpResponse, maxUploadFileSize); HttpResponseContent.TransferResponse(HttpResponseContent.CreateResponseStream(responseMaxHtmlData), outputResponse); } else { // Extension is allowed and the resource is // not a download file. if (extensions.Count(u => u.Contains(extension)) > 0 && !foundInDownload) { // Execute the correct page code behind. bool ret = HttpPageBase.PageInstance(BaseNameSpace, AssemblyName, urlFilePath, context); // If the resource code behind does not exist // then load only the content data. if (!ret) { // If no code behind then load the resource. byte[] responseResourceHtmlData = HttpResponseContent.ResourceFound(httpResponse, context.ActiveProcess, urlFilePath, extension); HttpResponseContent.TransferResponse(HttpResponseContent.CreateResponseStream(responseResourceHtmlData), outputResponse); } } else { // Get the response html. Create the response. Transfer the data. byte[] responseFoundAttachmentHtmlData = HttpResponseContent.ResourceAttachment(httpResponse, uploadFilePath, extension, fileName); HttpResponseContent.TransferResponse(HttpResponseContent.CreateResponseStream(responseFoundAttachmentHtmlData), outputResponse); } } } else { // Get the response html. Create the response. Transfer the data. byte[] responseNotFoundHtmlData = HttpResponseContent.FileNotFound(httpResponse); HttpResponseContent.TransferResponse(HttpResponseContent.CreateResponseStream(responseNotFoundHtmlData), outputResponse); } } catch (Exception ex) { try { if (httpResponse != null) { // Get the response html. Create the response. Transfer the data. byte[] responseErrorHtmlData = HttpResponseContent.OnError(httpResponse, ex); memoryInput = HttpResponseContent.CreateResponseStream(responseErrorHtmlData); // If the response stream has already been activated. if (outputResponse == null) { // Transfer the data. outputResponse = httpResponse.OutputStream; HttpResponseContent.TransferResponse(memoryInput, outputResponse); } else { // Send the response. HttpResponseContent.TransferResponse(memoryInput, outputResponse); } } } catch { } throw; } finally { try { if (inputRequest != null) { inputRequest.Close(); } } catch { } try { if (outputResponse != null) { outputResponse.Close(); } } catch { } try { if (memoryOutput != null) { memoryOutput.Close(); } } catch { } try { if (memoryInput != null) { memoryInput.Close(); } } catch { } } }