public static void ListGameObjects(CUDLR.RequestContext context) { string json = "["; UnityEngine.Object[] objects = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)); foreach (UnityEngine.Object obj in objects) { // FIXME object names need to be escaped.. use minijson or similar json += string.Format("\"{0}\", ", obj.name); } json = json.TrimEnd(new char[] { ',', ' ' }) + "]"; context.Response.WriteString(json, "application/json"); }
public static void Complete(RequestContext context) { string partialCommand = context.Request.QueryString.Get("command"); string found = null; if (partialCommand != null) found = Console.Complete(partialCommand); context.Response.WriteString(found); }
public static void Run(RequestContext context) { string command = context.Request.QueryString.Get("command"); if (!string.IsNullOrEmpty(command)) Console.Run(command); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.StatusDescription = "OK"; }
public static void Output(RequestContext context) { context.Response.WriteString(Console.Output()); }
public static void History(RequestContext context) { string index = context.Request.QueryString.Get("index"); string previous = null; if (!string.IsNullOrEmpty(index)) previous = Console.PreviousCommand(System.Int32.Parse(index)); context.Response.WriteString(previous); }
void HandleRequest(RequestContext context) { try { bool handled = false; for (; context.currentRoute < registeredRoutes.Count; ++context.currentRoute) { RouteAttribute route = registeredRoutes[context.currentRoute]; Match match = route.m_route.Match(context.path); if (!match.Success) continue; if (!route.m_methods.IsMatch(context.Request.HttpMethod)) continue; // Upgrade to main thread if necessary if (route.m_runOnMainThread && Thread.CurrentThread != mainThread) { lock (mainRequests) { mainRequests.Enqueue(context); } return; } context.match = match; route.m_callback(context); handled = !context.pass; if (handled) break; } if (!handled) { context.Response.StatusCode = (int)HttpStatusCode.NotFound; context.Response.StatusDescription = "Not Found"; } } catch (Exception exception) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = string.Format("Fatal error:\n{0}", exception); Debug.LogException(exception); } context.Response.OutputStream.Close(); }
void ListenerCallback(IAsyncResult result) { RequestContext context = new RequestContext(listener.EndGetContext(result)); HandleRequest(context); listener.BeginGetContext(new AsyncCallback(ListenerCallback), null); }
static void FindFileType(RequestContext context, bool download, out string path, out string type) { path = Path.Combine(fileRoot, context.match.Groups[1].Value); string ext = Path.GetExtension(path).ToLower().TrimStart(new char[] {'.'}); if (download || !fileTypes.TryGetValue(ext, out type)) type = "application/octet-stream"; }
static void WWWFileHandler(RequestContext context, bool download) { string path, type; FindFileType(context, download, out path, out type); WWW req = new WWW(path); while (!req.isDone) { Thread.Sleep(0); } if (string.IsNullOrEmpty(req.error)) { context.Response.ContentType = type; if (download) context.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}", Path.GetFileName(path))); context.Response.WriteBytes(req.bytes); return; } if (req.error.StartsWith("Couldn't open file")) { context.pass = true; } else { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.StatusDescription = string.Format("Fatal error:\n{0}", req.error); } }
static void FileHandler(RequestContext context, bool download) { string path, type; FindFileType(context, download, out path, out type); if (File.Exists(path)) { context.Response.WriteFile(path, type, download); } else { context.pass = true; } }
public static void Run(RequestContext context) { string command = Uri.UnescapeDataString(context.Request.QueryString.Get("command")); if (!string.IsNullOrEmpty(command)) Console.Run(context, command); }
public static void Log(string str, RequestContext context) { Log(str); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.StatusDescription = "OK"; }
void ListenerCallback(IAsyncResult result) { RequestContext context = new RequestContext(listener.EndGetContext(result)); HandleRequest(context); if (listener.IsListening) { listener.BeginGetContext(ListenerCallback, null); } }