private void RunTrace(HttpListenerContext callcontext) { try { var sb = new StringBuilder(); var sw = Stopwatch.StartNew(); var parameters = PrepareParameters(callcontext); sw.Stop(); sb.AppendLine("Prepare: " + sw.Elapsed); sw = Stopwatch.StartNew(); var result = _handler(parameters); sw.Stop(); sb.AppendLine("Execute: "+sw.Elapsed); sw = Stopwatch.StartNew(); var uson = result.ToUson(); sw.Stop(); sb.AppendLine("Usonify: " + sw.Elapsed); sw = Stopwatch.StartNew(); var json = uson.ToJson(); sw.Stop(); sb.AppendLine("Jsonify: " + sw.Elapsed); sw = Stopwatch.StartNew(); json = new JsonSerializer().Serialize("", result); sw.Stop(); sb.AppendLine("Jsonify 2: " + sw.Elapsed); callcontext.Finish(sb.ToString()); } catch (Exception ex) { callcontext.Finish(ex.ToString(), "text/plain", 500); } }
/// <summary> /// /// </summary> /// <param name="server"></param> /// <param name="callcontext"></param> /// <param name="callbackEndPoint"></param> /// <param name="cancel"></param> public void Run(IHostServer server, HttpListenerContext callcontext, string callbackEndPoint, CancellationToken cancel){ var abspath = callcontext.Request.Url.AbsolutePath; if (string.IsNullOrWhiteSpace(abspath) || abspath == "/") { abspath = DefaultPage; } var staticdescriptor = server.Static.Get(abspath, callcontext); if (null == staticdescriptor){ callcontext.Finish("no file found", "text/plain; charset=utf-8", status: 404); return; } var filetime = callcontext.SetLastModified(staticdescriptor.GetLastVersion()); if (filetime <= callcontext.GetIfModifiedSince()) { callcontext.Finish("", status: 304); } else { callcontext.Response.AddHeader("Qorpent-Disposition",staticdescriptor.FullName); if (staticdescriptor.IsFixedContent){ callcontext.Finish(staticdescriptor.FixedContent, staticdescriptor.MimeType + "; charset=utf-8"); } else{ using (var s = staticdescriptor.Open()) { callcontext.Finish(s, staticdescriptor.MimeType + "; charset=utf-8"); s.Close(); } } } }
/// <summary> /// /// </summary> /// <param name="server"></param> /// <param name="callcontext"></param> /// <param name="callbackEndPoint"></param> /// <param name="cancel"></param> public void Run(IHostServer server, HttpListenerContext callcontext, string callbackEndPoint, CancellationToken cancel){ var code = callcontext.Request.Url.Query.Replace("?", ""); string wikicode = "*** страница с данным кодом не найдена ***"; string tpl = "<html><body>${wikipage}</body></html>"; var wikidesc = server.Static.Get(code + ".wiki",withextensions:true); if (null == wikidesc){ wikidesc = server.Static.Get(code,withextensions:true); } if (null != wikidesc) { using (var s = wikidesc.Open()) { using (var r = new StreamReader(s)){ wikicode = r.ReadToEnd(); } if (!wikidesc.FullName.EndsWith(".wiki")){ if (wikidesc.FullName.EndsWith(".bxl") || wikidesc.FullName.EndsWith(".bxls") || wikidesc.FullName.EndsWith(".bsproj")){ wikicode = "[[code]]\r\n" + wikicode + "\r\n[[/code]]\r\n[[script-last type=bxl]]"; } else if (wikidesc.FullName.EndsWith(".js") || wikidesc.FullName.EndsWith(".css") || wikidesc.FullName.EndsWith(".cs")){ wikicode = "[[code]]\r\n" + wikicode + "\r\n[[/code]]"; } else{ wikicode = wikicode.Replace("\r\n", "\r\n\r\n "); } wikicode = "= Файл: [href:" + wikidesc.FullName + "]\r\n\r\n" + wikicode; } } } var tpldesc = server.Static.Get( "wiki.html"); if (null != tpldesc) { using (var s = tpldesc.Open()) { using (var r = new StreamReader(s)) { tpl = r.ReadToEnd(); } } } var wikipage = tpl.Replace("${wikipage}", wikicode); callcontext.Finish(wikipage,mimeType:"text/html"); }
private void AsynchronousEnd(HttpListenerContext callcontext){ if (null == currentAsyncCall) { callcontext.Finish("no asynchronous task ever started", "text/plain", 500); } currentAsyncCall.Wait(); if (currentAsyncCall.IsFaulted) { callcontext.Finish("last call to async fail with " + currentAsyncCall.Exception, "text/plain", 500); } else if (currentAsyncCall.IsCanceled) { callcontext.Finish("last call to async was cancelled", "text/plain", 500); } else { if (callcontext.Request.Url.AbsolutePath.EndsWith("/xml")) { callcontext.Finish(((UObj)currentAsyncCall.Result).ToXmlString(), "text/xml"); } else { callcontext.Finish(((UObj)currentAsyncCall.Result).ToJson(), "application/json"); } } }
private void RunSynchronous(HttpListenerContext callcontext){ try{ var parameters = PrepareParameters(callcontext); var obj = _handler(parameters); var result = obj.ToUson(); if (callcontext.Request.Url.AbsolutePath.EndsWith("/xml")){ callcontext.Finish(result.ToXmlString(), "text/xml"); } else{ callcontext.Finish(result.ToJson(), "application/json"); } } catch (Exception ex){ callcontext.Finish(ex.ToString(), "text/plain", 500); } }
private void AsynchronousBegin(HttpListenerContext callcontext){ if (null != currentAsyncCall){ if (!currentAsyncCall.IsCompleted){ callcontext.Finish("{\"state\":\"run\"}"); return; } } var parameters = PrepareParameters(callcontext); currentAsyncCall = Task.Run(() => _handler(parameters)); callcontext.Finish("{\"state\":\"start\"}"); }
/// <summary> /// Выполняет указанный запрос /// </summary> /// <param name="server"></param> /// <param name="callcontext"></param> /// <param name="callbackEndPoint"></param> /// <param name="cancel"></param> public void Run(IHostServer server, HttpListenerContext callcontext, string callbackEndPoint, CancellationToken cancel) { callcontext.Response.ContentEncoding = Encoding.UTF8; callcontext.Finish(_content,_mime,_status); }