示例#1
0
        /// <summary>
        /// Handles the ProcessRequest event of the server control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SharpDocPak.HttpRequestEventArgs"/> instance containing the event data.</param>
        void ServerProcessRequest(object sender, HttpRequestEventArgs e)
        {
            HttpListenerResponse response = null;
            bool keepAlive = false;
            try
            {
                Console.WriteLine("Url Requested: {0}", e.RequestContext.Request.Url);
                Console.Out.Flush();
                var url = e.RequestContext.Request.Url;
                response = e.RequestContext.Response;

                keepAlive = e.RequestContext.Request.KeepAlive;

                byte[] buffer = null;
                Content.Files.TryGetValue(url.AbsolutePath, out buffer);

                string path = url.AbsolutePath;

                //var lastTime = File.GetLastWriteTime(path);
                //var expireTime = DateTime.Now.AddDays(180);
                //Console.WriteLine("Expires: {0:r} Last-Modified: {1:r} KeepAlive: {2}", expireTime, lastTime, keepAlive);

                if (path.EndsWith(".js") || path.EndsWith(".css") || path.EndsWith(".htm"))
                {
                    if (path.EndsWith(".css"))
                    {
                        response.ContentType = "text/css";
                    }
                    else if (path.EndsWith(".js"))
                    {
                        response.ContentType = "text/javascript";
                    }
                    else if (path.EndsWith(".htm"))
                    {
                        response.ContentType = "text/html";
                    }

                    var textStream = new StreamReader(new MemoryStream(buffer), Encoding.UTF8);
                    response.ContentEncoding = textStream.CurrentEncoding;

                    // If URL query
                    if (path.EndsWith(".htm"))
                    {
                        var text = ApplySearchOptions(textStream.ReadToEnd());
                        if (IsQuery(url.Query))
                        {
                            text = RunSearchQuery(text, url.Query);
                        }
                        buffer = Encoding.UTF8.GetBytes(text);
                    }
                    response.ContentEncoding = Encoding.UTF8;
                }
                response.StatusCode = (int)HttpStatusCode.OK;
                response.StatusDescription = "OK";
                //response.AppendHeader("Expires", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:r}",expireTime));
                //response.AppendHeader("Last-Modified", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:r}", lastTime));
                response.KeepAlive = keepAlive;
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                if (!keepAlive)
                    response.OutputStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in server: {0}", ex);
            }

            try
            {
                if (!keepAlive)
                    response.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while closing: {0}", ex);
            }
        }
示例#2
0
        /// <summary>
        /// Handles the ProcessRequest event of the server control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="SharpDocPak.HttpRequestEventArgs"/> instance containing the event data.</param>
        void ServerProcessRequest(object sender, HttpRequestEventArgs e)
        {
            HttpListenerResponse response = null;
            bool keepAlive = false;

            try
            {
                Console.WriteLine("URL Requested: {0}", e.RequestContext.Request.Url);
                Console.Out.Flush();
                var url = e.RequestContext.Request.Url;
                response = e.RequestContext.Response;

                keepAlive = e.RequestContext.Request.KeepAlive;

                byte[] buffer = null;
                Content.Files.TryGetValue(url.AbsolutePath, out buffer);

                string path = url.AbsolutePath;

                //var lastTime = File.GetLastWriteTime(path);
                //var expireTime = DateTime.Now.AddDays(180);
                //Console.WriteLine("Expires: {0:r} Last-Modified: {1:r} KeepAlive: {2}", expireTime, lastTime, keepAlive);

                if (path.EndsWith(".js") || path.EndsWith(".css") || path.EndsWith(".htm"))
                {
                    if (path.EndsWith(".css"))
                    {
                        response.ContentType = "text/css";
                    }
                    else if (path.EndsWith(".js"))
                    {
                        response.ContentType = "text/javascript";
                    }
                    else if (path.EndsWith(".htm"))
                    {
                        response.ContentType = "text/html";
                    }

                    var textStream = new StreamReader(new MemoryStream(buffer), Encoding.UTF8);
                    response.ContentEncoding = textStream.CurrentEncoding;

                    // If URL query
                    if (path.EndsWith(".htm"))
                    {
                        var text = ApplySearchOptions(textStream.ReadToEnd());
                        if (IsQuery(url.Query))
                        {
                            text = RunSearchQuery(text, url.Query);
                        }
                        buffer = Encoding.UTF8.GetBytes(text);
                    }
                    response.ContentEncoding = Encoding.UTF8;
                }
                response.StatusCode        = (int)HttpStatusCode.OK;
                response.StatusDescription = "OK";
                //response.AppendHeader("Expires", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:r}",expireTime));
                //response.AppendHeader("Last-Modified", string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:r}", lastTime));
                response.KeepAlive       = keepAlive;
                response.ContentLength64 = buffer.Length;
                response.OutputStream.Write(buffer, 0, buffer.Length);
                if (!keepAlive)
                {
                    response.OutputStream.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception in server: {0}", ex);
            }

            try
            {
                if (!keepAlive)
                {
                    response.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception while closing: {0}", ex);
            }
        }