HttpServer.HttpServer.HandleRequest C# (CSharp) Method

HandleRequest() private method

private HandleRequest ( object oContext ) : void
oContext object
return void
        private void HandleRequest(object oContext)
        {
            HttpListenerContext context = (HttpListenerContext)oContext;
            try
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                string[] tokens = context.Request.RawUrl.Split('&');
                foreach (var token in tokens)
                {
                    string[] keyValuePair = token.Split('=');
                    if (keyValuePair.Length != 2) continue;
                    var key = WebUtility.UrlDecode(keyValuePair[0]);
                    var value = WebUtility.UrlDecode(keyValuePair[1]);
                    parameters.Add(key, value);
                }

                KeyValuePair<HttpHandlerAttribute, HttpHandlerDelegate> pair;
                String[] raw = context.Request.RawUrl.Split('&');
                if (raw[0] == "/favicon.ico") return;
                if (!_handlers.TryGetValue(raw[0], out pair)) throw new Exception("Missing handler.");

                context.Response.ContentType = "text/json";
                string result = pair.Value(this, context.Request, parameters);
                if (result == null) throw new Exception("Invalid response from handler " + context.Request.RawUrl + ".");
                context.Response.ContentEncoding = context.Request.ContentEncoding;
                using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, context.Response.ContentEncoding)) writer.Write(result);
            }
            catch (Exception e)
            {
                context.Response.ContentType = "text/json";
                context.Response.ContentEncoding = context.Request.ContentEncoding;
                using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, context.Response.ContentEncoding)) writer.Write(JsonEncode("Exception: " + e.ToString()));
                Log.Error("HttpServer", "An exception occurred in HandleRequest: " + e.Message);
            }
            finally
            {
                context.Response.Close();
            }
        }