public void Server_ProcessRequest(object sender, HttpContextEventArgs e) { // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = e.Context; HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. // Console.WriteLine(context.Request.Url); // Console.WriteLine(context.Request.RawUrl); // Console.WriteLine(context.Request.QueryString.ToString()); long start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); if (context.Request.LocalEndPoint.Port == VCport) { ControllerRequest(context); } ; if (Robots.ContainsKey(context.Request.LocalEndPoint.Port)) { long finish = (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start); Console.WriteLine("checkporttime: " + finish.ToString()); VirtualRobot VR = Robots[context.Request.LocalEndPoint.Port]; VR.RobotRequest(context); } }
private void ServerOnProcessRequest(object sender, HttpContextEventArgs eventArg) { HttpListenerContext context = eventArg.Context; try { context.Response.Headers["Server"] = "C0D3"; context.Response.Headers["Expires"] = "0"; context.Response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate"; string applicationPath = context.Request.Url.AbsolutePath.TrimEnd('/', '\\') + '/'; if (applicationPath.StartsWith(eventArg.Host.ApplicationPath, StringComparison.OrdinalIgnoreCase)) { applicationPath = applicationPath.Substring(eventArg.Host.ApplicationPath.Length - 1); } string[] segments = applicationPath.TrimEnd('/', '\\').Split('/', '\\'); HttpIgnoreAttribute ignore; ICommand cmd; bool execute = _commands.TryGetValue(context.Request.Url.AbsolutePath, out cmd); if (cmd == null && segments.Length == 2) { if (_commands.TryGetValue(segments[1], out cmd)) { execute = (!String.IsNullOrEmpty(context.Request.Url.Query) || context.Request.HttpMethod.ToUpperInvariant() != "GET"); } } if (cmd != null && cmd.TryGetAttribute(out ignore)) { throw new UnauthorizedAccessException("The command is not available."); } else if (cmd != null && execute) { ExecCommand(context, cmd); return; } else if (cmd != null || (cmd == null && segments.Length == 1)) { context.Response.ContentType = "text/html; charset=utf-8"; using (SwitchedOutputStream output = new SwitchedOutputStream(context.Response.OutputStream, ushort.MaxValue)) using (StreamWriter wtr = new StreamWriter(output)) { GenerateHtmlPage(wtr, eventArg.Host.ApplicationPath, cmd != null ? cmd.DisplayName : null); if (!output.OutputSent) { context.Response.ContentLength64 = output.BufferPosition; } output.Commit(); } return; } WriteErrorPage(context, 404, "Not Found", "The url is malformed or the command name is incorrect."); } catch (InterpreterException e) { try { WriteErrorPage(context, 400, "Bad Request", e.Message); } catch { } } catch (UnauthorizedAccessException e) { try { WriteErrorPage(context, 403, "Forbidden", e.Message); } catch { } } catch (Exception e) { try { WriteErrorPage(context, 500, "Internal Server Error", e.Message); } catch { } } }