YamlPathInfo?findPath(RequestProxy requestProxy, HttpListenerContext httpContext) { var request = httpContext.Request; string path = request.Url.LocalPath; string method = request.HttpMethod.ToLower(); foreach (var p in settings.paths) { if (p.path != path) { continue; } if (!string.IsNullOrEmpty(p.methods) && !p.methods.ToLower().Split(',').Contains(method)) { continue; } if (!string.IsNullOrEmpty(p.filter)) { try { IStringEvaluator evoluator = EvaluatorDecoder.decode(p.filter); var context = new RequestMatchingContext(); context.PathInfo = p; context.Request = requestProxy; context.HttpContext = httpContext; if (evoluator.evoluate(context) != StringEvaluator.TRUE) { continue; } } catch (FormatException ex) { log.error("ERROR! filter syntax is broken. filter: " + p.filter + " reason:" + ex.ToString()); } } return(p); } return(null); }
Task execContextAsync(HttpListenerContext context, int idx) { return(Task.Run(() => { try { var prefix = string.Format("{0:000}: ", idx); var req = context.Request; var requestProxy = new RequestProxy(context); log.info(prefix + "--------------------------------------------------------------------------------"); log.info(prefix + DateTime.Now, ConsoleColor.DarkGray); log.info(prefix + "===== Request =====", ConsoleColor.DarkGreen); log.info(prefix + req.HttpMethod + " " + req.Url.LocalPath, ConsoleColor.White); log.headers(prefix, "[Headers] ", req.Headers); if (req.HasEntityBody) { log.formatted(prefix, "[Body ] ", requestProxy.getBody()); } log.info(prefix + "===== Response =====", ConsoleColor.DarkGreen); var pathInfo = findPath(requestProxy, context); if (!pathInfo.HasValue) { pathInfo = new YamlPathInfo { response = (YamlResponseInfo)settings.notfound }; } var p = pathInfo.Value; Task taskWait = Task.Delay(p.wait > 0 ? p.wait : 0); if (p.command != null) { log.info(prefix + "command: " + p.command); List <string> commands = ShellParser.split(p.command); var process = new Process(); var startInfo = new ProcessStartInfo { WindowStyle = ProcessWindowStyle.Hidden, FileName = commands[0], Arguments = string.Join(" ", commands.GetRange(1, commands.Count - 1)) }; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); } var yamlres = p.response; var defres = settings.defaultResponse; int statudCode = int.Parse(yamlres.status ?? defres.status); var respHeaders = new Dictionary <string, string>(); var headers = yamlres.headers ?? defres.headers; if (headers != null) { foreach (var e in headers) { respHeaders[e.Key] = e.Value; } } string body = ""; if (yamlres.bodytext != null || yamlres.bodyfile != null) { body += yamlres.bodytext ?? ""; body += readFile(yamlres.bodyfile) ?? ""; } else { body += defres.bodytext ?? ""; body += readFile(defres.bodyfile) ?? ""; } taskWait.Wait(); writeResponse(context, prefix, statudCode, body, respHeaders); } catch (Exception ex) { Console.WriteLine("message: " + ex.Message); Console.WriteLine("stack trace: "); Console.WriteLine(ex.StackTrace); Console.WriteLine(); } })); }