Esempio n. 1
0
        //Will be called when an ASPX page is to be served.
        public void ProcessRequest(String page, string query, AspxRequestInfo dataHolder)
        {
            //catch the exception so we dont bring down the whole app

            try
            {
                if (string.IsNullOrEmpty(page))
                {
                    if (File.Exists(Path.Combine(m_PhysicalDirectory, "index.aspx")))
                    {
                        page = "index.aspx";
                    }
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "default.aspx")))
                    {
                        page = "default.aspx";
                    }
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "index.html")))
                    {
                        page = "index.html";
                    }
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "default.html")))
                    {
                        page = "default.html";
                    }
                }

                if (page != null && (page.ToLower().EndsWith("gif") || page.ToLower().EndsWith("jpg") || page.ToLower().EndsWith("png") || page.ToLower().EndsWith("html")) && File.Exists(Path.Combine(m_PhysicalDirectory, page)))
                {
                    var          _FileName     = Path.Combine(m_PhysicalDirectory, page);
                    FileStream   _FileStream   = new FileStream(_FileName, FileMode.Open, FileAccess.Read);
                    BinaryReader _BinaryReader = new BinaryReader(_FileStream);
                    long         _TotalBytes   = new FileInfo(_FileName).Length;
                    var          _Buffer       = _BinaryReader.ReadBytes((Int32)_TotalBytes);
                    _FileStream.Close();
                    _FileStream.Dispose();
                    _BinaryReader.Close();
                    dataHolder.ResponseStream.Write(_Buffer, 0, (int)_TotalBytes);
                }
                else
                {
                    AspxPage swr = new AspxPage(page, query, dataHolder);
                    HttpRuntime.ProcessRequest(swr);
                }
            }
            catch (Exception e1)
            {
                //Supress the internal exception. If needed we can pass the exception back
                dataHolder.ResponseStreamAsWriter.WriteLine("500 Internal Error.");
                throw new AspxException("Internal Error", e1);
            }
            finally
            {
                //Flush the response stream so that the Browser/calling application doesnt time out
                dataHolder.ResponseStreamAsWriter.Flush();
            }
        }
Esempio n. 2
0
        /**
         * This method will be called when a client/browser makes a request. We will pass on
         * required information to the hosting API from this method
         */
        private void ContextReceivedCallback(IAsyncResult asyncResult)
        {
            //Check if listener is listening other wise return
            if (!m_HttpListener.IsListening)
            {
                return;
            }

            //Get the context
            HttpListenerContext listenerContext = m_HttpListener.EndGetContext(asyncResult);

            //Lets get the Listener listening for the next request
            m_HttpListener.BeginGetContext(new AsyncCallback(ContextReceivedCallback), null);

            //If No AspNetEngine was configured, just respond with 404
            if (m_AspNetEngine == null)
            {
                listenerContext.Response.StatusCode = 404;
                listenerContext.Response.Close();
                return;
            }

            //Retrieve the URL requested
            String pageRequested = listenerContext.Request.Url.LocalPath;

            //Remove the "/alias"  from the begining page request as we just need to
            //pass the file and the query out to the Application Host
            pageRequested = AspxVirtualRoot.RemoveAliasFromRequest(pageRequested, m_AspNetEngine.VirtualAlias);

            //Prepare the DataHolder object that will be passed on for processing
            AspxRequestInfo dataHolder = AspxNetEngine.PrepareAspxRequestInfoObject(listenerContext);;

            //Look for Client Certificate if it has any
            if (listenerContext.Request.IsSecureConnection)
            {
                dataHolder.m_ClientCertificate = listenerContext.Request.GetClientCertificate();
                Console.WriteLine("Client certificate received.");
            }

            try
            {
                //Pass the request to the Hosted application
                m_AspNetEngine.ExecutingAppDomain.ProcessRequest(pageRequested, listenerContext.Request.Url.Query.Replace("?", ""), dataHolder);
            }
            catch (AspxException aspxException)
            {
                //Error occured.Log it and move on
                Console.WriteLine(aspxException);
            }

            Console.WriteLine(listenerContext.Request.Url.LocalPath + "...   " +
                              listenerContext.Response.StatusCode + " " + listenerContext.Response.StatusDescription);

            //Finally close the response or else the client will time out
            listenerContext.Response.Close();
        }
