void File(UMC.Net.NetContext context, String name) { if (System.IO.File.Exists(name)) { TransmitFile(context, name); } else { context.StatusCode = 404; } }
bool CheckUMC(UMC.Net.NetContext context) { var query = context.Url.Query; if (Utility.IsApp(context.UserAgent) || query.Contains("?jsonp=") || query.Contains("&jsonp=") || query.Contains("?_model=") || query.Contains("&_model=")) { return(true); } return(false); }
protected bool Resource(UMC.Net.NetContext context, string path) { foreach (var key in ResourceKeys) { var initer = Reflection.CreateInstance(key.Type) as Data.Sql.Initializer; if (initer.Resource(context, path)) { return(true); } } return(false); }
void Temp(UMC.Net.NetContext context) { var file = context.Url.LocalPath.Substring(5); string filename = UMC.Data.Utility.MapPath(String.Format("App_Data\\Static\\TEMP\\{0}", file)); switch (context.HttpMethod) { case "GET": File(context, filename); break; case "PUT": Utility.Copy(context.InputStream, filename); break; } }
public virtual void ProcessRequest(UMC.Net.NetContext context) { if (Authorization(context)) { string Prefix = UMC.Data.Utility.GetPrefix(context.Url); if (String.IsNullOrEmpty(Prefix)) { Process(context, string.Empty, string.Empty); } else { UMC.Net.INetHandler handler = UMC.Data.Reflection.CreateObject(UMC.Data.Reflection.Configuration("payment"), Prefix) as UMC.Net.INetHandler; handler.ProcessRequest(context); } } else { Data.JSON.Serialize(Mapping(), context.Output); } }
void TransmitFile(UMC.Net.NetContext context, String name) { var lastIndex = name.LastIndexOf('.'); var extName = "html"; if (lastIndex > -1) { extName = name.Substring(lastIndex + 1); } switch (extName.ToLower()) { case "pdf": context.ContentType = "application/pdf"; break; case "txt": context.ContentType = "text/plain"; break; case "htm": case "html": context.ContentType = "text/html"; break; case "json": context.ContentType = "text/json"; break; case "js": context.ContentType = "text/javascript"; break; case "css": context.ContentType = "text/css"; break; case "bmp": context.ContentType = "image/bmp"; break; case "gif": context.ContentType = "image/gif"; break; case "jpeg": case "jpg": context.ContentType = "image/jpeg"; break; case "png": context.ContentType = "image/png"; break; case "svg": context.ContentType = "image/svg+xml"; break; case "mp3": context.ContentType = "audio/mpeg"; break; case "mp4": context.ContentType = "video/mpeg4"; break; case "xml": context.ContentType = "text/xml"; break; default: context.ContentType = "application/octet-stream"; break; } var file = new System.IO.FileInfo(name); context.AddHeader("Last-Modified", file.LastWriteTimeUtc.ToString("r")); var Since = context.Headers["If-Modified-Since"]; if (String.IsNullOrEmpty(Since) == false) { var time = Convert.ToDateTime(Since); if (time >= file.LastWriteTimeUtc) { context.StatusCode = 304; return; } } using (System.IO.FileStream stream = System.IO.File.OpenRead(name)) { Utility.Copy(stream, context.OutputStream); } }
public virtual void ProcessRequest(UMC.Net.NetContext context) { var path = context.Url.LocalPath; var staticFile = Utility.MapPath("App_Data/Static" + path); var paths = new List <string>(context.Url.LocalPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)); if (paths.Count == 0) { paths.Add("index"); staticFile += "index.html"; } switch (context.HttpMethod) { case "GET": if (System.IO.File.Exists(staticFile)) { TransmitFile(context, staticFile); return; } break; } var lastPath = paths[paths.Count - 1]; switch (paths[0]) { case "Click": case "Page": IndexResource(context, "Page.js", "page"); return; case "TEMP": Temp(context); return; case "js": if (lastPath.EndsWith(".js")) { context.ContentType = "text/javascript"; var key = lastPath.Substring(0, lastPath.LastIndexOf('.')); switch (key) { case "Page": key = "UMC"; break; case "UMC.Conf": { var ks = ResourceKeys; for (var i = ks.Count - 1; i > -1; i--) { var initer = Reflection.CreateInstance(ks[i].Type) as Data.Sql.Initializer; if (String.IsNullOrEmpty(initer.ResourceJS) == false) { using (System.IO.Stream stream = initer.GetType().GetProperty("ResourceJS").DeclaringType.Assembly .GetManifestResourceStream(initer.ResourceJS)) { context.Output.WriteLine("/****{0} Conf****/", initer.Name); context.Output.WriteLine(new System.IO.StreamReader(stream).ReadToEnd()); } } } } return; } var rKey = GetResourceKey(key); if (rKey != null) { var initer = Reflection.CreateInstance(rKey.Type) as Data.Sql.Initializer; initer.Resource(context, path); return; } } break; case "UMC": if (paths.Count == 1) { IndexResource(context, "index"); return; } break; default: { if (CheckUMC(context)) { Process(context); return; } if (lastPath.IndexOf('.') > 0) { var names = lastPath.Split('.'); var configs = UMC.Data.Reflection.Configuration(names[0]); if (configs != null) { UMC.Net.INetHandler handler = UMC.Data.Reflection.CreateObject(configs, names[1]) as UMC.Net.INetHandler; if (handler != null) { handler.ProcessRequest(context); return; } } } switch (context.HttpMethod) { case "GET": var file = Utility.MapPath("App_Data/Static/" + String.Join("/", paths.ToArray()) + ".html"); if (System.IO.File.Exists(file)) { File(context, file); return; } if (paths.Count < 4) { var database = Reflection.Configuration("database") ?? new UMC.Configuration.ProviderConfiguration(); foreach (var key in ResourceKeys) { var initer = Reflection.CreateInstance(key.Type) as Data.Sql.Initializer; if (database.Providers.ContainsKey(initer.ProviderName)) { if (initer.Resource(context, path)) { return; } } } if (paths.Count < 2) { IndexResource(context, "index"); return; } } break; default: Process(context); return; } } break; } Process(context); }
protected virtual void Authorization(UMC.Net.NetContext context) { var cookie = context.Cookies[Membership.SessionCookieName];// : CookieKey; var sessionKey = String.Empty; string contentType = "Client/" + context.UserHostAddress; if (UMC.Data.Utility.IsApp(context.UserAgent)) { contentType = "App/" + context.UserHostAddress; } if (String.IsNullOrEmpty(cookie) == false) { sessionKey = cookie; } var ns = new NameValueCollection(); var sign = String.Empty; var hs = context.Headers; for (var i = 0; i < hs.Count; i++) { var key = hs.GetKey(i); switch (key.ToLower()) { case "umc-request-sign": sign = hs[i]; break; default: if (key.StartsWith("umc-")) { ns.Add(key, Uri.UnescapeDataString(hs[i])); } break; } } if (String.IsNullOrEmpty(sign) == false) { if (String.Equals(Utility.Sign(ns, Data.WebResource.Instance().AppSecret()), sign, StringComparison.CurrentCultureIgnoreCase)) { var roles = ns["umc-request-user-role"]; var id = ns["umc-request-user-id"]; var name = ns["umc-request-user-name"]; var alias = ns["umc-request-user-alias"]; var sid = Data.Utility.Guid(sessionKey, true).Value; if (String.IsNullOrEmpty(roles) == false) { var user = UMC.Security.Identity.Create(Utility.Guid(id) ?? sid, name, alias, roles.Split(',')); UMC.Security.Principal.Create(user, UMC.Security.AccessToken.Create(user, sid, contentType, 0)); } else { var user = UMC.Security.Identity.Create(Utility.Guid(id) ?? sid, name, alias); UMC.Security.Principal.Create(user, UMC.Security.AccessToken.Create(user, sid, contentType, 0)); } return; } } if (String.IsNullOrEmpty(sessionKey)) { var uid = Guid.NewGuid(); sessionKey = Utility.Guid(uid); context.AppendCookie(Membership.SessionCookieName, sessionKey); var user = new UMC.Security.Guest(uid); UMC.Security.Principal.Create(user, UMC.Security.AccessToken.Create(user, uid, contentType, 0)); } else { UMC.Security.Membership.Instance().Authorization(sessionKey, contentType); } }
void Process(UMC.Net.NetContext context) { if (String.IsNullOrEmpty(context.UserAgent)) { return; } context.AddHeader("Access-Control-Allow-Origin", "*"); context.AddHeader("Access-Control-Allow-Credentials", "true"); context.AddHeader("Access-Control-Max-Age", "18000"); context.AddHeader("Access-Control-Allow-Methods", "*"); context.AddHeader("Access-Control-Allow-Headers", "x-requested-with,content-type"); var QueryString = new NameValueCollection(context.QueryString); QueryString.Add(context.Form); var model = QueryString["_model"]; var cmd = QueryString["_cmd"]; var jsonp = context.QueryString.Get("jsonp"); if ("Upload".Equals(model) && "Command".Equals(cmd)) { try { var url = new Uri(QueryString["src"]); var wb = new Net.HttpClient(); var hash = UMC.Data.JSON.Deserialize <Hashtable>(wb.GetAsync(url).Result.Content.ReadAsStringAsync().Result).GetEnumerator(); QueryString.Clear(); while (hash.MoveNext()) { QueryString.Add(hash.Key.ToString(), hash.Value.ToString()); } model = QueryString["_model"]; cmd = QueryString["_cmd"]; if (String.IsNullOrEmpty(jsonp) == false) { QueryString.Add("jsonp", jsonp); } } catch { return; } } var Url = context.Url; var ip = context.Headers.Get("X-Real-IP"); if (String.IsNullOrEmpty(ip)) { ip = context.UserHostAddress;; } var host = UMC.Data.WebResource.Instance().APIHost();; if (String.IsNullOrEmpty(host)) { var cahash = context.Headers.Get("CA-Host"); if (String.IsNullOrEmpty(cahash) == false) { host = String.Format("https://{0}", context.Headers.Get("CA-Host")); } } if (String.IsNullOrEmpty(host) == false) { Url = new Uri(String.Format("{1}{0}", context.Url.PathAndQuery, host)); } Process(QueryString, Url, context, model, cmd, url => { context.Redirect(url.AbsoluteUri); }); }
protected virtual void Domain(UMC.Net.NetContext context) { context.Redirect(WebResource.Instance().WebDomain()); }
public virtual bool Resource(UMC.Net.NetContext context, string targetKey) { return(false); }
protected virtual bool Authorization(UMC.Net.NetContext context) { var path = context.Url.LocalPath; var paths = new List <string>(path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)); if (paths.Count > 0) { if (paths[paths.Count - 1].IndexOf('.') > -1) { paths.RemoveAt(paths.Count - 1); } } else { return(false); } var CookieKey = String.Empty; if (paths.Count == 2 || paths.Count >= 4) { CookieKey = paths[1]; } var cookie = String.IsNullOrEmpty(CookieKey) ? context.Cookies[Membership.SessionCookieName] : CookieKey; var sessionKey = Guid.Empty; string contentType = "Client/" + context.UserHostAddress; if (UMC.Data.Utility.IsApp(context.UserAgent)) { contentType = "App/" + context.UserHostAddress; } if (String.IsNullOrEmpty(cookie) == false) { sessionKey = UMC.Data.Utility.Guid(cookie, true) ?? Guid.Empty; } if (sessionKey == Guid.Empty) { sessionKey = Guid.NewGuid(); context.AppendCookie(Membership.SessionCookieName, UMC.Data.Utility.Guid(sessionKey)); var user = new UMC.Security.Guest(sessionKey); UMC.Security.Principal.Create(user, UMC.Security.AccessToken.Create(user, sessionKey, contentType, 0)); } else { UMC.Security.Membership.Instance().Authorization(sessionKey, contentType); } var urf = context.UrlReferrer; if (urf != null) { if (String.IsNullOrEmpty(urf.Query) == false) { var query = System.Web.HttpUtility.ParseQueryString(urf.Query.Substring(1)); var sp = UMC.Data.Utility.Guid(query["sp"]); if (sp.HasValue) { if (String.Equals(UMC.Security.AccessToken.Get("Spread-Id"), sp.ToString()) == false) { UMC.Security.AccessToken.Set("Spread-Id", sp.ToString()); } } } } return(true); }
static void Process(UMC.Net.NetContext context, String model, string cmd) { if (String.IsNullOrEmpty(context.UserAgent)) { return; } if (context.UrlReferrer != null) { context.AddHeader("Access-Control-Allow-Origin", String.Format("{0}://{1}", context.UrlReferrer.Scheme, context.UrlReferrer.Host)); } else { context.AddHeader("Access-Control-Allow-Origin", "*"); } context.AddHeader("Access-Control-Allow-Credentials", "true"); context.AddHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST"); context.AddHeader("Access-Control-Allow-Headers", "x-requested-with,content-type"); var QueryString = new NameValueCollection(context.QueryString); QueryString.Add(context.Form); if (string.IsNullOrEmpty(model)) { model = QueryString["_model"]; cmd = QueryString["_cmd"]; } var jsonp = context.QueryString.Get("jsonp"); if ("Upload".Equals(model) && "Command".Equals(cmd)) { try { var url = new Uri(QueryString["src"]); var wb = new Net.HttpClient(); var hash = UMC.Data.JSON.Deserialize <Hashtable>(wb.GetAsync(url).Result.Content.ReadAsStringAsync().Result).GetEnumerator(); QueryString.Clear(); while (hash.MoveNext()) { QueryString.Add(hash.Key.ToString(), hash.Value.ToString()); } model = QueryString["_model"]; cmd = QueryString["_cmd"]; if (String.IsNullOrEmpty(jsonp) == false) { QueryString.Add("jsonp", jsonp); } } catch { return; } } context.ContentType = "text/javascript;charset=utf-8"; var Url = context.Url; var ip = context.Headers.Get("X-Real-IP"); if (String.IsNullOrEmpty(ip)) { ip = context.UserHostAddress;; } var cahash = context.Headers.Get("CA-Host"); if (String.IsNullOrEmpty(cahash) == false) { Url = new Uri(String.Format("https://{1}{0}", context.Url.PathAndQuery, cahash)); } Process(QueryString, context.InputStream, context.OutputStream, Url, context.UrlReferrer, ip, context.UserAgent, model, cmd, url => { context.Redirect(url.AbsoluteUri); }); }