public static PanResponse onRequest(PanRequest request) { cw(request.Url, ConsoleColor.Green); if (request.Address.Length < 1) { return(PanResponse.ReturnHtml(data.website.indexHtmlPath)); } else if (request.Address[0].ToLowerInvariant() == "getbuttons") { return(PanResponse.ReturnJson(data.buttons)); } else if (request.Address[0].ToLowerInvariant() == "sendbtn") { try { if (request.Address.Length != 2) { return(PanResponse.ReturnCode(403)); } string[] btnCodes = request.Address[1].Split('+'); for (int i = 0; i < btnCodes.Length; i -= -1) { sim.Keyboard.KeyDown((VirtualKeyCode)Enum.Parse(typeof(VirtualKeyCode), btnCodes[i])); } for (int i = btnCodes.Length - 1; i >= 0; i--) { sim.Keyboard.KeyUp((VirtualKeyCode)Enum.Parse(typeof(VirtualKeyCode), btnCodes[i])); } //sim.Keyboard.KeyPress((VirtualKeyCode)Enum.Parse(typeof(VirtualKeyCode), btnCode)); return(PanResponse.ReturnCode(200)); } catch (Exception ex) { cw(ex.Message, ConsoleColor.Red); return(PanResponse.ReturnCode(500)); } } else { string virtAddress = request.Url; var matches = data.virtualPhysicalPathMatches.FindAll((e) => { return(e.virtualPath == virtAddress); }); if (matches.Count < 1) { return(PanResponse.ReturnCode(404)); } string physAddress = matches[0].physicalPath; if (!File.Exists(physAddress)) { return(PanResponse.ReturnCode(404)); } return(PanResponse.ReturnFile(physAddress)); } //sim.Keyboard.KeyPress(VirtualKeyCode.LWIN); //int value = (int)Enum.Parse(typeof(TestAppAreana.MovieList.Movies), KeyVal); return(PanResponse.ReturnCode(200)); }
public static PanResponse ReturnCode(int code, string content) //Return error with page { return(PanResponse.ReturnCode(code, Encoding.UTF8, content)); }
protected void WebsiteLife() { try { while (Listener.IsListening) { HttpListenerContext context = Listener.GetContext(); Task.Factory.StartNew(() => { Stream output = context.Response.OutputStream; // GET Cookies List <PanCookie> cookies = new List <PanCookie>(); foreach (Cookie c in context.Request.Cookies) { cookies.Add(new PanCookie(c.Name, c.Value, c.Path, c.Expires)); } // GET Headers Dictionary <string, string[]> headers = new Dictionary <string, string[]>(); System.Collections.Specialized.NameValueCollection cheaders = context.Request.Headers; foreach (string key in cheaders.AllKeys) { string current_key = key; string[] currentvalues = cheaders.GetValues(current_key); } // GET Data string url = context.Request.RawUrl; // Url string method = context.Request.HttpMethod; // Method Stream inputStream = new MemoryStream(); // Body bool hasEntityBody = context.Request.HasEntityBody; // Has Entity Body if (hasEntityBody) { context.Request.InputStream.CopyTo(inputStream); inputStream.Position = 0; } string[] acceptTypes = context.Request.AcceptTypes; // Accept Types Encoding contentEncoding = context.Request.ContentEncoding; // Content Encoding string contentType = context.Request.ContentType; // Content Type bool isLocal = context.Request.IsLocal; // Is Local string userAgent = context.Request.UserAgent; // User Agent string[] userLanguages = context.Request.UserLanguages; // User Languages IPEndPoint remoteEndPoint = context.Request.RemoteEndPoint; // User IP string userIP = remoteEndPoint.Address.ToString(); PanRequest request = new PanRequest(method, url, inputStream, cookies, hasEntityBody, acceptTypes, contentEncoding, contentType, headers, isLocal, userAgent, userLanguages, userIP); PanResponse response = onRequest.Invoke(request); // SET Code int code = response.Code; context.Response.StatusCode = code; // SET context.Response.ContentType = response.MIME; // SET Cookies if (response.Cookies == null) { response.Cookies = new List <PanCookie>(); } foreach (PanCookie c in response.Cookies) { string cookie = ""; cookie += (c.Name + "=" + (c.Value == null ? "" : c.Value)); if (c.Expires != null) { cookie += ("; Expires=" + c.Expires.ToString()); } cookie += ("; Path=" + c.Path); context.Response.Headers.Add("Set-Cookie", cookie); } response.OutputStream.CopyTo(output); response.OutputStream.Close(); response.OutputStream.Dispose(); output.Close(); context.Response.Close(); }); } } catch (ThreadAbortException ex) { // } catch (Exception ex) { throw new WebsiteException(ex); } }
public static PanResponse ReturnFile(Stream file, string mime, List <PanCookie> cookies = null) //Return File from stream { return(PanResponse.ReturnFile(file, mime, Encoding.UTF8, cookies)); }
public static PanResponse ReturnFile(string path, List <PanCookie> cookies = null) //Return File fron path { return(PanResponse.ReturnFile(path, Encoding.UTF8, cookies)); }
public static PanResponse ReturnHtml(string path, List <PanCookie> cookies = null) // Return Html page { return(PanResponse.ReturnHtml(path, Encoding.UTF8, cookies)); }
public static PanResponse ReturnHtml(string path, Encoding contentEncoding, List <PanCookie> cookies = null) // Return Html page { string html = File.ReadAllText(path); return(PanResponse.ReturnContent(html, contentEncoding, cookies)); }
public static PanResponse ReturnJson(object o, List <PanCookie> cookies = null) //Return json view of object (as string) { return(PanResponse.ReturnJson(o, Encoding.UTF8, cookies)); }
public static PanResponse ReturnContent(string content, List <PanCookie> cookies = null) //Return string (content) { return(PanResponse.ReturnContent(content, Encoding.UTF8, cookies)); }
public static PanResponse ReturnEmtry(List <PanCookie> cookies = null) { return(PanResponse.ReturnContent("", cookies)); }