Esempio n. 3
0
        /**
         * HttpListenerContext is not Marshallable and so we have an Marshallable object
         * that will contain all required objects that we need to pass on to our
         * implementation of SimpleWorkerRequest so we can handle PUT/POST and
         * Client Certificate requests. If any other object from HttpListenerContext class
         * is required and that Object extends MarshallByRefObject add it to the data holder
         * object here
         */
        public static AspxRequestInfo PrepareAspxRequestInfoObject(HttpListenerContext context)
        {
            AspxRequestInfo dataHolder = new AspxRequestInfo();

            dataHolder.m_CookieCollection       = context.Request.Cookies;
            dataHolder.m_Headers                = context.Request.Headers;
            dataHolder.m_RequestStream          = context.Request.InputStream;
            dataHolder.m_ResponseStream         = context.Response.OutputStream;
            dataHolder.m_UserAgent              = context.Request.UserAgent;
            dataHolder.m_HttpMethod             = context.Request.HttpMethod;
            dataHolder.m_ResponseStreamAsWriter = new StreamWriter(context.Response.OutputStream);
            return(dataHolder);
        }
 public AspxPage(String page, String query, AspxRequestInfo dataHolder) : base(page,query,dataHolder.ResponseStreamAsWriter)
 {
     m_EngineDataHolder = dataHolder;
 }
 /**
  * HttpListenerContext is not Marshallable and so we have an Marshallable object
  * that will contain all required objects that we need to pass on to our 
  * implementation of SimpleWorkerRequest so we can handle PUT/POST and 
  * Client Certificate requests. If any other object from HttpListenerContext class
  * is required and that Object extends MarshallByRefObject add it to the data holder
  * object here
  */
 public static AspxRequestInfo PrepareAspxRequestInfoObject(HttpListenerContext context)
 {
     AspxRequestInfo dataHolder = new AspxRequestInfo();
     dataHolder.m_CookieCollection = context.Request.Cookies;
     dataHolder.m_Headers = context.Request.Headers;
     dataHolder.m_RequestStream = context.Request.InputStream;
     dataHolder.m_ResponseStream = context.Response.OutputStream;
     dataHolder.m_UserAgent = context.Request.UserAgent;
     dataHolder.m_HttpMethod = context.Request.HttpMethod;
     dataHolder.m_ResponseStreamAsWriter = new StreamWriter(context.Response.OutputStream);
     return dataHolder;
 }
        //Will be called when an ASPX page is to be served.
        public void ProcessRequest(String page, string query, AspxRequestInfo dataHolder)
        { 
            //catch the exception so we dont bring down the whole app

            try
            {
                if (string.IsNullOrEmpty(page))
                {
                    if (File.Exists(Path.Combine(m_PhysicalDirectory, "index.aspx")))
                        page = "index.aspx";
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "default.aspx")))
                        page = "default.aspx";
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "index.html")))
                        page = "index.html";
                    else if (File.Exists(Path.Combine(m_PhysicalDirectory, "default.html")))
                        page = "default.html";
                }

                if (page != null && (page.ToLower().EndsWith("gif") || page.ToLower().EndsWith("jpg") || page.ToLower().EndsWith("png") || page.ToLower().EndsWith("html")) && File.Exists(Path.Combine(m_PhysicalDirectory, page)))
                {
                    var _FileName = Path.Combine(m_PhysicalDirectory, page);
                    FileStream _FileStream = new FileStream(_FileName, FileMode.Open, FileAccess.Read);
                    BinaryReader _BinaryReader = new BinaryReader(_FileStream);
                    long _TotalBytes = new FileInfo(_FileName).Length;
                    var _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
                    _FileStream.Close();
                    _FileStream.Dispose();
                    _BinaryReader.Close();                    
                    dataHolder.ResponseStream.Write(_Buffer, 0, (int)_TotalBytes);
                }
                else
                {
                    AspxPage swr = new AspxPage(page, query, dataHolder);
                    HttpRuntime.ProcessRequest(swr);
                }
            }
            catch (Exception e1)
            {
                //Supress the internal exception. If needed we can pass the exception back
                dataHolder.ResponseStreamAsWriter.WriteLine("500 Internal Error.");
                throw new AspxException("Internal Error", e1);
            }
            finally
            {
                //Flush the response stream so that the Browser/calling application doesnt time out
                dataHolder.ResponseStreamAsWriter.Flush();
            }
        }
 public AspxPage(String page, String query, AspxRequestInfo dataHolder) : base(page, query, dataHolder.ResponseStreamAsWriter)
 {
     m_EngineDataHolder = dataHolder;
 }