Пример #1
0
 public YingRouter yadd(YingRouteType ytype, String yuri, Func <HttpListenerRequest, YingResponse> yaction)
 {
     yroutes[ytype].Add(new YingRouteInfo
     {
         YUri    = yuri,
         YAction = yaction
     });
     return(this);
 }
Пример #2
0
        public void YRoute(YingRouteType ytype, HttpRequestEventArgs yargs)
        {
            getYConsole().sendYMessage($"{ytype.ToString()} > {yargs.Request.RawUrl}");

            //getYConsole().sendYMessage($"{ytype.ToString()} > {yargs.Request.RawUrl}");

            HttpListenerRequest  yrequest  = yargs.Request;
            HttpListenerResponse yresponse = yargs.Response;

            yroutes[ytype].ForEach((y) =>
            {
                if (y.isYMatch || yargs.Request.RawUrl == y.YUri)
                {
                    int ycount = Regex.Matches(yargs.Request.RawUrl, y.YUri).Count;
                    if (y.isYMatch && Regex.Matches(yargs.Request.RawUrl, y.YUri).Count == 0)
                    {
                        return;
                    }
                    try
                    {
                        YingResponse yyresponse = y.YAction.Invoke(yrequest);
                        YResponseBuilder(yresponse, HttpStatusCode.OK, yyresponse.ytype, Encoding.UTF8);
                        yresponse.WriteContent(yyresponse.ycontext);

                        /*try
                         * {
                         *  Regex yregex = new Regex("", RegexOptions.Compiled);
                         *  yregex.Match(Convert.ToString());
                         * }
                         * catch (FormatException yexception) { }
                         * catch (InvalidCastException yexception) { }
                         * catch (OverflowException yexception) { }
                         * catch (Exception yexception) { }*/
                    }
                    catch (YingAuthenticationException yexception)
                    {
                        YResponseBuilder(yresponse, (HttpStatusCode)yexception.getYCode(), ".json", Encoding.UTF8);

                        yresponse.WriteContent(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new YingError
                        {
                            error        = yexception.getYError(),
                            errorMessage = yexception.Message,
                            cause        = null
                        })));
                    }
                    catch (YingHttpException yexception)
                    {
                        YResponseBuilder(yresponse, (HttpStatusCode)yexception.getYCode(), ".html", Encoding.UTF8);
                    }
                    catch (Exception yexception)
                    {
                        YResponseBuilder(yresponse, HttpStatusCode.InternalServerError, ".html", Encoding.UTF8);
                    }
                    return;
                }
            });


            /*YResponseBuilder(yresponse, new YingResponseInfo
             * {
             *  YStatusCode = HttpStatusCode.NotFound,
             *  YStatusDescription = "Ying",
             *
             *  YContentType = YingMimeMapping.YGetMimeType(".json"),
             *  YContentEncoding = Encoding.UTF8
             * });*/

            /*yresponse.WriteContent(Encoding.UTF8.GetBytes("{\"Ying\": \"颖\"}" /*JsonMapper.ToJson()*/ /*));*/
            //y.Response.WriteContent
        }