using System.Collections.Generic; using System.IO; using System; using System.Net; namespace Tesses.WebServer { public class ServerContext { /// /// Some user data /// public object Tag {get;set;} /// /// Method (ex GET, POST, HEAD) /// public string Method { get; set; } public ServerContext(string method,Stream strm,string path,Dictionary> headers) { Method = method; NetworkStream = strm; RequestHeaders = headers; ResponseHeaders = new Dictionary>(); QueryParams = new Dictionary>(); RawUrl=path; StatusCode = 200; // /joel/path/luigi?local=jim&john_surname=connor&demi_surname=lovato&local=tim string[] splitUrl = path.Split(new char[] { '?' }, 2); if (splitUrl.Length > 0) { UrlPath = splitUrl[0]; OriginalUrlPath=splitUrl[0]; if (splitUrl.Length == 2) { //local=jim&john_surname=connor&demi_surname=lovato&local=tim //we want to split on & q_parm = splitUrl[1]; } else { q_parm = ""; } ResetQueryParms(); } } /// /// Reset query parms (If api sets them) /// public void ResetQueryParms() { QueryParams.Clear(); foreach (var item in q_parm.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries)) { //Console.WriteLine(item); var itemSplit = item.Split(new char[] { '=' }, 2); if (itemSplit.Length > 0) { string key = itemSplit[0]; string value = ""; if (itemSplit.Length == 2) { value = itemSplit[1]; } QueryParams.Add(key, value); } } } private string get_host() { if(RequestHeaders.ContainsKey("Host")) { return RequestHeaders.GetFirst("Host"); } return Server.Address.ToString(); } string q_parm; /// /// the /somepath/file?s=42&joel=file relative to Mount /// public string UrlAndQuery {get { if(!string.IsNullOrWhiteSpace(q_parm)) { return UrlPath + "?" + q_parm; } return UrlPath; }} /// /// Original Url Path /// /// public string OriginalUrlPath {get; private set;} /// /// Original Url path (includes query) /// /// public string RawUrl {get;private set;} /// /// Query parms string only /// public string QueryParamsString {get {return q_parm;}} /// /// Server ip /// public IPEndPoint Server { get; set; } /// /// Client ip /// public IPEndPoint Client { get; set; } /// /// Host name /// /// public string Host { get { return get_host(); } } /// /// Url path (can be eet by Moutable) /// public string UrlPath { get; set; } /// /// Query Params /// public Dictionary> QueryParams { get; set; } /// /// Request headers /// public Dictionary> RequestHeaders { get; set; } /// /// Response headers /// public Dictionary> ResponseHeaders { get; set; } /// /// TCP Stream for http server /// public Stream NetworkStream { get; set; } /// /// Status code for resource /// public int StatusCode { get; set; } } }