public virtual void HandleRequest(OSHttpRequest request, OSHttpResponse response)
        {
            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", true);
                response.SendChunked = false;
                IRequestHandler requestHandler;

                string path       = request.RawUrl;
                string handlerKey = GetHandlerKey(request.HttpMethod, path);

                if (TryGetStreamHandler(handlerKey, out requestHandler))
                {
                    byte[] buffer = null;
                    response.ContentType = request.ContentType;
                    if (requestHandler is IGenericHTTPHandler)
                    {
                        IGenericHTTPHandler HttpRequestHandler = requestHandler as IGenericHTTPHandler;
                        Stream   requestStream = request.InputStream;
                        Encoding encoding      = Encoding.UTF8;
                        string   requestBody   = string.Empty;
                        using (StreamReader reader = new StreamReader(requestStream, encoding))
                        {
                            requestBody = reader.ReadToEnd();
                        }
                        Hashtable keysvals        = new Hashtable();
                        Hashtable headervals      = new Hashtable();
                        string[]  querystringkeys = request.QueryString.AllKeys;
                        string[]  rHeaders        = request.Headers.AllKeys;

                        foreach (string queryname in querystringkeys)
                        {
                            keysvals.Add(queryname, request.Query[queryname]);
                        }
                        foreach (string headername in rHeaders)
                        {
                            headervals.Add(headername, request.Headers[headername]);
                        }
                        keysvals.Add("requestbody", requestBody);
                        keysvals.Add("headers", headervals);
                        keysvals.Add("form", request.Form);
                        keysvals.Add("uri", request.RawUrl);
                        keysvals.Add("contenttype", request.ContentType);
                        if (request.ContentEncoding != null && !keysvals.ContainsKey("encoding"))
                        {
                            keysvals.Add("encoding", request.ContentEncoding);
                        }
                        keysvals.Add("accepttypes", request.AcceptTypes);
                        DoHTTPGruntWork(HttpRequestHandler.Handle(path, keysvals), response);
                        return;
                    }
                    request.InputStream.Close();
                    if (buffer == null)
                    {
                        return;
                    }

                    if (!response.SendChunked)
                    {
                        response.ContentLength64 = buffer.LongLength;
                    }

                    try
                    {
                        response.OutputStream.Write(buffer, 0, buffer.Length);
                    }
                    catch (HttpListenerException)
                    {
                        m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine);
                    }

                    try
                    {
                        response.Send();
                    }
                    catch (SocketException)
                    {
                        m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine);
                    }
                    catch (IOException)
                    {
                        m_Output.LogMessage("error", "[HTTPD]: HTTPRequest terminated abnormally " + Environment.NewLine);
                    }
                }
                switch (request.ContentType)
                {
                case null:
                case "text/html":
                    HandleHTTPRequest(request, response);
                    return;

                default:
                    if (DoWeHaveAHTTPHandler(request.RawUrl))
                    {
                        HandleHTTPRequest(request, response);
                        return;
                    }
                    return;
                }
            }
            catch (SocketException e)
            {
                m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine);
            }
            catch (IOException e)
            {
                m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine);
            }
            catch (InvalidOperationException e)
            {
                m_Output.LogMessage("warn", "[HTTPD]: HandleRequest threw " + e.Message + Environment.NewLine);
            }
            return;
        }
        private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
        {
            Stream requestStream = request.InputStream;

            Encoding encoding    = Encoding.UTF8;
            string   requestBody = string.Empty;
            string   host        = string.Empty;

            using (StreamReader reader = new StreamReader(requestStream, encoding))
            {
                requestBody = reader.ReadToEnd();
            }

            Hashtable keysvals    = new Hashtable();
            Hashtable headervals  = new Hashtable();
            Hashtable requestVars = new Hashtable();

            string[] querystringkeys = request.QueryString.AllKeys;
            string[] rHeaders        = request.Headers.AllKeys;

            foreach (string queryname in querystringkeys)
            {
                keysvals.Add(queryname, request.Query[queryname]);
            }
            foreach (string headername in rHeaders)
            {
                headervals.Add(headername, request.Headers[headername]);
            }

            keysvals.Add("body", requestBody);
            keysvals.Add("uri", request.RawUrl);
            keysvals.Add("content-type", request.ContentType);
            keysvals.Add("http-method", request.HttpMethod);

            if (request.ContentEncoding != null && !keysvals.ContainsKey("encoding"))
            {
                keysvals.Add("encoding", request.ContentEncoding);
            }

            keysvals.Add("accepttypes", request.AcceptTypes);

            foreach (string queryname in querystringkeys)
            {
                if (!keysvals.ContainsKey(queryname))
                {
                    keysvals.Add(queryname, request.QueryString[queryname]);
                }
                if (!requestVars.ContainsKey(queryname))
                {
                    requestVars.Add(queryname, keysvals[queryname]);
                }
            }

            foreach (string headername in rHeaders)
            {
                headervals[headername] = request.Headers[headername];
            }

            if (headervals.Contains("Host"))
            {
                host = (string)headervals["Host"];
            }
            keysvals.Add("headers", headervals);
            keysvals.Add("form", request.Form);
            foreach (HttpInputItem item in request.Form)
            {
                if (!keysvals.ContainsKey(item.Name))
                {
                    keysvals.Add(item.Name, item.Value);
                }
                //m_log.Output(string.Format("{0}={1}\r\n", item.Name, item.Value));
            }

            if (keysvals.Contains("method"))
            {
                string            method = (string)keysvals["method"];
                GenericHTTPMethod requestProcessor;
                bool foundHandler = TryGetHTTPHandler(method, out requestProcessor);
                if (foundHandler)
                {
                    Hashtable responsedata1 = requestProcessor(keysvals);
                    DoHTTPGruntWork(responsedata1, response);
                }
                else
                {
                    FileRequest.FileResponse fileoutput = null;
                    Exception exceptionout = null;
                    if (m_fileRequestHandler.TryGetFile(request, out fileoutput, out exceptionout))
                    {
                        if (exceptionout == null)
                        {
                            SendFile(request, response, fileoutput);
                        }
                        else
                        {
                            if (exceptionout is System.Security.SecurityException || exceptionout is UnauthorizedAccessException)
                            {
                                SendHTML403(response);
                            }
                            else if (exceptionout is FileNotFoundException || exceptionout is DirectoryNotFoundException || exceptionout is ArgumentException || exceptionout is ArgumentNullException)
                            {
                                SendHTML404(response, host);
                            }
                            else
                            {
                                SendHTML500(response);
                            }
                        }
                    }
                    else
                    {
                        SendHTML404(response, host);
                    }
                }
            }
            else
            {
                GenericHTTPMethod requestProcessor;
                bool foundHandler = TryGetHTTPHandlerPathBased(request.RawUrl, out requestProcessor);
                if (foundHandler)
                {
                    Hashtable responsedata2 = requestProcessor(keysvals);
                    DoHTTPGruntWork(responsedata2, response);
                }
                else
                {
                    FileRequest.FileResponse fileoutput = null;
                    Exception exceptionout = null;
                    if (m_fileRequestHandler.TryGetFile(request, out fileoutput, out exceptionout))
                    {
                        if (exceptionout == null)
                        {
                            SendFile(request, response, fileoutput);
                        }
                        else
                        {
                            if (exceptionout is System.Security.SecurityException || exceptionout is UnauthorizedAccessException)
                            {
                                SendHTML403(response);
                            }
                            else if (exceptionout is FileNotFoundException || exceptionout is DirectoryNotFoundException || exceptionout is ArgumentException || exceptionout is ArgumentNullException)
                            {
                                SendHTML404(response, host);
                            }
                            else
                            {
                                SendHTML500(response);
                            }
                        }
                    }
                    else
                    {
                        SendHTML404(response, host);
                    }
                }
            }
        }
        internal void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
        {
            int    responsecode   = (int)responsedata["int_response_code"];
            string responseString = string.Empty;

            if (responsedata.ContainsKey("str_response_string"))
            {
                responseString = (string)responsedata["str_response_string"];
            }

            string contentType = string.Empty;

            if (responsedata.ContainsKey("content_type"))
            {
                contentType = (string)responsedata["content_type"];
            }

            string RedirectLocationHeader = string.Empty;

            if (responsedata.ContainsKey("redirect_location"))
            {
                RedirectLocationHeader = (string)responsedata["redirect_location"];
            }

            if (responsedata.ContainsKey("error_status_text"))
            {
                response.StatusDescription = (string)responsedata["error_status_text"];
            }
            if (responsedata.ContainsKey("http_protocol_version"))
            {
                response.ProtocolVersion = (string)responsedata["http_protocolversion_version"];
            }
            if (responsedata.ContainsKey("keepalive"))
            {
                bool keepalive = (bool)responsedata["keepalive"];
                response.KeepAlive = keepalive;
            }

            if (responsedata.ContainsKey("reusecontext"))
            {
                response.ReuseContext = (bool)responsedata["reusecontext"];
            }

            if (string.IsNullOrEmpty(contentType))
            {
                contentType = "text/html";
            }
            response.StatusCode = responsecode;

            switch (responsecode)
            {
            case 302:
                response.AddHeader("Location", RedirectLocationHeader);
                break;

            default:
                response.AddHeader("Content-Type", contentType);
                break;
            }



            byte[] buffer;
            if (!contentType.Contains("image"))
            {
                buffer = Encoding.UTF8.GetBytes(responseString);
            }
            else
            {
                buffer = Convert.FromBase64String(responseString);
            }
            response.SendChunked     = false;
            response.ContentLength64 = buffer.LongLength;
            response.ContentEncoding = Encoding.UTF8;

            try
            {
                response.OutputStream.Write(buffer, 0, buffer.Length);
            }
            catch (Exception ex)
            {
                m_Output.LogMessage("error", "[HTTPD]: Error - " + ex.Message + Environment.NewLine);
            }
            finally
            {
                try
                {
                    response.OutputStream.Flush();
                    response.Send();
                }
                catch (SocketException e)
                {
                    m_Output.LogMessage("warn", "There was an underlying socket error.  Perhaps the socket disconnected." + e.Message + Environment.NewLine);
                }
                catch (IOException e)
                {
                    m_Output.LogMessage("warn", "There was an IO issue: " + e.Message + Environment.NewLine);
                }
            }
        }